//Use google.load() to load the AJAX Feeds API
google.load('feeds', 1);

//launch jQuery code when the document is donwloaded and ready
jQuery(document).ready(initialize);	


/*
 * Initialize set all javascript code to have the web page working properly:
 * - Add 'plus' image to tasks headers 
 * - Bind handlers
 * - Fade In the page to show a little presentation effect.
 */
function initialize(){
		
	//create an image element with the 'plus' icon
	var img = new Image();
	$(img).attr('src', '_content/action_plus.gif');
	
	//add this image to the header of all tasks
	$(".job_task_header").prepend(img);
	
	//handler for tasks headers: show and hide the next description paragraph on click
	$(".job_task_header").click(function() {
		$(this).next().toggle('fast');
	}).next().hide();
	
	//handler for tabs. shows the correct content and sets the new active option
	$(".option").click(changeOption);
	
	//handler for the visit book form, send the form data and retrieve a xml response with a message and the book 
	$("#send_form").click(sendForm);
		
	//fade in the title and, when it finishes, call showContent to show the rest of the page
	$("#title").fadeIn('slow', showContent);
	
	//load comments in the visits Book
	getComments();
	
	//paint the background
	clouds();
}

/*
 * This function send the form content, receive the message response and book list and modify the visits book 
 * to show this information. This is AJAX!! ;)
 */
function sendForm(){
	
	$("#visits_response").text("...Enviando...");
	
	$.ajax({
			type: "POST",
			url: "../_layout/php/visitsBook.php",
			data: $("#visits_form").serialize(),
			complete: function(){
						
					  },
			success: function(xml){
						
						$("#last_comments").empty();
						
						$(xml).find("principal").each(function(){
							
							$(this).find("list").each(function(){
								$(this).find("entry").each(function(){
									var name = $(this).find("name").text();
									var comment = $(this).find("comment").text();
									
									$("#last_comments").append("<li><strong>"+ name +": </strong>"+ comment +"</li>");
								
								});
							});
							
							$(this).find("msg").each(function(){
								$("#visits_response").text("");
								$("#visits_response").append("<strong>"+ $(this).text() +"</strong>");								
							});
						});
						
					  }
	});
		
	return false;
	
}

/*
 * This function send the form content, receive the message response and book list and modify the visits book 
 * to show this information. This is AJAX!! ;)
 */
function getComments(){
	
  	$.get(
			'../_layout/php/visitsBook.php',
			function(data){
						
						$("#last_comments").empty();

						$(data).find("principal").each(function(){
							
							$(this).find("list").each(function(){
								$(this).find("entry").each(function(){
									var name = $(this).find("name").text();
									var comment = $(this).find("comment").text();
									
									$("#last_comments").append("<li><strong>"+ name +": </strong>"+ comment +"</li>");
								
								});
							});
							
							$(this).find("msg").each(function(){
								$("#visits_response").fadeOut('slow', function(){
									$("#visits_response").text("");
									$("#visits_response").append("<strong>"+ $(this).text() +"</strong>");		
									$("#visits_response").fadeIn('slow');
								});														
							});
						});
						
					  }
	);
}

/*
 * ShowContent fades in the page header and main content and selects the main option as active.
 */
function showContent(){
		
	$("#header").fadeIn('slow');
	$("#content0").fadeIn('slow');
	$("#footer").fadeIn('slow');
		
	$("#o0").removeClass();
	$("#o0").addClass("active");				
	
}
	
/*
 * Change the active tab and show the right content
 */
function changeOption(){
	
	//define the elements that will be changed
	var selected = $(this).attr("id");
	var old = $("#header .nav ul li a.active").attr("id");
	var content = "#content" + old.charAt(1);
	
	//hide the old page-title
	$("#pagetitle_text").hide();
	
	//hide old content
	$(content).hide();
	
	//show the new content
	showSelectedContent(selected);	
	
	//if the content selected is the projects page, load feeds
	if(selected == "o1"){
		//load feeds
		loadFeedControl();
	}
	
	//active the option tab clicked
	$("#header .nav ul li a.active").removeClass();
	$(this).removeClass();
	$(this).addClass("active");
								
	return false;
}

/*
 *  showSelectedContent fades in the content page-title and shows the content container
 */
function showSelectedContent(option){
	
	$("#content"+option.charAt(1)).show();
	$("#pagetitle_text").text($("#"+option).text());
	$("#pagetitle_text").fadeIn('slow');
	
}

/*
 * load feeds through Google feeds API 
 */
function loadFeedControl() {
	
  	var options = {
		numResults : 5,
		displayTime : 5000,
		fadeOutTime : 1000,
		scrollOnFadeOut : true
		}
    
    var feed  = "http://poramoralcodigo.juanmirodriguez.es/?feed=rss";
    var fg = new GFdynamicFeedControl(feed, "feedControl");
    
    var feed2  = "http://blogpolitica.juanmirodriguez.es/?feed=rss";
    var fg2 = new GFdynamicFeedControl(feed2, "feedControl2");
    
}

 // Browser Window Size and Position
 // copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
 // you may copy these functions but please keep the copyright notice as well

function pageWidth() {
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function pageHeight() {
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 

function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 

function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 

function posRight() {
	return posLeft()+pageWidth();
} 

function posBottom() {
	return posTop()+pageHeight();
}

function clouds(){	
	for (j = 0; j < 100; j++) {
		var baseX = Math.floor(Math.random() * pageWidth());
		var baseY = Math.floor(Math.random() * pageHeight());
		for (i = 0; i < 10; i++) {
			var x1 = Math.random();
			var y1 = Math.random();
			
			//Box-Muller-Wiener algorithm, get 2 idependient random gaussian variables 
			var x = Math.floor((Math.sqrt(-2*Math.log(y1))*Math.cos(2*Math.PI*x1))*20);
			var y = Math.floor((Math.sqrt(-2*Math.log(y1))*Math.sin(2*Math.PI*x1))*20);
			
			var sprite = Math.floor(Math.random() * 3);
			
			var newCloudSprite = document.createElement("img");
			//newCloudSprite.setAttribute("style", "position: absolute; left: " + (baseX + x) + "px; top: " + (baseY + y) + "px");
			newCloudSprite.style.position = "absolute";
			newCloudSprite.style.left     = (baseX + x) + "px";
			newCloudSprite.style.top      = (baseY + y) + "px";
			newCloudSprite.setAttribute("src", "_layout/images/cloudSprite" + (sprite) + ".png"); //
			newCloudSprite.style.zIndex = "-1"; //
			
			$(document.body).append(newCloudSprite);
		}
	}       		
	//gameLoop();		
}

function gameLoop(){
	
	//TODO game switch! :)
	
	t = setTimeout("gameLoop();", 20);
}
