﻿var heroes_length = 0;
$.each(heroes, function(i, n) { if (n != null) heroes_length++; });
var heroScrollPos = 1;
var heroMaxScrollPos = Math.ceil(heroes_length / 4);
var photoScrollPos = 1;
var photoMaxScrollPos = Math.ceil(photoCount / 12);
if (photoMaxScrollPos > 3) photoMaxScrollPos = 3;
var musicScrollPos = 1;
var musicMaxScrollPos = 3;
var arrHero = new Array();
var currentHero = 0;
var lastCount = 0;
var intervalID = null;
var autoSideKick = true;

$(document).ready(function() {
    getHeroPlay();
    $("#LessHeroesButton").click(function(event) {
        if (heroScrollPos == heroMaxScrollPos) {
            return false;
        }
        $("#SideKickView").animate({ top: "-=260" });
        heroScrollPos += 1;
        ResetHeroButtons();
        autoSideKick = false;
        event.preventDefault();
    });
    $("#MoreHeroesButton").click(function(event) {
        if (heroScrollPos == 1) {
            return false;
        }
        $("#SideKickView").animate({ top: "+=260" });
        heroScrollPos -= 1;
        ResetHeroButtons();
        autoSideKick = false;
        event.preventDefault();
    });
    // photo scroll button
    $("#LessPhotosButton").click(function(event) {
        if (photoScrollPos == 1) {
            return false;
        }
        photoScrollPos -= 1;
        PopulatePhotos();
        ResetPhotoButtons();
        event.preventDefault();
    });
    $("#MorePhotosButton").click(function(event) {
        if (photoScrollPos == photoMaxScrollPos) {
            return false;
        }
        photoScrollPos += 1;
        PopulatePhotos();
        ResetPhotoButtons();
        event.preventDefault();
    });
    $("#LessMusicButton").click(function(event) {
        if (musicScrollPos == 1) {
            return false;
        }
        $("#MusicList").animate({ left: "+=575" });
        musicScrollPos -= 1;
        ResetMusicButtons()
        event.preventDefault();
    });
    $("#MoreMusicButton").click(function(event) {
        if (musicScrollPos == musicMaxScrollPos) {
            return false;
        }
        $("#MusicList").animate({ left: "-=575" });
        musicScrollPos += 1;
        ResetMusicButtons();
        event.preventDefault();
    });
    $("#SideKick a").click(function(event) {
        $("#Hero .Heroes").hide();
        $("#" + $(this).attr("rel")).fadeIn(300);

        for (i = 0; i < heroes_length; i++) {
            if ("Hero" + heroes[i].id == $(this).attr("rel")) {
                currentHero = i;
                break;
            }
        }
        autoSideKick = true;
        beginInterval();
        event.preventDefault();
    });

    //    if(heroes_length>8){heroScrollPos=2;PopulateHeroes(2);$("#SideKickView").animate({top: "-=260"});}
    //    else{PopulateHeroes(1);}
    PopulateHeroes();
    ResetPhotoButtons();
    ResetMusicButtons();

    $("#PichuTabs a").click(function(event) {
        $("#PichuTabs a").removeClass("select");
        $(this).addClass("select");
        $("#PichuCompContent").addClass("PichuContent");
        $("#PichuEventContent").addClass("PichuContent");
        $("#PichuMusicContent").addClass("PichuContent");
        $("#" + $(this).attr("id") + "Content").removeClass();
        event.preventDefault();
    });

    beginInterval();
});
function PopulateHeroes() {
    $("#MainHero").empty();

    for (i = 0; i < heroes_length; i++) {
        $("#MainHero").append("<div class=\"Heroes\" id=\"Hero" + heroes[i].id + "\"><a href=\"" + heroes[i].link + "\" " + heroes[i].target + "><img src=\"" + heroes[i].pic + "\" /></a><div class=\"HeroTitle\"><h1>" + heroes[i].title + "</h1><span>" + heroes[i].summary + "</span></div></div>");
    }

    ResetHeroButtons();
}
function PopulatePhotos() {
    $.get("ajax/getfrontphotos.ashx?page=" + photoScrollPos + "&count=12", function(data) {
        $("#GalleryPort").html(data);
        JT_init();
        statsTracker();
    });
}
function ResetHeroButtons() {
    if (heroMaxScrollPos <= 1) {
        $("#LessHeroesButton").removeClass();
        $("#LessHeroesButton").addClass("LessHeroesGrey");
        $("#MoreHeroesButton").removeClass();
        $("#MoreHeroesButton").addClass("MoreHeroesGrey");
    }
    else {
        $("#LessHeroesButton").removeClass();
        $("#LessHeroesButton").addClass("LessHeroes");
        $("#MoreHeroesButton").removeClass();
        $("#MoreHeroesButton").addClass("MoreHeroes");
        if (heroScrollPos == 1) {
            $("#MoreHeroesButton").removeClass();
            $("#MoreHeroesButton").addClass("MoreHeroesGrey");
        }
        if (heroScrollPos == heroMaxScrollPos) {
            $("#LessHeroesButton").removeClass();
            $("#LessHeroesButton").addClass("LessHeroesGrey");
        }
    }
}
function ResetPhotoButtons() {
    $("#LessPhotosButton").removeClass();
    $("#LessPhotosButton").addClass("LessPhotos");
    $("#MorePhotosButton").removeClass();
    $("#MorePhotosButton").addClass("MorePhotos");
    if (photoScrollPos == photoMaxScrollPos) {
        $("#MorePhotosButton").removeClass();
        $("#MorePhotosButton").addClass("MorePhotosGrey");
    }
    if (photoScrollPos == 1) {
        $("#LessPhotosButton").removeClass();
        $("#LessPhotosButton").addClass("LessPhotosGrey");
    }
}
function ResetMusicButtons() {
    $("#LessMusicButton").removeClass();
    $("#LessMusicButton").addClass("LessMusic");
    $("#MoreMusicButton").removeClass();
    $("#MoreMusicButton").addClass("MoreMusic");
    if (musicScrollPos == musicMaxScrollPos) {
        $("#MoreMusicButton").removeClass();
        $("#MoreMusicButton").addClass("MoreMusicGrey");
    }
    if (musicScrollPos == 1) {
        $("#LessMusicButton").removeClass();
        $("#LessMusicButton").addClass("LessMusicGrey");
    }
}
function autoPlay() {
    currentHero++;
    //total hero < heroes_length
    if (currentHero >= heroes_length || currentHero > arrHero.length - 1) {
        currentHero = 0;
    }

    //only one hero
    if (heroes_length == 1) {
        if (intervalID != null) {
            window.clearInterval(intervalID);
        }
    }

    if (autoSideKick)
        moveSideKick(Math.ceil((currentHero + 1) / 4));

    $("#Hero .Heroes").hide();
    $("#" + arrHero[currentHero]).fadeIn(300);
}
function getHeroPlay() {
    $('.heroPlay').each(function() {
        arrHero.push($(this).attr("rel"));
    });
    if (arrHero.length % 4 == 1) {
        lastCount = arrHero.length - 1;
    }
}
function beginInterval() {
    if (intervalID != null) {
        window.clearInterval(intervalID);
    }
    if (arrHero.length > 1) {
        intervalID = setInterval('autoPlay()', 4000);
    }
}

function moveSideKick(pos) {
    if (heroScrollPos != pos) {
        if (heroScrollPos > pos)
            $("#SideKickView").animate({ top: "+=" + ((heroScrollPos - 1) * 260) });

        if (heroScrollPos < pos)
            $("#SideKickView").animate({ top: "-=260" });

        heroScrollPos = pos;
        ResetHeroButtons();
    }
}