﻿// JScript File

var selectedIndex;
var currIndex;
var photoShowLen = 4;
var MaxHeight = 500;
var MaxWidth = 500;
function PrepeareGallery()
{
	currIndex = selectedIndex = 0;
	LoadSmallImages();
	SetPrevNextVisible();
	Show();	
}

function LoadSmallImages()
{
	for (i=0; i< gallery.length; i++)
    {
        DownloadImage(gallery[i].SmallUrl,i, "img_" + gallery[i].Id);
    }   
}

function DownloadImage(url, index, pefix)
{
    var img = new Image();	
    img.Name = pefix + index; 
    img.src = url;
}
function Show()
{
	var endIndex = currIndex + photoShowLen < gallery.length ? currIndex + photoShowLen: gallery.length;
	var index = 1;
	for (var i=currIndex; i<endIndex; i++)
	{
		var currSpan = document.getElementById("spPic" + index);
		var spSeparator = document.getElementById("spSep" + index);
		currSpan.className = "galleryImage";
		currSpan.style.background = "url('" + gallery[i].SmallUrl + "')";
		$("#lnkGallery" + index).attr("href", gallery[i].GalleryUrl);
		spSeparator.style.width = "7px";
		currSpan.title =  gallery[i].Alt;
		currSpan.onclick = function(ii) {
			return function() {
				selectedIndex = ii;
				Show();
			};
		} (i);
		index ++;	
	}
	if (spSeparator == undefined) return;
	spSeparator.style.width = "0px";
}

function MoveNext()
{
	if (currIndex + photoShowLen == gallery.length)
		return;
	currIndex ++ ;
	SetPrevNextVisible();
	Show();
}

function MovePrev()
{
	if (currIndex == 0)
		return;
	currIndex --;
	SetPrevNextVisible();
	Show();
}

function SetPrevNextVisible()
{
	var btnNext = document.getElementById("pgRigh");
	var btnPrev = document.getElementById("pgLeft");
	btnNext.className = "galleryPagingRight";
	btnPrev.className = "galleryPagingLeft";
	if (currIndex == 0)
	{
		btnPrev.className = "galleryPagingInvisible";
	}
	if (currIndex + photoShowLen > gallery.length -1)
	{
		btnNext.className = "galleryPagingInvisible";
	}
}

function GetMaxWidth()
{
	var currWidth = 0;
	for(var i=0; i<gallery.length; i++)
	{
		if (currWidth < gallery[i].Width)
		{
			currWidth = gallery[i].Width;
		}
	}
	return currWidth;
}

function OpenPhotoGalleryWindow(url)
{
	//var maxWidth = GetMaxWidth();
	window.open(url,'new','toolbar=1,scrool=1,scrollbars=1,resizable=1');
	
}
