var firstimagestart=1;
var nextorprev=0;
var myloadedimage = new Array();
     myloadedimage[0]=1;
var imggallery=new Array();
var thumbnailnum;



function getstarted(loadarea, imgindex, img_id, current_imgid){
	checknext(img_id);
	if(current_imgid!=img_id){
		if(firstimagestart==1){
			firstimagestart=0;		
		}
		if(nextorprev==1){
			nextorprev=0;		
		}
				
		modifyimage(loadarea, imgindex, img_id);
		clearstyles();
		if(document.getElementById('_ctl1_img_' + Number(img_id)) !=null){
		    document.getElementById('_ctl1_img_' + Number(img_id)).className = "selected";
		}else{
		    document.getElementById('_ctl2_img_' + Number(img_id)).className = "selected";
		}
		window.scrollTo(0,0);
		
	}
}

function initGallery ( tempgallery, count, first_id, startimgnumber) {
	imggallery = tempgallery;
	
		for (x=0; x<imggallery.length; x++){
			var myimage=new Image()
			myimage.src=imggallery[x][0]
		}
	
	thumbnailnum = imggallery.length;
	current_imgid = first_id;
	if(document.getElementsByTagName("body")[0].className != null) var lang = document.getElementsByTagName("body")[0].className; 
	if(lang == "en"){
			document.getElementById("prev").innerHTML = "Prev";
			document.getElementById("prev").title = "Previus image";
			
			document.getElementById("next").innerHTML = "Next";
			document.getElementById("next").title = "Next image";
			
			}
	//currentwidth=startwidth;
	//currentheight=startheight;
	window.onload= new function(){loadfirstimage(startimgnumber)}
}

function modifyimage(loadarea, imgindex, img_id ){
	if(myloadedimage[imgindex]==null){
		var mynewimage=new Image()
		//alert(imggallery[imgindex][0]);
		mynewimage.src=imggallery[imgindex][0]
			mynewimage.onload= new function(){
				loadimagenow();
			}
		}else{
			loadimagenow();
	}
	function loadimagenow(){
		if (document.getElementById) {
				if(current_imgid!=img_id){
					var imgobj=document.getElementById(loadarea);
					var iptch=document.getElementById('iptc_info');
					//var photonum=document.getElementById('photocount')
					var phototitle=document.getElementById('PhotoText')
					imgobj.innerHTML=returnimgcode(imggallery[imgindex]);
					//iptch.innerHTML=returniptc(imggallery[imgindex]);
					//photonum.innerHTML=(Number(imgindex)+1)+" of "+imggallery.length+" Photos";
					phototitle.innerHTML="<strong>"+tempgallery[img_id][1]+"</strong> ";
					//initImage(loadarea);
					current_imgid=img_id;
					myloadedimage[imgindex]=1;
				}
			}
		return false
	}
}

function loadfirstimage(startimgnumber){

	var firstimageload = new Image();
	firstimageload.src = null;

	firstimageload.src = imggallery[startimgnumber][0];

	firstimageload.onLoad= new function(){

		var firstimg=document.getElementById('bigPhoto');

		firstimg.innerHTML=returnimgcode(imggallery[startimgnumber]);
		var firsttitle=document.getElementById('PhotoText');
		firsttitle.innerHTML="<strong>"+tempgallery[startimgnumber][1]+"</strong> ";
	}
}

function returnimgcode(theimg){
	var imghtml=""
	if (theimg[1]!="")
		imghtml=''
	imghtml+='<img src="'+theimg[0]+'" border="0" id="'+theimg[2]+'" />'
	if (theimg[1]!="")
		imghtml+=''
	return imghtml
}
function nextimage(current_imgid){
	
	newimgid = Number(current_imgid)+1;
	newimgindex =imggallery[newimgid][2]
	newimgid = imggallery[newimgid][2]
	checknext(newimgid);
	nextorprev=1;
	clearstyles();
	if(document.getElementById('_ctl1_img_' + Number(newimgid)) !=null){
	document.getElementById('_ctl1_img_' + Number(newimgid)).className = "selected";
	}else{document.getElementById('_ctl2_img_' + Number(newimgid)).className = "selected";}
	getstarted('bigPhoto',Number(newimgindex) ,Number(newimgid) , Number(current_imgid))

}

function previmage(current_imgid){
	newimgid = Number(current_imgid)-1;
	newimgindex =imggallery[newimgid][2]
	newimgid = imggallery[newimgid][2]
	checknext(newimgid);
	nextorprev=1;
	clearstyles();
	if(document.getElementById('_ctl1_img_' + Number(newimgid)) !=null){
	document.getElementById('_ctl1_img_' + Number(newimgid)).className = "selected";
	}else{document.getElementById('_ctl2_img_' + Number(newimgid)).className = "selected";}
	getstarted('bigPhoto',Number(newimgindex) ,Number(newimgid) , Number(current_imgid))
}

function clearstyles()
{
	var elem = document.images;
	
	for(var i = 0; i < elem.length; i++)
	{
		
		if (document.images[i].className =="selected")
		{
						
			document.getElementById(document.images[i].id).className = "";
		}
		
	}
}

function checknext(mynum){
	thumbmax=(Number(thumbnailnum)-1);
	if ( mynum < 1 ) {
		mm_shl('prev','hidden');
		mm_shl('next','visible');
	} else if ( mynum <  thumbmax ) {
		mm_shl('prev','visible');
		mm_shl('next','visible');
	} else {
		mm_shl('prev','visible');
		mm_shl('next','hidden');
	}
}

function mm_shl() { //v6.0
	var obj,args=arguments;
	if ((obj=MM_findObj(args[0]))!=null) {
		if (obj.style) {
			obj=obj.style;
		}
		obj.visibility=args[1];
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}