﻿var easein = 'easeOutCubic';
var topAdjust = 0; //this adjusts the veritical positon of the promo hero
var splashScreen = true;
var timer;
var sliding;

$(function () {
    sliding = false;

    if (browserInWhiteList(true)) {

        navoff_x = 42;
        setSocialNavPosition();

        $('#whatsNewShareSide').css('z-index', 10001);

        $('.bemail_li .dropdown-container').remove();
        checkSplash();

        $('#socialMediaHolder').hide();
        $('#hasJavaScript').show();
        $('#noscript').hide();
        $('#currentContentID').val(1);
        $('#mainRight').html($('#mainR0').html());

        //change to 800
        $('#mainRight').animate({ top: -900, left: 50 }, 1).delay(100)
            .animate({
                top: [topAdjust, easein], left: 50
            }, 1000, function () {

                $('#mainRightHolder').height(329);

                $('#featureNavHolder').css('top', '-24px');


                if ($.browser.msie) {
                    $('#mainRight, #mainRightAlt').css('background', 'url(../../images/homepage/promoBlank_Fall.jpg) no-repeat 0px 0px');
                    $('#mainRight img, #mainRightAlt img').remove();
                }
                //alert('wait');

               
                                $('#featureNavHolder').show(1, function () {
                                    $('#bottomNav').animate({ top: [topAdjust, easein] }, 100);
                                });

                $('#bottomNav').animate({ top: [topAdjust, easein] }, 100);
                //showTeaser();
            });


        $('#featureNav1, #featureNav2, #featureNav3').removeClass('currentFeature');


        $('#featureNav1, #featureNav2, #featureNav3').hover(
                function () {
                    highLight($(this).attr('id').replace('featureNav', ''), false);
                },
                function () {
                    highLight(getCurrentContentID(), true);
                }
            );

        clearInterval(timer);
        timer = window.setInterval(slide, 3000);

        $('.btnFeatureNav').click(function () {
            if (!sliding) {
                var thisID = parseInt($(this).attr('id').replace('featureNav', ''));



                clearInterval(timer);
                timer = window.setInterval(slide, 10000);
                $('#currentContentID').val(thisID);
                slide(250);
            }
        });

        $('.mlBtn').hide();


        $('#mainLeft2, #mainLeft3').animate({ opacity: 0 }, 1).delay(800)
            .animate({ opacity: 1
            }, 500, function () {



            });


        $('#sub1, #sub2, #sub3').animate({ opacity: 0 }, 1).delay(800)
            .animate({ opacity: 1
            }, 500, function () {



            });


        $('#mainLeft1').animate({ opacity: 0 }, 1).delay(800)
            .animate({ opacity: 1
            }, 500, function () {



            });

        $('.mlBtn').show().animate({ opacity: 0 }, 1).delay(800)
            .animate({ opacity: 1
            }, 500, function () {



            });
        //alert('inlooop');
        if ($.browser.msie) {
            if ($.browser.version == 6.0) {

            }


            if ($.browser.version < 8.0) {

                $('#bottomNavHolder').css('left', '0px');

                //zOrder IE fix
                /**/var zIndexNumber = 10000;
                $('div, a, ul, li').each(function () {
                    //console.log($(this).attr('id') + ' ' + zIndexNumber);
                    $(this).css('zIndex', zIndexNumber);
                    zIndexNumber -= 10;
                });

                //put navi buttons on top of animations.
                $('#featureNavHolder').css('zIndex', 10005);
                $('#ctl00_homeHref').css('position', 'relative');
                $('#ctl00_homeHref').css('position', 'absolute');

                $('.arrows').css('top', '-5px');


            }
            else if ($.browser.version == 8.0) {
                $('.arrows').css('top', '-3px');
            }


        }



    }
    else {

        //$('.main-nav').css('font-size', '13px !important');
        $('.main-nav').css("font-size", "13px");


    }
});

    function showTeaser() {

        $('#mainRightHolder').hide();
  

    var counter = '<div id="countdown_dashboard"><div class="dash days_dash dash1">'
    + '<div class="digit">0</div><div class="digit">0</div></div><div class="dash hours_dash dash2">'
    + '<div class="digit">0</div><div class="digit">0</div></div><div class="dash minutes_dash dash3">'
    + '<div class="digit">0</div><div class="digit">0</div></div></div>';

    var newContent = '<div><div style="height: 352px; width: 880px; margin: auto;"><a href="WhatsNew.aspx"><img src="/images/CountDown/Home_CountDown_03.png"></a>' + counter + '</div></div>';
    $('#hasJavaScript').before(newContent);

    countdown(2011, 9, 8);

    $('#countdown_dashboard').countDown({
        targetOffset: {
            'day': dday,
            'month': 0,
            'year': 0,
            'hour': dhour,
            'min': dmin,
            'sec': dsec
        },
        omitWeeks: true
    });
  //  $('#featureNavHolder').hide();
}


function countdown(yr, m, d) {
    var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    theyear = yr; themonth = m; theday = d;
    var today = new Date();
    var todayy = today.getYear();
    if (todayy < 1000)
        todayy += 1900;
    var todaym = today.getMonth();
    var todayd = today.getDate();
    var todayh = today.getHours();
    var todaymin = today.getMinutes();
    var todaysec = today.getSeconds();
    var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec;
    futurestring = montharray[m - 1] + " " + d + ", " + yr;
    dd = Date.parse(futurestring) - Date.parse(todaystring);
    dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1);
    dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1);
    dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1);
    dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1);
   // console.log(dd, dday, dhour, dmin, dsec);
}

function slide(time) {
    sliding = true;
    clearInterval(timer);
    timer = window.setInterval(slide, 10000);
    if (time != 250) time = 1500;

    getNextContentID()

    if ($('#currentDiv').val() == 'mainRight') {

        $('#currentDiv').val('mainRightAlt');


        slideIn(
            'mainRight',
            'mainRightAlt', time);



    }
    else {

        $('#currentDiv').val('mainRight');

        slideIn(
            'mainRightAlt',
            'mainRight', time);

    }

};


function slideIn(div1, div2, time) {

    var height = 305;
    $('#mainRightAlt').css("cssText", "height: " + height + "px !important");
    $('#mainRight').css("cssText", "height: " + height + "px !important");



    //alert($('#' + div2).attr('zIndex'));
    $('#' + div2).html($('#mainR' + getNextContentID()).html());


    //alert(div2 + ' ' + div1);


    //  $('#featureNavHolder').css('zIndex', 10005);


    //console.log(div1 + ' - ' + div2)
    //$('#' + div1).css('zIndex', 10005);
    

    $('#featureNav1, #featureNav2, #featureNav3').removeClass('currentFeature');
    $('#' + div1)
    .animate({ opacity: 0
    }, time);
    //alert($('#' + div1).html());



    var top = topAdjust - height;

    if (div2 == 'mainRight') {
        top = topAdjust;

    }

    //console.log(top);
    $('#' + div2).animate({ opacity: 0, left: 50, top: top }, 1,
        function () {

            incrementContentID();
            var number = getCurrentContentID();
            //console.log(number);
            highLight(number, true);

        }).show()
        .animate({ opacity: 1,
            left: [50, 'easeInBack']
        }, time, function () {



            //console.log(div2);
            addHeroClick(div2);
            sliding = false;
        });

}


function highLight(number, clearOthers) {


    if (clearOthers) {
        $('#featureNav1').removeClass('currentFeature1');
        $('#featureNav2').removeClass('currentFeature2');
        $('#featureNav3').removeClass('currentFeature3');
    }
    //console.log(getCurrentContentID());
    $('#featureNav' + number).addClass('currentFeature' + number );



    Cufon.replace('.btnFeatureNav', {
        color: '#939598'
    });

    Cufon.replace('.currentFeature1', {
        color: '#FFFFFF'
    });
    Cufon.replace('.currentFeature2', {
        color: '#FFFFFF'
    });
    Cufon.replace('.currentFeature3', {
        color: '#FFFFFF'
    });
}


function getNextContentID() {
    var id = $('#currentContentID').val();
    return id;
};

function getCurrentContentID() {

    var id = getNextContentID();


    id = id - 1;
    if (id < 1)
        id = 3;
    return id;
};

function incrementContentID() {
    var id = $('#currentContentID').val();
    id = parseInt(id);
    id = id + 1;
    if (id > 3)
        id = 1;
    $('#currentContentID').val(id);
}


function addHeroClick(div) {

    $('#mainRightHolder').unbind('click');

    $('#mainRight, #mainRightAlt').removeClass('clickable');


    if ($('img', '#' + div).attr('alt') != 'NEW FARM-FRESH LOOK') {




        $('#mainRight, #mainRightAlt').addClass('clickable');

        $('#mainRightHolder').unbind('click');
        $('#mainRightHolder').click(function () {

            if ($('#currentContentID').val() > -1) {

                var aspPrepend = "ctl00_ContentBody_";

                var href = $("#" + aspPrepend + "link" + getCurrentContentID()).html();

                window.location = href;
            }

        });




    }
    else {
        //#mainRight, #mainRightAlt
        if (div == 'mainRight') {
            $('#mainRightAlt').css('zIndex', 100001);
            $('#mainRight').css('zIndex', 10002);
        }
        else {
            $('#mainRightAlt').css('zIndex', 10002);
            $('#mainRight').css('zIndex', 10001);
        }


       // alert(div);



        if ($.browser.version < 8.0) {
            $('#' + div).prepend('<div title="Farm-Fresh Goodness Pledge" id="spot-click" style="position: relative; width: 194px; height: 35px; top: 42px !important; left: -360px;" class="clickable"></div>');
        }
        else {
            $('#' + div).prepend('<div title="Farm-Fresh Goodness Pledge" id="spot-click" style="position: relative; width: 194px; height: 35px; top: 42px !important; left: 0;" class="clickable"></div>');
        }
        $('#' + div + ' IMG').css("cssText", "top: -24px !important");

        $('.clickable').css('zIndex', 10003);

        $('.clickable').unbind('click');

        $(function () {
            //console.log($('.clickable').html());
            $('.clickable').click(function () {

               
                if ($('#currentContentID').val() > -1) {

                    var aspPrepend = "ctl00_ContentBody_";

                    var href = $("#" + aspPrepend + "link" + getCurrentContentID()).html();

                    window.location = href;
                }

            });
        })
        

        $('#' + div).removeClass('clickable');
        $('#mainRightHolder').unbind('click');
    }



   



   


//    if ($('img', '#mainRightAlt').attr('alt') != 'NEW FARM-FRESH LOOK') {
//        $('#mainRightAlt').addClass('clickable');
//    }
//    else {
//        $('#mainRightAlt').removeClass('clickable');
//    }
}


function checkSplash() {

    $.ajax({
        url: '/BEMail/HomePageSplash',
        cache: false,
        success: function (hasCookie) {
            if (!hasCookie) {
                showDialog();
            }
        }
    });
}

function showDialog() {



    $.ajax({
        url: '/BEMail/HomePageSplashDialog',
        success: function (html) {

            var div = $('<div id="dlg" style="display:none;">' + html + '</div>');
            $('div').last().append(div);
            $('#dlg').dialog({
                zIndex: 20000000,
                width: 840,
                height: 548,
                modal: true,
                position: 'center',
                resizable: false,
                draggable: false
            });


            $('#btn-dialog-close').click(
                function () {
                    $('#dlg').dialog('close');
                    return false;
                }
            );
        }
    });

}






