/***********************************************
 * Morretti (www.motorcycleseats.co.uk)
 *
 * Author: John Leighton
 *
 * Copyright (c)2010 NT-Designs
 * http://www.nt-designs.co.uk
 *
 * Version: 1.0
 * script.js
 **********************************************/

// **************************************************************
// Function:    areYouSure()
// Description: confirm user action
// Inputs:      none
// Outputs:     true or false
// **************************************************************
function areYouSure()
{
  return confirm("Are you sure you want to delete this item?")
}

// **************************************************************
// Function:    popupWindow( URL )
// Description: pop up a new browser window
// Inputs:      URL - the page to open in the new window
// Outputs:     none
// **************************************************************
function popupWindow( URL )
{
  day = new Date();
  id = day.getTime();

  eval( "manageimages = window.open( URL, 'manageimages', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=500,left=100,top=100' );" );
}

// **************************************************************
// Function:    ButtonRollOver( styleChange, item )
// Description: form button rollover
// Inputs:      styleChange
//              item
// Outputs:     none
// **************************************************************
function ButtonRollOver( styleChange, item )
{
  item.className = styleChange;
}

// **************************************************************
// Function:    jQuery Slideshow
// Description: form button rollover
// Inputs:      styleChange
// Outputs:     none
// **************************************************************
$(document).ready(function(){
  slideShow();
});

function slideShow() {

	//add a class the the first element
	$('#slides li:first-child').addClass('active');

	//Set the opacity of all images to 0
	$('.slide li').css({opacity: 0.0});

	//Get the first image and display it (set it to full opacity)
	$('.slide li:first-child').css({opacity: 1.0});

	//Get the caption of the first image from longdesc attribute and display it
	$('#slides .caption h2').html($('.slide li:first-child').find('img').attr('title'));
	$('#slides .caption p').html($('.slide li:first-child').find('img').attr('longdesc'));

	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',5000);

}

function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('.slide li.active')?  $('.slide li.active') : $('.slide li:first-child'));

	//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'))? $('.slide li:first-child') :current.next()) : $('.slide li:first-child'));

	//Get next image caption
	var caption_title = next.find('img').attr('title');
	var caption_description = next.find('img').attr('longdesc');

	//Set the fade in effect for the next image, show class has higher z-index
	current.addClass('last-active');
	next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function(){
		current.animate({opacity: 0.0}, 1000).removeClass('active last-active');
		//$('.caption').animate({opacity: 1.0}, 1000);
		$('#slides .caption h2').html(caption_title);
		$('#slides .caption p').html(caption_description);
	});
}


