var $j = jQuery.noConflict();

Imagen = {
	cut: function (imgs, callback) {
		var self1 = this;
		this.total = $j(imgs).size();
		this.actual = 0;
		$j(imgs).each(function () {
			$j(this).parent().css('position','relative');
			var self = this;
			function redim() {
				$j(self).css({
					width: "auto",
					height: "auto"
				});
				var container = $j(self).parent();
				var imgratio = self.width / self.height;
				var containerratio = container.width() / container.height();
				if (imgratio > containerratio) {
					$j(self).css({
						height: container.height(),
						width: "auto"
					});
				}
				else {
					$j(self).css({
						width: container.width(),
						height: "auto"
					});
				}
				self1.actual++;
				if (self1.actual == self1.total) {
					if (typeof callback == "function") {
						callback.call();
					}
				}
			}
			if (self.complete) {
				redim();
			}
			else {
				self.onload = function () {
					redim();
				}
			}
		});
	}
}


$j(function() { 
	$j('#slideshow').css("visibility","hidden");
	// Redimensionar
	Imagen.cut($j('img.redim'), function () {
		$j('#slideshow').css("visibility","visible");
		$j('#slideshow').cycle({ 
			fx:     'fade', 
			timeout: 4000,
			pause:1,
			speed:  1200,
			pager:  '#nav', 
			before: function() {  
				$j('#caption').html(this.alt); 
			},
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var index = $j(this).closest("li").prevAll("li").size() + 1;
				$j('ul.sidebar-gallery li a').css({'opacity':'','box-shadow':''});
				$j('ul.sidebar-gallery li:nth-child('+index+') a').css({'opacity':'.5','box-shadow':'2px 2px 5px #666'});
			}
		}); 
	});
	

}); 

/*
		Cufon.replace('h1,h2,h3,h4, .jqueryslidemenu ul:first > li > a',
					  {
		 hover: true

});
*/	
	
// If the HTML document is ready to be manipulated
$j(document).ready(function(){
	// Add the hover handler to the link
	$j("ul.social-icons li a").hover(
		function(){
			$j(this).find("img").animate({top : '-6px'}, 200);
		},
		function(){ 
			$j(this).find("img").animate({top : '0px'}, 200);
		}
	);
});

$j("input.focus:last").focus();

$j(document).ready(function(){
	$j("a[rel^='prettyPhoto']").each(function() {	
		//var $jimage = $j(this).contents("img");
		var $jimage = $j(this).find("img");
			$jhoverclass = 'hover_video';

	if($j(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) 
	$jhoverclass = 'hover_image';
		
	if ($jimage.length > 0)
	{	
		var $jhoverbg = $j("<span class='"+$jhoverclass+"'></span>").appendTo($j(this).find('span'));
		
			$j(this).bind('mouseenter', function(){
			//$jheight = $jimage.height();
			$jheight = $j(this).height();
			//$jwidth = $jimage.width();
			$jwidth = $j(this).width();
			//$jpos =  $jimage.position();		
			$jpos =  $j(this).position();
			$jhoverbg.css({height:$jheight, width:$jwidth, top:0, left:0});
		});
	}
	
});	

//$j("a[rel^='prettyPhoto']").contents("img").hover(function() {
$j("a[rel^='prettyPhoto']").find("img").hover(function() {
		$j(this).stop().animate({"opacity": "0.2"}, 400);
		},function() {
		$j(this).stop().animate({"opacity": "1"},400);
	});
});

// Para foto listado de inmuebles
$j(document).ready(function(){
	$j("a[rel^='prettyFicha']").each(function() {	
		var $jimage = $j(this).find("img");

	$jhoverclass = 'hover_image';
		
	if ($jimage.length > 0)
	{	
		var $jhoverbg = $j("<span class='"+$jhoverclass+"'></span>").appendTo($j(this).find('span'));
		
			$j(this).bind('mouseenter', function(){
			$jheight = $j(this).height();
			$jwidth = $j(this).width();		
			$jpos =  $j(this).position();
			$jhoverbg.css({height:$jheight, width:$jwidth, top:0, left:0});
		});
	}
	
});	

$j("a[rel^='prettyFicha']").find("img").hover(function() {
		$j(this).stop().animate({"opacity": "0.2"}, 400);
		},function() {
		$j(this).stop().animate({"opacity": "1"},400);
	});
});

