/**
 * JS
 * @author Darío Ruellan <druellan@ecimtech.com>
 * @copyright ecimTech 2009
 */

$(function(){


// ** Header ***********************************

	// Seteamos el dropDown
	$("#header .dropdown").hover(function(){
		$(this).find("ul").slideDown("fast", "easeOutBounce");
	},
	function(){
		$(this).find("ul").stop('false','true').fadeOut("fast");
	});


// ** Aside ***********************************
	

	// Animacion "saltitos"
	$("#nav li.bounce").hover(
	function()
	{
		var img = $("img", this).attr("src").split(".", 1);
		$("img", this).attr("src", img[0]+"@.gif");
		$("img", this)	.css(
						{
							top: -8
						})
						.stop(true)
						.animate({ top: 0 }, "normal", "easeOutBounce");
	},
	function()
	{
		var img = $("img", this).attr("src").split("@", 1);
		$("img", this).attr("src", img[0]+".gif");
	});
	
	// Animación tipo slide
	$("#nav li.slide").hover(
	function()
	{
		var img = $("img", this).attr("src").split(".", 1);
		$("img", this).attr("src", img[0]+"@.gif");
		$(this).children("a").animate({ left: -205 }, "normal", "swing");
	},
	function()
	{
		var img = $("img", this).attr("src").split("@", 1);
		$("img", this).attr("src", img[0]+".gif");
		$(this).children("a").stop(true).animate({ left: 0 }, "normal", "easeOutBounce");
	});
	
	// Seteamos submenues
	$("#nav .dropdown ul").css("width", "0");
	$("#nav .dropdown").hover(function()
	{
		$("ul", this).show().animate(
		{
			width: 200
		},"fast", "easeOutBounce");

	}, function(){
		$("#nav .dropdown ul").stop(false,true).fadeOut("fast", function(){ $(this).css("width", "0") });
	});


// ** Textarea ***********************************


	var textarea = $("#textarea");
	
	// Activamos modales genéricos
	$.nyroModalSettings(
	{
		processHandler: function(settings)
		{
			settings.title = "";
			
			var url = settings.url;
			if (url && url.indexOf('http://www.youtube.com/watch?v=') == 0)
			{
				$.nyroModalSettings(
				{
					type: 'swf',
					height: 355,
					width: 425,
					url: url.replace(new RegExp("watch\\?v=", "i"), 'v/')
				});
			}
		}
	});
	$("a.modal", textarea).nyroModal({
		bgColor: '#0A648F',
		contentError: 'Actualmente este contenido no está disponible.<br />Por favor, intente más tarde.<br /><a href="#" class="nyroModalClose">cerrar</a>',
		gallery: 'gal',
		closeButton: '<a href="#" class="nyroModalClose" id="closeBut"></a>',
		minWidth: 300,
  		minHeight: 270,
		galleryCounts: false
	});
	
	// Iniciammos tooltips
	$(".tooltip").each(function()
	{
		var grav = "s";
		if ( $(this).hasClass("north") )
			grav = "n";
		if ( $(this).hasClass("east") )
			grav = "e";
		if ( $(this).hasClass("west") )
			grav = "w";
		if ( $(this).hasClass("south") )
			grav = "s";
		$(this).tipsy(
		{
			fade: true,
			gravity: grav
		});
	});

});


// ** Easing ************************************


jQuery.extend( jQuery.easing,
{
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
