﻿var lnc = true;
var rnc = true;

function closeNavs()
{
    lNavHide();
    rNavHide();
}

function nextBackground(inc)
{
    //find the selectedImage place in the current gallery
    var pos = 0;
    for(var i = 0; i < cImages.length; i++)
    {
        if(cImages[i] == selectedImage)
            pos = i;
    }
    
    pos += inc;

    if(pos < 0) pos = cImages.length - 1;
    
    if(pos == cImages.length) pos = 0;
    
    setImage(cImages[pos]);
    getBackground(cImages[pos], true);
    
    /*for sliding. sliding function will not work with the current overflows placement
    var outDir = inc == 1 ? "right" : "left";
    var inDir = inc == 1 ? "left" : "right";

    slideBackground(cImages[pos], outDir, inDir);*/
    
    getImageInfomation(cImages[pos]);
    
    selectedImage = cImages[pos];
}

function lNavHide()
{
    if(lnc)
    {
        lnc = false;
        
        $("#leftNav").hide("slide", { easing: "easeIn", direction: "left" }, 500, function()
        {
            $("#leftShowNav").show("slide", { easing: "easeIn", direction: "left" }, 10, function()
            {
                if(cImages.length > 0)
                    $("#leftShowArrow").fadeIn("slow");
                    
                lnc = true;
            });
        });
    }
}

function rNavHide()
{
    if(rnc)
    {
        rnc = false;
        
        $("#rightNav").hide("slide", { easing: "easeIn", direction: "right" }, 500, function()
        {
           $("#rightShowNav").show("slide", { easing: "easeIn", direction: "right" }, 10, function()
            {
                if(cImages.length > 0)
                    $("#rightShowArrow").fadeIn("slow");
                
                rnc = true;
            });
        });
    }
}

function lNavShow()
{
    if(lnc)
    {
        lnc = false;
        
        $("#leftShowNav").hide("slide", { easing: "easeIn", direction: "left" }, 10, function()
        {
            $("#leftShowArrow").hide();
            
            $("#leftNav").show("slide", { easing: "easeIn", direction: "left" }, 500, function()
            {
                lnc = true;
            });
        });
    }
}

function rNavShow()
{
    if(rnc)
    {
        rnc = false;
        
        $("#rightShowNav").hide("slide", { easing: "easeIn", direction: "right" }, 10, function()
        {
            $("#rightShowArrow").hide();
            
            $("#rightNav").show("slide", { easing: "easeIn", direction: "right" }, 500, function()
            {
                rnc = true;
            });
        });
    }
}

function getImageInfomation(img)
{
    var target = "/targets/trg_imageInformation.aspx?imageid=" + img + "&r=" + Math.random();
    
    $.get(target, function(data)
    {
        $("#imageInformation").fadeOut("slow", function()
        {
            $("#imageInformation").html(data);
            $("#imageInformation").hide().fadeIn("fast");
         });
    });
}

function getBackground(img, fade)
{
    var target = "/targets/trg_background.aspx?img=" + img + "&r=" + Math.random();
    
    $.get(target, function(data)
    {
        if(fade)
        {
            $("#bg").fadeOut("slow", function()
            {
                $("#background").html(data);
                $("#bg").hide().fadeIn("fast");
            });
        }
        else
        {
            $("#background").html(data);
        }
    });
}

/*function slideBackground(img, outDir, inDir)
{
    var target = "/targets/trg_background.aspx?img=" + img + "&r=" + Math.random();
    
    $.get(target, function(data)
    {
        $("#bg").show("slide", { easing: "easeIn", direction: outDir }, 500, function()
        {
            $("#background").html(data);
            $("#bg").show("slide", { easing: "easeIn", direction: inDir }, 500);
        });
    });
}*/

function addImageToLightbox(img, user, action)
{
    var target = "/targets/trg_addRemoveLightboxImages.aspx?action=" + action + "&img=" + img + "&user=" + user + "&r=" + Math.random();
    
    $.get(target, function(data)
    {
        alert(data);
    });
}

function loadEffects()
{
    $(".ui-fade-rollover").each(function()
    {
        if ($(this).attr("effect_loaded") == "true") return;
    
        $(this).attr("effect_loaded", "true");
    
        var wrapper = "<div style='display: block; height: auto; width: auto; background-repeat: no-repeat; background-image: url(" + $(this).attr("src").replace("_off", "_over") + ")'></div>";
    
        if ($(this).parent().get(0).tagName == "A")
        {
            $(this).parent().wrap(wrapper);
        }
        else
        {
            $(this).wrap(wrapper);
        }

        $(this).bind("mouseenter", function(e)
        {
            $(this).stop().animate({ opacity: 0 }, 300);
        });
        
        $(this).bind("mouseleave", function(e)
        {
            $(this).stop().animate({ opacity: 1 }, 300);
        });
    });
}
function locationChangeWRand(ele)
{
    window.location.href = ele + "&r=" + Math.random();
}
