$(window).load(function () {
	fadePage();
	fullScreen();
	
	$(".imagecontainer img").each(function(){
		centrarImagen(this);
	});

});
$(window).resize(function() {
	fullScreen();
});

$(document).ready(function(){
	/* Form Contacto */
	$('a.botonEmail').click(function(e){
		$('#overlaywindow').fadeIn('fast');
	});
	$('#overlaywindow .close').click(function(e){
		$('#overlaywindow').fadeOut('slow');
	});
	$('#overlaywindow .overlay').click(function(e){
		$('#overlaywindow').fadeOut('slow');
	});
	
	
	if ($(".chosen-select").length > 0){
		$(".chosen-select").chosen();
	}
	
	if ($("#slideshow").length > 0){
		$('#slideshow').cycle({
			timeout: 			5000,
			containerResize: 	0,
			cleartypeNoBg: 		true,
			pager:  			'#slidenav', 
		     
		    // callback fn that creates a thumbnail to use as pager anchor 
		    pagerAnchorBuilder: function(idx, slide) { 
		        return '<li><a href="#"><img src="' + slide.childNodes[0].src + '" /></a></li>'; 
		    }
		});
	}
	if ($("#slideshowMobile").length > 0){
		$('#slideshowMobile').cycle({
			timeout: 			0,
			containerResize: 	0,
			cleartypeNoBg: 		true,
			prev:   '#slidenavMobile a.previous',
		    next:   '#slidenavMobile a.next'
		});
		
	}
	
	$(".popupLink").bind('click', function(){
		$('.popup').hide();
		$($(this).attr('href')).fadeIn('slow');
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
	
	$(".close").bind('click', function(){
		$(this).parent().fadeOut();
	});
	
	// Creating hoverscroll with fixed arrows
	if ($("#slidenav").length > 0){
	    $('#slidenav').hoverscroll({
	    	vertical: false,	// Display the list vertically or horizontally
			width:    650,		// Width of the list container
			height:   60,		// Height of the list container
			arrows:   false,		// Display direction indicator arrows or not
	        rtl:      false,     // Print images from right to left
			debug:    false     // Debug output in the firebug console
	    });
	}
});

function goTo(page) {
	var url="index.php";
	var url="";
	hu = window.location.search.substring(1);
	query = hu.replace(/page=[0-9][0-9]/g,"");
	query = query.replace(/page=[0-9]/g,"");
    url = url + "?page="+page+"&"+query;
    url = url.replace(/&&/g,'&');
	document.location.href = url;
	return false;
}

// Funcion que hace un FadeIn de la Página
function fadePage(){
	if ($(".fadePage #copy .ficha").length > 0){
		$(".fadePage #copy .ficha").fadeIn('slow');
	}
}

function centrarImagen (imagen) {
	var ancho = Math.ceil( $(imagen).outerWidth(true) / 2 );
	var alto = Math.ceil( $(imagen).outerHeight(true) / 2 );
	var cssObj = {
		      'left' : '50%',
		      'top' : '50%',
		      'margin-left' : '-' + ancho + 'px',
		      'margin-top' : '-' + alto + 'px'
		    }
	$(imagen).css({'position' : 'absolute'});
	$(imagen).animate(cssObj, 'Fast');
}

function centrarVert (imagen) {
	var alto = Math.ceil( $(imagen).height() / 2 );
	var cssObj = {
		      'top' : '50%',
		      'margin-top' : '-' + alto + 'px'
		    }
	$(imagen).css({'position' : 'absolute'});
	$(imagen).animate(cssObj, 'Fast');
}

function fullScreen (){
	window.viewport = {
	    height: function() { 
	        return $(window).height(); 
	    },
	    
	    width: function() {
	        return $(window).width();
	    },
	    
	    scrollTop: function() {
	        return $(window).scrollTop();
	    },
	    
	    scrollLeft: function() {
	        return $(window).scrollLeft();
	    }
	};
	viewportHeight = viewport.height();
	wrapperHeight = $('.wrapper').outerHeight(true);
	footerHeight = $('#footer').outerHeight(true);
	if ((wrapperHeight + footerHeight) < viewportHeight) {
		$('body').animate({height:'100%'}, 'slow');
		//$('body').css('height', '100%');
	} else {
		$('body').css('height', 'auto');
	}
}

function cerrarPopup(pid){
	   document.getElementById(pid).style.visibility = 'hidden';      
	}

