/**
 * Made for : Anita Caracolito
 * Made by  : Andre Cravo [cravo.andre@gmail.com]
 * On       : Saturday, December 27, 2008
 */
 
(function($) {
	$.fn.stripHtml = function() {
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		this.each(function() {
			$(this).html($(this).html().replace(regexp,""));
		});
		return $(this);
	}  
})(jQuery);

$(document).ready(function(){

	// setup browser specific style props -------------------------------------
	
	$(".content-area").css("-webkit-border-top-left-radius","10px");
	$(".content-area").css("-webkit-border-top-right-radius","10px");
	$(".content-area").css("-moz-border-radius-topleft","10px");
	$(".content-area").css("-moz-border-radius-topright","10px");
	
	$("#form h3 span").css("-webkit-border-top-left-radius","5px");
	$("#form h3 span").css("-webkit-border-top-right-radius","5px");
	$("#form h3 span").css("-moz-border-radius-topleft","5px");
	$("#form h3 span").css("-moz-border-radius-topright","5px");
	
	$("fieldset.group").css("-webkit-border-radius","5px");
	$("fieldset.group").css("-moz-border-radius","5px");
	
	$("#form fieldset.group p.form_row").css("-webkit-border-radius","5px");
	$("#form fieldset.group p.form_row").css("-moz-border-radius","5px");
	
	// setup menus tables ----------------------------------------------------
	$(".menus").hide();
	
	$("#em1-1").show();
	$("#menu-small-links li:first-child a").addClass("selected");
	$("#menu-small-links li a").each(function(i){
		$(this).click(function() {
			var elemId = "#em1-" + (i+1);
			var table = $(elemId);
			if (table) {
				// this should be replaced by hidding the previous item only
				$("table.small").hide();
				table.show();
				$("#menu-small-links li a:first-child").removeClass("selected");
				$(this).addClass("selected");
			}
			return false;					
		});
	});
	
	//$("#em2-1").css("display", "block");
	$("#em2-1").show();
	$("#menu-big-links li:first-child a").addClass("selected");
	$("#menu-big-links li a").each(function(i){
		$(this).click(function() {
			var elemId = "#em2-" + (i+1);
			var table = $(elemId);
			if (table) {
				// this should be replaced by hidding the previous item only
				$("table.big").hide();
				table.show();
				$("#menu-big-links li a:first-child").removeClass("selected");
				$(this).addClass("selected");
			}
			return false;					
		});
	});	

	// load data from external services ----------------------------------------------------

	if ($("#image-roll").length > 0) {
		// setup picasa fun
		$.getJSON("http://picasaweb.google.com/data/feed/base/user/anitacaracolito/album/SiteAlbum?kind=photo&max-results=6&thumbsize=72c&alt=json&callback=?", 
			function(data){
				$("#image-roll > div").empty();
				$.each(data.feed.entry, function(index, item) {
					
					var aElem = $("<a href=\"" + item.link[1].href + "\"></a>");
					aElem.bind("click", function() {
						window.open(item.link[1].href);
						return false;
					});
					var img = $("<img/>").attr("src", item.media$group.media$thumbnail[0].url).attr("alt", item.media$group.media$description.$t);
					aElem.append(img);
					$("#image-roll > div").append(aElem);
					
				});
			}
		);
	}


	if ($("#blog-roll").length > 0) {
		// setup blog list
		$.getJSON("http://www.blogger.com/feeds/1776338811734402390/posts/default?category=opini%C3%B5es&max-results=4&orderby=updated&alt=json-in-script&callback=?", 
			function(data) {
				if (data.feed.entry.length > 0) {
					$("#blog-roll").append("<ul />");
					$.each(data.feed.entry, function(index, item) {
						var description = item.content.$t.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, "").slice(0, 34) + "...";
						var aElem = $("<a href=\"" + item.link[4].href + "\">" + item.title.$t + "<span>"+description+"</span></a>");
						aElem.bind("click", function() {
							window.open(item.link[4].href);
							return false;
						});
						$("<li/>").append(aElem).appendTo("#blog-roll > ul");
					});
				}
			}
		);
	}
	
	if ($("#activity-roll").length > 0) {	
		// setup activities list
		$.getJSON("http://www.blogger.com/feeds/1776338811734402390/posts/default?category=actividades&max-results=4&orderby=updated&alt=json-in-script&callback=?", 
			function(data) {
				if (data.feed.entry.length > 0) {
					$("#activity-roll").append("<ul />");
					$.each(data.feed.entry, function(index, item) {
						var description = item.content.$t.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, "").slice(0, 34) + "...";
						var aElem = $("<a href=\"" + item.link[4].href + "\">" + item.title.$t + "<span>"+description+"</span></a>");
						aElem.bind("click", function() {
							window.open(item.link[4].href);
							return false;
						});
						$("<li/>").append(aElem).appendTo("#activity-roll > ul");
					});
				}
			}
		);
	}

	// add extra features ----------------------------------------------------

	// setup the link we wish to open in new windows
	var newPageLinks = $("a[rel=new]");
	$.each(newPageLinks, function(index, item) {
		item.onclick = function() {
			window.open(item.href);
			return false;
		};
	});
	
	// setup the zoom links
	var newPageLinks = $("a[rel=zoom]");
	$.each(newPageLinks, function(index, item) {
		item.onclick = function() {
			// get image url from href
			// show popup with image
			return false;
		};
	});
	
	// add signup form validation function ----------------------------------------------------

	$("#signupf").bind("submit", function(event) {
		var form = this;
		
		var submitForm = new Boolean(true);
		var response = new String();
		
		var validField = new Boolean(false);
			
		response = response.concat("<ol>");		
		var classes = form.classe;
		var clazz = "";
		for ( var i = 0; i < classes.length; i++) {
			if (form.classe[i].checked == true) {
				validField = true;
				clazz = form.classe[i].value;
				break;
			}
		}
		
		if (validField == false) {
			response = response.concat("<li>Deve escolher a classe para a qual pretende inscrever o educando.</li>");
			submitForm = false;
		}
			
		var name = form.nome.value;
		if (!name.match(/\w+/gi)) {
			response = response.concat("<li>Deve preencher o nome do educando.</li>");
			submitForm = false;		
		}

		var birthday = form.dnascimento.value;
		if (!birthday.match(/\d{2}-\d{2}-\d{4}/gi)) {
			response = response.concat("<li>Deve preencher a data de nascimento do educando.</li>");
			submitForm = false;		
		}

		var address = form.morada.value;
		if (!address.match(/\w+/gi)) {
			response = response.concat("<li>Deve preencher a morada do educando.</li>");
			submitForm = false;		
		}

		var mother = form.maenome.value;
		if (!mother.match(/\w+/gi)) {
			response = response.concat("<li>Deve preencher a nome da mãe.</li>");
			submitForm = false;
		}

		var father = form.painome.value;
		if (!father.match(/\w+/gi)) {
			response = response.concat("<li>Deve preencher a nome do pai.</li>");
			submitForm = false;		
		}

		var phone = form.telefone.value;
		if (!phone.match(/^(?:(?:\+\d{3}\s?)|(?:\d{5}\s?))?\d{9}$/gi)) {
			response = response.concat("<li>Deve preencher a telefone de contacto.</li>");
			submitForm = false;		
		}

		var emailAddress = form.email.value;
		if (!emailAddress.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/gi)) {
			response = response.concat("<li>Deve preencher a e-mail de contacto.</li>");
			submitForm = false;		
		}
		
		response = response.concat("</ol>");

		$("#info-zone").empty();
		
		if (submitForm == false) {
			// append errors to error zone!
			$("#info-zone").append("<p><strong>Ups! Algo não se encontra preenchido da melhor forma. Tenha em atenção as seguintes observações:</strong></p>");
			$("#info-zone").append($(response));
			$("#info-zone").attr("class", "error-msg-type");
			
		} else {
			// make the call with ajax
			var formData = {classe:clazz, nome:name, dnascimento:birthday, morada:address, maenome:mother, painome:father, telefone:phone, email:emailAddress, extra:form.extra.value};
			
			$.post(form.action, formData, 
				function(data, textStatus) {
					$("#info-zone").append(data);
					if (textStatus == "success") {
						$("#info-zone").attr("class", "success-msg-type");
					} else {
						$("#info-zone").attr("class", "error-msg-type");
					}
				}, "html"
			);
		}
		return false;
	});
	
});