$(window).load(function () {
	gestionSombras();
	ayco_control_rollover();
	control_nextStep();
	control_tabs();
	muestra_fecha();
	lanzarFancyBox();
	controla_tooltip();
	
	$.preLoadImages('img/ico_contactar_on-trans.png','img/ico_demo_on-trans.png','img/ico_freepaper_on-trans.png','img/ico_presupuesto_on-trans.png','img/ico_pie_facebook_on.gif','img/ico_pie_blog_on.gif','img/ico_pie_twitter_on.gif','img/ico_pie_youtube_on.gif','img/ico_pie_flikr_on.gif','img/ico_pie_rss_on.gif');
	
	set_ciclo();
	
	$("#newsPie").hover(
		function () {
			kill_ciclo();
		},
		function () {
			set_ciclo();
		}
	)
});

remplazarFuentes();

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

function gestionSombras(){
	
//	$("a.actPag span").dropShadow({left: 0, top: 1, opacity: 1, blur: 0, color: "#ffffff"});
	
}

function remplazarFuentes(){
	
		Cufon.replace('#menu a', { fontFamily: 'Mplus_M', textShadow: '#ffffff 1px 1px 0' });
		Cufon.replace('#menu a.activo', { fontFamily: 'Mplus_M', textShadow: '#555c68 0px 1px 0', color: '-linear-gradient( #ffffff,#dddddd)', hover: true });
		Cufon.replace('#subMenu a', { fontFamily: 'Mplus_R', hover: false });
		Cufon.replace('#subMenu a.actSubMenu', { fontFamily: 'Mplus_M', hover:false });
		Cufon.replace('H1', { fontFamily: 'Mplus_T' });
		Cufon.replace('H3', { fontFamily: 'Mplus_M'});
		Cufon.replace('a.moreNews', { fontFamily: 'Mplus_M' });
		Cufon.replace('.fechaNews', { fontFamily: 'Nilland_B' });
		Cufon.replace('.fechaNot', { fontFamily: 'Nilland_B' });
		Cufon.replace('#ordenar', { fontFamily: 'Mplus_R', textShadow: '#dddddd 0px 1px 0', color: '-linear-gradient(#999999,#333333)', hover: false });
		Cufon.replace('h2', { fontFamily: 'Mplus_R', textShadow: '#eeeeee 0px 1px 0', color: '-linear-gradient( #999999,#222222)', hover: false });
		Cufon.replace('h3.fNot', { fontFamily: 'Mplus_M', textShadow: '#eeeeee 0px 1px 0', color: '-linear-gradient( #666666,#111111)', hover: false });

}

function control_tabs () {
	$("div.tabs").tabs(".images > div", {
		effect: 'fade',
		fadeOutSpeed: "slow",
		rotate: true
	});
	setTimeout(function () {
		$("div.tabs").slideshow();
	}, 3000);
	
}

function control_nextStep () {
	$("#nextStep a").hover(
		function () {
			var $elsToFadeOut	= $("span#swap span:visible:not(#" + $(this).find("img").attr("rel") + ")");
			var $elToFadeIn		= $("span#" + $(this).find("img").attr("rel"));
			
			if ($elsToFadeOut.length>0) {
				$elsToFadeOut.stop(true,true).fadeOut('normal', function () {
					$elToFadeIn.fadeIn("normal");
				});
			} else {
				$elToFadeIn.fadeIn("normal");
			}
		},
		function () {
			var $elToFadeOut	= $("span#" + $(this).find("img").attr("rel"));
			$elToFadeOut.stop(true,true).fadeOut('normal');
		}
	);
}

function ayco_control_rollover () {
	$(".rollover[src*=_off]").unbind('hover').hover(
		function () {
			off_src	= $(this).attr("src");
			on_src	= off_src.replace("_off","_on");
			$(this).attr("src",on_src);
		},
		function () {
			on_src	= $(this).attr("src");
			off_src	= off_src.replace("_on","_off");
			$(this).attr("src",off_src);
		}
	);
}

function ayco_alertar (txt,callback) {
	var alertar_id = "info_" + new Date().getTime();
	$('<div id="' + alertar_id + '">' + txt + '</div>').dialog({
		bgiframe: true,
		modal: true,
		title: "Informaci&oacute;n",
		resizable: false,
		buttons: {
			Aceptar: function() {
				$("#" + alertar_id).dialog('close');
			}
		},
		beforeclose: function(event, ui) {
			switch (typeof(callback)) {
				case "function":
					callback.call();
					break;
				case "string":
					eval(callback);
					break;
			}
			$("#" + alertar_id).dialog('destroy').remove();
		}
	});
}

function ayco_confirmar (txt,callback_ok,callback_ko) {
	var confirmar_id = "info_" + new Date().getTime();
	$('<div id="' + confirmar_id + '">' + txt + '</div>').dialog({
		bgiframe: true,
		modal: true,
		title: "Informaci&oacute;n",
		resizable: false,
		buttons: {
			Aceptar: function() {
				switch (typeof(callback_ok)) {
					case "function":
						callback_ok.call();
						break;
					case "string":
						eval(callback_ok);
						break;
				}
				$("#" + confirmar_id).dialog('close');
			},
			Cancelar: function() {
				switch (typeof(callback_ko)) {
					case "function":
						callback_ko.call();
						break;
					case "string":
						eval(callback_ko);
						break;
				}
				$("#" + confirmar_id).dialog('close');
			}
		},
		beforeclose: function(event, ui) {
			$("#" + confirmar_id).dialog('destroy').remove();
		}
	});
}

function muestra_fecha(){
	
	$(".fechaNot").css({opacity:.1});
	
	$(".noticia").hover(
		function () {
			var $fechaNot = $(".fechaNot",$(this));
			$fechaNot.stop(true,true).animate({opacity:1},"slow");
			//$infoNot.siblings(".infoNot").animate({opacity:"1", width:"560"},"slow");
		},
		function () {		
			var $fechaNot = $(".fechaNot",$(this));
			$fechaNot.stop(true,true).animate({opacity:.1},"slow");
		}
	);
}

function lanzarFancyBox () {
	
	$("a.fancyImg").fancybox({'titlePosition'  : 'over'});

}

function controla_tooltip () {
	var $tooltip = $(".tooltip");
	$("#subMenu a.deshabilitado").hover(
		function () {
			$tooltip.stop(true,true).remove();
			
			$tooltip = $('<div class="tooltip"><div class="ttCornerLeft"></div><div class="ttMain">' + $(this).attr("rel") + '</div><div class="ttCornerRight"></div></div>').css({display: "none"}).appendTo($(this).parent());
			
			tooltip_left	= (((($(this).offset().left + $(this).width()) - $(this).offset().left) / 2) + $(this).offset().left) - ($tooltip.width()/2);
			tooltip_top	= $(this).offset().top - 45;
			$tooltip.css({left: tooltip_left, top: tooltip_top, display: 'block', opacity: 0}).animate({opacity: 1, top: '+=10'},'slow','');
		},
		function () {
			$tooltip.animate({opacity: 0, top: '-=5'},'fast','',function () {
				$tooltip.remove();
			});
		}
	).click(function () {
		return false;
	});
}

function tabModule () {
	var $contenedorTab		= $(".tabModInfo");
	var $contenedorLinks	= $("#tabLinks");
	var $tabsInfo			= $(".tabInfo",$contenedorTab);
	//var maxHeight			= 0;
	
	$tabsInfo.map(function () {
		$("<a href='#'></a>").appendTo($contenedorLinks);
		
		//maxHeight	= ((maxHeight<$(this).height()) ? $(this).height() : maxHeight );
		
		if ($tabsInfo.index($(this))>0) {
			$(this).css('display','none');
		} else {
			$("a:eq(" + $tabsInfo.index($(this)) + ")",$contenedorLinks).toggleClass("tabAct",true);
			$contenedorTab.height($(this).height());
		}
	});
	
	//$contenedorTab.height(maxHeight);
	
	var $tabControls	= $("a",$contenedorLinks);
	
	$tabControls.click(function () {
		$tabControls.toggleClass("tabAct",false);
		$(this).toggleClass("tabAct",true);
		
		$tabsInfo.fadeOut();
		$(".tabInfo:eq(" + $tabControls.index($(this)) + ")",$contenedorTab).fadeIn();
		$contenedorTab.animate({height: $(".tabInfo:eq(" + $tabControls.index($(this)) + ")",$contenedorTab).height() + "px"})
		
		return false;
	})
};

function set_news_position () {
	$("#newsPie .newPie").each(function () {
		if ($("#newsPie .newPie").index($(this))==0) {
			$(this).css({
				top:	0,
				opacity: 1
			})
		} else {
			$(this).css({
				top:	$(this).height(),
				opacity: 0
			})
		}
	});
}
var ciclo;
function set_ciclo () {
	set_news_position ();
	ciclo = setInterval(function () {
		//alert($("#newsPie .newPie:first").length)
		var height	= $("#newsPie").height();
		$("#newsPie .newPie:eq(0)").animate({top: -height, opacity: 0},2000,'easeOutQuart',function () {
			$(this).appendTo($("#newsPie"));
		});
		$("#newsPie .newPie:eq(1)").animate({top: 0, opacity: 1},2000,'easeOutQuart',function () {
			set_news_position ();
		});
	},8000);
}
function kill_ciclo () {
	clearInterval(ciclo)
}
