﻿var heroHeader_CurrentIndex = 0;
var heroHeader_PreviousIndex = 0;
var heroHeader_IsScrolling = false;
var heroHeader_Timer = null;
var heroHeader_MaxWidth = 1200;

function HeroHeader_FaderNext()
{
    if (heroHeader_Timer != null) clearTimeout(heroHeader_Timer);
    
    var previousLI = heroHeader_CurrentIndex;
    var currentLI;
    
    heroHeader_CurrentIndex++;
    if (heroHeader_CurrentIndex >= hero_ItemList.length-1)
        heroHeader_CurrentIndex=0;
        
    currentLI = heroHeader_CurrentIndex;
    
    if (currentLI == previousLI) return;
    
    var content = HeroHeader_GetImageHTML(currentLI, hero_ItemList[currentLI], $("#divHeroContent" + hero_ItemList[currentLI].index).html(), 'headerfaderli');
    $("#headerFadeul").prepend(content);
    $("[index=" + previousLI + "]","#headerFadeul").fadeOut(2000, function() { $("[index=" + previousLI + "]","#headerFadeul").remove(); } );
    
    heroHeader_Timer = setTimeout(HeroHeader_FaderNext, 9000);
}

function HeroHeader_GetWindowWidth()
{
    if (heroHeader_Timer != null) return "100%";
    var windWidth = document.body.offsetWidth;
    if (windWidth < 960) windWidth = 960;
    if (windWidth > heroHeader_MaxWidth) windWidth = heroHeader_MaxWidth;
    if (windWidth > 960 && windWidth < heroHeader_MaxWidth) windWidth = document.body.offsetWidth;
    return windWidth;

}
function SetArrowPosition(windWidth) {
    var Pos = (windWidth - 960) / 2;
    //$('.jcarousel-prev').css('left', Pos + heroLeft);
    //$('.jcarousel-next').css('right', Pos + heroRight);
}
function HeroHeader_ItemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = 1;

    if (carousel != null && carousel != undefined)
    {
        idx = carousel.index(i, hero_ItemList.length-1);
        var divHtml = HeroHeader_GetContentHTML(i, hero_ItemList[idx-1], false)
        carousel.add(i, divHtml);
    }
    else
    {
        $("#ulHeroHeader").append(HeroHeader_GetContentHTML(i, hero_ItemList[idx-1], true));
    }
    
    if (heroHeader_CurrentIndex < i)
        GetHeroBGUL().append(HeroHeader_GetImageHTML(i, hero_ItemList[idx-1]));
    else
        GetHeroBGUL().prepend(HeroHeader_GetImageHTML(i, hero_ItemList[idx-1]));
        
    // we'll specify the heroHeader_CurrentIndex, so the next/prev know what to move to
    heroHeader_PreviousIndex = heroHeader_CurrentIndex;
    heroHeader_CurrentIndex = i;
}

function HeroHeader_ItemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
}

function HeroHeader_GetImageHTML(index, item, divHtml, className)
{
    if (divHtml == undefined || divHtml == null) divHtml="";
    if (className == undefined || className == null) className="";
    
    
    return '<li id="liHeroHeaderBG' + index + '" index="' + index + '" class="headerbgli ' + className + '" style="width: ' + HeroHeader_GetWindowWidth() + 'px;">' +
            '<div class="headerbgdiv" style="background-image:url(' + item.url + ');">'  + 
                divHtml +
            '</div></li>';
}

function HeroHeader_GetContentHTML(index, item, withLI, display)
{
    var divHtml = $("#divHeroContent" + item.index).html();
    if (display != "none") display="";
    
    if (withLI)
        return '<li class="liHeroHeaderID" id="liHeroHeader' + index + '" index="' + index + '" style="width:' + HeroHeader_GetWindowWidth() + 'px; height: 364px; display: ' + display + ';">' +
                    divHtml +
               '</li>';
    else
        return divHtml;

}


function HeroHeader_SetBGWidth() {
    SetLiWidth();
    SetULWidth();
    ResetLiLocation();
    ResetRotator();
}

function ResetRotator() {
    var wid = HeroHeader_GetWindowWidth();
    $('#headerbgcontainer').width(wid);
    $('#liHeroHeaderBG1').width(wid);

    /*$('#divHeroheader').width(wid);
    $('.liHeroHeaderID').width(wid);
    /$('#divHeroCarousel').width(wid);
    $('#ulHeroHeader > li').width(wid);
    SetArrowPosition(wid);*/
}
function GetHeroBGUL()
{
    return $('#headerbgul');
}

/* Events */

/* END Events */

/* Common Functions */
function ResetLiLocation() {
    GetHeroBGUL().css('left', '0px');
    GetHeroBGUL().width(HeroHeader_GetWindowWidth());
    $("#liHeroHeaderBG" + heroHeader_PreviousIndex).remove();
}
function SetULWidth() {
    if (GetHeroBGUL() == null) return;
    
    var totalWidth = 0;
    GetHeroBGUL().find(">*").each(function() {
        var objLi = $(this);
        totalWidth += objLi.outerWidth();
    });

    GetHeroBGUL().width(totalWidth);
}
function SetLiWidth() {
    if (GetHeroBGUL() == null) return;
    GetHeroBGUL().find('>li').each(function() {
        $(this).css('width', HeroHeader_GetWindowWidth() + "px");
    });
}
function NextClick() {
    heroHeader_IsScrolling = true;
    var objLi = GetHeroBGUL().find(">li[index='" + heroHeader_CurrentIndex + "']");
    if (objLi == undefined || objLi.attr('index') == undefined) return false;
    GetHeroBGUL().css('left', "0px" );
    GetHeroBGUL().width(HeroHeader_GetWindowWidth() * 2);
    GetHeroBGUL().animate({ "left": "-=" + HeroHeader_GetWindowWidth() + "px" }, "slow", "swing", function() { heroHeader_IsScrolling = false; ResetLiLocation(); });
    return true;
}
function PrevClick() {
    heroHeader_IsScrolling = true;
    var objLi = GetHeroBGUL().find(">li[index='" + heroHeader_CurrentIndex + "']");
    if (objLi == undefined || objLi.attr('index') == undefined) return false;
    GetHeroBGUL().css('left', "-" + HeroHeader_GetWindowWidth() + "px" );
    GetHeroBGUL().width(HeroHeader_GetWindowWidth() * 2);
    GetHeroBGUL().animate({ "left": "+=" + HeroHeader_GetWindowWidth() + "px" }, "slow", "swing", function() { heroHeader_IsScrolling = false; ResetLiLocation(); });
    return true;
}
/* END Common Functions */
