
/*
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,links,options) {
     this.each(function() {
       //$.rotation(this,"#rotation1",images,options,0,0);
	   $.rotation(cont,cont1,images,links,options,0,0);
     });
   };
 
$.rotation = function(container,container1,images,links,options,current,last) {
        var settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          3000
        };
        if (options)
            $.extend(settings, options);

	var containerWidth = $(container).width();
	var containerHeight = $(container).height();
	var	atarget = "_blank";
if (images.length <= 1)
{
	if (images.length == 1)
		if (links[0] == undefined || links[0] == "")
			$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;'><img src='"+ images[0] + "' alt='' /></td></tr></table>")
		else
			$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;'><a href='" + links[0] + "' target='" + atarget + "'><img src='"+ images[0] + "' alt='' /></a></td></tr></table>");
	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() {
				if (links[last] == undefined || links[last] == "")
					$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='display:table-cell;vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;margin:0px;'><img src='"+ images[last] + "' alt='' /></td></tr></table>");	
				else
					$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='display:table-cell;vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;margin:0px;'><a href='"+ links[last] +"' style = 'width:"+containerWidth+"px;height:"+containerHeight+";'><img src='"+ images[last] + "' alt='' /></a></td></tr></table>");	
				return	        setTimeout((function() {
            $.rotation(container1,container,images,links,options,last,last);
        }), settings.timeout/2);
				//})1000);
				//$(container1).css('opacity','1');


			}
			else if (settings.type == "sequence")
			{
			current = 0;
			if (last == undefined)
			last=0;
			else
			last = last+1;
			if (links[last] == undefined || links[last] == "")
				$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;margin:0px;'><img src='"+ images[last] + "' alt='' /></td></tr></table>");
			else
				$(container1).html("<table style='border-collapse:collapse;height:"+containerHeight+"px;padding:0px;margin:0px;'><tr style='height:"+containerHeight+"px;'><td style='vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;border:none;padding:0px;margin:0px;'><a href='"+ links[last] +"' style = 'vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px';'><img src='"+ images[last] + "' alt='' /></a></td></tr></table>");
				return       setTimeout((function() {
            $.rotation(container1,container,images,links,options,last,last);
        }), settings.timeout/2);
			} 
	}  
	
	/* 			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)
		//alert(links[last]);
 			$(container1).html("");
		/* $(container1).append($('<a>')).attr('href',links[last]).append($('<img />')
		.attr('src', images[last])
		); */
		//$(link) = "";
		//$("link") = $(container1).append("<a href='"+links[last]+"'></a>");
		if (links[last] == undefined || links[last] == "")
		{
		table = $("<table></table>").attr("style","border-collapse:collapse;overflow:hidden;padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<tr></tr>").attr("style","padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<td></td>").attr("style","vertical-align:middle;text-align:center;padding:0px;margin:0px;border:none;height:"+containerHeight+"px;width:"+containerWidth+"px;")
		.append($('<img />').attr('src', images[last]))));
		
		}
		else
		{
		table = $("<table></table>").attr("style","border-collapse:collapse;overflow:hidden;padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<tr></tr>").attr("style","padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<td></td>").attr("style","vertical-align:middle;text-align:center;padding:0px;margin:0px;border:none;height:"+containerHeight+"px;width:"+containerWidth+"px;")
		.append($("<a></a>").attr("href",links[last]).attr("target",atarget).append($('<img />').attr('src', images[last])))));
		}
/* 		link = $("<a>").attr("href",links[last]).attr("target","_blank").attr("style","display:table-cell;vertical-align:middle;text-align:center;width:"+containerWidth+"px;height:"+containerHeight+"px;");
		link.append($('<img />')
		.attr('src', images[last])); */

		$(container1).html("");
		
		$(container1).append(table);
/* 		link = $(container1);
		link.append($('<img />')
		.attr('src', images[last]));
		$(container1).html(""); */
		//$('<img />').attr('src',images[last]).appendTo($('<a>').attr('href',links[last])).appendTo($(container1));
		//);
		//.load(function(){
        //$(container1).append( $(this) );
		//});
		 
		//$(container1).html("<img src='"+ images[last] + "' alt='' />");
		$(container1).css('opacity','1');
			       return setTimeout((function() {
            $.rotation(container1,container,images,links,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("");
			
		if (links[last] == undefined || links[last] == "")
		{
		table = $("<table></table>").attr("style","border-collapse:collapse;overflow:hidden;padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<tr></tr>").attr("style","padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<td></td>").attr("style","vertical-align:middle;text-align:center;padding:0px;margin:0px;border:none;height:"+containerHeight+"px;width:"+containerWidth+"px;")
		.append($('<img />').attr('src', images[last]))));
		
		}
		else
		{
		table = $("<table></table>").attr("style","border-collapse:collapse;overflow:hidden;padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<tr></tr>").attr("style","padding:0px;margin:0px;height:"+containerHeight+"px;width:"+containerWidth+"px;border:none;").append($("<td></td>").attr("style","vertical-align:middle;text-align:center;padding:0px;margin:0px;border:none;height:"+containerHeight+"px;width:"+containerWidth+"px;")
		.append($("<a></a>").attr("href",links[last]).attr("target",atarget).append($('<img />').attr('src', images[last])))));
		}
			
/* 		link.append($('<img />')
		.attr('src', images[last])); */	
		/* alert($(container).height());	 */
		//tr = $("tr");
		//td = $("td").attr("style","vertical-align:middle;text-align:center;");
		//link = $("<a>").attr("href",links[last]).attr("target","_blank");//.attr("style","display:table-cell;vertical-align:middle;text-align:center;width:inherit;height:inherit;");
		//img = $('<img />').attr('src', images[last]);
		//table.append(link);
/* 		 */
		$(container1).html("");
		$(container1).append(table);
/* 		table.append(tr);
		tr = tr.append(td);
		td = td.append(link);
		link = link.append(img); */
		
		
		
		
		
		//table.append($("tr").append(td.append(link.append(img))))
		//alert(width);

/* 		$(container1).append($('<a>').attr('href',links[last])).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,links,options,last,last);
        }), settings.timeout);
		});
		}),100);
  
	}


 }
 
 })(jQuery);

