var Vmargin = 18;
var Hmargin = 25;
var medianil = 10;
var c=0;
var t;
var timer_is_on=0;
var highestDiv = 0;
var t;
var num_cols=0;
var current_col=0;

function menuPosition(){
	$("#main").css({"left" : Hmargin, "top" : Vmargin} , 1000);
	$("#sec").css({"right" : Hmargin, "top" : Vmargin} , 1000);
	$("#menu").css({"height" : 2*Vmargin + highestDiv});
}
function footerPosition(){
	$("#logo").css({"right" : Hmargin, "bottom" : '13px'} , 1000);
	$("#stamps").css({"left" : Hmargin, "bottom" : '30px'} , 1000);
	$("#footer").css({"height" : 2*Vmargin + $("#logo").height()});
}
function rotate_supersize()
{
	var next_index = (1+Math.round($("#super_image").attr('index')));
	if(next_index>$('#super_image_list li').size()-1)
		next_index=0;
	$("#super_image img").attr("src","/images/home/"+$('#super_image_list li:eq('+next_index+')').html());
	$("#super_image").attr('index',next_index);
	$("#super_image img").load(function(){
		resizenow(); 
	});
}
//Adjust image size
function resizenow() {		
		//Define image ratio
		var ratio = 768/1024;
		
		//Gather browser size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio){
			$("#super_image").width(browserheight / ratio);
			$("#super_image").height(browserheight);
		} else {
			$("#super_image").width(browserwidth);
			$("#super_image").height(browserwidth * ratio);
		}
				
		//fix content width to fit the number of columns
		var width_aux=0;
		$(".column").each(function(){
			width_aux+=$(this).width()+100;
		});
//		if($(window).width()>width_aux)
//			$("#content").width($(window).width());
//		else{
//			$("#content").width(width_aux+350);
//		}
		$("#content").width(50000);
		if($(".flecha").size())
		$("#content_back").width($(window).width()-135);
		
		//position the content banner in the middle of the page
		$("#content").css({top: $(window).height()/2-$("#content").height()/2+5});
		$("#content_back").css({top: $(window).height()/2-$("#content").height()/2+5});
		$(".flecha").css({top: $(window).height()/2-$("#content").height()/2+5});
		
		if($("#multimedia_image img").size()){
			resize_multimedia();
		}
		$('#page').height($(window).height());
		$("#fb").css({'left':$(window).width()/2-75});
}	
function resize_multimedia() {		
		//Define image ratio
		var ratio = $("#multimedia_image img").height()/$("#multimedia_image img").width();
		//Gather browser size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();

		//Resize image to proper ratio
		if ((browserheight/browserwidth) > ratio){
			$("#multimedia_image").width(browserheight / ratio);
			$("#multimedia_image").height(browserheight);
		} else {
			$("#multimedia_image").width(browserwidth);
			$("#multimedia_image").height(browserwidth * ratio);
		}
		
		$("#multimedia_image img").height($(window).height()*.7);
		$("#multimedia_image img").width("");
		$("#multimedia_image img").css({'margin-top':$(window).height()*.3/2});
}	
//Adjust news container
function resize_news() {		
	$("#single").css({"padding-left":$(window).width()/2-340,"padding-right":$(window).width()/2-340,"height":$(window).height()-240})
}	

$(document).ready(function() {
	hover=true;
	$("#ppal > li").not(".children:visible").hover(
		function() {
			$(".children:visible").hide();
			$(".children",this).show();
			clearTimeout(hover);
		},
		function () {
			$(".children").stop(true,true);
			hover=setTimeout('$(".children:visible").fadeOut();',3000)
		}
	);
	
	///afegit GS G
	$("#ppal2 > li").not(".children:visible").hover(
		function() {
			$(".children:visible").hide();
			$(".children",this).show();
			clearTimeout(hover);
		},
		function () {
			$(".children").stop(true,true);
			hover=setTimeout('$(".children:visible").fadeOut();',3000)
		}
	);
	
	/////
	
	
	$("#menu").hover(
		function() {
		},
		function () {
			//$(".children:visible").fadeOut();
		}
	);
	
	$("#flecha_izq").click(
		function() {
			if(current_col>0){
				var offset=$(".col:eq("+(current_col-1)+")").width();
				if(offset){
					$("#content").animate({"left":'+='+Math.round(offset+30)},500, function(){});
					$("#sub3").animate({"left":'-='+Math.round(offset+30)},500, function(){});
					current_col-=1;
					$("#flecha_der img").css("opacity",1);
					
					if(current_col==0)
						$("#flecha_izq img").css("opacity",.3)
				}
			}
		}
	);
	$("#flecha_der").click(
		function() {
			if(current_col<num_cols-1){
				var offset=$(".col:eq("+current_col+")").width();
				if(offset){
					$("#content").animate({"left":'-='+Math.round(offset+30)},500, function(){
																  //complete
																  });
					$("#sub3").animate({"left":'+='+Math.round(offset+30)},500, function(){});
					current_col+=1;
					$("#flecha_izq img").css("opacity",1);
					
					if(current_col==num_cols-1)
						$("#flecha_der img").css("opacity",.3)
				}
			}
		}
	);
	
	jQuery.each($(".menuItem"), function(index){ 
		if($(this).height() > highestDiv){
			highestDiv = $(this).height();
		}
	});
	
	$("li").click(function() {
		//alert($(this).attr("id"))			   
	});
	
	$("#ppal").children(".page_item").children(".children").css({left:$("#ppal").width()+25});
	$("#ppal").width($("#ppal").width()+30);

	$("#ppal2").children(".page_item").children(".children").css({left:$("#ppal2").width()-575});
	$("#ppal2").width($("#ppal").width()+30);
	

	
	$("#sub3 li").each(function(){
		//$(this).html($("a", this).html());
	});
	
	menuPosition();
	footerPosition();
	$(window).bind("resize", function(){
	  resizenow(); 
	  resize_news();
	});
	
	
	var width_aux=0;
	$(".column").each(function(){
		width_aux+=$(this).width()+50;
	});
	if($(window).width()>width_aux+50)
		$("#content").width($(window).width());
	else{
		$("#content").width(width_aux+350);
	}
	resizenow();
	$("#super_image").append('<img src="/images/home/'+$('#super_image_list li:eq('+Math.ceil(Math.random()*$('#super_image_list li').size()-1)+')').html()+'">')
});

