/**
*    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 
*	 jQuery(".jmorph").jmorphGallery();
*/
jQuery.fn.jmorphGallery = function(settings) 
{
	return this.each( function()//do it for each matched element
	{
		
		settings = jQuery.extend(//provide default settings
		{
			timeout: 1000
		} , settings);

		var parent = jQuery(this);
		var images = new Array;
		var captions = new Array;
		var currentImageNr = 0;
		var preloadImage = new Image();
			
		
		//get all images
		jQuery(".band li a", parent).each(function(i)
		{
			images.push(jQuery(this).attr('href'));
		});
		
		//get all titles
		jQuery(".band li a", parent).each(function(i)
		{
			captions.push(jQuery(this).attr("title"));
		});

		//load first image
		//var myStringPre = images[currentImageNr];
		//var mySplitResultPre = myStringPre.split("?");
		preloadImage.src = images[currentImageNr]

		jQuery('.caption').html(captions[currentImageNr]).animate(settings.timeout);
		jQuery(preloadImage).bind('load',function ()
		{
			//var myString = images[currentImageNr];
			//var mySplitResult = myString.split("?");
			
			jQuery('.caption').hide(100);


			jQuery('.nextImg',parent).css('background-image','url(' + images[currentImageNr] + ')').
			width(preloadImage.width).
			height(preloadImage.height).
			css('opacity','0');

			jQuery('.imageContainer',parent).animate({marginLeft:320-(preloadImage.width/2) + "px"}),settings.timeout;
			jQuery('.currentImg',parent)
			.animate({width:preloadImage.width,height:preloadImage.height},settings.timeout,
			function(e) 
			{
				//sjQuery('.imageContainer',parent).css('margin-left',320-(preloadImage.width/2));
				jQuery('.caption').html(captions[currentImageNr]);//.animate(settings.timeout);
				jQuery('.currentImg',parent).css('background-image','url(' + images[currentImageNr] + ')');
				
				jQuery('.caption').show(300);
				
				jQuery('.nextImg',parent).css('opacity','1');
			});
			jQuery('.nextImg',parent).animate({opacity:'1'},settings.timeout);
		});

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


