// Mouse position Vars
var mx;
var my;


// Imported vars from PHP
var msgFormError;
var maxImgWidth;
var maxImgHeight;
var galleryFolder;
var langCerrar;
var langCargandoImg;

// Check for IE
var isIE = navigator.userAgent.indexOf("MSIE")>0;

// Auto-control Vars
thumbs_count = 25;
thumbs_loaded = false;
thumb_alpha = 0;

function thumbs(w,h){
	e = document.getElementById('thumbs');
	thumb_num = Math.floor(Math.random() * thumbs_count) + 1;
	
	if (!thumbs_loaded){
		e.MozOpacity = 0;
		e.innerHTML = "<img src='old/galeria/gal" + thumb_num + ".jpg' width='" + w + "' height='" + h + "' />";
		alphaup(e);
		thumbs_loaded = true;
	} else {
		alphadown(e);
		//e.innerHTML = "<img src='old/galeria/gal" + thumb_num + ".jpg' width='" + w + "' height='" + h + "' />";
		//alphaup(e);
	}
		
	//e.style.MozOpacity = "0.5";
	//e.style.filter = "alpha(opacity=50)";
	setTimeout("thumbs(" + w + "," + h + ")",5000);
}

function getXY(e) {
  mx = (window.Event) ? e.pageX : event.clientX;
  my = (window.Event) ? e.pageY : event.clientY;
}


function alphadown(e){
	c = e.style.MozOpacity;
	if (c > 0){
		e.style.MozOpacity -= 0.1;
		setTimeout("alphadown(" + e + ")",1000);
	}
}

function alphaup(e){
	c = e.style.MozOpacity;
	if (c < 100){
		e.style.MozOpacity += 0.1;
		setTimeout("alphaup(" + e + ")",1000);
	}
}

function showDescription(d,source){
	//getXY(e);
	
	d = document.getElementById(d);
	
	if (isIE && document.body.scrollTop){
		my = my + document.body.scrollTop;
	}
	
	
	with(d.style){
		top = (my - 60) + "px";
		left = (mx - 250) + "px";
		display = "block";
	}
	
	source.onmouseout = function(){
		d.style.display = "none";
	}
}

function swapImg(dst,newImg){
	document.getElementById(dst).src = newImg + "2.jpg";
}

function checkMsgForm(f){
	error = false;
	emailCheckRE = /^[a-z0-9\.-_]+\@[a-z0-9\.-_]+\.[a-z]{2,3}$/i;
	for (i=0;i<f.length;i++){
		e = f.elements[i];
		if (e.type == "text" && e.name == "contactName" && e.value == ""){
			error = true;
			//e.style.background = "#FFDDDD";
			//e.style.border = "1px solid #FFAAAA";
		}
		
		
		if (e.type == "text" && e.name == "contactEmail" && !emailCheckRE.test(e.value)){
			error = true;
			//e.style.background = "#FFDDDD";
			//e.style.border = "1px solid #FFAAAA";
		}
		
		if (e.type == "textarea" && e.name == "msg" && e.value == ""){
			error = true;
			
		}
		/*
		e.onfocus = function(){
				this.style.background = "";
				this.style.border = "";
		}
		*/
	}
	
	if (error){
		//alert("No se han rellenado correctamente todos los campos.");
		alert(msgFormError);
		return false;
	} else {
		return true;
	}
}
/*

function showMagnifier(){
	document.getElementById('magnifier').display = "block";
	document.
}
*/

function galleryLoaded(){
		document.getElementById('divImgLoading').style.display = "none";	
		document.getElementById('gallery').style.display = "block";
}

function preloadImages(){
	if(document.images){
		if(!document.imageArray) document.imageArray = new Array();
		var i,j = document.imageArray.length, args = preloadImages.arguments;
		
		for(i=0; i<args.length; i++){
			if (args[i].indexOf("#")!=0){
				document.imageArray[j] = new Image;
				document.imageArray[j++].src = args[i];
			}
		}
	}
}
/*
function fullimg(img,title,width,height){
	//var width=w, height=h;
	
	alert(img);
	
	if (width == undefined){
		width = maxImgWidth;
	} 
	
	if (height == undefined){
		height == maxImgHeight;
	}
		
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var wind = window.open('','fullimg', styleStr);
	var headHtml = '<head><title>'+title+'</title></head>';
	var bodyHtml = "<body style='background: #333333; margin: 0px; padding:0px;'><table height='100%' width='100%' cellspacing='0' cellpadding='0'><tr><td align='center' valign='middle'><a href='javascript:self.close()'><img src='" + galleryFolder + img + "3.jpg' border='0'></a></td></tr></table></body>";
	//alt='Haz clic para cerrar esta ventana' title='Haz clic para cerrar esta ventana'
	wind.document.write(headHtml + bodyHtml);
}
*/

function showFullImg(src,galleryImg){
	if (src){
		if (galleryImg == true){
			src = src.replace("2\.jpg","") + "3.jpg";
		}
		d = document.getElementById('fullImgDiv');
		c = document.getElementById('fullImgContainer');
		c.innerHTML = "<a href='javascript:closeFullImg();'><div id='fullImgLoading'><img src='img/loading_black.gif' /><br /><br />" + langCargandoImg + "</div><img src='" + src + "' style='display: none;' onclick='closeFullImg();' onload=\"document.getElementById('fullImgLoading').style.display='none'; this.style.display='block';\" /><br /><br />[" + langCerrar + "]</a>";
		d.style.display = "block";
	}
}

function closeFullImg(){
	document.getElementById('fullImgDiv').style.display = "none";
}

function moveFullImgDiv(){
	document.getElementById('fullImgDiv').style.top = document.body.scrollTop + "px";
}

document.onmousemove = getXY;