/************************************************************
*															*
* Copyright 2009 - gno.cat									*
* Disseny: Laura Morató - moratolaura@telefonica.net		*
* Programació: Osona Disseny Web - info@osonadissenyweb.com	*
*															*
************************************************************/

imatge = "";

//Funcions que controlen el formulari d'accés
function resetCamp(camp){
	switch(camp){
		case 'usuari':
			if ($('usuari').value == ''){
				$('usuari').value = 'usuari';
			}
		break;
		case 'contrasenya':
			if ($('contrasenya').value == ''){
				$('contrasenya').value = 'contrasenya';
			}
		break;
		case 'cercador':
			if ($('consulta').value == ''){
				$('consulta').value = 'cercador';
			}
		break;
	}
}

function emptyCamp(camp){
	switch(camp){
		case 'usuari':
			$('usuari').value = '';
		break;
		case 'contrasenya':
				$('contrasenya').value = '';
		break;
		case 'cercador':
				$('consulta').value = '';
		break;
	}
}

function login(oEvent){
	var iAscii;
	if (oEvent.keyCode){
		iAscii = oEvent.keyCode;
	}
	else if (oEvent.which){
		iAscci = oEvent.which;
	}
	else{
		return false;	
	}
	
	if (iAscii == 13){
		var url = 'http://www.gno.cat/administracio/core/functions/php/login.php';
		new Ajax.Request(url, {
			method: 'post',
			parameters: 'usuari=' + $('usuari').value + '&psw=' + $('contrasenya').value,
			onSuccess: function(transport) {
				if (transport.responseText == "incorrecte"){
					alert("Nom d'usuari i/o contrasenya incorrectes!");
				}
				else if (transport.responseText == "correcte"){
					window.location = 'http://www.gno.cat/administracio/za_admin.php';	
				}
			}
		});
	}
}

function loginAdmin(){
	var url = 'http://www.gno.cat/administracio/core/functions/php/login.php';
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'usuari=' + $('usuari').value + '&psw=' + $('contrasenya').value,
		onSuccess: function(transport) {
			if (transport.responseText == "incorrecte"){
				alert("Nom d'usuari i/o contrasenya incorrectes!");
			}
			else if (transport.responseText == "correcte"){
				window.location = 'http://www.gno.cat/administracio/za_admin.php';	
			}
		}
	});
}


/* Funció per ampliar imatge */
function ampliarImatge(id){
	imatge = id;
	var url = 'http://www.gno.cat/inc/ampliar-imatge.php';
	new Ajax.Updater('ampliacio-imatge', url, {
		method: 'post',
		parameters: 'id=' + id,
		onSuccess: function(transport){
			var t = setTimeout("posicionarElement()", 500);	
		}
	});
}

function posicionarElement(){
	dims = $('ampliacio-imatge').getDimensions();
	dims2 = $('ampliada').getDimensions();
	//Posició div
	pos = getAbsoluteElementPosition(imatge);
	posX = pos.left - ((dims.width/2) - 41);
	posY = pos.top - ((dims.height/2) - 41);
	$('ampliacio-imatge').style.top = posY + 'px';
	$('ampliacio-imatge').style.left = posX + 'px';
	//Veure div
	$('ampliacio-imatge').style.display = 'block';
	$('ampliacio-imatge').style.visibility = 'visible';
}

function tancarImatge(){
	$('ampliacio-imatge').style.display = 'none';
	$('ampliacio-imatge').style.visibility = 'hidden';
}


function getAbsoluteElementPosition(element) {
  element = $(element);
    
  var y = 0;
  var x = 0;
  while (element.offsetParent) {
    x += element.offsetLeft;
    y += element.offsetTop;
    element = element.offsetParent;
  }
  return {top:y,left:x};
}


function redimensionarFletxes(){
	dims = $('file').getDimensions();
	dims2 = $('fletxes').getDimensions();
	$('fletxes').style.width = dims.width + 'px';
	$('fletxes').style.display = 'block';
	$('fletxes').style.visibility = 'visible';
}


function ampliarInfo(id, tipus){
	var url = 'http://www.gno.cat/inc/nav-galeria.php';
	
	new Ajax.Updater('imatge-info', url, {
		method: 'post',
		parameters: 'id=' + id + '&tipus=' + tipus + '&info=text',
		evalJS: true,
		onSuccess: function(transport){
	
		}
	});
	
	new Ajax.Updater('imatge-gran', url, {
		method: 'post',
		parameters: 'id=' + id + '&tipus=' + tipus + '&info=imatge',
		evalJS: true,
		onSuccess: function(transport){

		}
	});
}