/**
*    vws_slideshow adapted from:
*
*    Morph gallery for jQuery 
*    Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com  
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with this program.  If not, see http://www.gnu.org/licenses/
*    
*    Examples 
*	 $(".jmorph").jmorphGallery();
*/

jQuery.fn.jmorphGallery = function(settings) 
{
	return this.each( function()//do it for each matched element
	{
		settings=jQuery.extend(//provide default settings
		{
			firstTimeout: 1000,	// First image appears instantly ** TAKE OUT FOR NOW AS STILL DELAY ON IMAGE APPEARING - NEED TO PRELOAD!!
			timeout: 1000,		// Subsequent images fade on
			interval: 6000
		},settings);

		var parent = $(this);
		var images = new Array;
var hrefs = new Array;
		var currentImageNr = 0;
		var preloadImage = new Image();

		var timeout=settings.firstTimeout;

		//get all images
//var urlPrefix='http://localhost/wassp/';
var urlPrefix='/';
//images.push('/wassp/wp-content/themes/hybrid-news/images/home-page/news2-photo.jpg');
//images.push('/wassp/wp-content/themes/hybrid-news/images/home-page/volunteers-photo.jpg');
//images.push('/wassp/wp-content/themes/hybrid-news/images/home-page/competition-photo.jpg');
var date=new Date();
var month=date.getMonth();
var date=date.getDate();
var isXmasSeason=(month==11 && date>=20) || (month==0 && date<=10);
var doSaveOurSSPs=false;
var doBusinessGames=true;
if (doBusinessGames)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/business-games.png');
if (doSaveOurSSPs)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/save-our-school-sport.jpg');
if (isXmasSeason)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/merry-xmas.jpg');
/****
images.push(urlPrefix+'wp-content/uploads/vws/pages/home/news2-photo.jpg');
if (doSaveOurSSPs)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/save-our-school-sport.jpg');
if (isXmasSeason)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/merry-xmas.jpg');
images.push(urlPrefix+'wp-content/uploads/vws/pages/home/volunteers-photo.jpg');
if (doSaveOurSSPs)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/save-our-school-sport.jpg');
if (isXmasSeason)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/merry-xmas.jpg');
images.push(urlPrefix+'wp-content/uploads/vws/pages/home/competition-photo.jpg');
****/
if (doBusinessGames)
	images.push(urlPrefix+'wp-content/uploads/vws/pages/home/business-games.png');

/*		$(".band li a", parent).each(function(i)
		{
// FF seems to be reading these in a different order?
			images.push($(this).attr('href'));
		});
*/
		//get all image links
//hrefs.push('/wassp/news');
//hrefs.push('/wassp/volunteering');
//hrefs.push('/wassp/competition');
if (doBusinessGames)
	hrefs.push(urlPrefix+'business-games');
if (doSaveOurSSPs)
	hrefs.push(urlPrefix+'save-our-ssps-campaign');
if (isXmasSeason)
	hrefs.push(urlPrefix+'news');
/****
hrefs.push(urlPrefix+'news');
if (doSaveOurSSPs)
	hrefs.push(urlPrefix+'save-our-ssps-campaign');
if (isXmasSeason)
	hrefs.push(urlPrefix+'volunteering');
hrefs.push(urlPrefix+'volunteering');
if (doSaveOurSSPs)
	hrefs.push(urlPrefix+'save-our-ssps-campaign');
if (isXmasSeason)
	hrefs.push(urlPrefix+'competition');
hrefs.push(urlPrefix+'competition');
****/
if (doBusinessGames)
	hrefs.push(urlPrefix+'business-games');
/*		$(".band li a div", parent).each(function(i)
		{
// F-ing FF doesn't read these in correctly?
			hrefs.push($(this).attr('name'));
		});
*/
		//load first image
		preloadImage.src = images[currentImageNr];
		$(preloadImage).bind('load',function ()
		{
$('.currentImgAnchor',parent).attr('href',hrefs[currentImageNr]);
			$('.nextImg',parent).css('background-image','url(' + images[currentImageNr] + ')').
			width(preloadImage.width).
			height(preloadImage.height).
			css('opacity','0');
			$('.currentImg',parent).animate({width:preloadImage.width,height:preloadImage.height},timeout,
			function(e) 
			{
//$('.currentImgAnchor',parent).attr('href',hrefs[currentImageNr]);
				$('.currentImg',parent).css('background-image','url(' + images[currentImageNr] + ')');
//$('.currentImg').attr('src',images[currentImageNr]);
//				$('.nextImg',parent).css('opacity','1');
				$('.nextImg',parent).css('opacity','0');
			});

			$('.nextImg',parent).animate({opacity:'1'},timeout);
			timeout=settings.timeout;	// Subsequent images after first use this anim interval

//			$('.nextImg',parent).animate({opacity:'0'},settings.timeout);
		});

		function loadImage()
		{	
			preloadImage.src = images[currentImageNr];
			var current = $('ul li',parent).removeClass('selected').eq(currentImageNr).addClass('selected');
//			var band = $('.band',parent);
//			band.animate({marginLeft:( - current.position().left + band.offset().left) + "px"});
		}
		
/*		$(".previous", parent).click(function(e)
		{
			currentImageNr <= 0 ? 0 : currentImageNr--;
			loadImage();
		});
		$(".next,.nextImg", parent).click(function(e)
		{
			currentImageNr >= images.length - 1 ? images.length - 1 : currentImageNr++;
			loadImage();
		});	
		
		$("div ul li", parent).click(function(e)
		{
			e.preventDefault();
			currentImageNr = $('li',this.parentNode).index(this);
			loadImage();
		});
*/

timeoutFunc=function() {
	if (++currentImageNr>=images.length)
		currentImageNr=0;
	loadImage();
var interval=settings.interval;
if (doSaveOurSSPs && (currentImageNr & 1)==0)
	interval*=2;
	window.setTimeout(timeoutFunc,interval);	// Reset timer callback
}
var interval=settings.interval;
if (doSaveOurSSPs && (currentImageNr & 1)==0)
	interval*=2;
window.setTimeout(timeoutFunc,interval);


	});
};

