/*FUNCTION ALTURA*/
function showHeight(ele,h,sec) {	

	if(sec>""){
		$("#"+sec).css("height",h);	
	//	alert("sec:"+sec+" h:"+h);
	}
	else{
		//$("#mainAbout").css("height",h);
		//alert("sec:"+sec+" h:"+h);
		$("#featured").css("height",h);
		$("#unphilosophy").css("height",h);
		$("#news").css("height",h);
		$("#clients").css("height",h);
		$("#company").css("height",h);
		$("#contact").css("height",h);		
    }
}
var SeccionGlobal;		
var IndiceGlobal=0;		
var semaforo = 0; /*inicio*/

$(document).ready(function() {
	

	
	
	$("#scrollcont").jScrollPane({showArrows: true});
	//calcula el alto de los logos de Clients	
	$(".clients li a img").each(function(){
			var altoimagen = $(this).height()
			if (altoimagen  > "49") {
				$(this).addClass("imagenAlta")
			}		    
		  });
	//Cuenta la cant de PEOPLE
	var cantPeople = $(".people_cont li").length;
	if (cantPeople > 6) {
		$("#people_next, #people_prev, #peopleThumb_prev, #peopleThumb_next").show();
	}
	
	
	$('form.NiceIt').NiceIt();
	showHeight("window", $(window).height()); 	
	$(window).resize(function(){		
		showHeight("window", $(window).height(),SeccionGlobal);
	});
	$(".s1").click(function () { 
		$('body').scrollTo( '#featured', 1000);	
		showHeight("",$(window).height(), "featured");
		SeccionGlobal = "featured";
		$(".s1").addClass("selected");
		$(".s2, .s3,.s4,.s5,.s6").removeClass("selected");
		return false 		
	})	
	$(".s2").click(function () { 
		$('body').scrollTo( '#unphilosophy', 1000);
		showHeight("",$(window).height(), "unphilosophy");
		SeccionGlobal = "unphilosophy";
		$(".s2").addClass("selected");
		$(".s1, .s3,.s4,.s5,.s6").removeClass("selected");		
		return false 		
	})	
	$(".s3").click(function () { 
		$('body').scrollTo( '#news', 1000 );
		showHeight("",$(window).height(), "news");	
		SeccionGlobal = "news";
		$(".s3").addClass("selected");
		$(".s2, .s1,.s4,.s5,.s6").removeClass("selected");	
		return false 		
	})	
	$(".s4").click(function () { 
		$('body').scrollTo( '#clients', 1000 );	
		showHeight("",$(window).height(), "clients");
		SeccionGlobal = "clients";
		$(".s4").addClass("selected");
		$(".s2, .s3,.s1,.s5,.s6").removeClass("selected");		
		return false 		
	})	
	$(".s5").click(function () { 
		$('body').scrollTo( '#company', 1000 );	
		showHeight("",$(window).height(), "company");
		$(".s5").addClass("selected");
		$(".s2, .s3,.s4,.s1,.s6").removeClass("selected");		
		SeccionGlobal = "company";				
		return false 		
	})	
	$(".s6").click(function () { 
		$('body').scrollTo( '#contact ', 1000 );
		showHeight("",$(window).height(), "contact");	
		$(".s6").addClass("selected");
		$(".s2, .s3,.s4,.s5,.s1").removeClass("selected");			
		SeccionGlobal = ".contact";	
		return false 		
	});	

//	
// BACKGROUND SLIDER
//----------------------------------------------------------------------------------------------------
//

	/* inicio de slide */
	var counter = 1;
	$("#total").html(photos.length);
	$("#actual").html(counter);
	var descripcion = photos[0].title + " | " + photos[0].firstline;
	$("#descripcion").html(descripcion);

	
	// Backwards navigation
	$("#back").click(function() {


			if(semaforo)
				return false;

			stopAnimation();
			semaforo=1;
			navigate("back");
			setTimeout ( "changeSemaforo()", 1000 );


			if(counter==1){
				$("#actual").html(photos.length);
				counter=photos.length;
				IndiceGlobal=photos.length;
			}else{
				$("#actual").html((counter-1));
				counter=counter-1;
				IndiceGlobal=IndiceGlobal-1;
			}
			index = counter - 1;
			

	});
	
	// Forward navigation
	$("#next").click(function() {
	
			if(semaforo)
				return false;

	
			stopAnimation();
			semaforo=1;
			navigate("next");
			setTimeout ( "changeSemaforo()", 1000 );
			
			if(counter==photos.length){
				$("#actual").html("1");
				counter=1;
				IndiceGlobal=1;
				
			}else{
				$("#actual").html((counter+1));
				counter=counter+1;
				IndiceGlobal=IndiceGlobal+1;
			}
			index = counter - 1;
	});
	
	var interval;
	
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/btn_pause.png)" });
		navigate("next");
		
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		descripcion = photos[currentImg - 1].title + " | " + photos[currentImg - 1].firstline;
		$("#descripcion").html(descripcion);

		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		$("#VideoLink").attr("href",photoObject.work_youtube);

		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	// interval = setInterval(function() {
		// navigate("next");
	// }, slideshowSpeed);
	
	//************************************ FOCUS FIELD
		$('input[type="text"], textarea').addClass("idleField");
       		$('input[type="text"], textarea').focus(function() {
       			$(this).removeClass("idleField").addClass("focusField");
    		    if (this.value == this.defaultValue){ 
    		    	this.value = '';
				}
				if(this.value != this.defaultValue){
	    			this.select();
	    		}
    		});
    		$('input[type="text"], textarea').blur(function() {
    			$(this).removeClass("focusField").addClass("idleField");
    		    if ($.trim(this.value) == ''){
			    	this.value = (this.defaultValue ? this.defaultValue : '');
				}
    		});
	
});
function changeSemaforo(){
	semaforo=0;
}

