jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};
jQuery(document).ready(function($){
	$('html').removeClass('no-js');
	
	//Head slider
	
	$('#topSlider').rdy(function(){
		
		var wrap = $('.sliderGallery'),
				autoSlide,
				autoSlideTime = 8000;
		
		$('li:first',wrap).css('display','block');
		
		$('.arrowNav').live('click',function(){
			var t = $(this);
			
			if (t.hasClass('right')) {
				if ($('li:last',wrap).is(':visible')) {
					$('li:first',wrap).fadeIn(900).siblings().fadeOut();
				} else {
					$('li:visible',wrap).next().fadeIn(900).siblings().fadeOut();
				}
			} else {
				$('li:visible',wrap).prev().fadeIn(900).siblings().fadeOut();
			}
			
			return false;		
		});
		function autoplay() {
			$('#topSlider .arrowNav.right').click();
		};
		
		autoSlide = window.setInterval(function(){autoplay();},autoSlideTime);
		$('#topSlider').bind('mouseenter mouseleave', function(e){
			window.clearInterval(autoSlide);
			if(e.type=='mouseleave'){
				autoSlide = window.setInterval(function(){autoplay();},autoSlideTime);
			}
		});
	});//topSlider
	
	
	//Fade gallery
	
	$('.fadeGallery').each(function(){
		var main = $(this),
				thumbs = $('.thumbNavHr',main),
				thumbH = 153,
				maxPos = main.hasClass('insideGallery') ? thumbH*($('li',thumbs).length-2) : thumbH*($('li',thumbs).length-3),
				curPos = parseInt(thumbs.css('marginTop')) || 0;				
				
		if (main.hasClass('insideGallery')) {
			$('.thumbsWrap').css('height',thumbH*2+35);
		} else {
			$('.thumbsWrap').css('height',thumbH*3+37);
		}

		$('a',thumbs).live('click',function(){
			var t = $(this),
					index = t.parent().index();
			$('.bigImageWrap li',main).eq(index).fadeIn().siblings().fadeOut();
			
			$(this).parent().addClass('s').siblings().removeClass();
			
			return false;
		});
		
		$('a:first',thumbs).click();
		
		//Thumbs nav
		$('.thumbNavDown',main).live('click',function(){
			var t = $(this);
					
			if (thumbs.is(':animated')) {return false;};		
				
			curPos = parseInt(thumbs.css('marginTop'));
			thumbs.animate({
				marginTop : -curPos!=maxPos ? '-=' + thumbH : 0
			});
			
			 
					
			return false;
		});
		
	});//fadeGallery
	
	
	
	//Inline gallery
	
	$('.inlineGallery').each(function(){
		var wrap = $(this).find('.slidesContainer'),
				liw = $(this).hasClass('homePage') ? 294 : 460,
				lis = $('li',wrap).size(),
				maxPos = $(this).hasClass('homePage') ? liw * (lis - 3) : liw * (lis - 2),
				curPos = parseInt(wrap.css('marginLeft'), 10) || 0;
				
		wrap.css('width',lis*liw);
						
		$(this).delegate('.navArrow', 'click', function(){
			if(wrap.is(':animated')) { return false; }
			var t = $(this),
					curPos = parseInt(wrap.css('marginLeft'), 10) || 0;
			wrap.animate({
				marginLeft:t.hasClass('leftRedArrow') ? (curPos < 0 ? '+=' + liw : 0) : (curPos > -maxPos ? '-='+liw : 0)
			});
			
			return false;

		});		
		
	});
	
});
