// JavaScript Document

$(document).ready(function() {
	
if ($('body#product').length > 0) {
	
	var defaultsection = $('.inner section:eq(0)').attr('id');
	$('.innersection').hide();
	$('#' + defaultsection).show();
		
	//--------------------------------------------------------------- Tabs with hash tags ------//		
	
	var defaultsection = $('.inner section:eq(0)').attr('id');
		
	if(location.hash == "") loc = '#' + defaultsection;
	else loc = location.hash.replace('/', '');
		
	if($(loc).length == 0) loc = '#' + defaultsection;
	
	$('.inner section').hide();
	$(loc).show();
		
	$('#sidebar li').removeClass('current');
	$('#sidebar li').each(function() {
		var navurl = $(this).find('a').eq(0).attr('href').replace('#', '#/');
		if(loc.replace('#', '#/') == navurl) $(this).addClass('current');
	});
		
	var doctitle = document.title;
	document.title = $('#sidebar li.current').text() + ' | ' + doctitle;
		
	$(window).hashchange(function(){
			
		if(location.hash == "") loc = '#' + defaultsection;
		else loc = location.hash.replace('/', '');	
					
		if($(loc).length == 0) loc = '#' + defaultsection;
					
		$('.inner section').hide();
		$(loc).show();
		
		$('#sidebar li').removeClass('current');
		$('#sidebar li').each(function() {
			var navurl = $(this).find('a').eq(0).attr('href').replace('#', '#/');
			if(loc.replace('#', '#/') == navurl) $(this).addClass('current');
		});
			
		document.title = $('#sidebar li.current').text() + ' | ' + doctitle;
		
		return false;
	});
		
	$('#sidebar li a').live('click', function() {
		if($(this).attr('href').charAt(0) == '#') {
			var url = $(this).attr('href').replace('#', '#/');
			location.hash = url;
			return false;
		}
	});
	
	//--------------------------------------------------------------- Image Gallery ------//
	
	//Create span elements inside the thumbnail images
	$('#overview div.thumbnails a').prepend('<span></span>');
	
	//Creating div and image for image that is loaded and then revealed
	$('#overview figure').prepend('<div id="product-load-container"><img id="product-load-image" src="" width="500" height="360" alt="Loading"></div>');
	
	//Click event for thumbnail images
	$('#overview div.thumbnails a').click(function(event){
		event.preventDefault();
		if ($(this).parent().not('.current').length > 0) {
			$('#overview div.thumbnails a span').hide().removeClass('loading');
			$('#overview div.thumbnails li').removeClass('current');
			$(this).parent().addClass('current');
			$(this).children('span').show();
			$(this).children('span').addClass('loading');
			$('#product-load-image').attr('src', $(this).attr('href'));
		}
	});
	
	//Execute once the image has loaded
	$('#product-load-image').load(function(){
		$('#overview div.thumbnails a span.loading').fadeOut(350, function() {
    		$('#overview div.thumbnails a span.loading').removeClass('loading');
  		});
		$('#product-load-container').animate({
    		width: '565'
  		}, 350, function() {
    		$('#product-image').attr('src', $('#product-load-image').attr('src'));
			$('#product-load-container').width(0);
  		});
	});

}
});
