jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};
jQuery(document).ready(function($){
	$('html').removeClass('no-js');
	
	//Move gradient background
	//$('body').filter(function(){ return $('.alloggi').length ? this : '' }).find('#wrap').css('backgroundPosition','left 647px');
	$('body').filter(function(){ return $('.homepage').length ? this : '' }).find('#wrap').css({'backgroundImage':'url(tl_files/themes/villabarberina/images/homeBg.jpg)','backgroundPosition':'left 310px'});
	$('body').filter(function(){ return $('.homepage').length ? this : '' }).find('#container').css({'paddingTop':'10px'});

	
	
	//Home gallery round corners on images
	$('.gallery.inlineSlider').rdy(function(){
		$('li',this).each(function(){
			var	t = $(this),
					src = $('img',t).attr('src');
			t.css('backgroundImage','url('+src+')')
		});
	});
	
	
	//Gallery
	
	function gallery(t){
		var thumbs = $('.wrapThumb',t),
				bigs = $('.wrapBig',t),
				lis = $('li',thumbs).length,
				prev = $('.prev',t),
				next = $('.next',t);
						
				
		//Fade gallery
		
		if (bigs.length>0 && thumbs.length>0) {
			$('li',thumbs).bind('click',function(){
				var thi = $(this),
						clicked = thi.index();

				$('li',bigs).eq(clicked).fadeIn().siblings().fadeOut();

				thi.addClass('s').siblings().removeClass();
				
				return false;
			});
			
			$('li:first',thumbs).click();
			
		};
		
		
		//Inline fade gallery
		
		if (t.hasClass('inlineSlider')) {
			var auotplayInterval = 4000;
			next.add(prev).bind('click',function(){
				if ($('li:animated',t).length>0) {return false;}
				try{
					console.log($('li:animated',t));
				}catch(err){}
				if ($(this).hasClass('next')) {
					if ($('li:last',t).is(':visible')) {
						$('li:first',t).fadeIn().siblings().fadeOut(); //Transition to first item
					} else {
						$('li:visible',t).next().fadeIn().siblings().fadeOut(); //Transition to next item
					}
				} else {
					$('li:visible',t).prev().fadeIn().siblings().fadeOut(); //Transition to prev item
				}
			});
			
			function autoplay(){
				next.click();
			}
			
			autoSlide = window.setInterval(function(){
				autoplay();
			},auotplayInterval);
			
			$('.inlineSlider').bind('mouseenter mouseleave', function(e){
				window.clearInterval(autoSlide);
				if(e.type=='mouseleave'){
					autoSlide = window.setInterval(function(){
						autoplay();
					},auotplayInterval);
				}
			})
		};
						
				
		//Horizontal slider	
		
		if (t.hasClass('horizontal')) {
			var	step = $('li',thumbs).outerWidth(true),
					maxPos = step*(lis-Math.round($('.thumbOverflow',t).width()/step)), 
					curPos = parseInt(thumbs.css('margin-left')) || 0;
			
			thumbs.css('width',step*lis);		//Set width for thumbs list
		
			//Sliding thumbs 
			prev.add(next).click(function(){
				var th = $(this);
				
				curPos = parseInt(thumbs.css('margin-left')) || 0; //Re-define current position
				
				if (thumbs.is(':animated')) {return false;} // Return slider to 0 at more than one click
				
				thumbs.animate({
					'marginLeft' : 
						th.hasClass('prev') ? (curPos>0 ? '+='+step : 0) //Actions on prev arrow
						: (-curPos!=maxPos ? '-='+step : 0) //Actions on next arrow
				});
				
								
				return false;
			});
						
		}
		
		if (t.hasClass('vertical')) {
			var step = $('li',thumbs).outerHeight(true),
					maxPos = step*(lis-Math.round($('.thumbOverflow',t).height()/step)), 
					curPos = parseInt(thumbs.css('margin-top')) || 0;
					
			thumbs.css('height',step*lis);
			
			next.click(function(){
				var th = $(this);

				curPos = parseInt(thumbs.css('margin-top')) || 0; //Re-define current position
				
				if (thumbs.is(':animated')) {return false;} // Return slider to 0 at more than one click
				
				thumbs.animate({
					'marginTop' : 
						th.hasClass('prev') ? (curPos>0 ? '+='+step : 0) //Actions on prev arrow
						: (-curPos!=maxPos ? '-='+step : 0) //Actions on next arrow
				});
				
				return false;
				
			});
					
		};
		
		
	}
	
	$('.gallery').each(function(){
		gallery($(this));
	})
	
	
	//Ajax popup window
	
	$('.pinsWrap a').live('click',function(){		
		var conW = $('#container').width();
				
		$.get(this.href,function(data){
			
			$('<div id="wrapPopup"><a class="icon popUpClose">close</a></div>').css('width',conW-100).appendTo('#container');
			
			$('#wrapPopup').append($(data));
			
			gallery($('.gallery'));
			
			$('#wrapPopup .popUpClose').click(function(){
				$(this).parent().remove();
			});
			
		});
		
		
		return false;
	})
	
	
});
