var photos= new Array();
var photoTitles= new Array();
var minWidth=200;
var	minHeight=140;
var photoWidth, photoHeight, marginTop;
var eventListenerOnload=null;
/*----------------------------------------------------------------------------------------*/
function closePhotoBox(){
	new Effect.Opacity('photoContent',{from:1,to:0,duration:0.5,afterFinish:function(){
		$('photoContent').style.display='none';
		$('photoHolder').style.display='none';
		$('photoBackground').style.display='none';
	}});
}
/*----------------------------------------------------------------------------------------*/
function photoBoxError(err){
  $('photoPreloader').style.display='none';
  alert(err);
  $('photoHolder').style.display='none';
  $('photoBackground').style.display='none';  
}
/*----------------------------------------------------------------------------------------*/
function photoBox(path, thisPic){
	$('photoFrame').style.width=minWidth+'px';
	$('photoFrame').style.height=minHeight+'px';
	$('photoFrame').style.marginTop='15%';
	$('photoContent').style.display='none';
	$('photoTitleHolder').hide();
	$('buttonPrev').style.display='inline';
	$('buttonNext').style.display='inline';
	$('photoBackground').style.display='block'; 
	$('photoHolder').style.display='block';
	$('photoPreloader').style.display='block';
	
	str=thisPic.id.split('_');
	thisItem=path+str[1];
	
	itms=$$('.galleryItems');
	for(i=0; i<itms.length; i++){
		str=itms[i].id.split('_');
		photos[i]=path+str[1];
		if(photos[i]==thisItem)thisNum=i;
		photoTitles[i]=itms[i].title;
	}
	showPhoto(thisNum);
}
/*----------------------------------------------------------------------------------------*/
function showPhoto(thisPhotoNumber){
	try{
		photosNuber=photos.length;
		if(thisPhotoNumber<0)thisPhotoNumber=0; 
		if(thisPhotoNumber>(photosNuber-1))thisPhotoNumber=photosNuber-1;
		
		if(thisPhotoNumber==0)$('buttonPrev').style.display='none';
		else $('buttonPrev').style.display='inline';
		
		if(thisPhotoNumber==(photosNuber-1))$('buttonNext').style.display='none';
		else $('buttonNext').style.display='inline';
		
		$('currentPhoto').innerHTML=' '+(thisPhotoNumber+1)+'/'+photosNuber+' ';
		
		var myWidth=0, myHeight=0;
			if(typeof(window.innerWidth)=='number'){//Non-IE
				myWidth=window.innerWidth;
				myHeight=window.innerHeight;
			}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){//IE 7-től
				myWidth=document.documentElement.clientWidth;
				myHeight=document.documentElement.clientHeight;
			}
		maxWidth=myWidth-60;
		maxHeight=myHeight-90;
		
		if(maxWidth<minWidth || maxHeight<minHeight)throw "Túl kicsi a böngésző mérete a képmegjelenítéshez!";
			
		photoSrc=photos[thisPhotoNumber];
		photoSrcArray=photoSrc.split('-');
		photoWidth=photoSrcArray[1];
		photoHeight=photoSrcArray[2];
			
		photoRratio=photoWidth/photoHeight;
	
		if(photoWidth<minWidth){photoWidth=minWidth; photoHeight=Math.floor(photoWidth/photoRratio);}
		if(photoHeight<minHeight){photoHeight=minHeight; photoWidth=Math.floor(photoHeight*photoRratio);}
		
		if(photoWidth>maxWidth){photoWidth=maxWidth; photoHeight=Math.floor(photoWidth/photoRratio);}
		if(photoHeight>maxHeight){photoHeight=maxHeight; photoWidth=Math.floor(photoHeight*photoRratio);}
			
		marginTop=Math.floor(((Math.floor((maxHeight-photoHeight)/2))/maxHeight)*50)+2;
			
		if(photoWidth>maxWidth || photoHeight>maxHeight || photoWidth<minWidth)throw "Túl kicsi a böngésző mérete a kép arányaihoz képest!";
		
		if(eventListenerOnload==null)eventListenerOnload=$('photoContent').observe('load',function(event){photoLoaded();});
		
		$('photoContent').width=photoWidth;
		$('photoContent').height=photoHeight;
		$('photoTitle').innerHTML=photoTitles[thisPhotoNumber];
		$('photoContent').src=photoSrcArray[0];
	} catch(er){photoBoxError(er)};
}
/*----------------------------------------------------------------------------------------*/
function photoLoaded(){
	if($('photoFrame').getStyle('display')=='block'){
		new Effect.Morph('photoFrame', {style:'width:'+photoWidth+'px; Height:'+photoHeight+'px; margin-top:'+marginTop+'%;', duration:0.6, queue:{position:'end', scope:'photo', limit:1 }, afterFinish: function (){
			$('photoPreloader').style.display='none';
			$('photoContent').setOpacity(0);
			$('photoContent').style.display='block';
				new Effect.Opacity('photoContent',{from:0,to:1, duration: 0.5, afterFinish:function(){
					$('photoContent').setOpacity(1);
				}});
			Effect.SlideDown('photoTitleHolder',{duration: 1.0});
		}});
	}
}
/*----------------------------------------------------------------------------------------*/
function changePhoto(direction){
	new Effect.Opacity('photoContent',{from:1,to:0, duration: 0.5, afterFinish: function(){
		$('photoContent').style.display='none';
		$('photoTitleHolder').hide();
		$('photoPreloader').style.display='block';
		currString=$('currentPhoto').innerHTML;
		current=Number(trim(currString.split('/')[0]))-1;
		next=current+Number(direction);
		showPhoto(next);
	}});
}
