/**
* Hero Viewer
*
* @version	1.0
* @author	Jasal Vadgama - Live Nation UK
* @require	jquery
* @license	GPL v3
**/

currentPanel = 0;

function heroInit() {
    // clone menu and add to hero player
    $(".slider").html($(".heroMenu").clone().removeClass("heroMenu"));

    // set ul width
    $(".slider ul").css({
        width: ($(".slider img").width() * $(".heroMenu li").length) + "px"
    });
    $(".menuSlider ul").css({
        width: ($(".menuSlider li").width() * $(".heroMenu li").length) + 1 + "px"
    });

    $(".heroMenu a").each(function(i) {
        $(this).click(function() {
            swapImage(i);
            return false;
        });
    });

    $(".heroMenu a:first").find("img").addClass("active");

    setHover();

    setTimer();
}

function swapImage(panelToShow) {
    clearTimeout(t);

    // make sure panel is in range
    if (panelToShow > $(".heroMenu li").length - 1) { currentPanel = 0; }
    else if (panelToShow < 0) { currentPanel = $(".heroMenu li").length - 1 }
    else { currentPanel = panelToShow; }

    // scroll to panel
    scrollPos = $(".slider img").width() * currentPanel;
    $(".slider ul").animate({ marginLeft: "-" + scrollPos + "px" }, "slow");

    $(".heroMenu .active").removeClass("active");
    $(".heroMenu a").eq(currentPanel).find("img").addClass("active");

    menuScrollPos = ($(".menuSlider img").width() + 5) * ($(".menuSlider li").length - 7);

    if (currentPanel > 4) { $(".heroMenu").animate({ marginLeft: "-" + menuScrollPos + "px" }, "slow"); }
    else { $(".heroMenu").animate({ marginLeft: "0px" }, "slow"); }

    setTimer();
}

function setHover() {
    $("<div class='controls'><a class='prev' href='#' title='Previous item'>Prev</a><a class='next' href='#' title='Next item'>Next</a></div>").appendTo($(".slider"));

    $(".slider").mouseover(function() { $(".controls").css({ display: "block" }); }).mouseout(function() { $(".controls").css({ display: "none" }); });

    $(".controls .prev").click(function() {
        swapImage(currentPanel - 1);
        return false;
    });

    $(".controls .next").click(function() {
        swapImage(currentPanel + 1);
        return false;
    });
}

function setTimer() {
    t = setTimeout("swapImage(" + (currentPanel + 1) + ");", 5000);
}