var menu,links,clocation,clocation_interval,parseLinks,twitter,disfrutar;

function empty(v)
{
	var p = /^[\s]*$/i;
	return p.test(v);
}

function isMail(v)
{
	var p = /^[a-z\.\_\-\+0-9]+@[a-z\.\_\-\+0-9]+\.[a-z]{2,4}$/i;
	return p.test(v);
}

$.fn.disableSelection = function()
{
    $(this).attr('unselectable', 'on').css('-moz-user-select', 'none').each(function()
	{ 
    	this.onselectstart = function() { return false; };
	});
};

function scrollDown(d)
{
	var scUP=null;
	
	function sc()
	{
		$(d).scrollTop($(d).scrollTop()+3);
	}
	
	scUP = setInterval(sc,40);
	$(this).mouseout(function(){clearInterval(scUP);scUP=null;});
}

function scrollUp(d)
{
	var scDOWN=null;
	
	function sc()
	{
		$(d).scrollTop($(d).scrollTop()-3);
	}

	scDOWN = setInterval(sc,40);
	$(this).mouseout(function(){clearInterval(scDOWN);scDOWN=null;});
}

function CLASS_Twitter()
{
	var interval;
	
	function next()
	{
		$('.tweets .container').animate({top:-60},{duration:800,complete:fix});
	}
	
	function fix()
	{
		var first = $('.tweets .container p').eq(0).clone();
		$('.tweets .container p').eq(0).remove();
		$('.tweets .container').css({top:0});
		$('.tweets .container').append(first);
	}
	
	//interval = setInterval(next,10000);
}

function CLASS_Disfrutar()
{
	function over()
	{
		$(this).find('img').stop(true,true).fadeTo(200,1);
	}
	
	function out()
	{
		$(this).find('img').stop(true,true).fadeTo(200,0.9);
	}
	$('div.disfrutar div.platos a img').fadeTo(0,0.9);
	$('div.disfrutar div.platos a').mouseenter(over).mouseleave(out);
}

$(document).ready(function()
{
	parseLinks =  new (function()
	{
		function AJAXload()
		{
			return;
			if (!$(this).attr('target'))
			{
				var to = '#!'+$(this).attr('href');
				window.location = to;
				clocation = window.location.toString();
				menu.autoMarkItem();
				return false;
			}
		}
		
		function refresh()
		{
			$('#content').find('a').click(AJAXload);
		}
		
		refresh();
		this.refresh = refresh;
	})();
	
	/* Menu class */
	menu = new (function()
	{
		function over()
		{
			$(this).find('div.bg').stop().animate({'top':0});
		}
		
		function out()
		{
			if (!$(this).parent('li').hasClass('on'))
				$(this).find('div.bg').stop().animate({'top':-38});
		}
		
		function mark(o)
		{
			if ($('#menu li.on').length && !$(o).parent('li').hasClass('on'))
				$('#menu li.on a div.bg').animate({'top':-38});
			
			$('#menu li').removeClass('on');
			$(o).parent().addClass('on').find('div.bg').stop().animate({'top':0});
		}
		
		function pick()
		{
			var location = window.location.toString();
			clocation = location;
			
			mark(this);
			
			/*window.location = '#!'+$(this).attr('href');
			return false;*/
		}
		
		function autoMarkItem()
		{
			var location = window.location.toString().replace(/^http[s]?\:\/\/[^\/]+/i,'');
			
			if (location.indexOf('#!') < 0)
				location = '#!'+location;
			
			var section = location.split('#!')[1],a = $('#menu li a');

			if (a.length < 1)
				return;
			
			for (var i = 0; i < a.length; i++)
			{
				if ((section.length == 1 && $(a[i]).attr('href') == section) || (section.length > 1 && $(a[i]).attr('href').length > 1 && section.indexOf($(a[i]).attr('href')) == 0)/* || (section.length > 1 && $(a[i]).attr('href').length > 1 && $(a[i]).attr('href').indexOf(section) == 0)*/)
				{
					mark($(a[i]));
					return;
				}
			}
			
			$('#menu li.on a div.bg').animate({'top':-38});
			$('#menu li').removeClass('on');
		}
		
		function parseLocation()
		{
			var location = window.location.toString();

			if (clocation == location)
				return;
			
			clocation = location;
						
			autoMarkItem();
		}
				
		$('#menu a').click(pick).mouseenter(over).mouseleave(out).prepend('<div class="bg"></div>');
		parseLocation();
		
		this.parseLocation = parseLocation;
		this.autoMarkItem = autoMarkItem;
	})();
	
	//twitter = new CLASS_Twitter();
	disfrutar = new CLASS_Disfrutar();
	
	clocation_interval = setInterval(menu.parseLocation,300);
				
	if ($("#heading img").length > 1){
		$('#heading').slidy({ animation:	'fade', pause:		true, width: 760, height: 299, speed: 1000, time: 4000 });
	}
	loadScrollForText();
	
});

function loadScrollForText(){
	if ($(".scroll")){	
		$(".scroll").each(function(index){
			textObject = $(this).children(".up").attr("onmouseover")+"";
			textObject = textObject.replace("function onmouseover(event)","");
			textObject = textObject.replace("function onmouseover()","");
			textObject = textObject.replace("{","");
			textObject = textObject.replace("scrollUp(\"","");
			textObject = textObject.replace("scrollUp('","");
			textObject = textObject.replace("\");","");
			textObject = textObject.replace("');","");
			textObject = textObject.replace("}","");
			textObject = jQuery.trim(textObject);
					//alert(textObject);			
			switch (textObject){
				case "#receta-home-content":
					ancho = "260";
					break;
				case "#tips-home-content":
					ancho = "45";
					break;	
				
				case ".texto":
					ancho = "240";
					if ($(textObject).parent().attr("class").indexOf("maneras_disfrutar") >= 0){
						ancho = "273";
					} else if ($(textObject).parent().attr("class").indexOf("contacto") >= 0){
						ancho = "230";
					} else if ($(textObject).attr("id").indexOf("mini-scroll") >= 0){
						ancho = "64";
					}										
					break;	
				case ".presentacion":
					ancho = "64";
					break;
			}
			
			newHeight = $(textObject).height();
			
			if (!$(textObject).is("input")){
				if (newHeight > ancho){
					$(textObject).css({"height":ancho+"px","overflow":"hidden"});
					$(this).show();
				} else {
					$(this).hide();
				}
			}
		});			
	}	
}

window.onbeforeunload = function()
{
	if (clocation_interval)
		clearInterval(clocation_interval);
}
