$(document).ready(function(){
	
	/* NAVI ANIMATION */
        // Animation Main-Navi - Default
	/*$(".main_nav li").hover(
		function(){
			$(this).find('a:nth-child(1)').css({"color":"#fff","font-weight":"bold"});
			$(this).find('ul:first').find('a').css({"color":"#999"});
			$(this).find('ul:first').find('ul').find('a').css({"color":"#fff"});
			$(this).find('ul:first').stop().slideDown();
		},function(){
			$(this).find('ul:first').delay(200).stop().slideUp();
			$(this).find('a:nth-child(1)').css({"color":"#999","font-weight":"normal"});
	});
	
        $(".sub_navi li").click(function(){
		var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			$('.sub_navi ul:visible').stop().slideUp('normal');
			$('.sub_navi ul:visible').prev().find("span").css({"background":"url('../images/content_img/menu_icons.png') -89px -63px no-repeat"});
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('.sub_navi ul:visible').stop().slideUp('normal');
			$('.sub_navi ul:visible').prev().find("span").css({"background":"url('../images/content_img/menu_icons.png') -89px -63px no-repeat"});
			checkElement.slideDown('normal');
			checkElement.prev().find("span").css({"background":"url('../images/content_img/menu_icons.png') -75px -63px no-repeat"});
			return false;
		}
	});*/
	
	// Animation Main-Navi - Auto-SlideDown
	$(".main_nav li").hover(
		function(){
			$(this).find('ul:first').stop(true, true).slideDown();
			$(this).find('ul:first').find('span.arrow').css({"background-position":"-75px -63px"});
			$(this).find('ul:first').find('ul').stop(true, true).slideDown();
		},function(){
			$(this).find('ul:first').delay(300).stop(true, true).slideUp();
			$(this).find('ul:first').find('span.arrow').css({"background-position":"-89px -63px"});
			$(this).find('ul:first').find('ul').stop(true, true).slideUp();
	});
	
	/* PROMO HEADER SLIDESHOW */
	var items = $("#header_images > li").size();
	var zindex = 100;
	for(var i=1; i<=items; i++){
		zindex--;
		$("#header_images li:nth-child("+i+")").css({"top":"0"});
		$("#header_images li:nth-child("+i+")").css({"z-index":zindex});
	}
	
	$("#numero li:nth-child(1)").css({"background":"url('../images/content_img/content_icons.png') -26px -9px no-repeat"});
	$("#numero li:nth-child(1) a").css({"color":"#000"});
	
	var promo_header_interval = setInterval('slideshow("x",'+items+')',10000);
	$("#header_images li").mouseenter(function(){	clearInterval(promo_header_interval);	});
	$("#header_images li").mouseleave(function(){
		clearInterval(promo_header_interval);
		promo_header_interval = setInterval('slideshow("x",'+items+')',10000);
	});
	
	/* COLLECIO */
	$("#part li").hover(
		function(){
			$("#part li:nth-child(1)").removeClass("active");
			$(this).addClass("active");
		},
		function(){
			$(this).removeClass("active");
		}
	);
	
	$("#part").mouseleave(function(){
		$("#part li:nth-child(1)").addClass("active");
		$("#collectio_images li:nth-child(1)").show();
	});
	
	$("#collecio").mouseleave(function(){
		$("#part li:nth-child(1)").addClass("active");
		for(var x=1; x<=items_coll; x++){
			$("#collectio_images li:nth-child("+x+")").hide();
		}
		$("#collectio_images li:nth-child(1)").show();
	});
	
	var items_coll = $("#collectio_images > li").size();
	for(var f=1; f<=items_coll; f++){
		if($("#part li:nth-child("+f+")").hasClass("active")){
			$("#collectio_images li:nth-child("+f+")").show();
		}else{
			$("#collectio_images li:nth-child("+f+")").hide();
		}
	}
	$("#part li").hover(
		function(){
			var index = $("#part li").index(this);
			index +=1;
			$("#collectio_images li:nth-child("+index+")").show();
		},
		function(){
			for(var f=1; f<=items_coll; f++){
				$("#collectio_images li:nth-child("+f+")").hide();
			}
		}
	);
	$("#collectio_images li").hover(
		function(){
			for(var x=1; x<=items_coll; x++){
				$("#part li:nth-child("+x+")").removeClass("active");
			}
			$("#collectio_images li").hide();
			$(this).show();
			var index = $("#collectio_images li").index(this);
			index +=1;
			$("#part li:nth-child("+index+")").addClass("active");
		},
		function(){
			for(var x=1; x<=items_coll; x++){
				$("#collectio_images li").hide();
				$("#part li:nth-child("+x+")").removeClass("active");
			}
		}
	);
	
	/* BANNER SLIDE (CATALEG) */
	$(".image").hover(
		function(){	$(this).find(".titulo").stop(true).animate({top:"111px",height:"30px"});	},
		function(){	$(this).find(".titulo").stop(true).animate({top:"140px",height:"0px"});		}
	);
	
	/* CATALEG SLIDE */
	/* DEFINE VARIABLES */
	var original_duration_multiplier = 10;
	
	var user_os = navigator.platform;
	if(user_os.substr(0,3) == "Win"){
		original_duration_multiplier = original_duration_multiplier/1;		//Adjustment for slower? windows
	}	
	
	/* DO NOT CHANGE */
	var number_of_images = $('#image_content').find('li').size();;
	var number_of_rows = $('#image_content').find('ul').size();
	var images_per_row = $('#image_content').find('ul:first-child').find('li').size();
	var li_marginRight = parseFloat($('#image_content').find('li').css("margin-right"));
	var li_marginBottom = parseFloat($('#image_content').find('li').css("margin-bottom"));
	
	// set container-height
	var container_height = ((parseFloat($('#image_content').find('li').css("height"))+li_marginBottom) * number_of_rows)+20+(20-li_marginBottom);
	$('#image_container').css({"height":container_height});
	
	// IE fix
	var IEwidth_li = 0;
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var IEversion = getInternetExplorerVersion();
		if(IEversion<8){
			var negativeTop = -container_height;
			$('#image_content').css({"top":negativeTop});
			IEwidth_li = parseFloat($('#image_content').find('.image_wrapper').css("width"));
			$('#image_content').find('li').css({"width":IEwidth_li});
			//$('#image_content').find('a').css({"width":IEwidth_li});
		}
	}
	
	// position the arrows and the text in the scrollbars
	var arrow_position = -1*((container_height/2)+10);
	$('#scrollbar_left_text').css({marginTop:arrow_position});
	$('#scrollbar_right_text').css({marginTop:arrow_position});
	
	var container_width = parseFloat($('#image_container').css("width"));
	// set content-width (slider)
	var content_width = ((parseFloat($('#image_content').find('li').css("width"))+li_marginRight) * images_per_row)+34+(34-li_marginRight);
	// IE fix
	/*if (navigator.appName == 'Microsoft Internet Explorer'){
		var IEversion = getInternetExplorerVersion();
		if(IEversion<8){
			content_width = ((IEwidth_li+li_marginRight) * images_per_row)+34+(34-li_marginRight);
		}
	}*/
	if(content_width<=container_width){
		content_width=container_width;
		$('#scrollbar_left').css({"visibility":"hidden"});
		$('#scrollbar_right').css({"visibility":"hidden"});
	}
	$('#image_content').css({"width":content_width});
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var IEversion = getInternetExplorerVersion();
		if(IEversion<8){
			//content_width +=1000;
			//$('#image_content').css({"width":content_width});
		}
	}
	
	var original_duration = $('#image_content').width()*original_duration_multiplier;
	var slide_width = content_width-container_width;
	var slide_rtl = -1 * slide_width;
	    slide_rtl = slide_rtl+"px";
	    
	var marginLeft_now, duration;
	$('#scrollbar_right').mouseenter(function(){
		marginLeft_now = parseFloat($('#image_content').css("margin-left"));
		duration = (original_duration * (content_width+marginLeft_now) / content_width)/2;
		//console.log("right: "+duration);
		$('#image_content').animate({"margin-left": slide_rtl},duration,'linear');
	});
	$('#scrollbar_right').mouseleave(function(){
		$('#image_content').stop(true);
	});
	
	var slide_ltr = 0;
	    slide_ltr = slide_ltr+"px";
	var duration_difference;
	$('#scrollbar_left').mouseenter(function(){
		marginLeft_now = parseFloat($('#image_content').css("margin-left"));
		duration_difference = (original_duration * (content_width+marginLeft_now) / content_width);
		duration = (original_duration-duration_difference)/2;
		//duration = (original_duration * (content_width+marginLeft_now) / content_width)/2;
		//console.log("left: "+duration);
		$('#image_content').animate({"margin-left": slide_ltr},duration,'linear');
	});
	$('#scrollbar_left').mouseleave(function(){
		$('#image_content').stop(true);
	});
	
	/* HOVER EFFECT */
	$('#scrollbar_left').hover(
		function(){
			$(this).find('.scrollbar_bg').animate({"opacity": "0.25"},"slow");
			$(this).find('.scrollbar_bg').css({"background":"#000"},"slow");
			$(this).stop(true).animate({"width": "100px"},"slow");
			$('#scrollbar_left_text').find('span').css({"background":"url('/images/content_img/content_icons.png') -5px -220px no-repeat"});
		},
		function(){
			$(this).find('.scrollbar_bg').css({"background":"#fff"},"slow");
			$(this).find('.scrollbar_bg').animate({"opacity": "1"},"slow");
			$(this).stop(true).animate({"width": "34px"},"slow");
			$('#scrollbar_left_text').find('span').css({"background":"url('/images/content_img/content_icons.png') -24px -220px no-repeat"});
		}
	);
	$('#scrollbar_right').hover(
		function(){
			$(this).find('.scrollbar_bg').animate({"opacity": "0.25"},"slow");
			$(this).find('.scrollbar_bg').css({"background":"#000"},"slow");
			$(this).stop(true).animate({"width": "100px"},"slow");
			$('#scrollbar_right_text').stop(true).animate({"left": "-8px"},"slow");
			$('#scrollbar_right_text').find('span').css({"background":"url('/images/content_img/content_icons.png') -76px -220px no-repeat"});
		},
		function(){
			$(this).find('.scrollbar_bg').css({"background":"#fff"},"slow");
			$(this).find('.scrollbar_bg').animate({"opacity": "1"},"slow");
			$(this).stop(true).animate({"width": "34px"},"slow");
			$('#scrollbar_right_text').stop(true).animate({"left": "-73px"},"slow");
			$('#scrollbar_right_text').find('span').css({"background":"url('/images/content_img/content_icons.png') -55px -220px no-repeat"});
		}
	);
	
	// MAPA WEB / Column-Height
	if($('#mapaweb_row1')){
		var row1_height = new Array();
		var highest = 0;
		$('#mapaweb_row1').find('div').each(function(){
			if(highest < $(this).height()){
				highest = $(this).height();
			}
		});
		$('#mapaweb_row1').find('div').each(function(){
			$(this).css({"height":highest});
		});
	}
	if($('#mapaweb_row2')){
		var row1_height = new Array();
		var highest = 0;
		$('#mapaweb_row2').find('div').each(function(){
			if(highest < $(this).height()){
				highest = $(this).height();
			}
		});
		$('#mapaweb_row2').find('div').each(function(){
			$(this).css({"height":highest});
		});
	}
	
	/* WALL OF FRAME - HOME */
	drawShape();
	$('.align_left').prev().css({ "background-position":"right center" });
		
	// IE fix
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var IEversion = parseFloat(getInternetExplorerVersion());
		if(IEversion<8){
			$('.wof_image').each(function(){
				var width = $(this).width();
				if(width==150){
					$(this).css({ "background-position":"-75px 0" });
				}
			});
		}
	}
	
	/* WALL OF FRAME - MODULOS */
	hoverImage();
	placeUserInfo();
	
	// CHECK INPUT FORMS
	// CONTACTO
	var input_and_textarea_Values = new Array();
	$('.enterText').each(function(){
	    if($(this).val()!=""){
		input_and_textarea_Values[$(this).index()] = $(this).val();
	    }
	});
	var currentIndex = 0;
	$('.enterText').click(function(){
	    currentIndex = $(this).index();
	    if($(this).val()==input_and_textarea_Values[currentIndex] || $(this).hasClass('error')){
		$(this).val("");
		if($(this).hasClass('password')){
			//alert($(this).attr('type'));
			//$(this).attr('type','password');
		}
	    }
	});
	$('.enterText').blur(function(){
	    if($(this).val()==""){
		$(this).val(input_and_textarea_Values[currentIndex]);
	    }
	});
    
	// Edit input
	/*$('input').click(function(){
		$('input').each(function(){
			if($(this).hasClass('required_input') && $(this).val() == ""){
				$(this).val('{t section="messages"}*campo necesario{/t}');
			}
		});
		if($(this).hasClass('required_input') && ($(this).val() == '{t section="messages"}*campo necesario{/t}' || $(this).val() == '{t section="messages"}campo vacío{/t}' || $(this).val() == '{t section="messages"}email invalido{/t}')){
			$(this).val("");
		}
	});*/
	
	// Select
	$('.select_arrow').click(function(){
		var clicked_object_id = $(this).parent().parent().attr("id");
		$('.select_arrow').each(function(){
			if($(this).parent().parent().attr("id") != clicked_object_id){
				$(this).parent().parent().find('.sub_select').stop().hide();
			}
		});
		$(this).parent().parent().find('.sub_select').stop().slideToggle();
	});
	$('.sub_select').find('li').click(function(){
		$(this).parent().stop().slideToggle();
		var wert = $(this).find('a').html();
		$(this).parent().parent().find('input').val(wert);
	});
});

/* PROMO HEADER SLIDESHOW FUNCTION*/
var start=1;
var k=0;
var j, this_zindex, new_zindex;
function slideshow(num,item_no){
	if(num != "x"){
		j=num;
		item_no = $("#header_images > li").size();
		for(var e=1; e<=item_no; e++){
			if(e!=j){
				$("#header_images li:nth-child("+e+")").fadeOut(2000);
				$("#numero li:nth-child("+e+")").css({"background":"url('../images/content_img/content_icons.png') -60px -9px no-repeat"});
				$("#numero li:nth-child("+e+") a").css({"color":"#fff"});
			}
		}
		
		$("#header_images li:nth-child("+j+")").fadeIn(1000);
		$("#numero li:nth-child("+j+")").css({"background":"url('../images/content_img/content_icons.png') -26px -9px no-repeat"});
		$("#numero li:nth-child("+j+") a").css({"color":"#000"});
		k=j;
	}else{
		if(k<item_no){
			if(k==0){k=start;}
			j = k+1;
			if(j>item_no){ j=1; }
			
			$("#header_images li:nth-child("+k+")").fadeOut(2000);
			$("#numero li:nth-child("+k+")").css({"background":"url('../images/content_img/content_icons.png') -60px -9px no-repeat"});
			$("#numero li:nth-child("+k+") a").css({"color":"#fff"});
			
			$("#header_images li:nth-child("+j+")").fadeIn(2000);
			$("#numero li:nth-child("+j+")").css({"background":"url('../images/content_img/content_icons.png') -26px -9px no-repeat"});
			$("#numero li:nth-child("+j+") a").css({"color":"#000"});
			
			k++;
		}else{
			k=1;
			$("#numero li:nth-child("+item_no+")").css({"background":"url('../images/content_img/content_icons.png') -60px -9px no-repeat"});
			$("#numero li:nth-child("+item_no+") a").css({"color":"#fff"});
			
			$("#header_images li:nth-child("+k+")").fadeIn(2000);
			$("#numero li:nth-child("+k+")").css({"background":"url('../images/content_img/content_icons.png') -26px -9px no-repeat"});
			$("#numero li:nth-child("+k+") a").css({"color":"#000"});
		}
	}
}

/* CHECK IE VERSION */
function getInternetExplorerVersion(){
	var browser = navigator.userAgent;
	var pattern  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (pattern.exec(browser) != null){
		browserVersion = parseFloat( RegExp.$1 );
	}
	return browserVersion;
}

/* WALL OF FRAME - HOME - Draw Shape*/
function drawShape(){
    $('.WOF_textos').each(function(){
	var canvas = $(this).parent().find('canvas');
	var color = $(this).css("background-color");
	
	if(color != "transparent" && color != "none"){
	    if (canvas[0].getContext){
		if(canvas.hasClass('left_canvas')){
			var ctx = canvas[0].getContext('2d');
			ctx.beginPath();
			ctx.moveTo(0,0);
			ctx.lineTo(20,20);
			ctx.lineTo(0,20);
			ctx.fillStyle = color;
			ctx.fill();
		}else if(canvas.hasClass('right_canvas')){
			var ctx = canvas[0].getContext('2d');
			ctx.beginPath();
			ctx.moveTo(20,0);
			ctx.lineTo(20,20);
			ctx.lineTo(0,20);
			ctx.fillStyle = color;
			ctx.fill();
		}
	    }
	}
    });
}

/* WALL OF FRAME - MODULO */
function hoverImage(){
	$('.wof_image').hover(
		function(){
			var height_parent = parseInt($(this).css("height"));
			var height_user_info = parseInt($(this).find('.user_info').css("height"));
			var margin_top = height_parent - height_user_info;
			
			$(this).find('.user_info').stop().animate({marginTop:margin_top},"slow");
			
			var IEversion = getInternetExplorerVersion();
			if (navigator.appName == 'Microsoft Internet Explorer' && IEversion<8){
			margin_top -=16;
			$(this).find('.user_info').stop().animate({marginTop:margin_top},"slow");
			$(this).find('canvas').stop().animate({"height":"16px"});
			}
		},
		function(){
			$(this).find('.user_info').stop().animate({marginTop:'310px'},"slow");
			if (navigator.appName == 'Microsoft Internet Explorer'){
			var IEversion = getInternetExplorerVersion();
			if(IEversion<8){	$(this).find('canvas').stop().delay(200).animate({"height":"0"}); }
			}
		}
	);
}

function placeUserInfo(){
	var margin_top = 0;
	$('.user_info').each(function(){
		// Set Margin-Top
		margin_top = parseInt($(this).parent().css("height"));
		$(this).css({"margin-top":margin_top});
		// IE fix
		if (navigator.appName == 'Microsoft Internet Explorer'){
			var IEversion = getInternetExplorerVersion();
			if(IEversion<8){
				var IE_marginTop = "center "+0;
				$(this).parent().css({"background-position":IE_marginTop});
			}
		}
		
		// Draw arrow
		var canvas = $(this).find('canvas');
		var color = $(this).find('.information').css("background-color");
		if(color != "transparent" && color != "none"){
		    if (canvas[0].getContext){
			var ctx = canvas[0].getContext('2d');
			    ctx.beginPath();
			    ctx.moveTo(16,0);
			    ctx.lineTo(16,16);
			    ctx.lineTo(0,16);
			    ctx.fillStyle = color;
			    ctx.fill();
		    }
		}
	});
}

