/**
 * 
 */

function arrowkeys_init() {
	var handler = arrowkeys_handle_keypress;
	//var handlerMouse = point_it;
	if (document.addEventListener) {
		document.addEventListener("keydown", handler, false);
		document.addEventListener("click", handler, false);

	} else {
		document.attachEvent("onkeydown", handler);
		document.attachEvent("onclick", handler);
		// Internet Explorer needs to remove event handlers on page unload
		// in order to avoid memory leaks.		
		if (window.attachEvent) {
			window.attachEvent("onunload", function() {
				document.detachEvent("onkeydown", handler);
				document.detachEvent("onclick", handler);
			});
		}
	}
}

function clearBox(element) {
	element.value = "";
}

function cargaLogo() {
	if (getActiveStyleSheet() == 'dark') {
		document.getElementById('logoes').src = 'templates/delicious_v1_1_mod/images/logo_es_b.png';
	} else if (getActiveStyleSheet() == 'light') {
		document.getElementById('logoes').src = 'templates/delicious_v1_1_mod/images/logo_es_w.png';
	}
}

function browsing(imagePreviousId, imageNextId, imageId, monId) {
	var existprev = imagePreviousId;
	var existnext = imageNextId;
	
	if (monId=="") {
		if (existprev != imageId) {
			document.write('<a href="index.php?showimage='
							+ imagePreviousId
							+ '" id="menu_browse_prev" onclick="document.location=\'index.php?showimage='
							+ imagePreviousId + '\';"></a>');
		}
		if (existnext != imageId) {
			document.write('<a href="index.php?showimage='
							+ imageNextId
							+ '" id="menu_browse_next" onclick="document.location=\'index.php?showimage='
							+ imageNextId + '\';"></a>');
		}
	} else {
		if (existprev != imageId) {
			document.write('<a href="index.php?showimage='
							+ imagePreviousId + '&monid=' + monId 
							+ '" id="menu_browse_prev" onclick="document.location=\'index.php?showimage='
							+ imagePreviousId + '\';"></a>');
		}
		if (existnext != imageId) {
			document.write('<a href="index.php?showimage='
							+ imageNextId + '&monid=' + monId 
							+ '" id="menu_browse_next" onclick="document.location=\'index.php?showimage='
							+ imageNextId + '\';"></a>');
		}
	}
}

function arrowkeys_handle_keypress(e) {
	var target;
	if (!e) {
		var e = window.event;
	}
	if (e.target) {
		target = e.target;
	} else if (e.srcElement) {
		target = e.srcElement;
	}
	if (target.nodeType == 3) { // defeat Safari bug
		target = target.parentNode;
	}
	if (!(target.tagName == 'INPUT' || target.tagName == 'TEXTAREA')) {
		KEY_RIGHT = 39;
		KEY_LEFT = 37;
		KEY_ESCAPE = 27;
		MOUSE_CLICK_FF = 1;
		MOUSE_CLICK_IE = 0
		var arrowkeys_next = document.getElementById('menu_browse_next');
		var arrowkeys_previous = document.getElementById('menu_browse_prev');
		var key = e.which || e.keyCode;

		if (key) {
			switch (key) {
			case KEY_RIGHT:
				if (arrowkeys_next) {
					window.location = arrowkeys_next.href;
				}
				break;
			case KEY_LEFT:
				if (arrowkeys_previous) {
					window.location = arrowkeys_previous.href;
				}
				break;
			case KEY_ESCAPE:
				if (document.getElementById('info').style.display != 'none') {
					createCookie('info', 'false', 365);
					Effect.toggle('info', 'BLIND');
				} else if (document.getElementById('comments').style.display != 'none') {
					createCookie('comments', 'false', 365);
					Effect.toggle('comments', 'BLIND');
				}
				/*else if (document.getElementById('social').style.display != 'none') {
					createCookie('social', 'false', 365);
					Effect.toggle('social', 'BLIND');
				}*/
				if (document.getElementById('loginpanel').style.display != 'none') {
					Effect.toggle('loginpanel', 'BLIND');
				}
				return false;
			case MOUSE_CLICK_IE || MOUSE_CLICK_FF:
				if (target.parentNode.tagName != 'A') {
					var photo = document.getElementById('photo');
					
					var top = Top(photo);
					var left = Left(photo);
					
					var height = photo.height;
					var width = photo.width;
					
					var xPoint = e.clientX;
					var yPoint = e.clientY;
					
					if (!(xPoint>left && xPoint<left+width && yPoint>top && yPoint<top+height)) {
						if (document.getElementById('info').style.display != 'none') {
							createCookie('info', 'false', 365);
							Effect.toggle('info', 'BLIND');
						} else if (document.getElementById('comments').style.display != 'none') {
							createCookie('comments', 'false', 365);
							Effect.toggle('comments', 'BLIND');
						} /*else if (document.getElementById('social').style.display != 'none') {
							document.getElementById('socialA').innerHTML='[+] Social';
							createCookie('social', 'false', 365);
							Effect.toggle('social', 'BLIND');
						}*/
						if (document.getElementById('loginpanel').style.display != 'none') {
							Effect.toggle('loginpanel', 'BLIND');
						}
						return false;
					}
				}
			}
		}
	}
}

function Left(obj) {
	var curleft = 0;
	if (obj.offsetParent)
		while (1) {
			curleft += obj.offsetLeft;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function Top(obj) {
	var curtop = 0;
	if (obj.offsetParent)
		while (1) {
			curtop += obj.offsetTop;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/*
function showSocial() {
	
	createCookie('info', 'false', 365);
	createCookie('comments', 'false', 365);
	createCookie('social', 'false', 365);
	if (document.getElementById('social') != null
			&& document.getElementById('social').style != null
			&& document.getElementById('social').style.display != null
			&& document.getElementById('social').style.display != 'none') {
		if (document.getElementById('social').style.display != 'none') {
			createCookie('social', 'false', 365);
		} else {
			createCookie('social', 'true', 365);
		}
	} else {
		createCookie('social', 'true', 365);
	}
	
	if (document.getElementById('info').style.display != 'none') {
		Effect.toggle('info', 'BLIND');
	}
	if (document.getElementById('comments').style.display != 'none') {
		Effect.toggle('comments', 'BLIND');
	}

	if (document.getElementById('social').style.display != 'none') {
		document.getElementById('socialA').innerHTML='[+] Social';
	} else {
		document.getElementById('socialA').innerHTML='[<span style=\'font-size:11px;font-family:Courier New, Courier New, monospace;\'>&#0150;</span>] Social';
	}
	 
	Effect.toggle('social', 'BLIND');
	//Effect.toggle('sociallikebar', 'BLIND');
	
	return false;
}
*/

function showInfo() {
	if (document.getElementById('comments').style.display != 'none') {
		Effect.toggle('comments', 'BLIND');
	}
	/*if (document.getElementById('social').style.display != 'none') {
		Effect.toggle('social', 'BLIND');
		document.getElementById('socialA').innerHTML='[+] Social';
	}*/

	Effect.toggle('info', 'BLIND');
	return false;
}

function showComments() {

	createCookie('info', 'false', 365);
	createCookie('comments', 'false', 365);
	//createCookie('social', 'false', 365);
	if (document.getElementById('comments') != null
			&& document.getElementById('comments').style != null
			&& document.getElementById('comments').style.display != null
			&& document.getElementById('comments').style.display != 'none') {
		if (document.getElementById('comments').style.display != 'none') {
			createCookie('comments', 'false', 365);
		} else {
			createCookie('comments', 'true', 365);
		}
	} else {
		createCookie('comments', 'true', 365);
	}

	if (document.getElementById('info').style.display != 'none') {
		Effect.toggle('info', 'BLIND');
	}
	/*if (document.getElementById('social').style.display != 'none') {
		Effect.toggle('social', 'BLIND');
		document.getElementById('socialA').innerHTML='[+] Social';
	}*/
	Effect.toggle('comments', 'BLIND');
	return false;

}

function showLogin() {
	createCookie('info', 'false', 365);
	createCookie('comments', 'false', 365);
	//createCookie('social', 'false', 365);
	if (document.getElementById('comments') != null
			&& document.getElementById('comments').style != null
			&& document.getElementById('comments').style.display != null
			&& document.getElementById('comments').style.display != 'none') {
		if (document.getElementById('comments').style.display != 'none') {
			createCookie('comments', 'true', 365);
		} else {
			createCookie('comments', 'false', 365);
		}
	}
	if (document.getElementById('info') != null
			&& document.getElementById('info').style != null
			&& document.getElementById('info').style.display != null
			&& document.getElementById('info').style.display != 'none') {
		if (document.getElementById('info').style.display != 'none') {
			createCookie('info', 'true', 365);
		} else {
			createCookie('info', 'false', 365);
		}
	}
	/*if (document.getElementById('social') != null
			&& document.getElementById('social').style != null
			&& document.getElementById('social').style.display != null
			&& document.getElementById('social').style.display != 'none') {
		if (document.getElementById('social').style.display != 'none') {
			createCookie('social', 'true', 365);
		} else {
			createCookie('social', 'false', 365);
		}
	}*/

	Effect.toggle('loginpanel', 'BLIND');
	return false;
}

function logoutComments() {
	document.logoutForm.submit();
}

function validarRegistro() {
	var nombre = document.registerForm.registerfirstname.value;
	var apellidos = document.registerForm.registerlastname.value;
	var email = document.registerForm.registeremail.value;
	var pass = document.registerForm.registerpassword.value;
	var repass = document.registerForm.registerrePassword.value;

	if (nombre == '') {
		document.getElementById('reg_error').innerHTML = 'Debes completar el nombre';
		return;
	}
	if (apellidos == '') {
		document.getElementById('reg_error').innerHTML = 'Debes completar los apellidos';
		return;
	}
	if (email == '') {
		document.getElementById('reg_error').innerHTML = 'Debes completar el email';
		return;
	}
	if (pass == '') {
		document.getElementById('reg_error').innerHTML = 'Debes completar la contrase–a';
		return;
	}
	if (repass == '') {
		document.getElementById('reg_error').innerHTML = 'Debes reescribir la contrase–a';
		return;
	}

	var atpos = email.indexOf('@');
	var dotpos = email.lastIndexOf('.');
	if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
		document.getElementById('reg_error').innerHTML = 'Debes introducir un mail correcto';
		return false;
	}

	if (pass != repass) {
		document.getElementById('reg_error').innerHTML = 'Las contrase&ntilde;as no coinciden';
		return false;
	}
	document.registerForm.submit();
}

function validarLogin() {
	var login = document.loginForm.loginemail.value;
	var pass = document.loginForm.loginpassword.value;

	if (login == '') {
		document.getElementById('login_error').innerHTML = 'Debes completar el email';
		return;
	}
	if (pass == '') {
		document.getElementById('login_error').innerHTML = 'Debes completar la contrase&ntilde;a';
		return;
	}

	document.loginForm.submit();
}

/*********************************************************************************************/
function toggle(div_id) {
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	else {el.style.display = 'none';}
}

function blanket_size(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		blanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			blanket_height = document.body.parentNode.clientHeight;
		} else {
			blanket_height = document.body.parentNode.scrollHeight;
		}
	}
	
	var blanket = document.getElementById('blanket');
	blanket.style.height = blanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	blanket_height = screen.availHeight-200;
	popUpDiv_height=(blanket_height/2)-(popUpDiv.style.height.substring(0,3)/2);//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
}

function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width = screen.availWidth;
	window_width=(window_width/2)-(popUpDiv.style.width.substring(0,3)/2);//150 is half popup's width
	popUpDiv.style.left = window_width + 'px';
}

function popup(windowname) {
	blanket_size(windowname);
	window_pos(windowname);
	toggle('blanket');
	toggle(windowname);		
}

function validateSendMail() {
	var nameFrom = document.shareMail.namefrom.value;
	var emailFrom = document.shareMail.mailfrom.value;
	var nameTo = document.shareMail.nameto.value;
	var emailTo = document.shareMail.mailto.value;

	if (nameFrom == '') {
		document.getElementById('mail_error').innerHTML = 'Debes completar tu nombre';
		return;
	}
	if (emailFrom == '') {
		document.getElementById('mail_error').innerHTML = 'Debes completar tu email';
		return;
	}
	if (nameTo == '') {
		document.getElementById('mail_error').innerHTML = 'Debes completar el nombre de tu amigo';
		return;
	}
	if (emailTo == '') {
		document.getElementById('mail_error').innerHTML = 'Debes completar el email de tu amigo';
		return;
	}

	var atpos = emailFrom.indexOf('@');
	var dotpos = emailFrom.lastIndexOf('.');
	if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailFrom.length) {
		document.getElementById('mail_error').innerHTML = 'Debes introducir un mail origen correcto';
		return false;
	}

	var atpos2 = emailTo.indexOf('@');
	var dotpos2 = emailTo.lastIndexOf('.');
	if (atpos2 < 1 || dotpos2 < atpos2 + 2 || dotpos2 + 2 >= emailTo.length) {
		document.getElementById('mail_error').innerHTML = 'Debes introducir un mail destino correcto';
		return false;
	}
	document.shareMail.submit();
}

function validateSuscribeMail() {
	var emailFrom = document.suscribeMailForm.mailfrom.value;

	if (emailFrom == '') {
		document.getElementById('mail_error').innerHTML = 'Debes completar tu email';
		return;
	}
	var atpos = emailFrom.indexOf('@');
	var dotpos = emailFrom.lastIndexOf('.');
	if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailFrom.length) {
		document.getElementById('mail_error').innerHTML = 'Debes introducir un mail correcto';
		return false;
	}
	document.suscribeMailForm.submit();
}

