var slideShowSpeed = 5000

// Duration of crossfade (seconds)
var crossFadeDuration = 3

// Specify the image files
var Pic = new Array() // don't touch this

var t
var j = 0
var p ;

var preLoad  = new Array();
var newTitle = ""
var newDescr = "";
var imgOnloadTxt = "";
var mainImgId = "mainImg";
var imgStyleTxt = "";
//
var albumType = "";

var currImg = "";

function viewNewPhoto(img)
{
	

//	alert('111');
	thumbBoxObj = getEl('img_div_container');
//	return;	
	w = 800;
	h = 500;
	var imageWidth = img.width;
	var imageHeight = img.height;
	
	//alert(imageHeight + " " + imageWidth + " " + img.src );
	
	if ( imageHeight > h )
	{
		var k =  imageHeight / imageWidth;
//		img.width = imageWidth * k ;
		img.width = h / k ;
		img.height = h ;


	}
	else if (imageWidth > w)
	{
		var k =  imageWidth / imageHeight  ;
		img.width = w ;
		img.height = w / k;		
		
	}
	getEl('mainImg').parentNode.replaceChild(img , getEl('mainImg'));
	
//	getEl('fullPhoto').href = img.src;
	img.setAttribute('id', 'mainImg')
	
	changeLocButtons(j)
	if (albumType == "alternative")
	{
		viewShowWind();
	}
//	try{
	currImg = img.src;
//alert(img.src);
		
		try {
			getEl('photo').value = unescape(img.src);
				printComments();
		}catch(e) {}
}

	
function fillImgAray()
{
	imgDiv = document.getElementById('thumb_imgs');
	imgs = imgDiv.getElementsByTagName('img');
	if (Pic.length == 0)
		for (i=0; i<imgs.length; i++)
		{
			Pic[i] = imgs[i].getAttribute('slideshow_img');
			//alert(Pic[i]);
		}
		
	p = Pic.length;

	if ( p == 0 )
	{
		if ( albumType != "alternative" )
			getEl('startSSButton').style.display = 'none';
		else
		{
			$("#startSSButtonMain").css("display", 'none');
			$("#startSSButtonMain").hide();
		}

		return;
	}
	loadPhotoData(0);
	writePhotoData();
	imgOnloadTxt = getEl('mainImg').getAttribute('onLoad');
	imgStyleTxt = getEl('mainImg').style.border;
	if (albumType != "alternative")
	{
		runSlideShow( false );
	}
}


function runSlideShow( once )
{
	if (once)
	{
   		j = j + 1
   		if (j > (p-1)) j=0
	}
	

	//alert(preLoad[j].src);
	if ( p == 0 )
		return;
	if (!preLoad[j])
	{
   		preLoad[j] = new Image();
   		preLoad[j].src = Pic[j];
//   		alert(preLoad[j].width);
   		
	}
	//alert(preLoad[j].src);
	loadPhotoData(j);
	//alert('222222');
//	document.getElementById('mainImg').setAttribute("onLoad", document.getElementById('mainImg').getAttribute('onLoad') + " alert('11111');");
//	alert(document.getElementById('mainImg').getAttribute('onLoad'));
try{
	newImgEll = document.getElementById('imgMainEl');
	document.removeChild(newImgEll);
	
}catch(e){}
	
	newImg = document.createElement('img');
	newImg.setAttribute("src", preLoad[j].src);
	
	newImg.onload = function() {writePhotoData(); viewNewPhoto(this); }
	newImg.removeAttribute('width');
	newImg.removeAttribute('height');
	
	newImg.setAttribute("id", "imgMainEl");
	newImg.style.border = imgStyleTxt;
	//newImg."style", imgStyleTxt);
//	newImg.onload = function() {writePhotoData(); viewNewPhoto(this); }	
//	newImg.setAttribute("onLoad", imgOnloadTxt + " viewNewPhoto(this);");
	//alert(newImg.style.getAttribute("border"));
	
	//alert('aimg_'+ j);
	
	//writePhotoData(ael.title, 'aimg_'+ j);
	if (once)
	{
		getEl('stopSSButton').style.display = "";
		getEl('startSSButton').style.display = "none";

		t = setTimeout('runSlideShow(true)', slideShowSpeed)
	}
}
function getEl(id)
{
	return document.getElementById(id);
}

function loadPhotoData(j)
{
	if ( false == (ael = getEl('aimg_'+ j)) )
		return;
	newTitle = ael.title;
	newDescr = ael.getAttribute('pdesc');
}

function writePhotoData()
{
	if ( newTitle == null )
		newTitle = "";
	if ( newDescr == null )			
		newDescr = "";

	document.getElementById('photo_title').innerHTML = newTitle;
	document.getElementById('photo_desc').innerHTML  = newDescr;
}



function stopSlideShow()
{
	clearTimeout(t);
	getEl('stopSSButton').style.display = "none";
	getEl('startSSButton').style.display = "";
	
}

// SECOND VARIANT
//-----------------------------------------------------------------------------------------------
function fillImgAray2()
{

	albumType = "alternative";
	fillImgAray();
//	alert('asdasd');
	windW = getWindowWidth();
	windH = getWindowHeight();
	//alert(windH);
	$("#PR_overlay").height(windH);
	$("#PR_overlay").width(windW);
	$("#PR_overlay").hide();
	
	
//	$("#PR_overlay").css({ height: "700px", width: "700px" });
	//$("#PR_overlay").width(700);
	topPos = windW/2 - (850/2)
	$("#PR_window").css("left", topPos);
	$("#viewCloseBtn").show();
	


//	$("div.PR_window_css").height(windH);
	

	$("a.thickbox").click(function(){
		goPhoto2(this.id.substring(5));

	return false;
	});
	//alert(getEl('PR_window').style.height)
	
	
}
function goPhoto2(kuda)
{
	if (kuda == 'prev')
	{
		j--;
		if (j<0)
			j=0;
	}
	else if (kuda == 'next')
	{
		j++;
		if (j>p-1)
			j=p-1;
	}
	else if ( !isNaN(parseInt(kuda)) )
		j = parseInt(kuda)


//alert(kuda);
	changeLocButtons(j)		
	runSlideShow2( false );
	
}

function runSlideShow2( stat )
{
	runSlideShow( stat );
}
function viewShowWind()
{
	$("#PR_window").show()
	$("#PR_overlay").show()
	
}

function closeView()
{
	$("#PR_window").hide();
	$("#PR_overlay").hide();
	stopSlideShow();
	return false;
}



/*function initSlideShow()
{
	var newEl = document.createElement('IMG');
	newEl.setAttribute('src', Pic[0]);
	newEl.setAttribute('id', 'mainImg');
	document.getElementById('img_container').appendChild(newEl);
	runSlideShow3();
}
function runSlideShow2()
{
   j = j + 1
   if (j > (p-1)) j=0

	//alert(preLoad[j].src);
	if (!preLoad[j])
	{
   		preLoad[j] = new Image();
   		preLoad[j].src = Pic[j];
	}
	//alert(preLoad[j].src);

   document.getElementById('mainImg').src = preLoad[j].src;

   t = setTimeout('runSlideShow2()', slideShowSpeed)
}


function stopSlideShow2()
{
	clearTimeout(t);
	//document.getElementById('img_container').removeChild(document.getElementById('img_container').firstChild);
}

function initSlideShow3()
{
//	var newEl = document.createElement('IMG');
//	newEl.setAttribute('src', Pic[0]);
//	newEl.setAttribute('id', 'mainImg');
//	document.getElementById('img_container').appendChild(newEl);
	runSlideShow3();
}

function runSlideShow3()
{
   j = j + 1
   if (j > (p-1)) j=0

   
	//alert(preLoad[j].src);
//	getEl('aimg_'+j).Click();//=true;
//		$("a.thickbox").click(function(){
	//TB_remove();
	var t = getEl('aimg_'+j).title;
	var g = "gallery";
	TB_show(t,getEl('aimg_'+j).href,g);
//	goNext();
	getEl('aimg_'+j).blur();
	//return false;

   t = setTimeout('runSlideShow3()', slideShowSpeed)
}
*/
//----------------------------------------------------------------------------------------------------------------
function goPhoto(kuda)
{
	if (kuda == 'prev')
	{
		j--;
		if (j<0)
			j=0;
	}
	else if (kuda == 'next')
	{
		j++;
		if (j>p-1)
			j=p-1;
	}
	else if ( !isNaN(parseInt(kuda)) )
		j = parseInt(kuda)


	changeLocButtons(j)		
	runSlideShow( false );
	
}

function changeLocButtons(cImg)
{
	if (cImg == 0)
		setVisible('buttPrev', 'hidden');
	else
		setVisible('buttPrev', 'visible');
		
	
	if (cImg == p-1)
		setVisible('buttNext', 'hidden')
	else
		setVisible('buttNext', 'visible');
	
}

function setVisible(id, stat)
{
	getEl(id).style.visibility = stat;
}

function getWindowHeight()
{
        var windowHeight = 0;
        if (typeof(window.innerHeight) == 'number') {
                windowHeight = window.innerHeight;
        } else {
                if (document.documentElement && document.documentElement.clientHeight) {
                        windowHeight = document.documentElement.clientHeight;
                } else {
                        if (document.body && document.body.clientHeight) {
                                windowHeight = document.body.clientHeight;
                        }
                }
        }
        return( windowHeight );
}

function getWindowWidth()
{
	return window.innerWidth ? window.innerWidth : document.body.offsetWidth;
}
