/**
*    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(
		{
			timeout: 200
		} , settings);

		var parent = $(this);
		var images = new Array;
		var currentImageNr = 0;
		var preloadImage = new Image();

		//get all images
		$(".band li a", parent).each(function(i)
		{
			images.push($(this).attr('href'));
		});
	
		
		
		
		//load first image
		preloadImage.src = images[currentImageNr];
		
			

			$(preloadImage).bind('load',function ()
			{
				
				if(firstOff != true){
					$("#myContent").remove();
				$('.nextImg').css('background-image','url(' + images[currentImageNr] + ')').
				width(650).
				height(353).
				css('opacity','0');
				
				
				$('.currentImg')
				.animate({width:650,height:353},settings.timeout,
				function(e) 
				{
					$('.currentImg').css('background-image','url(' + images[currentImageNr] + ')');
					$('.nextImg').css('opacity','1');
					
				});
				$('.nextImg').animate({opacity:'1'},settings.timeout);
				}else{
				firstOff=false;
				}
			
			});			
			
		

		
		function loadImage()
		{	
			
	
				preloadImage.src = images[currentImageNr];
				var current = $('ul.band li').eq(currentImageNr);
				var band = $('.band');
		

			
			//band.animate({marginLeft:( - current.position().left + band.offset().left) + "px"});
		}
		

		
		$(".previous").click(function(e)
		{
			
			currentImageNr <= 0 ? 0 : currentImageNr--;
			loadImage();
		});
		
		$(".next,.nextImg").click(function(e)
		{
			
			currentImageNr >= images.length - 1 ? images.length - 1 : currentImageNr++;
			loadImage();
		});	
		
		$("div ul.band li").click(function(e)
		{
			
			turnOff($.currentOn);
			turnOn($(this,parent).attr("id"));
			e.preventDefault();
			currentImageNr = $('li',this.parentNode).index(this);
			$.myHyperLink= $(this).children(':first-child').attr("class");
			$("#main_image_link").attr("href",$.myHyperLink);
			loadImage();
			
		});
		

				
	});
};

