var $j = jQuery.noConflict();
$j(document).ready(function() {

	function aleatorio(inferior,superior) {
		numPosibilidades = superior - inferior;
		aleat = Math.random() * numPosibilidades;
		aleat = Math.round(aleat);
		return parseInt(inferior) + aleat;
	} 

	$j('div.nbr-gallery').each(function() {
		var num = $j(this).children('div.photo').length;
		var ini = (650 - (110 + (num-1) * 30)) / 2;

		$j(this).children('div.photo').each(function(p) {
			$j(this).css({ left: (ini+(p*30))+'px', top: aleatorio(0,20)+'px' });
		});

		var anchor = 'div.photo a[rel=' + $j(this).attr('id') + '_group]';
		$j(this).find(anchor).fancybox({
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'titlePosition'	: 'over'
		});
	});

	$j('a[rel=single]').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition'	: 'over'
	});
	
	$j('a[rel=video]').click(function() {
		$j.fancybox({
			'padding'       : 0,
			'autoScale'     : false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'easingIn'      : 'easeOutBack',
			'easingOut'     : 'easeInBack',
			'title'         : this.title,
			'width'         : 680,
			'height'        : 495,
			'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'          : 'swf',
			'swf'           : {
				'wmode'            : 'transparent',
				'allowfullscreen'  : 'true'
			}
		});
		return false;
	});
	
	$j('div.photo').hover(function() {
		var num = $j(this).parent().children('div.photo').length;
		var ini = (650 - (110 + (num-1) * 30)) / 2;
		var pos = $j(this).parent().children('div.photo').index(this);

		$j(this).parent().children('div.photo').each(function(p) {
			if ( p<pos ) $j(this).stop().animate({ left: (ini-30+(p*30))+'px', top: aleatorio(0,20)+'px' });
			else if ( p>pos ) $j(this).stop().animate({ left: (ini+70+(p*30))+'px', top: aleatorio(0,20)+'px' });
			else $j(this).stop().animate({ left: (ini+(p*30))+'px', top: '-50px' });
		});

	}, function() {
		var num = $j(this).parent().children('div.photo').length;
		var ini = (650 - (110 + (num-1) * 30)) / 2;

		$j(this).parent().children('div.photo').each(function(p) {
			$j(this).stop().animate({ left: (ini+(p*30))+'px', top: aleatorio(0,20)+'px' });
		});

	});

	$j('.cmninp, .cmntxt').focus(function(){
		$j(this).addClass('cmnfocus').removeClass('cmnblur');
	}).blur(function(){
		$j(this).removeClass('cmnfocus').addClass('cmnblur');
	});

});
