/*////////////////////////////////
//                              //
//                              //
//            Common            //
//                              //
//                              //
////////////////////////////////*/

if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.id == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].id == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

function getFileName()
{
	var string = location.href.substring(location.href.lastIndexOf('/')+1);
	return string.substring(0,string.lastIndexOf('?'));
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

function getEventTarget(evt) {
	var targ = evt.target ? evt.target : evt.srcElement;

	if(targ !== null) {
		if(targ.nodeType == 3)
		{
			targ = targ.parentNode;
		}
	}

return targ;
}

function getElementsByClass(searchClass,num,node,tag) {
	var classElements = new Array();
	if ( node == null )
	{
		node = document;
	}
	if ( tag == null )
	{
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var senum = 0;
	var j=0;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0; j == 0 && i < elsLen; i++) {
		if (els[i].className)
		{
		if (els[i].className==searchClass) {
			if (senum==num)
			{
				classElements = els[i];
				j++;
			}
			senum++;
		}
		}
	}
	return classElements;
}

function goToUrl(url, hash)
{
	if (url.substr(0,3)=='id:')
	{
		url = $(url.substring(3)).getAttribute('nexturl');
	}
	url = url.replace(";:COLOR:;", setColor);
	var myUrls = window.location.href.split("#");
	var myUri = myUrls[0];
	var myanchor = "#"+url; 
	var myLocation = ''; 
	myLocation = myUri + myanchor; 
	if (hash!='onlyhash')
	{
		loaded=false;
		window.location.href = myLocation;
		noClick = 'no';
		window_handler("load");
		play=0;
	}
	window.location.hash = myanchor;
}

function getScrollBarWidth () {
	var inner = document.createElement('p');
	inner.style.width = "100%";
	inner.style.height = "200px";
	
	var outer = document.createElement('div');
	outer.style.position = "absolute";
	outer.style.top = "0px";
	outer.style.left = "0px";
	outer.style.visibility = "hidden";
	outer.style.width = "200px";
	outer.style.height = "150px";
	outer.style.overflow = "hidden";
	outer.appendChild (inner);
	
	document.body.appendChild (outer);
	var w1 = inner.offsetWidth;
	outer.style.overflow = 'scroll';
	var w2 = inner.offsetWidth;
	if (w1 === w2) 
	{
		w2 = outer.clientWidth;
	}
	document.body.removeChild (outer);
	
	return (w1 - w2);
}


/*////////////////////////////////
//                              //
//                              //
//          Set variables		//
//                              //
//                              //
////////////////////////////////*/


var informations;
var numOfAlbum;
var startImage;
var albumCount;
var glob_grid_size = 190;
var glob_mosaic_size = 85;
var glob_time = 0;
var glob_num = 0;
var glob_scrollbar;
var urlName = getFileName();
var urlView = getURLParam('view');
var albumName = getURLParam('album');
var stopratio;
var stopSizeH;
var stopSizeW;
var winW; 
var winH;
var imgW = 500;
var imgH = 350;
var imgNH;
var imgNW;
var ration = 0;
var setColor;
image_elements = new Array();
image_preload_urls = new Array();
var  noClick;
var loaded=false;
var mouse_control_moves = 0;
var show_slider = 0;
glob_sharelink_size = 190;
var imagePreloadList;
var currentImageNum;
var play = 0;

/*////////////////////////////////
//                              //
//                              //
//         	image view  		//
//                              //
//                              //
////////////////////////////////*/

function image_view_load(imageName, controls)
{
	informations = $('information').value.split(";");
	numOfAlbum = informations[0];
	startImage = informations[1];
	//$("img").style.width = '130px';
	if ($('next').getAttribute('nexturl') == '' || $('next').getAttribute('nexturl') == null)
	{
		var next = $('nextnone');
		$('next').id = 'nextnone';
		next.id = 'next';
	}
	if ($('prev').getAttribute('nexturl') == '' || $('prev').getAttribute('nexturl') == null)
	{
		var prev = $('prevnone');
		$('prev').id = 'prevnone';
		prev.id = 'prev';
	}
	
	color = getURLParam('color');
	
	var bodyelem = document.getElementsByTagName('body')[0];
	var topElem = $('top');
	
	if(color=='White')
	{
		bodyelem.style.backgroundColor = 'white';
		topElem.style.borderBottomColor = 'rgb(0,0,0)';
	}
	if(color=='ltGrey')
	{
		bodyelem.style.backgroundColor = 'rgb(150, 150, 150)';
		topElem.style.borderBottomColor = 'rgb(0,0,0)';
	}
	if(color=='dkGrey')
	{
		bodyelem.style.backgroundColor = 'rgb(50, 50, 50)';
		topElem.style.borderBottomColor = 'rgb(150,150,150)';
	}
	if(color=='Black')
	{
		bodyelem.style.backgroundColor = 'rgb(0, 0, 0)';
		topElem.style.borderBottomColor = 'rgb(150,150,150)';
	}
	
	image_view_resize(imageName);

	$("all").style.visibility = 'visible';
	
	if (controls!='hide')
	{
		toggle_controls();
	}
	
	preloadNow();
}

function preloadNow() {
	imagePreloadList = $('nextimageinformation').value;

	insertIntoPreloadImages();
}

function insertIntoPreloadImages() {	
	var imagePreloadArray = new Array();
	
	var tempimageName;
	
	imagePreloadArray = imagePreloadList.split(';:;');
	
	var imageName = $('img').getAttribute('imageName');
	for(num=0; imagePreloadArray.length > num; num++)
	{
		if (imageName == imagePreloadArray[num].split(';;')[0])
		{
			currentImageNum = num;
		}
	}
	
	if (currentImageNum+2>=imagePreloadArray.length)
	{
		var next = $('nextnone');
		$('next').id = 'nextnone';
		next.id = 'next';
	}
	
	if (currentImageNum<=0)
	{
		var prev = $('prevnone');
		$('prev').id = 'prevnone';
		prev.id = 'prev';
	}
	
	for(count=0; count<=6; count++)
	{
		
		num = currentImageNum+count-2;
		
		if (num>=0 && num < imagePreloadArray.length)
		{
			if (count!=2)
			{
				
				var width = imagePreloadArray[num].split(';;')[1];
				
				var height = imagePreloadArray[num].split(';;')[2];
				
				var ration = width/height;
				
				var local_winW = winW;
				var local_winH = winH;
				
				if (ration>=1)//square/wide
				{
					if (((local_winW-20)/ration)>=(local_winH-76))
					{
						width = Math.round((local_winH-76)*ration);
						height = Math.round(local_winH-76);
					}
					else
					{
						height = Math.round((local_winW-20)/ration);
						width = Math.round(local_winW-20);
					}				
				}
				else//tall
				{
					if (((local_winW-20)/ration)>=(local_winH-76))
					{
						width = Math.round((local_winH-76)*ration);
						height = Math.round(local_winH-76);
					}
					else
					{
						height = Math.round((local_winW-20)/ration);
						width = Math.round((local_winW-20));
					}
				}
				var imageUrl = "ajaxinfo/loadimage.php?image="+imagePreloadArray[num].split(';;')[0]+"&album="+albumName+"&height="+height+"&width="+width;
				var preload = $('loadimage'+count);
	
				if (preload.src!=imageUrl)
				{
					
					preload.setAttribute('imagename', imagePreloadArray[num].split(';;')[0]);
					
					preload.lang = width+';'+height+';'+"albums/"+albumName+"/thumbs/"+imagePreloadArray[num].split(';;')[0];
					
					preload.src = imageUrl;
					
					preloadimageandstore(preload, imagePreloadArray[num].split(';;')[0]);
				}
			
				$('loadimage'+count).setAttribute('load', 'true');
			}
		}
		
		if (currentImageNum==0)
		{
			
		}
		if (num+1<imagePreloadArray.length)
		{
			
		}
	}
}

function startPreload(order) {
	var imagePreloadArray = new Array();
	var tempimageName;
	
	imagePreloadArray = imagePreloadList.split(';:;');
	imageName = $('img').getAttribute('imageName');
	
	for(num=0; imagePreloadArray[num]!='' && imagePreloadArray[num]!='undefined' && imagePreloadArray[num]; num++)
	{
		if (imagePreloadArray[num-1])
		{
			if (imageName == imagePreloadArray[num-1].split(';;')[0])
			{
					img = $('img');
					var width = imagePreloadArray[num].split(';;')[1];
					var height = imagePreloadArray[num].split(';;')[2];
					
					ration = width/height;
					
					var local_winW = winW;
					var local_winH = winH;
					
					if (ration>=1)//square/wide
					{		
						if (((local_winW-20)/ration)>=(local_winH-76))
						{
							width = Math.round((winH-76)*ration);
							height = Math.round(winH-76);
						}
						else
						{
							height = Math.round((local_winW-20)/ration);
							width = Math.round(local_winW-20);
						}				
					}
					else//tall
					{
						if (((local_winW-20)/ration)>=(local_winH-76))
						{
							width = Math.round((local_winH-76)*ration);
							height = Math.round(local_winH-76);
						}
						else
						{
							height = Math.round((local_winW-20)/ration);
							width = Math.round((local_winW-20));
						}
					}
					imageUrl = "ajaxinfo/loadimage.php?image="+imagePreloadArray[num].split(';;')[0]+"&album="+albumName+"&height="+height+"&width="+width;
					var preload = new Image();
					preload.src = imageUrl;
					preloadimageandstore(preload, imagePreloadArray[num].split(';;')[0]);
			}
		}
	}
}

function preloadimageandstore(img, imgName)
{ 
	if(!img.complete)
	{ 
		var func = function() {preloadimageandstore(img, imgName);};
		imgWait=setTimeout(func, 10);
	} 
	else{ 
		image_preload_urls[imgName] = img.src;
	} 
}

function loadnextimage(order, controls) {
	imagePreloadArray = imagePreloadList.split(';:;');
	
	num = currentImageNum-2;
	
	var oldimage = $('img');
	if (order=='next')
	{
		var newimage = $('loadimage3');
		
		newimage.id = 'img';
		$('loadimage4').id = 'loadimage3';
		$('loadimage5').id = 'loadimage4';
		$('loadimage6').id = 'loadimage5';
		$('loadimage1').id = 'loadimage6';
		
		oldimage.id = 'loadimage1';
		
		newimagename = imagePreloadArray[num+3].split(';;')[0];
		
		newimagetitle = imagePreloadArray[num+3].split(';;')[3];
		
		if (num+6<=imagePreloadArray.length)
		{
			if ($('prev').className != 'prev')
			{
				var prev = $('prev');
				$('prevnone').id = 'prev';
				prev.id = 'prevnone';
			}
			
			$('next').setAttribute('nexturl', "?image="+imagePreloadArray[num+4].split(';;')[0]+"&album="+albumName+"&view=image&last="+getURLParam('last')+"&color=;:COLOR:;"); 
		}
		else
		{
			var next = $('nextnone');
			$('next').id = 'nextnone';
			next.id = 'next';
		}
		
		$('prev').setAttribute('nexturl', "?image="+imagePreloadArray[num+2].split(';;')[0]+"&album="+albumName+"&view=image&last="+getURLParam('last')+"&color=;:COLOR:;"); 
		
		currentImageNum++;
	}
	else
	{
		var newimage = $('loadimage1');
		
		newimage.id = 'img';
		$('loadimage0').id = 'loadimage1';
		
		oldimage.id = 'loadimage0';
		
		newimagename = imagePreloadArray[num+1].split(';;')[0];
		
		newimagetitle = imagePreloadArray[num+1].split(';;')[3];
		
		if (num>=0)
		{
			if ($('next').className != 'next')
			{
				var next = $('next');
				$('nextnone').id = 'next';
				next.id = 'nextnone';
			}
			
			$('prev').setAttribute('nexturl', "?image="+imagePreloadArray[num].split(';;')[0]+"&album="+albumName+"&view=image&last="+getURLParam('last')+"&color=;:COLOR:;");
		}
		else
		{
			var prev = $('prevnone');
			$('prev').id = 'prevnone';
			prev.id = 'prev';
		}
		$('next').setAttribute('nexturl', "?image="+imagePreloadArray[num+2].split(';;')[0]+"&album="+albumName+"&view=image&last="+getURLParam('last')+"&color=;:COLOR:;"); 
		
		currentImageNum--;
	}
	if (!$('img').complete && order=='next')
	{
		$('img').src = "albums/"+albumName+"/thumbs/"+imagePreloadArray[num+3].split(';;')[0];
	}
	else if (!$('img').complete && order!='next')
	{
		$('img').src = "albums/"+albumName+"/thumbs/"+imagePreloadArray[num+1].split(';;')[0];
	}
	
	$('debug').value = currentImageNum;
	
	oldimage.src = 'mages/gray500.gif';
	
	oldimage.onmousemove = null;
	newimage.onmousemove = toggle_controls;
	
	newimage.setAttribute('imagename', newimagename);
	oldimage.setAttribute('imagename', null);
	
	oldimage.src = null;

	oldimage.style.display = 'none';
	newimage.style.display = 'inline';
	
	oldimage.className = 'hidden';
	newimage.className = '';
	
	oldimage.setAttribute('load', 'false');

	if ($('down').tagName == 'a' || $('down').tagName=='A')
	{
		$('down').href = "ajaxinfo/loadimage.php?image="+newimagename+"&album="+albumName;
	}
	
	$('header_imageview').innerHTML = newimagetitle;
	
	image_view_load(newimagename, controls);
	image_view_load(newimagename, controls);
}



function showing_slider() {
	if (show_slider==0)
	{
	(function() 
		{
			var zoom_slider = $('zoom_slider');
			
			new Control.Slider(zoom_slider.down('.handle'), zoom_slider, 
			{
				range: $R(80, 300),
				sliderValue: 190,
				onSlide: function(value) 
				{
					glob_sharelink_size = value;
					render_share();
				},
				onChange: function(value) 
				{ 
					glob_sharelink_size = value;
					render_share();
				}
			});
		})();
	}
	show_slider=1;
}

function image_view_resize(imagename)
{
	var imagePreloadArray = new Array();
	
	imagePreloadArray = imagePreloadList.split(';:;');
	
	if (imagename=='' || imagename=='undefined' || imagename==null)
	{
		imagename = imagePreloadArray[currentImageNum].split(';;')[0];
	}
	var image = $("img");	
	image.setAttribute('style', '');
	info = image.lang;
	
	var infos = info.split(";");
	
	imgW = infos[0]; 
	imgH = infos[1];
	ration = imgW/imgH;

	var local_winW = winW;
	var local_winH = winH;
	
	$('all').style.height = local_winH - 76;
	$('header_imageview').style.left = (local_winW-($('header_imageview').offsetWidth))/2+'px';
	
	if (ration>=1)//square/wide
	{		
		if (((local_winW-20)/ration)>=(local_winH-76))
		{
			width = Math.round((local_winH-76)*ration);
			height = Math.round(local_winH-76);
		}
		else
		{
			height = Math.round((local_winW-20)/ration);
			width = Math.round((local_winW-20));
			
			image.style.marginTop = (((local_winH-76)-(image.offsetHeight))/2)+'px';
		}
		image.width = width;
		image.height = height;
		var preload = new Image();
  		preload.src = "ajaxinfo/loadimage.php?image="+imagename+"&album="+albumName+"&height="+Math.round(height)+"&width="+Math.round(width);
		glob_num++;
		image_view_wait_image_load(preload,image, glob_num);
	}
	else//tall
	{
		if (((local_winW-20)/ration)>=(local_winH-76))
		{
			width = Math.round((local_winH-76)*ration);
			height = Math.round(local_winH-76);
		}
		else
		{
			height = Math.round((local_winW-20)/ration);
			width = Math.round(local_winW-20);
		}
		image.width = width;
		image.height = height;
		var preload = new Image();
  		preload.src = "ajaxinfo/loadimage.php?image="+imagename+"&album="+albumName+"&height="+height+"&width="+width;
		glob_num++;
		image_view_wait_image_load(preload,image, glob_num);
	}
	image.style.position = 'fixed';
	image.style.left = ((local_winW-width)/2)+'px';
	$('controls').style.left = ((local_winW-286)/2)+'px';
	$('controls').style.top = (local_winH-156)+'px';
	$('shareWindow').style.left = ((local_winW-620)/2)+'px';
}

function image_view_wait_image_load(preload,image,number)
{ 
	if(!preload.complete && number==glob_num)
	{ 
		var func = function() {image_view_wait_image_load(preload,image,number);};
		imgWait=setTimeout(func, 10);
	} 
	else{ 
		image.src = preload.src;
	} 
}

function time_control_hide(mouse_check, waited)
{
	if(mouse_check==mouse_control_moves)
	{
		if (waited>=2000)
		{
			$('controls').fade({ duration: 0.5, from: 1, to: 0 });
		}
		else 
		{
			
			waited = waited+50
			var func = function() {time_control_hide(mouse_check, waited);};
			mouseWait=setTimeout(func, 50);
		}
	} 
}

function toggle_controls() {
	if ($('controls').style.display == "none")
	{
		$('controls').style.display = "block"
		document.getElementsByTagName('body')[0].style.cursor = 'auto';
	}
	
	mouse_control_moves++;
	
	time_control_hide(mouse_control_moves, 0);
}

function playpause(run) {
	imagePreloadArray = imagePreloadList.split(';:;');
	
	var playpauseelem = $('playpause');
	
	if (run!='run')
	{
		if (playpauseelem.getAttribute('nonstatus')=='play')
		{
			playpauseelem.className = 'pause';
			playpauseelem.setAttribute('nonstatus', 'pause');
			play=1;
			
		}
		else
		{
			playpauseelem.className = 'play';
			playpauseelem.setAttribute('nonstatus', 'play');
			play=0;
			clearTimeout(playing);
		}
	}
	
	if (play==1)
	{

		if (currentImageNum+3<=imagePreloadArray.length)
		{
			func = function() {
				if (play==1)
				{
					goToUrl('id:next', 'onlyhash');
					loadnextimage('next', 'hide');
					playpause('run');
				}
			}
			playing=setTimeout(func, 5000);
		}
		else
		{
			playpauseelem.className = 'play';
			playpauseelem.setAttribute('nonstatus', 'play');
			play=0;
			clearTimeout(playing);
		}
	}
}

function render_share() {
	var linktype = Form.getInputs('shareSettings','radio','linktype').find(function(radio) { return radio.checked; }).value;
	
	var linktarget = Form.getInputs('shareSettings','radio','linkTarget').find(function(radio) { return radio.checked; }).value;
	
	linkText = '<a href="::URL::">::INNER::</a>';

	if (linktype=='image')
	{
		$('shareTypeText').style.display = 'none';
		$('shareTypeImage').style.display = 'block';
		imgSize = Math.round(glob_sharelink_size);
		
		var image = $("img");	
		info = image.lang;
		
		var infos = info.split(";");
		
		imgW = infos[0]; 
		imgH = infos[1];
		thumbsrc = infos[2];
		
		ration = imgW/imgH;
		
		if (ration>=1)//square/wide
		{		
			width = imgSize;
			height = Math.round(imgSize/ration);
		}
		else//tall
		{
			height = imgSize;
			width = Math.round(imgSize*ration);
		}
		
		linkText = linkText.replace("::INNER::", '<img width="'+width+'" height="'+height+'" src="http://'+window.location.host+window.location.pathname+thumbsrc+'"/>');
	}
	else
	{
		$('shareTypeText').style.display = 'block';
		$('shareTypeImage').style.display = 'none';
		
		shareText = $("shareText").value;
		
		linkText = linkText.replace("::INNER::", shareText);
	}
	if (linktarget=='grid')
	{
		linkText = linkText.replace("::URL::", "http://"+window.location.host+window.location.pathname+"#?album="+albumName+"&view=grid&color=");	
	}
	else if (linktarget=='mosaic')
	{
		linkText = linkText.replace("::URL::", "http://"+window.location.host+window.location.pathname+"#?album="+albumName+"&view=mosaic&color=");	
	}
	else
	{
		linkText = linkText.replace("::URL::", window.location.href);	
	}
		
	$('sharePreview').innerHTML = linkText;
	
	$("shareCopyText").value = linkText;
}

/*////////////////////////////////
//                              //
//                              //
//         	Album view  		//
//                              //
//                              //
////////////////////////////////*/

function album_view_load() {
	informations = $('information').value.split(";");
	numOfAlbum = informations[0];
	startImage = informations[1];
	setColor = getURLParam('color');
	if (setColor=='' || setColor=='undefined')
	{
		setColor = 'Black';	
	}
	else if (setColor!='Black' && setColor!='White' && setColor!='ltGrey' && setColor!='dkGrey')
	{
		setColor = 'Black';	
	}
	album_view_resize('reflect');
	colorChange('click', setColor)
	if (urlView == 'mosaic')
	{
		mosaic_view_slider_resize(85);
		mosaic_view_slider_resize(85);
		$("bg").style.visibility = 'hidden';
		$("bg").style.margin = '0';
		$("bg").style.height = '0';
		$("view").style.visibility = 'visible';
	}
	if (urlView == 'grid')
	{
		$("bg").style.visibility = 'hidden';
		$("bg").style.margin = '0';
		$("bg").style.height = '0';
		$("bg").style.padding = '0';
		$("bg").style.display = 'none';
		$("view").style.visibility = 'visible';
		(function() 
		{
			var zoom_slider = $('zoom_slider');
			if(glob_grid_size=='')
			{
				glob_grid_size=190;
			}
			new Control.Slider(zoom_slider.down('.handle'), zoom_slider, 
			{
				range: $R(80, 300),
				sliderValue: glob_grid_size,
				onSlide: function(value) 
				{
					if(value==300 || value==80)
					{
						grid_view_resize_slider(value, 'reflect');
					}
					else
					{
						grid_view_resize_slider(value);
					}
					glob_grid_size = value;
				},
				onChange: function(value) 
				{ 
					if(value==300 || value==80)
					{
						grid_view_resize_slider(value, 'reflect');
					}
					else
					{
						grid_view_resize_slider(value);
					}
					glob_grid_size = value;
				}
			});
		})();		
	}
	if (urlView == 'mosaic')
	{
		if (glob_mosaic_size=='')
		{
			glob_mosaic_size=85;	
		}
		(function() 
		{
			var zoom_slider = $('zoom_slider');
	
			new Control.Slider(zoom_slider.down('.handle'), zoom_slider, 
			{
				range: $R(50, 120),
				sliderValue: glob_mosaic_size,
				onSlide: function(value) 
				{
					mosaic_view_slider_resize(value);
					mosaic_view_slider_resize(value);
					glob_mosaic_size = value;
				},
				onChange: function(value) 
				{
					mosaic_view_slider_resize(value);
					mosaic_view_slider_resize(value);
					glob_mosaic_size = value;
				}
			});
		})();
		mosaic_view_slider_resize(glob_mosaic_size);
		mosaic_view_add_new_image('image1');
	}
}

function album_view_resize(reflect)
{	
	var width;
	var local_winW = winW;
	var local_winH = winH;
	var scrollbar = glob_scrollbar;
	if(scrollbar==17)
	{
		scrollbar=0;
	}
	else if (scrollbar==15)
	{
		scrollbar -= 6;
	}
	if (urlView == 'grid')
	{
		$('imagesgrid').style.width = local_winW+'px';
		$('imagesgrid').style.height = (local_winH-96)+'px';
		grid_view_resize_slider(null, reflect);
		grid_view_resize_slider(null, reflect);
	}
	if (urlView == 'mosaic')
	{
		
		$('imagesmos').style.height = (local_winH-96)+'px';
		mosaic_view_add_new_image('none');
	}
	$('bottom').style.top = (local_winH-40)+'px';
	$('bottom').style.width = local_winW+'px';
}

function grid_view_resize_slider(value, update) {
	if (urlView == 'grid')
	{
	var imgSize = value || glob_grid_size;
	var local_winW = winW;
	var local_winH = winH;
	
	
	imgSize = parseInt(imgSize);
	
	var scrollbar = glob_scrollbar;
	
	var local_winW = local_winW-scrollbar;
	
	var minImgW = imgSize+20;
	
	var maxNum = local_winW/minImgW;
	
	maxNum = parseInt(maxNum); 
	
	var size = local_winW/maxNum;
	var op;
	
	size = parseInt(size);
	
	var margintop	=0;
	var toptimes	=1;
	var times		=0;
	var num			=0;
	
	glob_time++;
	
	image_elements = Array();
	
	while (num<numOfAlbum) 
	{
		imageelement = $('img'+num);
		element = $('div'+num);
		spelement = $('span'+num);
		inpelement = $('form'+num);
		textelement = $('text'+num);
		bgElem = $('imagesgrid');
		
		image_elements[num] = imageelement;
		
		info = element.lang;
		
		var mySplitResult = info.split(";");
		
		imageName = mySplitResult[0];
		orig_width = mySplitResult[1];
		orig_height = mySplitResult[2];
		thumbpath = mySplitResult[3];
		
		if (update=='reflect' || update=='resize')
		{
			var preload = new Image();
  			preload.src = thumbpath;
			grid_view_wait_image_load(preload, imageelement, num);
			Reflection.add(imageelement, { opacity: 1/4, height: 1/4 });
		}
		
		
		ratio = orig_width/orig_height;
		
		canvaselement = getElementsByClass('reflection', num);
		
		if (document.all && !window.opera) {
			changer=1;
		}
		else
		{
			changer=1/4;
		}
		
		if (times>=maxNum)
		{
			margintop = ((imgSize+(80/300)*imgSize)+50)*toptimes;
			times=0;
			toptimes++;
		}
		if (setColor=='White' || setColor=='ltGrey')
		{
			spelement.style.color = 'rgb(40, 40, 40)';	
			spelement.style.textShadow = 'rgb(255, 255, 255) 0px 0px 2px';
		}
		else {
			spelement.style.color = 'rgb(204, 204, 204)';
			spelement.style.textShadow = 'rgb(0, 0, 0) 0px 0px 2px';
		}
		if (ratio>=1)//wide
		{
			width = imgSize;
			height = width/ratio;
			element.style.width = size;
			element.style.left = size*times+((size-width)/2)+'px';
			element.style.top = margintop+(imgSize+((50/300)*imgSize)-height)+10+'px';
			textelement.style.top = height+'px';
			textelement.style.width = width+'px';
			textelement.style.height = (height/4)-1+'px';
			imageelement.width = width;
			imageelement.height = height;
			canvaselement.style.width = width+"px";
			canvaselement.style.height = height*changer+"px";
			miniW = 140;
			miniH = 140/ratio;
		}
		else
		{
			height = imgSize;
			height = (height+((50/300)*imgSize));
			width = height*ratio;
			marginleft = size*times;
			element.style.width = size;
			element.style.left = marginleft+((size-width)/2)+'px';
			element.style.top = margintop+10+'px';
			textelement.style.top = height+'px';
			textelement.style.width = width+'px';
			textelement.style.height = (height/4)-1+'px';
			imageelement.height = height;
			imageelement.width = width;
			canvaselement.style.width = width+"px";
			canvaselement.style.height = height*changer+"px";
			miniH=140;
			miniW=140*ratio;
		}
		times++;
		num++;
	}
	lastNum=maxNum;
	}
}

function mosaic_view_resize_center_image(elementname,newornot){
	var local_winW = winW;
	var local_winH = winH;
	imgW = imgNW;
	imgH = imgNH;
	
	var scrollbar = glob_scrollbar;
	
	if(scrollbar==17)
	{
		local_winH += 20;
		scrollbar=0;
	}
	else if (scrollbar==15)
	{
		scrollbar -= 6;
	}
	var image = $("mosaicimg");	
	local_winW=local_winW-400-scrollbar;

	if (local_winW >= local_winH)//square/wide
	{
		if (((local_winW-20)/ration)>=(local_winH-150))
		{
			image.width = (local_winH-150)*ration;
			image.height = local_winH-150;
		}
		else
		{
			image.height = (local_winW-20)/ration;
			image.width = (local_winW-20);
		}
		image.style.left = ((local_winW-image.offsetWidth)/2)+5+'px';
		image.style.top = ((local_winH-image.offsetHeight)/2)+'px';
		image.src = "ajaxinfo/loadimage.php?image="+elementname+"&album="+albumName+"&height="+Math.round(image.height)+"&width="+Math.round(image.width);
		image.name = elementname;
		if (newornot!='none')
		{
			mosaic_view_wait_image_load(image);
		}
	}
	else//tall
	{	
		if (((local_winW-20)/ration)>=(local_winH-150))
		{
			image.width = (local_winH-150)*ration;
			image.height = local_winH-150;
		}
		else
		{
			image.height = (local_winW-20)/ration;
			image.width = (local_winW-20);
		}
		image.style.left = ((local_winW-image.offsetWidth)/2)+5+'px';
		image.style.top = ((local_winH-image.offsetHeight)/2)+'px';
		image.style.minWidth = 200*ration+'px';
		image.style.minHeight = 200+'px';
		image.src = "ajaxinfo/loadimage.php?image="+elementname+"&album="+albumName+"&height="+Math.round(image.height)+"&width="+Math.round(image.width);
		image.name = elementname;
		if (newornot!='none')
		{
		mosaic_view_wait_image_load(image);
		}
	}
	$('mosaic_photo_text').style.top = image.offsetHeight+image.offsetTop+5+'px';
	$('mosaic_photo_text').style.left = ((local_winW-$('mosaic_photo_text').offsetWidth)/2)+'px';
}

function bottomMouseUp() {
	if (urlView == 'grid')
	{
	var func = function() {grid_view_resize_slider(glob_grid_size, 'reflect');};
	test=setTimeout(func, 20);
	}
}

function grid_view_click_newimage(e) {
	var outside = 'out';
	var num=0;
	var wide;
	var tall;
	e = e || window.event;
	
	while (num<=numOfAlbum-1) 
	{
		image = 'img'+num;
		if(!clickedOutsideElement(image,e))
		{
			outside = 'in';
			id = image;
		}
		
		num++;
	}
	if (outside=='in')
	{
		var image = $(id);
		var filename = image.name;
		goToUrl("?image="+filename+"&album="+albumName+"&view=image&last=grid&color="+setColor);
	}
}

function mosaic_view_add_new_image(image)
{
	if (image!='none')
	{
		var info = $(image).lang;
		var imgS = info.split(";");
		
	
		file = imgS[0]; 
		imgW = imgS[1];
		imgH = imgS[2];
		
		$('mosaic_photo_text').innerHTML = imgS[3];
			
		var ratio = imgW/imgH;
		ration = ratio;
		imgNW = imgW;
		imgNH = imgH;
	}
	else
	{
		var file = $('mosaicimg').name;
	}
	mosaic_view_resize_center_image(file, image);
}

function grid_view_wait_image_load(preload, img, num)
{
	if(!preload.complete)
	{
		var func = function() {grid_view_wait_image_load(preload, img, num);};
		imgWait=setTimeout(func, 10);
	} 
	else{
		img.src = preload.src;
		Reflection.add(img, { opacity: 1/4, height: 1/4 });
	} 
}

function mosaic_view_wait_image_load(img)
{ 
	if(!img.complete)
	{ 
		img.style.visibility = 'hidden';
		var func = function() {mosaic_view_wait_image_load(img);};
		imgWait=setTimeout(func, 10);
	} 
	else{ 
		img.style.visibility = 'visible';
	} 
}

function mosaic_view_slider_resize(size) { 
	var num=1;
		
	while (num<=numOfAlbum) 
	{
		image = 'image'+num;
		$(image).width = size;

		num++;
	}
}

function mosaic_view_click_newimage()
{
	var filename = $('mosaicimg').name;
	goToUrl("?image="+filename+"&album="+albumName+"&view=image&last=mosaic&color="+setColor);
}

/*////////////////////////////////
//                              //
//                              //
//         	skim 		  		//
//                              //
//                              //
////////////////////////////////*/

function skim_view_load(imageName)
{
	document.getElementsByTagName('body')[0].style.backgroundColor = '#000';
	informations = $('information').value.split(";");
	albumCount = informations[2];
	
	skim_view_resize();
}

function skim_view_resize()
{
	var local_winW = winW;
	var local_winH = winH;
	
	$('container').style.height = (local_winH)+'px';
	
	var scrollbar = glob_scrollbar;
	
	local_winW = local_winW-scrollbar;
	
	var minImgW = 187;
	
	var maxNum = local_winW/minImgW;
	
	maxNum = parseInt(maxNum); 
	
	var size = local_winW/maxNum;
	
	size = parseInt(size);
	
	var margintop		=0;
	var margintop_minus =0;
	var grouptimes		=0;
	var toptimes		=0;
	var times			=0;
	var num				=0;
	var type;
	
	glob_time++;
	
	while (num<albumCount) 
	{
		if (times==maxNum)
		{
			$('debug').value = 'times: '+times+' ; maxNum: '+maxNum; 
			toptimes++;
			margintop = (182*(toptimes))+(25*(toptimes+1));
			times=0;
		}
		albumHolder = $('albumHolder'+(num+1));
		
		type = albumHolder.getAttribute('typeofelement');
		
		if (type!='group')
		{
			if (albumHolder.lang != 'hide')
			{
				albumHolder.style.display = 'block';
				albumHolder.style.left = (size*(times))+((size-187)/2)+15+'px';
				albumHolder.style.top = margintop+50+'px';
				times++;
			}
			else {
				albumHolder.style.display = 'none';
					
			}
		}
		else
		{
			
			if (num!=0 && times!=0)
			{
			toptimes++;
			}
			grouptimes++;
			times=0;
			margintop = (182*(toptimes))+(25*(toptimes))+(30*grouptimes);
			albumHolder.style.top = margintop+30+'px';
			albumHolder.style.left = (size*(times))+((size-187)/2)+15+'px';
			albumHolder.style.width = local_winW-(albumHolder.offsetLeft*2)+'px';
			
		}
		num++;
		/*
		albumHolder = $('albumHolder'+(num+1));
		if (albumHolder.lang.substr(0,5)=='group')
		{
			toptimes++;
			margintop = (172*(toptimes))+(25*(toptimes+1))+(40*grouptimes);
			grouptimes++;
			times=0;
		}
		if (times>=maxNum)
		{
			toptimes++;
			margintop = (172*(toptimes))+(25*(toptimes+1));
			times=0;
		}
		albumHolder.style.left = (size*(times))+((size-187)/2)+15+'px';
		albumHolder.style.top = margintop+60+'px';
		times++;
		num++;
		if (albumHolder.lang.substr(0,5)=='group')
		{
			margintop = (172*(toptimes))+(25*(toptimes+1))+(40*grouptimes);
			albumHolder.style.width = local_winW-(parseInt(albumHolder.style.left)*2)+'px';
			times=0;
		}
		*/
	}
	
}

function show_hide_group(group_to_hide) {
	var num	=0;
	var type;
	while (num<albumCount) 
	{
		albumHolder = $('albumHolder'+(num+1));
		
		type = albumHolder.getAttribute('typeofelement');
		group = albumHolder.getAttribute('group');
		if (type!='group' && group==group_to_hide)
		{
			
			if (albumHolder.lang == 'hide')
			{
				albumHolder.lang = 'show';
			}
			else {
				albumHolder.lang = 'hide';
			}
		}
		else if (type=='group' && group==group_to_hide) {
			if (albumHolder.lang == 'hide')
			{
				albumHolder.lang = 'show';
				albumHolder.className = 'albumGroup';
			}
			else {
				albumHolder.lang = 'hide';
				albumHolder.className = 'albumGroupComp';
			}
		}
		num++;
	}
	skim_view_resize();
}

/*////////////////////////////////
//                              //
//                              //
//         	other 		  		//
//                              //
//                              //
////////////////////////////////*/

function clickedOutsideElement(elemId,e) {
	e = e || window.event;
	var theElem = getEventTarget(e);
	
	while(theElem !== null) {
		if(theElem.id == elemId)
		{
			return false;
		}
		
		theElem = theElem.offsetParent;
	}
	return true;
}

function colorChange(action, color)
{
	pickerElem = $('color'+color);
	whiteElem = $('colorWhite');
	greyltElem = $('colorltGrey');
	greydkElem = $('colordkGrey');
	blackElem = $('colorBlack');
	bottomElem = $('bottom');
	topElem = $('top');
	if(action=='click')
	{
		setColor = color;
		if (urlView == 'grid')
		{
			elem = $('view');
		}
		else
		{
			elem = $('view');
			elemC = $('mosaic_photo_text');
		}
		classerName = pickerElem.className;
		classerName = classerName.replace("Select", '');
		classerName = classerName.replace("Hover", '');
		pickerElem.className = classerName+'Select';
		if(color=='White')
		{
			if (urlView == 'grid')
			{
				elem.style.backgroundColor = 'white';
			}
			else
			{
				elem.style.backgroundColor = 'white';
				elemC.style.color = 'rgb(0,0,0)';
			}
			bottomElem.style.borderTopColor = 'rgb(0,0,0)';
			topElem.style.borderBottomColor = 'rgb(0,0,0)';
			greyltElem.className = 'colorltGrey';
			greydkElem.className = 'colordkGrey';
			blackElem.className = 'colorBlack';
		}
		if(color=='ltGrey')
		{
			if (urlView == 'grid')
			{
				elem.style.backgroundColor = 'rgb(150, 150, 150)';
			}
			else
			{
				elem.style.backgroundColor = 'rgb(150, 150, 150)';
				elemC.style.color = 'rgb(0,0,0)';
			}
			bottomElem.style.borderTopColor = 'rgb(0,0,0)';
			topElem.style.borderBottomColor = 'rgb(0,0,0)';
			whiteElem.className = 'colorWhite';
			greydkElem.className = 'colordkGrey';
			blackElem.className = 'colorBlack';
		}
		if(color=='dkGrey')
		{
			if (urlView == 'grid')
			{
				elem.style.backgroundColor = 'rgb(50, 50, 50)';
			}
			else
			{
				elem.style.backgroundColor = 'rgb(50, 50, 50)';
				elemC.style.color = 'rgb(150,150,150)';
			}
			bottomElem.style.borderTopColor = 'rgb(150,150,150)';
			topElem.style.borderBottomColor = 'rgb(150,150,150)';
			greyltElem.className = 'colorltGrey';
			whiteElem.className = 'colorWhite';
			blackElem.className = 'colorBlack';
		}
		if(color=='Black')
		{
			elem.style.backgroundColor = 'rgb(0, 0, 0)';
			if (urlView == 'grid')
			{
				elem.style.backgroundColor = 'rgb(0, 0, 0)';
			}
			else
			{
				elem.style.backgroundColor = 'rgb(0, 0, 0)';
				elemC.style.color = 'rgb(150,150,150)';
			}
			bottomElem.style.borderTopColor = 'rgb(150,150,150)';
			topElem.style.borderBottomColor = 'rgb(150,150,150)';
			greyltElem.className = 'colorltGrey';
			greydkElem.className = 'colordkGrey';
			whiteElem.className = 'colorWhite';
		}
	}
}

window.onresize = function()
{
	if (loaded==true)
	{
		window_handler("resize");
	}
}

window.onload = function()
{
	window_handler("load");
}

function gray_image_reflect_load(img)
{
	if(!img.complete)
	{
		var func = function() {gray_image_reflect_load(img);};
		imgWait=setTimeout(func, 10);
	} 
	else{ 
		album_view_load();
	} 
}

function window_handler(action)
{
	anaction = action;
	urlName = getFileName();
	urlView = getURLParam('view');
	albumName = getURLParam('album');
	if(typeof(window.innerWidth) === 'number' ) 
	{
		//Non-IE
		winW = window.innerWidth;
		winH = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}
	if (anaction=='load')
	{
		
		glob_scrollbar = getScrollBarWidth();
		
		new Ajax.Request("ajaxinfo/input_start_info.php?album="+getURLParam('album')+"&image="+getURLParam('image')+"&view="+getURLParam('view')+"&last="+getURLParam('last')+"&color="+getURLParam('color')+"&start="+getURLParam('start')+"&count="+getURLParam('count'), {
            method: "post",onSuccess: function(transport) {
				respText = transport.responseText;
				$('information').value = respText;
			},asynchronous:false
		});
		
		informations = $('information').value.split(";");
		numOfAlbum = informations[0];
		startImage = informations[1];
		numberofalbums = informations[2];
		nowimage = informations[3];
		if (urlView=='mosaic')
		{
			new Ajax.Request("includes/album.php?album="+getURLParam('album')+"&image="+getURLParam('image')+"&view="+getURLParam('view')+"&last="+getURLParam('last')+"&color="+getURLParam('color'), {
           		method: "post",onSuccess: function(transport) {
					respText = transport.responseText;
					$('ajax_here').innerHTML = "";
					$('ajax_here').innerHTML = respText;
					$('view').style.visibility = 'visible';
					album_view_load();
				},asynchronous:false
			});
			Event.observe(document.body, 'click', function(event) {
				if (noClick=='')
				{
					var element = Event.element(event);
					var num=1;
					if (element.id == 'mosaicimg')
					{
						mosaic_view_click_newimage(element);	
					}	
					while (num<=numOfAlbum) 
					{
						image = $('image'+num);
						if (image==element)
						{
							mosaic_view_add_new_image(image.id);	
						}
						num++;
					}
				}
			});
			noClick = '';
		}
		else if (urlView=='grid')
		{
			new Ajax.Request("includes/album.php?album="+getURLParam('album')+"&image="+getURLParam('image')+"&view="+getURLParam('view')+"&last="+getURLParam('last')+"&color="+getURLParam('color')+"&start="+getURLParam('start')+"&count="+getURLParam('count'), {
           		method: "post",onSuccess: function(transport) {
					respText = transport.responseText;
					$('ajax_here').innerHTML = "";
					$('ajax_here').innerHTML = respText;
					$('view').style.visibility = 'visible';
					var preload = new Image();
					preload.src = "images/gray500.gif";
					gray_image_reflect_load(preload);
				},asynchronous:false
			});
		}
		else if (urlView=='image')
		{
			new Ajax.Request("includes/image.php?album="+getURLParam('album')+"&image="+getURLParam('image')+"&view="+getURLParam('view')+"&last="+getURLParam('last')+"&color="+getURLParam('color'), {
           		method: "post",onSuccess: function(transport) {
					respText = transport.responseText;
					$('ajax_here').innerHTML = "";
					$('ajax_here').innerHTML = respText;
					$('view').style.visibility = 'visible';
					preloadNow();
					image_view_load(nowimage);
					image_view_resize(nowimage);
					show_slider=0;
					glob_sharelink_size = 190;
				},asynchronous:false
			});
		}
		else
		{
			new Ajax.Request("includes/skim.php?album="+getURLParam('album')+"&image="+getURLParam('image')+"&view="+getURLParam('view')+"&last="+getURLParam('last'), {
           		method: "post",onSuccess: function(transport) {
					respText = transport.responseText;
					$('ajax_here').innerHTML = "";
					$('ajax_here').innerHTML = respText;
					skim_view_load();
				},asynchronous:false
			});
		}
	}
	if ($('view')) $('view').style.height = winH+'px';
	if (anaction=='resize')
	{
		if (urlView=='grid' || urlView=='mosaic')
		{
			album_view_resize('resize');
		}
		else if (urlView=='image')
		{
			image_view_resize();
		}
		else
		{
			skim_view_resize();
		}
	}
	loaded=true;
}
