

function swapBackground(){
	if($("#egg1").css("display") == "block" || $("#egg1").css("display") == "inline"){
		$("#egg1").fadeOut(2000);
		$("#egg2").delay(200).fadeIn(2200);
		
	} else {
		$("#egg2").fadeOut( 2000);
		$("#egg1").delay(200).fadeIn(2200);
		
	}
}


function showSelection(selection){
	selection = typeof(selection) != 'undefined' ? selection : "all";
	var totalfg = $(".firms-group").length;
	var totalpg = $(".project-type").length;
	var totalao = $(".admin-option").length;
	
	$(".firms-group, .project-type, .admin-option").fadeOut(400, function(){
		if (--totalfg > 0 || --totalpg > 0 || --totalao > 0){
			return;
		} else {
			if(selection == "all"){
				$(".firms-group, .project-type, .admin-option").fadeIn(400);
			} else {
				$("#"+selection).fadeIn(400);
			}
		}
	});
}



/* ------------------ ON DOM READY -------------------- */
$(function(){
	
	
	/*------------------------------------------------------------------------*/
	/*animations for index.php*/
	
	$("#egg1").each( function(){
		$(this).load( $(this).attr("src"), function(){
			$(this).delay(500).fadeIn(2000);
		});
	});
	
	$("#s-home > h1").delay(3000).fadeIn(1500, function(){
		swapBackground();
		$("#s-home div.column").animate({height : '250px'}, 1000);
		
		setInterval("swapBackground()", 8000);
	});
	
	//$("#s-home div.column").delay(8000).fadeIn(8000);
	
	/*------------------------------------------------------------------------*/
	
	
	/*------------------------------------------------------------------------*/
	/*add event click on submenu items*/
	$(".submenu > ul > li > a").click(function(){
		$(".submenu > ul > li > a").removeClass("selected");
		$(this).addClass("selected");
		
		var str = $(this).attr("href").split("=");
		var value = str[str.length-1]; 
		
		showSelection(value);
		return false;
	});
	
	
	
	/*------------------------------------------------------------------------*/
	
	
	/*------------------------------------------------------------------------*/
	/*animations for firms.php & projects.php*/
	
	$("#gallery li img").each( function(){
		$(this).load( $(this).attr("src"), function(){
			$(this).fadeIn(2000);
		});
	});
	
	$(".project-box").hover(function(){
		$("div", this).stop().animate({bottom : '0'}, 300);
	}, function(){
		$("div", this).stop().animate({bottom : '-40px'}, 300);	
	});
	
	$(".firm-box").hover(function(){
		$(this).stop().animate({opacity : '1'}, 200);
	}, function(){
		$(this).stop().animate({opacity : '0.25'}, 200);	
	});
	
	
	/*------------------------------------------------------------------------*/
	
	
	
	
	/*------------------------------------------------------------------------*/
	/*animations for firm.php & project.php*/
	
	$(".firm-box img, .project-box img").each( function(){
		$(this).load( $(this).attr("src"), function(){
			$(this).fadeIn(2000);
			$(".project-box div").fadeIn(2000);
		});
	});
	
	
	$("#a-project a, #a-firm a").hover(
		function(){
			var that = this;
			
			//it keeps track of the timer (via the element's data)
			$(this).data('timer', setTimeout(function(){showBoxInfo(that)},500));
			
			function showBoxInfo(param){
				//after the timer we show the element
				$("span.container", param).fadeIn();
			}
			
		},
		function(){
			//onmouseout we hide the element
			$("span.container", this).fadeOut();
			
			//to clear the timer if a mouseout event is fired before cosuming the timer.
			var timer = $(this).data('timer');
         	if (timer) $(this).data('timer', clearTimeout(timer));

		}
	);
	
	
	/*code to make the slider gallery*/
	
	var total_imgs = $("#gallery li").length;
	var img_width = 640;
	var pixels = "0";
	var current_position = 0;
	
	//to set the total width of the ul-list so we don't have to do it in the CSS file
	$("#gallery").css("width",total_imgs*img_width+"px");
	
	//if there's only one img
	if($("#gallery").innerWidth() == img_width ){
		$("#next, #previous").css("visibility", "hidden");
	}
	
	$("#next").click(function(){
		
		if($('#gallery').css("left") == "-"+(total_imgs-1)*img_width+"px"){
			pixels = "0";
			current_position=0;
		} else {
			pixels = "-"+(current_position+1)*img_width;
			current_position++;
		};
		
		$('#gallery').animate(	{"left"		:pixels+"px"}, 
								{duration	:"200"});
		
		return false;
	});
	
	$("#previous").click(function(){
		
		if($('#gallery').css("left") == "0px"){
			pixels = "-"+(total_imgs-1)*img_width;
			current_position=total_imgs-1;	
		} else {
			pixels = "-"+(current_position-1)*img_width;
			current_position--;
		};
						
		$('#gallery').animate(	{"left"		:pixels+"px"}, 
								{duration	:"slow"});
		
		return false;
	});
	
	/*------------------------------------------------------------------------*/
	
	
	
	
	/*------------------------------------------------------------------------*/
	/*handles the contact form*/
	$("#contact-form").submit(function(event){
		
		var errors = false;
	
		//check input data
		$("#contact-form input").each( function (){
			$(this).css("background-color", "#CFD1D2");
			
			if(	$(this).val() == ""){ 
				errors = true;
				$(this).css("background-color", "#FFCCCC");
			}
		});
		
		if (errors) return false; //if something is missing or wrong we stop the operation
		
		var result = new Array ("	El correo se ha enviado satisfactoriamente!<br />En breve recibirá notícias nuestras.",
								"	Ha habido un problema enviando el correo!<br />Vuelve a intentarlo más tarde.");
		
		$("#contact-form submit").attr("disabled", "disabled");
		
		$.post(	$("#contact-form").attr("action"), $("#contact-form").serialize(), function(data){
			$("#form-result").html(result[0]);
			$("#form-result").addClass("success");
			$("#form-result").css("display", "block");
		})
		.error( function(){ 
			$("#form-result").html(result[1]);
			$("#form-result").addClass("fail");
		})
		.complete( function(){
			$("#contact-form submit").removeAttr('disabled');
		});
		
		
		event.preventDefault();
	});
	
	/*------------------------------------------------------------------------*/
	
	
	
	/*------------------------------------------------------------------------*/
	//function to open popup window

	$(".popup").click( function(){
		
		window.open(	$(this).attr("href"),
						"popup",
						"height = 768, width = 1024, scrollbars=yes" )
		
		return false;
	});
	/*------------------------------------------------------------------------*/
	
	
	
	/*suki signature*/
	/*------------------------------------------------------------------------*/
	$('.suki-signature .glyph').not('.gk').css('opacity', 0);
	$('.suki-signature').hover(
		function(){
			$('.gs', this).stop(true,true).animate( {opacity: 1}, 50);
			$('.gu', this).stop(true,true).animate( {left: '10px', opacity: 1}, 100);
			$('.gk', this).stop(true,true).animate( {left: '20px'}, 150);
			$('.gi', this).stop(true,true).animate( {left: '31px', opacity: 1}, 200);
			$('.string', this).stop(true,true).delay(200).fadeIn(400);
			$('.bg', this).stop(true,true).fadeIn(200);
		}, 
		function(){
			$('.gs', this).stop(true,true).delay(400).animate( {opacity: 0}, 200);
			$('.gu', this).stop(true,true).delay(400).animate( {left: '0', opacity: 0}, 150);
			$('.gk', this).stop(true,true).delay(400).animate( {left: '0'}, 100);
			$('.gi', this).stop(true,true).delay(400).animate( {left: '0', opacity: 0}, 50);
			$('.string', this).stop(true,true).delay(400).fadeOut(50);
			$('.bg', this).stop(true,true).delay(500).fadeOut(500);
		}
	);

	/*------------------------------------------------------------------------*/
	
	
});

