// JavaScript Document
var http_request = false;
function createRequest() {
        http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			return http_request;
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			return http_reqest;
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				return http_request;
			} catch (e) { return false; }
		}
	}

	return http_request;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function checkLogin() {
	var myForm = document.forms.loginForm;

	if (!myForm.user.value || !myForm.pass.value ||
		myForm.user.value == 'Usuario' || myForm.pass.value == 'Usuario')
		return false;

	return true;
}

function login() {
	var myForm = document.forms.loginForm;

	http_request = createRequest();
	if (!http_request) {
		alert( 'Error: No se pudo crear objeto HttpXmlRequest.' )
		return;
	}

	http_request.open( 'POST', 'xml/xml_login.php', true );
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.onreadystatechange = performLogin;
	http_request.send( 'user=' + myForm.user.value + '&pass=' + myForm.pass.value);

}

function performLogin() {
	if (http_request.readyState == 4) {

		if (http_request.status == 200) {
			var root = http_request.responseXML.getElementsByTagName('root').item(0);
			if (root.getAttribute('success') == '1') {
				createCookie( 'SSId', root.getAttribute('sessionid') );
				window.location = 'usernews.php';
			}
			else {
				alert( 'Nombre de usuario o contraseńa no válidos.' );
				document.forms.loginForm.pass.value = '';
			}
		}
		else {
			alert( 'Se ha producido un error enviando la información.\nInténtelo más tarde (' + http_request.status + ')' );
		}
	}
}

function logoff() {
	http_request = createRequest();
	if (!http_request) {
		alert( 'Error: No se pudo crear objeto HttpXmlRequest.' )
		return;
	}

	http_request.open( 'POST', 'xml/xml_logoff.php', false );
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send( 'sessionId=' + readCookie('SSId') );

	if (http_request.status == 200) {
		eraseCookie('SSId');
		window.location = 'index.php';
	}
	else {
		alert( 'Se ha producido un error enviando la información.\nInténtelo más tarde (' + http_request.status + ')' );
	}
}

function setEmpty(obj) {
	if (obj.value == 'Usuario')
		obj.value = '';
}

function setReset(obj) {
	if (obj.value == '')
		obj.value = 'Usuario';
}

function checkSearch() {
	var myForm = document.forms.searchForm;

	if (!myForm.search.value)
		return false;

	return true;
}

function setHeights() {
	var left = document.getElementById('left');
	var center = document.getElementById('center');
	xHeight( center, Math.max( xHeight(center), xHeight(left) ));
	
	positionMenu();
	window.onresize = positionMenu;
}

var menuPosList = Array();
function positionMenu() {
	_calculateBrowserSize();
	var menuLeft;

	if (browserWindowWidth > 689) {
		menuLeft = 23 + ((browserWindowWidth / 2) - (689 / 2));
	}
	else {
		menuLeft = 23;
	}
	
	if (navigator.userAgent.indexOf('MSIE') != -1) {
		menuLeft += 7;
	}
		
	var left = document.getElementById('left');
	var menuA = left.getElementsByTagName('div');
	
	if (!menuPosList.length) {
		setArray = true;
	}
	else {
		setArray = false;
	}
	
	for (var n=0, a=0; n < menuA.length; n++) {
		if (menuA.item(n).className.substring(0, 4) == 'm0l0' || 
		   menuA.item(n).className.substring(0, 4) == 'm0l1' || 
		   menuA.item(n).className.substring(0, 4) == 'm0l2') {
			if (setArray) 
				menuPosList[a] = Number(menuA.item(n).style.left.substring(0, menuA.item(n).style.left.length-2));
			
			menuA.item(n).style.left = (menuPosList[a] + menuLeft) + 'px';
			
			a++;
		}
	}
	//MENU_POS[0].block_left = menuLeft;
}

var browserWindowHeight = 0;
var browserWindowWidth = 0;

function _calculateBrowserSize() {
	// calculate window width - height
	if (navigator.userAgent.indexOf('MSIE') != -1) {
		if (document.documentElement.clientWidth == 0)
			browserWindowWidth = document.body.clientWidth;
		else	
			browserWindowWidth = document.documentElement.clientWidth;
			
		if (document.documentElement.clientHeight == 0)
  			browserWindowHeight = document.body.clientHeight;
		else
			browserWindowHeight = document.documentElement.clientHeight;
	}
	else {
		browserWindowWidth = window.innerWidth;
  		browserWindowHeight = window.innerHeight;
	}
	
	// calculate document width height
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else {// Explorer Mac;
		 //would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	
	// set the max of both
	browserWindowWidth = Math.max(browserWindowWidth, x);
	browserWindowHeight = Math.max(browserWindowHeight, y);
}

var newsObj = false;
function switchNews() {
	if (!newsObj) {
		document.getElementById('homeNews').style.display = 'block';
		newsObj = true;
	}
	else {
		document.getElementById('homeNews').style.display = 'none';
		newsObj = false;
	}
}

var imgSmallSize = [170, 170];
var curPos = [0,0]
function setImagePosition() {
	
	var imgBig = document.getElementById('prodImageBig');
	var imgSmall = document.getElementById('prodImage')
	
	if (imgSmall.offsetX) {
		x = imgSmall.offsetX;
		y = imgSmall.offsetY;
	}
	else {
		if (imgSmall.getBoundingClientRect) {
			var rect = imgSmall.getBoundingClientRect();
			x = rect.left;
			y = rect.top;
			//w = rect.right - rect.left;
			//h = rect.bottom - rect.top;
		}
		else {
			if (document.getBoxObjectFor) {
				var rect = document.getBoxObjectFor(imgSmall);
				x = rect.x;
				y = rect.y;
				//w = rect.width;
				//h = rect.height;
			}
		}
	}
	
	// adjust x and y to scroll 
	
	x = x + document.documentElement.scrollLeft;
	y = y + document.documentElement.scrollTop;;

	curPos = [x, y];
	imgBig.style.top = (y - 1) + 'px';
	imgBig.style.left = (x -1) + 'px';
	imgBig.style.width = imgSmallSize[0] + 'px';
	imgBig.style.height = imgSmallSize[1] + 'px';
}

var animation;
function showImg() {
	animation = setInterval('moveImg()', 20);
	document.getElementById('prodImageBig').style.display = 'block';
}

var offset = 5;
function moveImg() {
	curPos[0] -= offset;
	imgSmallSize[0] += offset;
	imgSmallSize[1] += offset;
	
	if (imgSmallSize[0] >= imgBigSize[0] || imgSmallSize[1] >= imgBigSize[1]) {
		clearInterval(animation);
		imgSmallSize[0] = imgBigSize[0];
		imgSmallSize[1] - imgBigSize[1];
	}
	
	var imgBig = document.getElementById('prodImageBig');
	imgBig.style.top = curPos[1] + 'px';
	imgBig.style.left = curPos[0] + 'px';
	imgBig.style.width = imgSmallSize[0] + 'px';
	imgBig.style.height = imgSmallSize[1] + 'px';
}

function hideImg() {
	clearInterval(animation);
	document.getElementById('prodImageBig').style.display = 'none';
	imgSmallSize = [170, 170];
	setImagePosition();
}