


function initMainPicture(ref_id, img_id)
{
	var mainImg    = $('mainPicture');
	mainImg.refId  = ref_id;
	mainImg.imgId  = img_id;
}
	
function prevPicture()
{
	getAjaxPicture(-1, 'getPicture.php');	
}

function prevPicture2()
{
	getAjaxPicture(-1, 'getPicture2.php');	
}

function nextPicture()
{
	getAjaxPicture(1, 'getPicture.php');
}

function nextPicture2()
{
	getAjaxPicture(1, 'getPicture2.php');
}

function getAjaxPicture(direction, file)
{
	var Img  = $('mainPicture');
	
	var img1 = $('thumb0');
	var img2 = $('thumb1');
	var img3 = $('thumb2');

	
	new Effect.Opacity('thumb0', 
						{
					   		duration: 0.5, 
							from:1.0, 
							to:0.0
						}
					  );
	
	new Effect.Opacity('thumb1', Object.extend(
							{
					   			duration: 0.5, 
								from:1.0, 
								to:0.0,
								delay:0.3
							}
					  	));
	new Effect.Opacity('thumb2', Object.extend(
							{
					   			duration: 0.5, 
								from:1.0, 
								to:0.0,
								delay:0.5,
								afterFinishInternal: function(effect) 
								{
									var req = new Ajax.Request(
											'/server/' + file,
											{
												method: 'post',
												parameters: {ref_id:Img.refId, img_id:Img.imgId, dir:direction},
												onComplete: reloadPicture
											}
											);
								}
							}
					  	));
	
}

function reloadPicture(result)
{
	var newImgA 	= eval("(" + result.responseText + ")");
	img1        = newImgA[0];
	img2        = newImgA[1];
	img3        = newImgA[2];
	
	imgReal1 = $('thumb0');
	imgReal2 = $('thumb1');
	imgReal3 = $('thumb2');
	
	var Img 	    = $('mainPicture');
	Img.imgId  = img3.id;
	//Img.imgId  	    = newImg.id;     // Renew the mainPicture DOM element with new imgId value
	
	imgPreloader1   = new Image();
	imgPreloader2   = new Image();
	imgPreloader3   = new Image();
		
	// once image is preloaded, appear the image
	imgPreloader3.onload=function()
	{
		imgReal1.src 	 = img1.name;
		imgReal2.src 	 = img2.name;
		imgReal3.src 	 = img3.name;
		
		
		new Effect.Appear(imgReal1, {duration: 0.5});
		
		Effect.Appear(imgReal2, Object.extend(
								{
					   				duration: 0.5, 
									delay:0.3
								}
					  	));
		Effect.Appear(imgReal3, Object.extend(
								{
					   				duration: 0.5, 
									delay:0.5
								}
					  	));
		
	}
	
	imgPreloader1.src = img1.name;
	imgPreloader2.src = img2.name;
	imgPreloader3.src = img3.name;

}


function largerWhenOver(aLink)
{
	var mainImg    = $('mainPicture');
	var img 	   = aLink.getElementsByTagName('img')[0];
	var thumb	   = img.src;
	
	//src = ;
	
	if(thumb.indexOf("video_icon.jpeg") != -1) // found
	{
		$('mainPictureLink').style.display = "none";
		$('videoParam').value 	= img.alt;
		if($('videoEmbed'))
		{
			$('videoEmbed').src 	= img.alt;
		}
		$('mainVideo').style.display = "";
		//alert($('mainVideo').style.display);
	}
	else
	{
		mainImg.nextSrc = thumb.replace('pic_sml', 'pic_lrg');
		
		var lnk = mainImg.parentNode;
		if(lnk)
		{
			lnk.href = thumb.replace('thumbs/pic_sml', 'pic');	
		}
		
		new Effect.Opacity(mainImg, Object.extend(
								{
									duration: 0.3, 
									from:1.0, 
									to:0.0,
									afterFinishInternal: appearMainPicture
								}
							));
	
	}
}

function openGallVideo(aLink)
{
	$('mainPictureLink').style.display = "none";
	$('videoParam').value 	= aLink;
	$('videoEmbed').src 	= aLink;
	$('mainVideo').style.display = "block";
}

function appearMainPicture()
{
	var mainImg  = $('mainPicture');
	src 		 = mainImg.nextSrc;
	imgPreloader = new Image();
	
	imgPreloader.onload = function()
	{
		if($('mainPictureLink').style.display == "none")
		{
			$('mainVideo').style.display 	= "none";
			$('mainPictureLink').style.display 	= "block";	
		}
		mainImg.src = src;
		new Effect.Appear(mainImg, {duration: 0.3});
	}
	
	imgPreloader.src = src;
}

function openVideo()
{
	$('galleryContainer').hide();
	$('videoContainer').show();
}

function openGallery()
{
	$('galleryContainer').show();
	$('videoContainer').hide();
}

function loadMovie(lnk)
{
	var movie = lnk.getElementsByTagName('div')[0];
	
	if(movie)
	{
		$('videoFrame').innerHTML = movie.innerHTML;
	}
}