var SERIESTRANSITION = 600;
var TRANSITIONTIME = 600;
var DRINKTRANSITIONTIME = 1000;
var NORMALDELAY = 4000;
var DRINKANIMATIONDELAY = 400;
var DRINKANIMATIONTWEEN = 0;

var i = 0;
var timeoutarray;
var currentTime;
var introPlaying = true;

var carouselCycle;

var rtime = new Date();
var timeout = false;
var delta = 200;

function resizeend() {
    if (new Date() - rtime < delta) {
        setTimeout(resizeend, delta);
    } else {
        timeout = false;

        movebar();
    }
}

function movebar() {
    var contentwidth = $("#main").width();
    var windowwidth = $(window).width();
    if (windowwidth > 1424) {
        $("aside.feature").css("width", "1144px");
    }
    else {
        $("aside.feature").css("width", "auto");

    }

    if(windowwidth > 1424)
        $("#skip-intro").css("left", "888px");
    else if (windowwidth > 970)
        $("#skip-intro").css("left", (windowwidth/2 + 175)+"px");
    else
        $("#skip-intro").removeAttr("style");
}

$(document).ready(function () {

    image_url = new Array();
    image_url[0] = "/images/home/series_1/slide1.jpg";
    image_url[1] = "/images/home/series_1/slide2.jpg";
    image_url[2] = "/images/home/series_1/slide3_empty.jpg";
    image_url[3] = "/images/home/series_1/slide4.jpg";
    image_url[4] = "/images/home/series_1/slide5.jpg";
    image_url[5] = "/images/home/series_1/glass1.jpg";
    image_url[6] = "/images/home/series_1/glass2.jpg";
    image_url[7] = "/images/home/series_1/glass3.jpg";
    image_url[8] = "/images/home/series_1/glass4.jpg";
    image_url[9] = "/images/home/series_1/glass5.jpg";
    image_url[10] = "/images/home/series_2/slide1.jpg";
    image_url[11] = "/images/home/series_2/slide2.jpg";
    image_url[12] = "/images/home/series_2/slide3.jpg";
    image_url[13] = "/images/home/series_3/slide1.jpg";
    image_url[14] = "/images/home/series_3/slide2.jpg";
    image_url[15] = "/images/home/series_3/slide3.jpg";
    image_url[16] = "/images/home/series_4/arrow_carousel.png";
    image_url[17] = "/images/home/series_4/fruitfamily_bottles.jpg";
    image_url[18] = "/images/home/series_4/fruitfamily_boxes.jpg";
    image_url[19] = "/images/home/series_4/sodafamily.jpg";
    image_url[20] = "/images/home/series_4/energyfamily.jpg";

    var i = 0;
    for (i = 0; i <= 20; i++)
        $('<img/>')[0].src = image_url[i];

    playIntro(); /* start intro on pageload */

    /* click to skip intro */
    $('#skip-intro').click(function () {
        if (introPlaying) {
            skipIntro();
        }
        else {
            $('#skip-intro').text("Skip Intro");
            playIntro();
        }
    });


    $(window).resize(function () {
        if ($.browser.msie) {
            rtime = new Date();
            if (timeout === false) {
                timeout = true;
                setTimeout(resizeend, delta);
            }
        }
        movebar();
    });
    movebar();


});


function playIntro() {
    $('#series4').fadeOut(600, function () { quickHide(introPlaying); });  /* fadeout series4 when replaying intro */
    $('#final-carousel').cycle('stop'); /* initialize jquery cycle */
    $('#lead-box').find('*').removeAttr('style'); /* clear out all jquery styling from fadeouts when replaying intro */

    timeoutarray = []; /* global array of timeouts ids */

    introPlaying = true;
    currentTime = 0;


    var testCSS;

    $('#lead-box').find('*').each(function (index) { });

    i = 0;

    //$.get("http://quirksmode.org/pix/homepage_tables.gif");

    /* this here is a long series of timeouts for sequential animation*/
    /* timing constants are global variables, currentTime is sequentially added for every animated sequence */
    /* in other words...."Javascript never stops" */

    /*series 1*/


    timeoutarray[i] = setTimeout(function () {
        $("#series1").fadeIn(SERIESTRANSITION);

    }, currentTime); i++;
    currentTime = SERIESTRANSITION + NORMALDELAY + currentTime;
    timeoutarray[i] = setTimeout(function () { $("#series1 .slide1").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = TRANSITIONTIME + NORMALDELAY + currentTime;
    timeoutarray[i] = setTimeout(function () { $("#series1 .slide2").fadeOut(TRANSITIONTIME); $("#series1 .lead-text1").fadeOut(TRANSITIONTIME); $("#series1 .lead-text2").fadeIn(TRANSITIONTIME); }, currentTime); i++;
    currentTime = TRANSITIONTIME + DRINKTRANSITIONTIME + currentTime;
    timeoutarray[i] = setTimeout(function () { $("#series1 .animated-drink-box").fadeIn(TRANSITIONTIME); }, currentTime); i++;
    currentTime = TRANSITIONTIME + currentTime;
    timeoutarray[i] = setTimeout(function () { $("#series1 .animated-drink1").fadeOut(DRINKANIMATIONDELAY); }, currentTime); i++;
    currentTime = currentTime + DRINKANIMATIONDELAY + DRINKANIMATIONTWEEN;
    timeoutarray[i] = setTimeout(function () { $("#series1 .animated-drink2").fadeOut(DRINKANIMATIONDELAY); }, currentTime); i++;
    currentTime = currentTime + DRINKANIMATIONDELAY + DRINKANIMATIONTWEEN;
    timeoutarray[i] = setTimeout(function () { $("#series1 .animated-drink3").fadeOut(DRINKANIMATIONDELAY); }, currentTime); i++;
    currentTime = currentTime + DRINKANIMATIONDELAY + DRINKANIMATIONTWEEN;
    timeoutarray[i] = setTimeout(function () { $("#series1 .animated-drink4").fadeOut(DRINKANIMATIONDELAY); }, currentTime); i++;
    currentTime = currentTime + DRINKANIMATIONDELAY + DRINKANIMATIONTWEEN + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series1 .slide3").fadeOut(TRANSITIONTIME); $("#series1 .lead-text2").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series1 .slide4").fadeOut(TRANSITIONTIME); $("#series1 .animated-drink5").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series1").fadeOut(TRANSITIONTIME); }, currentTime); i++;


    /*series 2*/

    timeoutarray[i] = setTimeout(function () { $("#series2").fadeIn(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series2 .slide1").fadeOut(TRANSITIONTIME); $("#series2 .lead-text1").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series2 .slide2").fadeOut(TRANSITIONTIME); $("#series2 .lead-text3").fadeIn(50); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series2 .lead-text3").fadeOut(10); $("#series2").fadeOut(TRANSITIONTIME); }, currentTime); i++;


    /*series 3*/

    timeoutarray[i] = setTimeout(function () { $("#series3").fadeIn(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series3 .slide1").fadeOut(TRANSITIONTIME); $("#series3 .lead-text1").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series3 .slide2").fadeOut(TRANSITIONTIME); }, currentTime); i++;
    currentTime = currentTime + TRANSITIONTIME + NORMALDELAY;
    timeoutarray[i] = setTimeout(function () { $("#series3").fadeOut(TRANSITIONTIME); }, currentTime); i++;


    /* series 4 */
    /*this calls skipintro()*/
    timeoutarray[i] = setTimeout(function () { skipIntro(); }, currentTime); i++;

}

function skipIntro() {
    $('#series1, #series2, #series3').fadeOut(600, function () { quickHide(introPlaying); });
    $('#skip-intro').text("Play Intro");
    introPlaying = false;
    clearAllTimeOuts();
    i = 0;
    $("#series4").fadeIn(TRANSITIONTIME);
    $('#final-carousel').cycle();
}

/* clears out all timeouts */
function clearAllTimeOuts() {

    for (j = 0; j < timeoutarray.length; j++) {
        clearTimeout(timeoutarray[j]);
    }
}

function quickHide(introplaying) {
    if (introplaying == true) {
        $('#series4').hide();
    }
    else {
        $('#series1, #series2, #series3').hide();
        clearAllTimeOuts();
    }
}
