jQuery.fn.exists = function(){return jQuery(this).length>0;}
$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

var cache = [];
// Arguments are image paths relative to the current page.
function preLoadImages() {
	var args_len = arguments.length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
		cacheImage.src = arguments[i];
		cache.push(cacheImage);
	}
}

$( document ).ready( function() {
	$("a[rel='pop-up']").click(function () {
			var features = "height=550,width=470,scrollTo,resizable=1,scrollbars=1,location=0";
			newwindow=window.open(this.href, 'Popup', features);
			return false;
	});
	
	if ($('#flashMessage').exists()) {
		$('#flashMessage').show('slide', {direction: 'up'} );
		$("#flashMessage").pause(5000).fadeOut('slow');
	}

	if ($('#flashMessage.error-message').exists()) {
		$('#login_header').fadeIn();
	}
	
	/*** MENU ****/
	$('#menu > li').bind('mouseover', menu_open);
	$('#menu > li').bind('mouseout',  menu_timer);

	$("LI.login").click(function () {
		if($("#login_header").css("display") == 'none'){
			$("#login_header").fadeIn();
		}else{
			$("#login_header").fadeOut();
		}
		return false;
	});

	
} );

/*****************
	MENU
*****************/
var timeout		= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var aactivo		= 0;

function menu_open(){
	menu_canceltimer();
	menu_close();
	ddmenuitem = $(this).find('UL').css('visibility', 'visible');
	aactivo = $(this).find('A.tienesubmenu');
	aactivo.addClass('activo');
}
function menu_close(){
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
	if(aactivo) aactivo.removeClass('activo');
}
function menu_timer(){
	closetimer = window.setTimeout(menu_close, timeout);
}
function menu_canceltimer(){
	if(closetimer){  window.clearTimeout(closetimer);
		closetimer = null;
	}
}
document.onclick = menu_close;



/* Smooth scrolling */
$(function(){
    $('a[href*=#]').click(function() {
    
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
        
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
            }
        }
    });
});

