bing = {}
var shSpeed = 400;
var isOut = false;
var to = 0;
var timeOuts = new Array();
var feedLoaded = false;
$(document).ready(function() {
	bing.init();
});

bing = {
	init:function(){
		$('.single_hero').fadeTo(10,0);
		$('.single_hero_first').fadeTo(shSpeed,1);
		$('.single_hero_first_widget').fadeTo(shSpeed,1);
		$('.navMenu').fadeTo(shSpeed, 0).hide();
		$('.navMenu').find('.flyout_transparent').fadeTo(shSpeed, 0);
		$('.navMenu').find('p').fadeTo(shSpeed, 0);
		$('.highlightNav').fadeTo(shSpeed, 0);	


		bing.util.initLeftNav();
		bing.util.initSlider();
		//bing.util.expander();
	}
}

bing.util = {
	initLeftNav: function(){
		
		
		$("ul#listNav > li").each(function(i){
			var newIdx = i+1;
			/*var thisEl = $(this).find('h2')[0];
			var newWidth = $(thisEl).width() + 11;

			$(this).width(newWidth);*/
			$(this).bind({
				mouseenter: function(){
					//if($(".single_hero").is(":animated")){
						// NOTHING
					//}else{
						clearTimeout(timeOuts[0]);
						clearTimeout(timeOuts[1]);
						//$('.single_hero_first').stop(true,true).fadeTo(1200,0);
						//$("#theGrey").stop(true,true).delay(600).show().fadeTo("fast", 1);
						$('.navMenu').fadeTo('fast', 0).hide();
						$('.navMenu').find('.flyout_transparent').fadeTo(shSpeed, 0).hide();
						$('.navMenu').find('p').fadeTo(shSpeed, 0).hide();
						$('.navMenu').prev().find('a').css('text-decoration', 'none');
						$('.navMenu:eq('+i+')').prev().find('a').css('text-decoration', 'underline');
						timeOuts[0] = setTimeout(function(){bing.util.fadeThisin(this, newIdx)}, 500);
						
					//}
				},
				mouseleave: function(){
					//if($(".single_hero").is(":animated")){
						// NOTHING
					//}else{
						clearTimeout(timeOuts[0]);
						clearTimeout(timeOuts[1]);
						
						$(this).find('.navMenu').fadeTo(shSpeed, 0).hide();
						$(this).find('.navMenu').find('p').fadeTo(shSpeed, 0).hide();
						$(this).find('.navMenu').find('.flyout_transparent').fadeTo(shSpeed, 0).hide();
						$(this).find('.highlightNav').fadeTo(shSpeed, 0).hide();
						$(this).find('a').css('text-decoration', 'none');
						
						timeOuts[1] = setTimeout(function(){bing.util.showFirst(this)}, 600);
					//}
				}
			});
		});
		
	},
	showFirst : function(el){
		clearTimeout(timeOuts[1]);
		//$("#theGrey").fadeTo("fast", 0).hide();
		
		$('.single_hero').fadeTo(500, 0);
		$('.single_hero_first').stop(true, true).show().fadeTo(500,1);
		$('.single_hero_first_widget').stop(true, true).show().fadeTo(500,1);
	},
	fadeThisin : function(el, newIdx){
		var idx = newIdx - 1;
		$('.single_hero').fadeTo(500,0);
		$('.single_hero:eq('+newIdx+')').stop(true, true).show().fadeTo(500, 1);
		$('.highlightNav').fadeTo('fast', 0).hide();
		$('.single_hero_first_widget').fadeTo(500,0);

        //$("#theGrey").fadeTo("fast", 0).hide();
        $('.navMenu:eq(' + idx + ')').stop(true, true).delay(400).show().fadeTo(800, 1);
        $('.navMenu:eq(' + idx + ')').find('p').stop(true, true).delay(400).show().fadeTo(800, 1);
        $('.navMenu:eq(' + idx + ')').find('.flyout_transparent').stop(true, true).delay(400).show().fadeTo(800, 0.6);
        $('.highlightNav:eq(' + idx + ')').stop(true, true).delay(400).show().fadeTo(800, 0.25);

        //$('.single_hero_first').hide();
    },
    expander: function () {
        if (feedLoaded) {
            $('#feed_content_copy p span').expander({
                slicePoint: 120,  // default is 100
                expandText: '' // default is 'read more...'
            });
        }
    },
    initSlider: function () {
        var currentLink;
        var doRotate = true;
        var timeouts = new Array();
        currentLink = "";
        $(document).ready(function () {

            $('#feed_content_copy .feed_content_copy_single').hide().css("top", -41);
            $('#feed_content_copy .feed_content_copy_single:first').css('top', 12).show();

            currentLink = $('#feed_content_copy .feed_content_copy_single:first');

            initHandler();

            //timeouts[0] = setTimeout(function(){switchTab(currentLink)}, 7000);
            timeouts[1] = setTimeout(rotate, 3000);
            //currentLink.click(stopRotate);
        });

        function rotate() {
            if (doRotate) {
                var nextLink;
                if ($(currentLink).hasClass("last_feed")) {
                    $("#feed_content_copy .feed_content_copy_single:first").css("top", -41);
                    nextLink = $("#feed_content_copy .feed_content_copy_single:first");
                } else {
                    nextLink = $(currentLink).next();
                }
                switchTab(nextLink);
                currentLink = nextLink;

                timeouts[2] = setTimeout(rotate, 4000);

            }
        }
        function stopRotate() {
            clearTimeout(timeouts[0]);
            clearTimeout(timeouts[1]);
            clearTimeout(timeouts[2]);
            clearTimeout(timeouts[3]);
            timeouts[0] = 0;
            timeouts[1] = 0;
            timeouts[2] = 0;
            timeouts[3] = 0;
            doRotate = false
        }

        function restartRotate() {
            doRotate = true;
            rotate();
        }

        function switchTab(selected) {

            $(selected).css('top', -41);
            $(currentLink).animate({
                top: 41
            }, 500, function () {
                $(this).hide();
                $(selected).show().animate({
                    top: 12
                }, 500);
            });
        }

        function initHandler() {
            if (feedLoaded) {
                $('#feed_control a').click(function () {
                    if ($(this).hasClass('play') == true) {
                        $(this).find('img').css('top', -19);
                        stopRotate();
                        $(this).removeClass('play');
                    } else {
                        $(this).find('img').css('top', 0);
                        restartRotate();
                        $(this).addClass('play');
                    }
                });
            }
        }

    }
}

