
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};


//IE
var isIE = !$.support.htmlSerialize,
    isIE7 = isIE && parseInt($.browser.version,10) === 7,
    isIE8 = isIE && parseInt($.browser.version,10) === 8;
        
        
var showcase = {};

showcase.quicknav = (function ($){
    var navitems, infobox, tiptimer, opened, infoOpened;
    
    function reset () {
        navitems.find('li').each( function () {
            $(this).stop().css({ color: '#FFFFFF', backgroundPosition: 'right 0' })
        });
    }
    
    function clearAll() {
        
        clearTimeout( tiptimer );
        if ( !infoOpened ) {
            infobox.stop().css({
				height: '378px',
				width: '825px',
				top: '85px',
				left: '130px',
				overflow: 'visible',
				opacity: 1
			});
        }
    }
    
    function bind () {
        navitems.find('li').mouseenter( function () {
            clearAll();

            var el = $(this),
                cls = el.attr('class'),
                current = infobox.find('#'+cls);

            if ( opened !== cls ) {
                
                reset();
                el.stop().animate({
                        color: '#FF6600'
                    }, 200, function () {
                        el.css({ backgroundPosition: 'right -23px' });
                    });
				
				infobox.find('div.twoColumnShell').css({ display: 'none' });
				infobox.show( 800, function (c) {
					infoOpened = true;
					current.fadeIn( 400 )
				});
				
                opened = cls;
            }
            
        }).mouseleave( function () {
        
            tiptimer = setTimeout( function () {
                opened = null;
                if(isIE8) {
                	infobox.hide();
                } else {
                	infobox.hide(700);
                }
               
                infoOpened = false;
                reset();
            }, 100);
        });
        
        infobox.mouseenter( function () { clearTimeout( tiptimer ); })
               .mouseleave( function () {
                    tiptimer = setTimeout( function() {
                        opened = null;
                if(isIE8) {
                	infobox.hide();
                } else {
                	infobox.hide(700);
                }
                        infoOpened = false;
                        reset();
                    }, 100);
                });
    }
    
    $(document).ready( function() {
        navitems = $('.navItems', '#showcase');
        infobox = $('#infoBox');
        
        bind();
    });
    
}(jQuery));


$(document).ready(function() {
    
    //Init
    var timeOut, timeOut2,
        count = 1,
        wait = 7000,
        paused = false, 
        //carousel vars
        carouselItems = $('.showPromo').length,
        carouselFull = carouselItems - 2,
        calc = carouselItems * 310,
        displacement = calc+'px',
        promoCount = 1,
        moving = false,
        $arrowL = $('#arrowL'),
        $arrowR = $('#arrowR');
        
        $('#quickNav ul.navItems li:last').css('border-bottom-width', 0);
        //console.log($('#quickNav ul.navItems li:last'));
    
    if(promoCount == 1){
        $arrowL.addClass('disable');
    }
    if(promoCount == carouselFull){
        $arrowR.addClass('disable');
    }
    $('#displace').css('width', displacement);
    
    //Showcase promos
    var items = $('.showItem').length,
        $status = $('#statusBar .status'),
        $showItem = $('.showItem'),
        $pause = $('#pause'),
        $next = $('#next');
        
    $showItem.each(function(i) {
        $('#showNav').append($('<div />').attr('class', 'block').attr('index', i));
    })
    
    $block = $('.block');
    
    /*$block.each(function(i) {
        $(this).attr('index', i);
    });*/
    
    //adjust width for IE7
    if(isIE7) {
        var w = items*16;
        $('#containNav').width(w);
    }
    
    
    
    $pause.add($next).css('opacity', '0');
    
    //bring in quicknav and promos
    $(window).load(function() {
        $showItem.filter(':first').fadeIn(500);
        $block.filter(':first').css({ 'background': '#FFF', 'border-color': '#CCC' });
        $('#navControls, #quickNav').fadeIn(500);
        //$('#quickNav').animate({ 'top': '22px' }, 2000);
        status('start');
        var promoAnimateDelay = 100;    
        $('.showPromo','#carousel').each(function(i,el) {
            var $el = $(el);
            $el.attr('data-index',i+1);

            function animatePromo() {
                $el.fadeIn(500);
            }
            animatePromo();
            function showArrows() {
                $arrowL.add($arrowR).fadeIn('slow')
            }
            if ( i===3 ) {
                setTimeout(showArrows,500)
            }
        });

    
    
    })
    
    // block nav clicks
    $block.click(function() {
        status('stop');
        if(paused == true) {
            $status.pause();
        }else{
            $pause.css('background-image', 'url(/en_US/Media/images/homepage/btn_play.png)');
            $status.pause();
            paused = true;
        }
        
        var clicked = $(this).attr('index');
        //console.log(clicked+' = '+count);
        if((count - 1) != clicked) {
            $showItem.css('display', 'none');
            $block.css({'background': '#ff5109', 'border-color': '#cc6600'});
            $showItem.eq(clicked).fadeIn(800);
            $block.eq(clicked).css({'background': '#FFF', 'border-color': '#CCC'});
            count = parseInt(clicked) + 1;
        }
    });
    
    
    /**
    * KF - I don't want to just delete this encase we need to rollback to this functionality,
    * Once approved this code will be deprecated by the showcase.quicknav object.
    
    * --START--
    
    var $infoBox = $('#infoBox'),
        $navItems = $('ul.navItems','#showcase');
    
    // quick link widget 

    $navItems.find('li').hover(function() {
        var $this = $(this),
            activeItem = $this.attr('class');
        $this.animate({color: '#ff6600'}, 200).css('background-position', 'right -23px');
        $infoBox.find('div.twoColumnShell').css('display', 'none');
        $('#' + activeItem).fadeIn(800);
    }, function () {
        $(this).animate({color: '#FFF'}, 200).css('background-position', 'right 0');
    });
    
    $infoBox.find('div').hover(function() {
        var activeItem = $(this).attr('id');
        $('.' + activeItem).animate({color: '#ff6600'}, 200).css('background-position', 'right -23px');
    }, function() {
        $navItems.find('li').animate({color: '#FFF'}, 200).css('background-position', 'right 0');
    });
    
    $('#hpOverlay').click(function() {
        hideToolTip();
    })
    
    $navItems.add($infoBox).hover(function() {
        clearTimeout(timeOut);
        $infoBox.show(800);
        $('#hpOverlay').fadeIn(500);
        if(paused != true){
            $status.pause();
        }
    }, function() {
        timeOut = setTimeout(hideToolTip, 800);
    }); 
    
    * --END--
    */

    
    // controls
    
    $('#navControls').mouseenter(function() {
        clearTimeout(timeOut2);
        var _pos = $('#showNav').width() + 5;
        $pause.animate({
            'left': _pos + 'px',
            'opacity': '1' 
        },300);
        $next.animate({
            'left': (_pos + 20) + 'px',
            'opacity': '1'
        },600);
    }).mouseleave(function() {
        timeOut2 = setTimeout(hideBtns, 600);
    });
    
    
    $pause.click(function() {
        if(paused != true) {
            $(this).css('background-image', 'url(/en_US/Media/images/homepage/btn_play.png)');
            $status.pause();
            paused = true;
        }else {
            $(this).css('background-image', 'url(/en_US/Media/images/homepage/btn_pause.png)');
            $status.resume();
            paused = false;
        }
    });
    
    $next.click(function() {
        paused = true;
        $pause.css('background-image', 'url(/en_US/Media/images/homepage/btn_play.png)');
        showNext();
        status('stop');
        $status.pause();    
    });
    
    //carousel mouseovers
    var promoHover = $('.showPromo');
    promoHover.mouseenter(function(){

        var current = this;
        promoHover.each( function() {
            if(this !== current ) {
                $(this).stop().animate({ opacity: .5 }, 200);
            }
        });

    }).mouseleave(function(){

        var current = this;
        promoHover.each( function() {
            if(this !== current ) {
                $(this).stop().animate({ opacity: 1 }, 200);
            }
        });
    });

    $arrowL.click(function() {
        $arrowR.removeClass('disable');
        
        if(moving == false && !$(this).hasClass('disable')){
            promoCount--;
            movePromos('left');
        }
        if(promoCount == 1) {
            $(this).addClass('disable');
        }
    });

    $arrowR.click(function() {
        $arrowL.removeClass('disable');
        
        if(moving == false && !$(this).hasClass('disable')){
            ++promoCount;
            movePromos('right')
        }
        
        if(promoCount == carouselFull){
            $arrowR.addClass('disable');
        }
    });
    


function movePromos(direction) {
    moving = true;
    if(direction == 'left') { 
        sign = '+'; 
    } else {
        sign = '-';
    }   
    $('#displace').animate({
        marginLeft: sign+'=310'
    }, 400, function() {
        moving = false;
    })
}

//hide functions
function hideBtns() {
    if(paused != true) {
        $next.stop(true).animate({
            'left': '100px',
            'opacity': '0'
        },300);
        $pause.stop(true).animate({
            'left': '80px',
            'opacity': '0' 
        },600);
    }
}

function hideToolTip() {
    $navItems.find('li').css('color', '#fff');
    $infoBox.stop(true, true).hide(800);
    $('#hpOverlay').stop(true, true).fadeOut(500);
    if(paused != true) {
        $status.resume();
    }
}


// timer status
function status(action) {
    if(action == 'start') {
        $status.animate({
            width: '100%'
        }, wait, function() {
            $status.css('width', '0%');
            showNext();
            status('start');
        });
    }else if(action == 'stop') {    
        $status.stop(true).css('width', '0%');
        status('start');
    }
}

//show next billboard

function showNext() {
    //console.log(items+' = '+count);
    $showItem.css('display', 'none');
    $block.css({'background': '#ff5109', 'border-color': '#cc6600'});
    if(items > count) {
        $showItem.eq(count).fadeIn(800);
        $block.eq(count).css({'background': '#FFF', 'border-color': '#CCC'});   
        count++;
    }else {
        count=0;
        $showItem.eq(count).fadeIn(800);
        $block.eq(count).css({'background': '#FFF', 'border-color': '#CCC'});
        count++;
    }
    
}

//get param function
function GetParam(name) {
    var start=location.search.indexOf("?"+name+"=");
    if (start<0) start=location.search.indexOf("&"+name+"=");
    if (start<0) return '';
    start += name.length+2;
    var end=location.search.indexOf("&",start)-1;
    if (end<0) end=location.search.length;
    var result='';
    for(var i=start;i<=end;i++) {
        var c=location.search.charAt(i);
        result=result+(c=='+'?' ':c);
    }
    return unescape(result);
}

});
