﻿var heroHeader_CurrentIndex = 0;
var heroHeader_PreviousIndex = 0;
var heroHeader_IsScrolling = false;

function HeroHeader_GetWindowWidth()
{
    var windWidth = document.body.offsetWidth;
    if (windWidth < 960) windWidth = 960;
    if (windWidth > 1200) windWidth = 1200;
    if (windWidth > 960 && windWidth < 1200) 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);
        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)
{
    var divHtml = ''; //$("#divHeroContent" + item.index).html();
    
    return '<li id="liHeroHeaderBG' + index + '" index="' + index + '" class="headerbgli" style="width: ' + HeroHeader_GetWindowWidth() + 'px;">' +
            '<div class="headerbgdiv" style="background-image:url(' + item.url + ');">'  + 
                divHtml +
            '</div></li>';
}

function HeroHeader_GetContentHTML(index, item, withLI)
{
    var divHtml = $("#divHeroContent" + item.index).html();
    
    if (withLI)
        return '<li class="liHeroHeaderID" id="liHeroHeader' + index + '" index="' + index + '" style="width:' + HeroHeader_GetWindowWidth() + 'px; height: 364px;">' +
                    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 */
