/***********************************************************************************
Ultimate jQuery slider
October 2009 Gilbert Pellegrom
http://www.gilbertpellegrom.co.uk
**********************************************************************************/
$(document).ready(function(){

	//find starting index based on hash
	var start = 0;
	if(window.location.hash != ''){
		if($('#product_' + window.location.hash.replace(/#/, '')).size() > 0){
			var id = window.location.hash;
			var afterCount = $(id + ' ~ img').size();
			start = ($('#cycle img').size() - afterCount) - 1;
		}
	}
			
	//set up jQuery Cycle
	$('#cycle').cycle({
		fx:'fade', 
		speed:1000,
	      timeout:0,
		pager:'#nav', //populate the bullet navigation
		next:'#next-arrow', //next arrow link
		before:onBefore, //our onBefore callback
		startingSlide:start //set the starting slide
	});
		
	function onBefore(){
		//don't do if initial load
		if($('#content div:visible').size() > 0){
			var img = this;
			//Dynamically set window title. Not SEO friendly.
			$('#content').animate({opacity:0}, 500);
			$('#content div:visible').slideUp(500, function(){
				$('#content #product_' + img.id).slideDown(500);
				$('#content').animate({opacity:1}, 500);
			});
		}
	}
	
	//initial load
	var img = $('#cycle img:eq('+ start +')');
	//Dynamically set window title. Not SEO friendly.
	
	$('#cycle').animate({opacity:1}, 800, '', function(){
		$('#nav').animate({opacity:1}, 500);
	});
	
	$('#content div:eq('+ start +')').slideDown(500, function(){
		$('#content').animate({opacity:1}, 500, '', function(){
			$('#footer').animate({opacity:1}, 500);
		});
	});
	
	//next arrow fade
	$('#feature').hover(function(){
		if(jQuery.support.opacity){
			$('#next-arrow', this).stop().animate({opacity:1}, 500);
		}
	}, function(){
		if(jQuery.support.opacity){
			$('#next-arrow', this).stop().animate({opacity:0}, 500);
		}
	});
	
	//next arrow hover
	$('#next-arrow').hover(function(){
		if(jQuery.support.opacity){
			$('span', this).stop().animate({opacity:1}, 500);
		}
	}, function(){
		if(jQuery.support.opacity){
			$('span', this).stop().animate({opacity:0}, 500);
		}
	});
		
	//IE compatibility
	if(!jQuery.support.opacity){
		$('#next-arrow span').css('opacity', 0);
	}
});