//configs
var local = ($.cookies.get("local"))? $.cookies.get("local"):"es";
var other_local = (local=="es")? "en":"es";
var project_on = false; // para el boton inf
var video_on=false;

$(document).ready(function(){
	// botones del menu
	$("ul#menu li").each(function(){
		var prefix = $(this).attr("id").substr(3,3);
		$(this).attr("title",lang[prefix][local]);
		// language
		if (prefix=="idi") $(this).css("background-image", "url('images/bt_"+prefix+"_"+other_local+".png')");
		$(this).qtip({
			content: {
				attr: 'title'
			}
		});
	});

	// hides other lang elements
	function set_lang(){
		$(".lang_"+local).css("display", "block");
		$(".lang_"+other_local).css("display", "none");
	}
	set_lang();
	
	// shows img related info
	function img_info(idx){
		$("#photo_info").html($("#content ul li").eq(idx).find("span").html());
	}
	
	// scroll setup
	function slide_setup(){
		$("#fm_pre").addClass('disabled');
		config = new Array();
		var idx_lis = $("#content ul li").length;
		config["li_width"] = $("#content ul li").eq(0).width();
		$("#content ul").width(idx_lis*config["li_width"]);
		if (($("#content ul").width()-config["li_width"])<=$("#content").width()) {
			$("#fm_sig").addClass('disabled');
		}else{
			$("#fm_sig").removeClass('disabled');			
		}
		config["up_to"] = $("#content ul").width()-$("#content").width();
		config["slided"] = Math.abs($("#content ul").offset().left-$("#content").offset().left);
		config["idx"]=0;
		img_info(config["idx"]);
		return config;
	}
	
	// rollover de contenido y carga proyectos
	var bg_col = ["#694bb6","#00f00d","#ff7c31","#f800b6","#ff0000","#694bb6","#00f00d","#ff7c31","#f800b6","#ff0000"];
	function do_project_list(){
		$("ul#project_list li").mouseover(function(){
			var this_url = $(this).find("a").attr("href");
			$("<div/>").css({ position:"absolute", top:$(this).offset().top, left:$(this).offset().left,
				opacity: 0.9, width : $(this).width()-(20*2), height : $(this).height()-(20*2),
				background : bg_col[$(this).index()]
			}).addClass("pop").html($(this).find("img").attr("alt"))
			.click(function(){
				$("#content").load(this_url+" #project",function(data){
					if (data.indexOf("iframe")!=-1){
						video_on=true;
						$("#fm_vid").css("background-image","url('images/bt_vid_on.png')");
					}
					$(".pop").remove();					
					slide_config=slide_setup();
					project_on=true;
					set_lang();
				});
			}).appendTo("body");
		});
	}
	do_project_list();
	
	$(".pop").live("mouseout", function(){
		$(".pop").remove();
	});
	
	// Botones de navegación
	// home
	$("#fm_fra").live("click", function() {
		if ($(this).children().is("a")){
			location.href=$(this).children().attr("href");
		} else {
			$("#content").load("index.php #project_list",function(){
				slide_config=slide_setup();
				do_project_list();
				project_on=false;
				video_on=false;
				$("#fm_vid").css("background-image","url('images/bt_vid.png')");
			});	
		}
	});	
	
	// botones whats your level
	$("#fm_wha").live("click", function() {
		location.href=$(this).find("a").attr("href");
	});
	
	// boton idioma
	$("#fm_idi").live("click", function() {
		$.cookies.set("local", other_local);
		local = other_local;
		other_local = (local=="es")? "en":"es";	
		set_lang();
		$(this).css("background-image", "url('images/bt_idi_"+other_local+".png')");
		$("ul#menu li").each(function(){
			var prefix = $(this).attr("id").substr(3,3);
			$(this).attr("title",lang[prefix][local]);
		});
	});

	// boton mensaje
	$("#fm_con").live("click", function() {
		$("#content").html('<iframe src="send_msg" scrolling="no" width="900" height="600" frameborder="0"></iframe>');
	});
	
	
	// boton video
	$("#fm_vid").live("click", function() {
		if (video_on){
			$("#project_details").css("display", "none");
			$("#project_video").toggle();
		}
	});
	
	// más información o FM
	$("#fm_inf").live("click", function() {
		if (project_on){
			// shows project info
			$("#project_video").css("display", "none");			
			$("#project_details").toggle();
		}else{
			// shows FM info
			var this_url = $(this).find("a").attr("href");
			$("#content").load(this_url+" #project", function(){
				slide_config=slide_setup();
				project_on=false;
				set_lang();
			});
		}
	});
	
	// botones previo y anterior
	var slide_config=slide_setup();	
	$("#fm_sig").live("click", function(e){
		e.stopPropagation();
		$(".pop").remove();
		if (slide_config["slided"] <= slide_config["up_to"]+1 && !$(this).hasClass('disabled')) // up_to+1 is a IE hack 
		{
			$("#content ul").animate({left:'-='+slide_config["li_width"]},500,function(){$("#fm_pre").removeClass('disabled');});
			slide_config["slided"] = Math.abs($("#content ul").offset().left-$("#content").offset().left)+(2*slide_config["li_width"]);
			slide_config["idx"] = slide_config["idx"]+1;
			img_info(slide_config["idx"]);
		} else {
			$(this).addClass('disabled');
		}
	});

	$("#fm_pre").live("click", function(e){
		e.stopPropagation();
		$(".pop").remove();
		if ($("#content ul").offset().left+config["li_width"] <= $("#content").offset().left 
		&& !$(this).hasClass('disabled')) {
			$("#content ul").animate({left:'+='+slide_config["li_width"]},500,function(){$("#fm_sig").removeClass('disabled');	});
			slide_config["slided"] = Math.abs($("#content ul").offset().left-$("#content").offset().left);
			slide_config["idx"] = slide_config["idx"]-1;
			img_info(slide_config["idx"]);			
		} else {
			$(this).addClass('disabled');
		} 
	});	
});
