
/*
parameters:
first div should be position:absolute and second position:relative if they are with other child divs in parent div in div position:relative doesnt matter
images is an array of full paths for the cms to the gallery images
options is in example below
 can be accessed 
	<script>
	$("#body").ready(function() {
//$("#rotation").rotation(images);
$.rotation("#rotation","#rotation1",images,{
//speed: 'slow',
speed: 2000,
timeout: 5000,
type: 'sequence',
animationtype: 'slide'
});
 });
</script> 
in html page
*/

(function($) {
 
   $.fn.rotation = function(cont,cont1,images,options) {
     this.each(function() {
       //$.rotation(this,"#rotation1",images,options,0,0);
	   $.rotation(cont,cont1,images,options,0,0);
     });
   };
 
$.rotation = function(container,container1,images,options,current,last) {
        var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          3000
        };
        if (options)
            $.extend(settings, options);
if (images.length <= 1)
{
	if (images.length == 1)
		$(container1).html("<img src='"+ images[0] + "' alt='' />")
	else
		$(container1).html("No images in gallery");
		
	return;
}
else if (images.length > 1)
{			
	 if ($(container1).html() == "")
	{
			if (settings.type == "randomstart" || settings.type == "random")
			{
				current = Math.floor(Math.random() * images.length);
				last = current;
				//setTimeout((function() {
				$(container1).html("<img src='"+ images[last] + "' alt='' />");	
				return	        setTimeout((function() {
            $.rotation(container,container1,images,options,last,last);
        }), settings.timeout);
				//})1000);
				//$(container1).css('opacity','1');


			}
			else if (settings.type == "sequence")
			{
			current = 0;
			if (last == undefined)
			last=0;
			else
			last = last+1;
/* 			setTimeout((function() {
			$(container1).html("<img src='"+ images[current] + "' alt='' />");
			}),1000); */
			//setTimeout(1000);
			$(container1).html("<img src='"+ images[last] + "' alt='' />");
			//alert(container1 + ' have' + $(container1).html());
 			//$(container1).hide();
/*			 	$('<img />')
    .attr('src', images[last])
    .load(function(){
        $(container1).append( $(this) );
    }); 
	if (last == 0)
	$(container1).show(); */
	//alert(last);
				return       setTimeout((function() {
            $.rotation(container1,container,images,options,last,last);
        }), settings.timeout/2);
			//$(container1).css('opacity','1');

			} 
	}  
	
	/* 			if (settings.type == "randomstart")
					settings.type = "sequence"; */
	//$(container1).html("<img src='"+ images[current] + "' alt='' />");	
	/* $(container).css('opacity','100');	 */

 	if (settings.type == "random")
	while (current == last)
		last = Math.floor(Math.random() * images.length);
	else if (settings.type == "sequence" || settings.type == "randomstart")	
		{
		last = last + 1;
		if (last >= images.length)
			last = 0;
		} 
}
  


	//alert(current);
	if (settings.animationtype == "fade")
	{
/* 	working fade
	$(document).ready(function(){
		$(container).html("<img src='"+ images[last] + "' alt='' />").ready(function(){
		$(container).css('opacity','1');
		$(container1).fadeTo(settings.speed, 0);
 		$(container).css('z-index','3');
		$(container1).css('z-index','4');  
		});
		}); */
		$(container1).fadeTo(settings.speed, 0,function(){
		$(container1).css('z-index', '3');
		$(container).css('z-index', '4');
		//if (!$.browser.msie)
 			$(container1).html("");
		$(container1).append($('<img />')
		.attr('src', images[last])
		);
		//.load(function(){
        //$(container1).append( $(this) );
		//});
		 
		//$(container1).html("<img src='"+ images[last] + "' alt='' />");
		$(container1).css('opacity','1');
			       return setTimeout((function() {
            $.rotation(container1,container,images,options,last,last);
        }), settings.timeout);
		});
		
	}
else if (settings.animationtype == "slide")	
	{	

		return setTimeout((function() {
		$(container1).slideUp(settings.speed, function(){
		$(container1).css('z-index','3');
		$(container).css('z-index','4');
		//if (!$.browser.msie)
			$(container1).html("");
		$(container1).append($('<img />')
		.attr('src', images[last])
		);
		//.load(function(){
        //$(container1).append( $(this) );
		//});
		
		
		//$(container1).html("<img src='"+ images[last] + "' alt='' />");
		$(container1).show();
		
			       return setTimeout((function() {
            $.rotation(container1,container,images,options,last,last);
        }), settings.timeout);
		});
		}),100);
  
	}


 }
 
 })(jQuery);

