//var arrayPic = [<?php echo GetPicAsJQueryArrayString ($GalleryPicPath); ?>];

var maxNumber = arrayPic.length - 1;



$(function() {

    $('#loadingDiv')
		    .hide()  // hide it initially
		    .ajaxStart(function() {
		        $(this).show();
		    })
		    .ajaxStop(function() {
		        $(this).hide();
		    });




    $('#next').click(function() {
        next();
        load();
        return true;
    });

    $('#mainImage').click(function() {
        next();
        load();
        return true;
    });

    $('#previous').click(function() {
        previous();
        load();
        return true;
    });


    load();

});


$(window).keydown(function(event) {

    switch (event.keyCode) {

        case 39: // vasak nool
        case 32: // space
        case 102: // 6 ja vasak
            next();
            load();
            break;
        case 37:
        case 100:
            previous();
            load();
            break;
        case 76: // L 

            if ($('#navigation').is(':visible') && $('#navigation').parents(':hidden').length == 0) {
                $('#mainMenu').hide();
                $('#navigation').hide();
                $('#navigation2').hide();
                $('#pageBottomDiv').hide();
                //alert ($('#bodyTag').css('bgcolor'));
            } else {
                $('#mainMenu').show();
                $('#navigation').show();
                $('#navigation2').show();
                $('#pageBottomDiv').show();
            }

            break;


    }
});





function GetPicPath() {
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();
    if (windowHeight < galleryWindowHeightForNormPic) {
        return galleryPicSmalerPath;
    }    
	if (windowWidth < galleryWindowWidthForNormPic) {
        return galleryPicSmalerPath;
    }
    return galleryPicNormPath;

}
function load() {
    var myFile = document.location.toString();
    var arrayPlaceNumber = 0;
    if (myFile.match('#')) {
        var myAnchor = myFile.split('#')[1];

        if (!isNaN(myAnchor)) {
            if (parseInt(myAnchor, 10) <= maxNumber) {
                arrayPlaceNumber = myAnchor;
            }
        }
    }

    var imageToLoade = arrayPic[arrayPlaceNumber];

    var picPath = GetPicPath();
    var url = picPath + "/" + imageToLoade;

    loadImage(url);

    // pre loade
    //if (parseInt(myAnchor, 10) < maxNumber ){
    //			var newimage = new image();
    //			$(newimage).attr("src", arrayPic[arrayPlaceNumber+1]);
    //}

}


function previous() {
    var myFile = document.location.toString();
    if (myFile.match('#')) {
        var myAnchor = myFile.split('#')[1];

        if (isNaN(myAnchor)) {
            window.location.hash = '#' + maxNumber;
        }
        else {
            var previousID = parseInt(myAnchor) - 1;
            if (previousID < 0) {
                previousID = maxNumber;
            }
            window.location.hash = '#' + previousID;
        }
    }
    else {
        window.location.hash = '#' + maxNumber;
    }
}

function next() {

    var myFile = document.location.toString();
    if (myFile.match('#')) {

        var myAnchor = myFile.split('#')[1];
        if (isNaN(myAnchor)) {
            window.location.hash = '#' + 1;
        }
        else {
            var nextID = parseInt(myAnchor) + 1;
            if (nextID > maxNumber) {
                nextID = 0;
            }
            window.location.hash = '#' + nextID;
        }
    }
    else {
        window.location.hash = '#' + 1;
    }
}

function loadImage(url) {
    //$("#mainImage").hide();
    //$("#mainImage").attr("src", url);
    //$("#mainImage").fadeIn();
    //return true;

    $("#loadingDiv").show();

    var img = $("#mainImage");
    $(img).load(function() {
        // $("#mainImage").css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
        $("#loadingDiv").hide();
        //$('#loader').removeClass('loading').append(this);
        $("#mainImage").fadeIn("fast");
    }).error(function() {
        // notify the user that the image could not be loaded
    }).attr('src', url);
}








/// 
function mouseOver_RR() {
    document.getElementById("next").src = "../_sys_v2/img/rr-dark.gif"
}
function mouseOut_RR() {
    document.getElementById("next").src = "../_sys_v2/img/rr-light.gif"
}


function mouseOver_LL() {
    document.getElementById("previous").src = "../_sys_v2/img/ll-dark.gif"
}
function mouseOut_LL() {
    document.getElementById("previous").src = "../_sys_v2/img/ll-light.gif"
}
	
