﻿var currentItem = 0;
var animationPointer;
var messagePointer;
var animationIsRunning = false;
var categoriesList =
                    ["Flow.aspx",
                    "LifeForms.aspx",
                    "Nature.aspx",
                    "Twilight.aspx",
                    "Particles.aspx",
                    "Landscape.aspx",
                    "Figures.aspx"];

var timelineList = ["2004.aspx",
                    "2005.aspx",
                    "2006.aspx",
                    "2007.aspx",
                    "2008.aspx",
                    "2009.aspx",
                    "2010.aspx"];

$(document).ready(function() {

    $(".FavoritesMessage").hide();

    $("#ModalBackground").fadeTo(1, 0.85);
    $("#EnlargModal").hide();
    $("#ModalForground img").click(function() {
        HideEnlargedImage();
    });

    $("#Image img").hide();

    $("div.LinksCollection div").css("background-color", "#ffffff").css("color", currentColor);

    $("div.LinksCollection div").click(function(event) {
        StopAnimation();
        SelectItem($(this));
    });

    $("div#PortfolioMenu ul li a").mouseover(function(event) {
        $(this).addClass("PortfolioMenuHove");
    });
    $("div#PortfolioMenu ul li a").mouseout(function(event) {
        $(this).removeClass("PortfolioMenuHove");
    });

    $("#btnPlay").click(function(event) {
        if (animationIsRunning == false) {
            StartAnimation();
        }
        else {
            StopAnimation();
        }
    });

    $("#btnMagnify").click(function(event) {
        StopAnimation();
        ShowEnlargedImage($(this).attr("imgsrc"));
    });

    $("#btnFavorites").click(function(event) {
        StopAnimation();
        SaveFavorite();
    });

    if (isTimeLine == "True") {
        var pageName = GetPageName().replace(".aspx", "");
       
        $(".TimeLineContainer a").each(function() {
       
            if ($(this).text() == pageName || (pageName == 2004 && $(this).text() == "2003")) {
                
                $(this).wrapInner("<b></b>");
            }
        });
    }
    if (navigateTo) {
        SelectItemByIndex(parseInt(navigateTo));
    }
    else {
        StartAnimation();
    }
});

function SelectItem(eItem) {

    var currentImage = "#Image img:eq(" + currentItem + ")";
    if (animationIsRunning == false) {
        var imageIndex = $("div.LinksCollection div").index($(eItem));
        currentImage = "#Image img:eq(" + imageIndex + ")";
    }

    var largeImagePath = $(eItem).attr("ImageLarge");
    if (largeImagePath && largeImagePath != "") {
        $("#btnMagnify").attr("imgsrc", largeImagePath).show();
        $("#MagnifySeperator").show();
    }
    else {
        $("#btnMagnify, #MagnifySeperator").hide();
    }
    
    $("div.LinksCollection div").css("background-color", "#ffffff").css("color", currentColor);
    $(eItem).css("background-color", currentColor).css("color", "#ffffff");
    $("#Image img").fadeOut("fast");
    $(currentImage).attr("alt", $(eItem).attr("ImageTitle")).fadeIn("normal");
    $("#Toolbox h3").attr("innerHTML", $(eItem).attr("ImageTitle")).attr("ImageID", $(eItem).attr("ImageID"));
    var description = $(eItem).attr("ImageDescription") + " | " + $(eItem).attr("ImageYear");
   
    $("#Toolbox p#ImageDescription").attr("innerHTML", description);    
    $("#Toolbox p#ImageDimentions").attr("innerHTML", $(eItem).attr("ImageDimentions"));
    currentItem = $("div.LinksCollection div").index($(eItem)) + 1;
}

function AnimateImages() {

    if ($("div.LinksCollection div").length < currentItem + 1) {
        currentItem = 0;
        GoToNextPage();    
    }
    SelectItem($("div.LinksCollection div").get(currentItem));
}

function SelectItemByIndex(iIndex) {
    var item = $("div.LinksCollection div").get(iIndex);
    if (!item) {
        item = $("div.LinksCollection div").get(0);
    }
    SelectItem(item);
}

function StopAnimation() {
    $("#btnPlay").attr({
        src: "images/icon_play.gif",
        alt: "play",
        title: "play"
        });
    clearInterval(animationPointer);
    animationIsRunning = false;
}

function StartAnimation() {
    if (!animationIsRunning) {
        $("#btnPlay").attr({
            src: "images/icon_stop.gif",
            alt: "stop",
            title: "stop"
            });
        animationIsRunning = true;
        AnimateImages();
        animationPointer = setInterval("AnimateImages()", "3000");        
    }
}



function SaveFavorite() {

    var imageIndex = currentItem - 1;
    var currentImage = "#Image img:eq(" + imageIndex + ")";
    
    var imageID = $("#Toolbox h3").attr("ImageID");
   
    var requestString = "&id=" + imageID ;

    clearInterval(messagePointer);
    
    $.post("SaveFavorite.aspx?" + requestString, "", function(data) {
        $(".FavoritesMessage p").html(data);
        $(".FavoritesMessage").fadeIn("normal");
        messagePointer = setTimeout(" $('.FavoritesMessage').fadeOut('normal'); clearInterval(messagePointer);", 2500);
        $("a[href='Favorites.aspx']").addClass("FavoritesLinkFull");
    }, "text");
}


function ShowEnlargedImage(sPath) {

    if (sPath) {
        $("#ModalForground img").attr("src", sPath);
        $("#EnlargModal").show();
    }    
}

function HideEnlargedImage() {

    $("#EnlargModal").hide();
    $("#ModalForground img").attr("src", "images/Loading.gif");
}

function GoToNextPage() {
    var currentList = categoriesList;
    if (isTimeLine == "True") {
        currentList = timelineList;
    }
    
    pageName = GetPageName();
    var i = 0;
    
    for (i; i < currentList.length; i++) {
        if (currentList[i] == pageName) {
            break;
        }
    }
    if (i + 1 == currentList.length) {
        i = 0;
    }
    else {
        i++;
    }
    location.href = currentList[i];
}

function GetPageName() {
    // remove Query String
    var url = location.href.split("?")[0];
    
    var urlSegments = url.split("/");
    var pageName = urlSegments[urlSegments.length - 1];
    
    return pageName;
    
}

