$(document).ready(function() {		
	
	//Execute the slideShow
	slideShow();
	
	$('#links ul li').hover(function(){highlight_link($(this));},function(){unhighlight_link($(this));});
	
	$('#links ul li.rentals').mouseover(function(){show_dropdown($(this), 'rentals');})
	$('#links ul li.rentals').mouseout(function(){start_timer('hide_dropdown("rentals")', 'rentals');})
	
	$('#links ul li.scrubbers').mouseover(function(){show_dropdown($(this), 'scrubbers');})
	$('#links ul li.scrubbers').mouseout(function(){start_timer('hide_dropdown("scrubbers")', 'scrubbers');})
	
	$('.item_name a').click(function(){change_item_info($(this))});
});

mytimer = new Array();

function slideShow() {

	//Set the opacity of all images to 0
	$('#gallery span').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery span:first').css({opacity: 1.0});
	
	
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery span.show')?  $('#gallery span.show') : $('#gallery span:first'));
	//alert(current);
	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery span:first') :current.next()) : $('#gallery span:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
		
}

function highlight_link(data){
	
	data.addClass('over');
	
	}
function unhighlight_link(data){
	data.removeClass('over');
	}

	
function change_item_info(data){
	$('.defaultImg').hide();
	id=data.attr('name');
	$.get('../xml/index/'+id,{ }, function(result){
		$('#info_box').hide();
		name=result.name;
		fname=result.fname;
		desc=result.description;
		thumb=result.thumb_path+result.fname;
		full=result.full_path+result.fname;
		base_url=result.base_url+'index.php/';
		
		
		$('#info_box h1').text(name);
		$('#info_box #description').html(desc);
		$('#info_box a').attr({ 
          href: base_url+'img_pro/index/'+fname+'/1000/1000.jpg',
          title: name
        })
        
        Shadowbox.setup("a.shadowbox");
        
		$('#info_box img').attr('src',base_url+'img_pro/index/'+fname+'/400/170');
		
		
		$('#info_box').fadeIn('slow');
		},'json'
		)
	}

function show_dropdown(data, elem){
	
	if(mytimer[elem]){
		clearTimeout(mytimer[elem])
		mytimer[elem] = null;
		}
	$('.dropdown:not(.'+elem+')').hide();
	
	
	var pos = data.offset();
	var width = data.width();
	var height = data.height();
	var dwidth = $('.dropdown.'+elem).width();
	
	var dif = (width/2)-(dwidth/2);
		
	$('.dropdown.'+elem).css({ "left": (pos.left+dif) + "px", "top": (pos.top + height+10) + "px" });
	$('.dropdown.'+elem).fadeIn(800);
	}
function hide_dropdown(elem){
	$('.dropdown.'+elem).fadeOut(800);
	
	}
function start_timer(data, elem){
	mytimer[elem] = setTimeout(data,1000);
	}
