var ini_width=314;
var ini_height=440;
var ini_title_margin=70;
var ini_title_font=24;
var ini_sub_title_margin=37;
var ini_sub_title_font=20;
var doc_width;
var doc_height;
var new_width;
var new_height;
var position_slide=0;
var num_objects;
var move_left=0;
var end_document_h=0;
var move;

var menu_height=82;
var menu_height_min=82;

var menuStatus="show";
var actualSection="";

var animation=new Array();

var resizeImgs=false;

var t;
var posLeft=0;

var canMove=false;

function moveImages(){
	if(canMove==true){
		images=$(".images");
		var iLeft=parseInt(images.css("left").replace("px",""));
		if((iLeft<0 && move>0) || (move<0 && iLeft>posLeft)){
			images.css("left",iLeft+move+"px");
		}
		var iLeft=parseInt(images.css("left").replace("px",""));
		if(iLeft>0){
			images.css("left","0");
		}
		var iLeft=parseInt(images.css("left").replace("px",""));
		if(iLeft<posLeft && posLeft<0){
			images.css("left",posLeft+"px");
		}
	}
	clearInterval(t);
	t=setTimeout("moveImages()",100);
	
}


$(document).ready(function(){
	resizeContent();
	
	$("a").live("focus",function(){
		$(this).blur();
	});
	
	$("#main ul.works li a").live("mouseenter",function(){
		var id_anim=$(this).attr("id");
		if(!animation[id_anim]){
			$(this).find(".work_bg").animate({
				left:'0px'
			},400,function(){
				animation[id_anim]=false;
			});
			animation[id_anim]=true;
		}
	});
	$("#main ul.works li a").live("mouseleave",function(){
		$(this).find(".work_bg").animate({
			left:'-235px'
		},300);
	});
	
	$(".container_images").live("mousemove",function(event){
		var divLeft=305;
		var pageX=event.pageX-divLeft;
		var middle=(doc_width-divLeft)/2;
		move=(middle-pageX)/10;
		if(move>-10 && move<10) move=0;
	});
	$(".container_images").live("mouseenter",function(event){
		t=setTimeout("moveImages()",100);
	});
	
	$(".container_images").live("mouseleave",function(event){
		clearInterval(t);
	});

	$("#search").live("keydown", function(event) {
		if (event.keyCode == 13) {
			search($("#search").val());
		}
	});
	
	$("#yubino").mouseenter(function(){
		$(this).fadeTo("slow",1);
	});
	$("#yubino").mouseleave(function(){
		$(this).fadeTo("slow",0.6);
	});

	$("#newsletter_mail").keydown(function(code){
		if(code.keyCode==13){
			$.ajax({ 
				type: 	'POST',
				data: {'email': $(this).val()},
				url:	'/ws/newsletter.php',
				success:  	function(code){
					if(code=="ok"){
						$("#newsletter").find(".message").html("Tu email se ha a&ntilde;adido correctamente a nuestra newsletter.<br/><br/>Muchas Gracias!");
					}else if(code=="exist"){
						$("#newsletter").find(".message").html("El mail introducido ya se encuentra en la lista.");
					}else{
						$("#newsletter").find(".message").html("Se ha producido un error al introducir el mail en la lista.<br/><br/>Vuelve a intentarlo pasados unos minutos. Gracias!");
					}
				}
			});
		}
	});

});

$(".btn_show").live("click",function(event){
event.preventDefault();
	menuShow();
	resizeContent();
});

$(".btn_hide").live("click",function(event){
event.preventDefault();
menuHide();
resizeContent();
});

$(window).resize(function() {
	resizeContent();
});
function resizeContent(){
	doc_width=$(window).width();
	doc_height=$(window).height();
	new_height=parseInt((doc_height/2)-22);
	new_width=parseInt((new_height*ini_width)/ini_height);
	new_title_margin_top=parseInt(new_height-((ini_title_margin*new_height)/ini_height)-14);
	new_title_font=parseInt((ini_title_font*new_height)/ini_height);
	new_sub_title_margin_top=parseInt(new_height-((ini_sub_title_margin*new_height)/ini_height)-14);
	new_sub_title_font=parseInt((ini_sub_title_font*new_height)/ini_height);
	end_document_h=parseInt($("#main").css("height").replace("px",""));
	
	$("body").css("height",(doc_height-65)+"px");
	$("#contact").css("height",(doc_height)+"px");
	$("#clients").css("height",(doc_height)+"px");
	$("#newsletter").css("height",(doc_height)+"px");
	$("#knowus").css("height",(doc_height)+"px");
	$("#work_detail").css("height",(doc_height)+"px");
	$("#work_detail").css("width",(doc_width)+"px");
	$("#work_detail .images").css("height",(doc_height)+"px");
	$("#search_results").css("height",(doc_height)+"px");
	$("#footer").css("top",doc_height-parseInt($("#footer").css("height").replace("px","")));
	
	
	$(".jScrollPaneContainer").css("height",(doc_height-24)+"px");
	$(".jScrollPaneContainer").css("width",(doc_width)+"px");

	$(".works").jScrollPane();
	
	if(resizeImgs==true){
		$("#work_detail .images img").stop(true,true)
	}

	img_height=doc_height-165;
	if(img_height>750){
		img_height=750;
	}else if(img_height<360){
		img_height=360;
	}
	
	$("#work_detail .images img").stop(true,true).animate({
		height: img_height+"px"
	},500,function(){
	});

	var ancho=0;
	$(".wImg").each(function(){
		ancho+=parseInt($(this).css("width").replace("px",""))+15;
	});
	$(".images").css("width",ancho+"px");
		
}

function menuShow(){
	if(menuStatus=="hide"){
		menuStatus="show";
		$("#menu ul").fadeIn();
		$(".logo").fadeIn();
		$(".btn_show").fadeOut("fast",function(){
			$(".btn_show").html("Ocultar");
			$(".btn_show").removeClass("btn_show").addClass("btn_hide");
		});
		$(".btn_show").fadeIn("fast");
		$(".section").animate({
			top: "82px"
		},499);
		$("#menu").animate({
			top: "0px"
		},500);
		resizeContent();
	}
}

function menuHide(){
	if(menuStatus=="show"){
		menuStatus="hide";
		$("#menu ul").fadeOut();
		$(".logo").fadeOut();
		$(".btn_hide").fadeOut("fast",function(){
			$(".btn_hide").html("Mostrar");
			$(".btn_hide").removeClass("btn_hide").addClass("btn_show");
		});
		$(".btn_hide").fadeIn("fast");
		$(".section").animate({
			top: "42px"
		},500);
		$("#menu").animate({
			top: "-40px"
		},500);
		resizeContent();
	}
}

function sectionShow(section){
	section=section || "";
	if(actualSection==""){
		actualSection=section;
		if(section=="work_detail"){
			$("#"+section).fadeIn();
		}else{
			$("#"+section).animate({
				left: "0px"
			},500);
		}
	}else if(actualSection!=section){
		sectionHide(actualSection);
		actualSection=section;
		if(section=="work_detail"){
			$("#"+section).fadeIn();
		}else{
			$("#"+section).animate({
				left: "0px"
			},500);
		}
	}
	if(section!="work_detail"){
		$(".selected").removeClass("selected");
		$("#menu_"+section).addClass("selected");
	}
	resizeContent();				
}

function sectionHide(section){
		section=section || "";
		if(section=="work_detail"){
			if(section==""){
				$("#"+actualSection).fadeOut();
			}else{
				$("#"+section).fadeOut();
			}
		}else{
			if(section==""){
				$("#"+actualSection).animate({
					left: "-500px"
				},500);
			}else{
				$("#"+section).animate({
					left: "-500px"
				},500);
			}							
		}
		actualSection="";
		$(".selected").removeClass("selected");
		$("#"+category_class).addClass("selected");
		resizeContent();
}

function loadWork(id){
	var loading='<a href="#" class="close" onclick="sectionHide(\'work_detail\')"><img src="/img/btn_close.png" alt="" /></a><img style="display:block;margin:100px auto;" src="/img/loading.gif" alt="loading" />';
	$("#work_detail .content").html(loading);
	sectionShow('work_detail');
	$.ajax({ 
		type: 	'POST',
		data: {'id':id},
		url:	'/ws/loadWorks.php',
		success:  	function(code){
			code = code.replace(/^\s*|\s*$/g,"");
			$("#work_detail .content").html("");
			$("#work_detail .content").html(code);
		}
	});
	return false;
}

function search(text){
	var loading='<a href="#" class="close" onclick="sectionHide(\'search_results\')"><img src="/img/btn_close.png" alt="" /></a><img style="display:block;margin:100px auto;" src="/img/loading.gif" alt="loading" />';
	$("#search_results .content").html(loading);
	sectionShow('search_results');
	$.ajax({ 
		type: 	'POST',
		data: {'search':text},
		url:	'/ws/search.php',
		success:  	function(code){
			code = code.replace(/^\s*|\s*$/g,"");
			$("#search_results .content").html("");
			$("#search_results .content").html(code);
			resizeContent();
		}
	});
}

