function delegate(o, m){ return function() { return m.call(o) } }

var selected_str = "";
var lastSize = 0;

function init()
{
	switch(selected_str)
	{
		case "Cerveza":
			$('#cervezaMenu ul li').click( function(){
				if( !$(this).hasClass('selected') )
				{
					$('#cervezaMenu ul li.selected').removeClass('selected');
					$(this).addClass('selected');
					var realPos = [1,3,5,7,2,4,6];
					$('#cervezaSlider > ul').animate({"margin-left":"-"+(realPos[$(this).index()]*824)+"px"},1000,"easeOutBack");
				}
			});
		break;
		case "Coors":
			$('#coorsMenu ul li').click( function(){
				if( !$(this).hasClass('selected') )
				{
					$('#coorsMenu ul li.selected').removeClass('selected');
					$(this).addClass('selected');
					$('#coorsSlider > ul').animate({"margin-left":"-"+($(this).index()*824)+"px"},1000,"easeOutBack");
				}
			});
		break;
	}
}
function resizer( size, animated )
{
	var ls = parseInt($("#mainContent_wrap").css("height"),10);
	if( ls != size )
	{
		lastSize = size; 
		if( animated )
		{
			$("#mainContent_wrap").animate({height:size},"fast");
		}
		else
		{
			$("#mainContent_wrap").css({height:size});
		}
	}
	return ls;
}
function getSection( _load, _data )
{
	var f = function() 
	{
		$('#ajax_content').empty();
		$('#ajax_content').load(_load, _data, function(response, status, xhr) 
		{
			processing = false;
			if (status == "error") 
			{
				alert("Por el momento no esta disponible la sección solicitada, por favor intentelo mas tarde.");
			}
			else
			{
				resizer( parseInt($('#ajax_content').css("height")), true );
				$('#ajax_content').fadeIn(100);
				
				if( hasFlash && (selected_str == "ProyectCold" )  )
				{
					init();
				}
				else
				{
					QueryLoader.clean();
					QueryLoader.selectorPreload = "#ajax_content";
					QueryLoader.init();
					$("<div></div>").appendTo(QueryLoader.overlay).addClass("Qoverpic");
					$(QueryLoader.overlay).css({ top: 0, left: 0});
					QueryLoader.onComplete(function()
					{
						init(); 
					});
				}
			}
		});
	};
	if( $('#ajax_content').html().length == 0 ) f(); else $('#ajax_content').fadeOut(100, f);	
};
function sectionsChange()
{
	getSection("/wp-content/themes/clm2/php/sections.php", {section:selected_str} );
}
function menuClick( e )
{
	if( (selected_str+"_m") != e )
	{
		if( e == "Blog_m" )
		{
			$(".header_menu ul li").removeClass('selected');
			$("#Blog_m").addClass('selected');
			window.location.hash = selected_str = "Blog";
			setSoloMusica(true);
		}
		else if( e == "Home_m" )
		{
			$(".header_menu ul li").removeClass('selected');
			$("#Home_m").addClass('selected');
			window.location.hash = selected_str = "Home";
			toggleHome( true );
		}
		else
		{
			$(".header_menu ul li").removeClass('selected');
			$("#" + e ).addClass('selected');
			window.location.hash = selected_str = e.substring(0, e.indexOf('_'));
			toggleHome( false, sectionsChange );
		}
	}
}

function triggerMenu( aux, elseAux )
{
	if( jQuery.inArray(aux, ["#Home","#Coors","#ProyectCold","#Cerveza","#Diviertete","#Radio","#Blog","#Radio2","#NFL"]) >= 0 )
	{
		$(aux + "_m").trigger("click");
		if( aux == "#NFL") menuClick("NFL_m");
	}
	else if( elseAux )
	{
		$(elseAux + "_m").trigger("click");
	}
}
function setSoloMusica( aux )
{
	$('.header_musica_btn').toggleClass('selected', aux);
	if( aux )
	{
		if( $('#mainContent_wrap').css("display") != "none" )
		{
			$('#mainContent_wrap').animate({height:0},"fast", function()
			{
				$(this).css("display","none");
			});
		}
	}
	else
	{
		if( $('#mainContent_wrap').css("display") == "none" )
		{
			$('#mainContent_wrap').css("display","block");
			$('#mainContent_wrap').animate({height:((selected_str == "Blog" || selected_str == "Home") ? 950: lastSize)},"fast");
			if(selected_str == "Blog")
			{
				toggleHome( true );
			}	
		}	
	}
}
function toggleHome( aux, f )
{
	setSoloMusica(false);
	if( aux )
	{
		if( $('#grid_wrap').css('display') == "none" )
		{
			$('#ajax_content').fadeOut(100, function(){
				$('#grid_wrap').css('display','block');
				$('#mainContent_wrap').animate({height:950},"fast");
				$('#ajax_content').css('display','none');
				$('#grid_wrap > div:first-child').fadeIn(100, function () 
				{
					if( $(this).next().length > 0 )
					{
						$(this).next().fadeIn(100, arguments.callee);
					}
					else
					{
						
					}
				});
			});
		}
	}
	else
	{
		if( $('#grid_wrap').css('display') != "none" )
		{
			$('#grid_wrap > div:last-child').fadeOut(100, function () 
			{
				if( $(this).prev().length > 0 )
				{
					$(this).prev().fadeOut(100, arguments.callee);
				}
				else
				{
					$('#grid_wrap').css('display','none');
					$('#mainContent_wrap').animate({height:600},"fast");
					//$('#mainContent_wrap').css('display','block');
					//$('#ajax_content').fadeIn('100ms');
					f();
				}
			});
		}
		else f();
	}
}

jQuery.noData = {};

$(document).ready(function()
{
	
	/** MENU **/
	$('.header_menu ul li').not("#Xled_m, #NFL_m").click(function(){ menuClick($(this).attr('id')) });
	
	$('.header_musica_btn').click(function()
	{
		setSoloMusica( !$('.header_musica_btn').hasClass('selected') );
	});
	
	triggerMenu( window.location.hash, "#Home" );
	
	$(window).hashchange( function()
	{
		triggerMenu( window.location.hash );
	});
	
});
