function constructNavMenu(){
	$(".navMenu ul ").css({display: "none"});
	$(".navMenu li a.viewmore").hover(function(){
		parent = $(this).parent().parent();
		parent.find('ul:first:hidden').css({visibility: "visible",display: "none"}).slideDown(400);
	},function(){
		parent = $(this).parent().parent();
		parent.find('ul:first').hover(function(){
		}, function(){$(this).slideUp(400);});		
	});
}

function submitForm(form_id){
	if(form_id.search("#") != -1){ form_id = form_id.replace("#",""); }
	document.getElementById(form_id).click();
}

function checkMail(email){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
}

function getAjaxObject(){
	var object = false;
	try { object = new ActiveXObject("Msxml2.XMLHTTP");  }
	catch (e) {
	try { object = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (E) {
		object = false; }
	}
	if (! object && typeof XMLHttpRequest!='undefined'){
		object = new XMLHttpRequest();
	}
	return object;
}

function showDiv(divBefore, divAfter){
	if(document.getElementById(divBefore).style.display == 'none'){
		document.getElementById(divBefore).style.display = 'block';
		document.getElementById(divAfter).style.display = 'none';
	}else{
		document.getElementById(divBefore).style.display = 'none';
		document.getElementById(divAfter).style.display = 'block';
	}
}

function doingRequest(id, type){
	var oAjax = getAjaxObject();
	params = "tool=doingRequest&type="+type+"&id="+id;
	oAjax.onreadystatechange = function(){ 
		if(oAjax.readyState == 4){
			response = oAjax.responseText;
			document.location.href = webroot + 'registrate';
		}
	}
	oAjax.open("POST", webroot + "ajax.php", true);
	oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	oAjax.setRequestHeader("Content-length", params.length);
	oAjax.send(params);
}

function alertFB(message){
	$.fancybox('<div class="alertFB">' + message + '</div>');
}

function setLogin(form){
	var oAjax = getAjaxObject();	
	params = "tool=setLogin&email=" + form.email.value + "&password=" + form.password.value + "&rememberme=" + (form.rememberme.checked ? 1 : 0);
	
	oAjax.onreadystatechange = function(){ 
		if(oAjax.readyState == 1){
			document.getElementById('divLoginLoading').style.display = 'block';
			document.getElementById('divLogin').style.display = 'none';
		}
		if(oAjax.readyState == 4){
			document.getElementById('divLoginLoading').style.display = 'none';
			document.getElementById('divLogin').style.display = 'block';
			response = oAjax.responseText;
			if(response.toLowerCase().search("error") != -1){
				alertFB(response);
				form.email.focus();
			}else if(response.toLowerCase().search("exit") != -1){
				document.location.reload();
			}else if(response.toLowerCase().search("http://") != -1 && response.toLowerCase().search("<html") == -1){
				document.location.href = response;
			}else{
				if(response == ""){
					form.submit();
					response = "El servidor no ha emitido una respuesta, vuelva a intentarlo en un momento";
				}else{
					alert(response);
				}
			}
		}
	}
	oAjax.open("POST", webroot + "ajax.php", true);
	oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	oAjax.setRequestHeader("Content-length", params.length);
	oAjax.send(params);
}

function setRecovery(form){
	var oAjax = getAjaxObject();	
	params = "tool=setRecovery&email=" + form.email.value;	
	
	oAjax.onreadystatechange = function(){ 
		if(oAjax.readyState == 1){
			document.getElementById('loading-module-form').style.display = 'block';
			document.getElementById('recovery-module-form').style.display = 'none';
		}
		if(oAjax.readyState == 4){
			document.getElementById('loading-module-form').style.display = 'none';
			document.getElementById('recovery-module-form').style.display = 'block';
			response = oAjax.responseText;
			if(response.toLowerCase().search("error") != -1){
				alert(response);
				form.email.focus();
			}else{
				form.email.value = form.email.title;
				form.email.className = "defaultTextActive";
				
				alert(response);
				
				document.getElementById('recovery-module-form').style.display = 'none';
				document.getElementById('login-module-form').style.display = 'block';
			}
		}
	}	
	oAjax.open("POST", webroot + "ajax.php", true);
	oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	oAjax.setRequestHeader("Content-length", params.length);
	oAjax.send(params);
}

$(document).ready(function(){	
	if($('#password-login').val() == '') {
		$('#password-login-clear').show();
		$('#password-login').hide();
	}else{
		$('#password-login-clear').hide();
		$('#password-login').show();
	}
	$('#password-login-clear').focus(function() {
		$('#password-login-clear').hide();
		$('#password-login').show();
		$('#password-login').focus();
	});
	
	$('#password-login').blur(function() {
		if($('#password-login').val() == '') {
			$('#password-login-clear').show();
			$('#password-login').hide();
		}
	});
});

function loadIframe(url){
	//document.getElementById('iframe-element').src = url;
	$("iframe").src(url, function(duration) {
	  alert("That took " + duration + " millis.");
	}, {
	  timeout: function() { alert("oops! timed out."); },
	  timeoutDuration: 30000
	});
}
function getCancelView(){
	document.getElementById('staticView').style.display = "block";
	document.getElementById('loadingView').style.display = "none";
	document.getElementById('ajaxView').style.display = "none";		
}

function cleanParam(value, default_value, sent_null){
	sent_null = sent_null || false;
	if (sent_null){
		value = (typeof value == 'undefined') ? default_value : value;
	}else{
		value = value || default_value;
	}
	return value;
}

function callClockRemaining(timeNow, timeFuture, prediv, callBackEnd){
	try{
		hourDiv = document.getElementById(prediv + 'hour');
		minuteDiv = document.getElementById(prediv + 'minute');
		secondDiv = document.getElementById(prediv + 'second');
		
		if(timeFuture > timeNow){
			remaining = timeFuture - timeNow;
			remainingHour = parseInt(remaining / (60 * 60));
			remaining -= remainingHour * (60*60);
			
			remainingMinute = parseInt(remaining / (60));
			remaining -= remainingMinute * (60);
			
			remainingSecond = parseInt(remaining);
			remaining -= remainingSecond;
			
			hourDiv.innerHTML = (remainingHour < 10 ? "0" : "") + remainingHour;
			minuteDiv.innerHTML = (remainingMinute < 10 ? "0" : "") + remainingMinute;
			secondDiv.innerHTML = (remainingSecond < 10 ? "0" : "") + remainingSecond;
			
			setTimeout("callClockRemaining('" + (parseInt(timeNow) + 1) + "','" + timeFuture + "','" + prediv + "','" + callBackEnd + "');",1000);
		}else{
			if(callBackEnd == ""){
				hourDiv.innerHTML = "00";
				minuteDiv.innerHTML = "00";
				secondDiv.innerHTML = "00";
			}else{
				setTimeout(callBackEnd,0);
			}
		}
	}catch(e){
		alert(e);
	}
}

function openPopupGalery(id_article, id_pic, show_galery){
	if(show_galery){ 
	   popupGalery = window.open(webroot + "galeria/" + id_article + "/" + id_pic, "popupGalery","status=no,toolbar=no,width=1250,height=680,resizable=no,location=no,menubar=no,directories=no,scrollbars=no"); 
	   popupGalery.moveTo(0, 0); 
	   popupGalery.focus();
	}else{ 
	   document.location.href = webroot + "registrate/noticia/" + id_article + "/" + id_pic;
	}
}
