﻿



//window.onerror = null;




function funerrTrap() {
    return true;
}

//window.onerror = funerrTrap;


function getPageVals(val) {
    try {
        //PageMethods.GetPageData(val,false, rotateInit, onFailed, ctx);
    } catch (e) {
        //error occured
    }
}

function getPageValsNoRotate(val) {
    try {
        //PageMethods.GetPageData(val, true, rotateInitNoRotate, onFailed, ctx);
    } catch (e) {
        //error occured
    }
}

//will be called from the flashplayer to send an e-mail
function sendEventToFriend(fname, femail, yname, yemail, vidLink, prodId) {

    var dt = "fname=" + fname + "&femail=" + femail + "&yname=" + yname + "&yemail=" + yemail + "&vidLink=" + vidLink + "&prodId=" + prodId;
    //alert(dt);
    $.get("../products/sendToFriendProcess.aspx?" + dt);

}


var isPaused = false;


function subCatOver(element)
{
    var nextDisplay = '#' + element.id + '_over';
    //alert(nextDisplay);
    $(element).css('display', 'none');
    $(nextDisplay).css('display', 'block');
}

function subCatOut(element)
{
    var currDisplay = '#' + element.id.replace('_over', '');
    //alert(currDisplay);
    $(element).css('display', 'none');
    $(currDisplay).css('display','block');
}

function subCatOverMoto(element) {
    var nextDisplay = '#' + element.id + '_over';
    $('div[id$=_over]').css('display', 'none');
    //$(element).css('display', 'none');
    $(nextDisplay).css('display', 'block');
}

function subCatOutMoto(element) {
    var currDisplay = '#' + element.id.replace('_over', '');
    $(element).css('display', 'none');
    $(currDisplay).css('display', 'block');
}


function rollOver(element) {

    element.src = element.src.replace('.jpg', '_over.jpg');

}

function rollOut(element){

    element.src = element.src.replace('_over.jpg', '.jpg');
    
}


function rollOverGif(element) {

    element.src = element.src.replace('.gif', '_over.gif');

}

function rollOutGif(element){

    element.src = element.src.replace('_over.gif', '.gif');

}


function show(element) {
    isPaused = true;
    $(element).fadeIn('fast');
}

function hide(element) {
    isPaused = false;
    $(element).fadeOut(300);
}

function doShuffle(o) {

     for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
     {
         return o;
     }
}

//GLOBALLY USED VARIABLES

//toggle if the next background main image has been loaded yet
//        var nextBackgroundReady = false;
var totalContents = 0;
var pageContents = new Array();
var currIndex = 0;
// IS THE NAME OF THE CONTAINING DIVS THAT ARE SUPPOSED TO ROTATE the differen ba
var currentDisplayed = 'div#mainContainer_1';
var nextDisplayed = 'div#mainContainer_2';


/***
SETTINGS FOR TRANSITIONS
**/
var swapInSpeed = 3000;
var swapOutSpeed = 3000;
//speed before next image is loaded
var rotateSpeed = 10000;
var backgroundLoaded = false;
var needsRotate = true;

//I can possibly add a flag
function rotateInit(pageVals) {
    pageContents = doShuffle(pageVals);
    totalContents = pageContents.length;
    displayArray(pageVals);
    if (totalContents > 0) {
        needsRotate = true;
        loadNextBackground();
    }
}

function displayArray(arr) {
    for (i = 0; i < arr.length; i++) {
        arr[i];
    }
}

function rotateInitNoRotate(pageVals) {
    pageContents = pageVals;    
    totalContents = pageContents.length;

    if (totalContents > 0) {
        needsRotate = false;
        loadNextBackground();
    }
}



function loadNextBackground() {
    
    var tempDisplayed;
    //zero based vs. one based... (<obj>.length starts at 1)
    if (currIndex < totalContents && isPaused == false) {
        //get the image that is required for this background
        var currImage = $(pageContents[currIndex]).find('#bgImage').attr('src');
        

        
        currImage = currImage.replace('url(', '').replace(')', '');
        //if FireFox, the url is returned without quotes..
        currImage = currImage.replace('\'', '').replace('\'', '');

        var img = new Image();
   
        //once the image has been loaded, fade in next, fade out current
        $(img).load(function() {
       
            //set the contents of the next item to be displayed
            $(nextDisplayed).html(pageContents[currIndex]);
            //fade new in
            $(nextDisplayed).fadeIn(swapInSpeed);
           
            //fade new out
 
            $(currentDisplayed).fadeOut(swapOutSpeed);
            //remove the loading class
            if (currIndex == 0) {
                $(currentDisplayed).removeClass('loadingMain');
            }
            //toggle logic --> set next ID to current, current ID to next
            tempDisplayed = nextDisplayed;
            nextDisplayed = currentDisplayed;
            currentDisplayed = tempDisplayed;

            //increase the array item
            currIndex = currIndex + 1;
            if (needsRotate == true) {
                //let them enjoy it before you give them something else to enjoy
                setTimeout('loadNextBackground()', 10000);
            }

        }).error(function() {
            //try it again
            loadNextBackground();
        }).attr('src', currImage);
    }
    else if(isPaused == true)
    {
        setTimeout('loadNextBackground()', rotateSpeed);
    }
    else {
        //restart the magic
        currIndex = 0;
        loadNextBackground();
    }
   
}

//onfailed --> log error if able to connect
function onSuccess(ex, ctx, methodName) {
    //alert('method Success'); 
    // get_stackTrace(), get_message(), 
    // get_statusCode(), get_timedOut() 
}

//onfailed --> log error if able to connect
function onFailed(ex, ctx, methodName) {
   //alert(methodName + ' : AN ERROR OCCURED' + ex.get_message() + '<<--Status Code  ::  stackTrace-->>' + ex.get_stackTrace()); 
    // get_stackTrace(), get_message(), 
    // get_statusCode(), get_timedOut() 
}
//create a context
var ctx = {
    CurrentValue: 123456,
    CurrentDate: new Date()
};

/*
END --> 
*/
