/*
Copyright 2008, 2009 Copyleft S. de R.L. de C.V.

This file is part of GuiasDF.

GuiasDF is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GuiasDF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GuiasDF.  If not, see <http://www.gnu.org/licenses/>
*/
$(document).ready(function() {

	//rating
	var clicked = 0;
	$(".rating").each(function() {
		rating = $(this).attr("title");
		article = $(this).attr("id");
		if($(this).attr("lang")=='disabled')
			clicked = 1;
		for(i=1; i<=5; i++) {
			if(rating >= 1) {
				$(this).append('<img src="images/frontend/star-on.png" width="24" height="22" alt="' + article + '" class="star" id="star_' + article + "_" + i + '" title="' + i + '" />');
				rating--;  
			}
			else if(rating == 0.5) {
				$(this).append('<img src="images/frontend/star-left-on.png" width="24" height="22" alt="' + article + '" class="star" id="star_' + article + "_" + i + '" title="' + i + '" />');
				rating=0;
			}
			else {
				$(this).append('<img src="images/frontend/star.png" width="24" height="22" alt="' + article + '" class="star" id="star_' + article + "_" + i + '" title="' + i + '" />');
			}
		}		
	});
	
	$(".star").mouseover(function() {
		if(clicked==0) {	
			star = $(this).attr("title");
			article = $(this).attr("alt");
			//console.log($(this).attr("title"));
			for(i=1; i<=star; i++) {
				$("#star_" + article + "_" + i).attr("src","images/frontend/star-over.png");
			}
		}
	});
	
	$(".star").mouseout(function() {
		if(clicked==0) {
			star = $(this).attr("title");
			article = $(this).attr("alt");	
			rating = $("#" + article).attr("title");
			//console.log(rating);
			for(i=1; i<=5; i++) {
				if(rating >= 1) {
					$("#star_" + article + "_" + i).attr("src","images/frontend/star-on.png");
					rating--;  
				}
				else if(rating == 0.5) {
					$("#star_" + article + "_" + i).attr("src","images/frontend/star-left-on.png");
					rating=0;
				}
				else {
					$("#star_" + article + "_" + i).attr("src","images/frontend/star.png");  						
				}
			}
		}
	});
	
	$(".star").click(function() {
		if(clicked==0) {
			rating = $(this).attr("title");
			for(i=1; i<=5; i++) {
				if(rating >= 1) {
					$("#star_" + article + "_" + i).attr("src","images/frontend/star-on.png");
					$(this).removeClass("star");
					rating--;  
				}
				else {
					$("#star_" + article + "_" + i).attr("src","images/frontend/star.png");
					$(this).removeClass("star");					
				}
			}
			clicked = 1;
			$.ajax({
				type: "GET",
				url: "/rate-0/",
				data: "id_article=" + article + "&rating=" + $(this).attr("title") + "&ajax=1",
				success: function(msg){
					if(msg==0) {
						alert("El artículo ya fue calificado por este usuario");
					}					
				}
			});				
		}
	});	
	
	//confirm delete
	$(".fn-delete a img").click(function() {
		var el = $(this);
		var uri = el.parent().attr("href").split("?");
		if(window.confirm("¿Está seguro que desea eliminar el registro seleccionado?")) {
			$.ajax({
				type: "GET",
				url: "/" + uri[0],
				data: uri[1] + "&ajax=1",
				success: function(msg){
					if(msg==1) {
						el.parent().parent().parent().fadeOut("slow");
					}
				}
			});
		}
		return false;
	});
	
	// validate form
	$(".validatefrm").submit(function() {
		var frm = $(this);					
		var errors = 0;
		frm.find("input").each(function(i) {
			var el = $(this);			
			if($.trim(el.val())!="" && el.attr("alt")=="email" ) {
				if(!check_email($.trim(el.val()))){
					alert("Escriba una dirección de e-mial válida");
					el.focus();
					errors = 1;
					return false;
				}
			}
			if(el.attr("req")=="true") {
				if(el.attr("type")=="text" || el.attr("type")=="password") {
					if($.trim(el.val())=="") {
						alert(el.attr("title"));
						el.focus();
						errors = 1;
						return false;
					}
				}
			}
		});
		if(errors==1) {
			return false;
		}
		frm.find("select").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if($.trim(el.val())=="0") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});
		if(errors==1) {
			return false;
		}
	});
	
	// menu rollover 
	$(".menu").mouseover(function() {
		var image = $(this).attr("src").replace(".png", "");
		$(this).attr("src",image+"-on.png");
		var cssObj = {
			'background-color' : $(this).attr("title")
		}		
		$(".submenu").css(cssObj); 
	});
	$(".menu").mouseout(function() {
		var image = $(this).attr("src").replace("-on.png", "");
		$(this).attr("src",image+".png");
	});
	
	$(".submenu_item").mouseout(function() {
		var cssObj = {
			'background-color' : $(this).attr("title")
		}		
		$("#submenu_item_" + $(this).attr("alt")).css(cssObj);
		//$(this).find("a").css({ color: "white" });
	});
	
	$(".submenu_item").mouseover(function() {
		var cssObj = {
			'background-color' : '#bbbbbb'
		}		
		$("#submenu_item_" + $(this).attr("alt")).css(cssObj);
	});
	
	// replace page titles with images
	var txtpage = $("#pagetitle").text().split(" ");
	$("#pagetitle").empty();
	for(i=0; i<txtpage.length; i++) {
		$("#pagetitle").append('<img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=16&color=000000&text='+txtpage[i]+'%20" alt="" />');
	}
	var txtpage = $(".pagetitle").text().split(" ");
	$(".pagetitle").empty();
	for(i=0; i<txtpage.length; i++) {
		$(".pagetitle").append('<img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=16&color=000000&text='+txtpage[i]+'%20" alt="" />');
	}	
	
	var txtsection = $("#sectiontitle").text().split(" ");
	$("#sectiontitle").empty();
	for(i=0; i<txtsection.length; i++) {
		$("#sectiontitle").append('<img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=20&color=000000&text='+txtsection[i]+'%20" alt="" />');
	}

	$(".homearticle").each(function(i) {	
		txthome = $(this).text().split(" ");
		urlhome = $(this).find("a").attr("href");
		$(this).empty();
		for(i=0; i<txthome.length; i++) {
			$(this).append('<a href="'+urlhome+'"><img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=12&color=000000&text='+txthome[i].toUpperCase()+'%20" alt="" /></a>');
		}
	});
	
	$(".whatsnew").each(function(i) {	
		txtwhatsnew = $(this).text().split(" ");
		urlwhatsnew = $(this).find("a").attr("href");
		$(this).empty();
		for(i=0; i<txtwhatsnew.length; i++) {
			$(this).append('<a href="'+urlwhatsnew+'"><img src="http://txt.editorialmapas.com/includes/imageheader.php?font=GriffithGothic-CondRegular&size=12&color=000000&text='+txtwhatsnew[i].toUpperCase()+'%20" alt="" /></a>');
		}
	});
	
	$("#numberarticle").each(function(i) {	
		txthome = $(this).text().split(" ");
		urlhome = $(this).find("a").attr("href");
		$(this).empty();
		for(i=0; i<txthome.length; i++) {
			$(this).append('<a href="'+urlhome+'"><img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=12&color=000000&text='+txthome[i].toUpperCase()+'%20" alt="" /></a>');
		}
	});
	
	var txtcurrnum = $("#currentnumber").text().split(" ");
	$("#currentnumber").empty();
	for(i=0; i<txtcurrnum.length; i++) {
		$("#currentnumber").append('<img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=14&color=000000&text='+txtcurrnum[i]+'%20" alt="" />');
	}
	
	var txtpastnum = $("#pastnumber").text().split(" ");
	$("#pastnumber").empty();
	for(i=0; i<txtpastnum.length; i++) {
		$("#pastnumber").append('<img src="http://txt.editorialmapas.com/includes/imageheader.php?font=Helveticaneue95&size=14&color=000000&text='+txtpastnum[i]+'%20" alt="" />');
	}
	
	
/* validates an email address */
function check_email(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false
	}
	if (len<2) {
		alert(errStr)
		return false
	}
	return true;
}	
	
});