/*
 * jQuery WG Rotate plug-in 1.0
 * Copyright (c) 2008 Roberto Lee (webgenerator.nl)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2011-05-25
 * Rev: 9
 *
 *
 * REQUIREMENTS:
 * - Cycle plugin
 * - WG_SizeElemAsElem function
 *-  wg_img_li_collect plugin
 *
 *
 *
 *
 */
 (function($){
     $.fn.extend({
         WG_Rotate: function(options) {
	        var defaults = {
				type: 'plugin_cycle', 							//plugin_cycle,random  //cycling needs the cycling plugin http://malsup.com/jquery/cycle/
				plugin_cycle_config: {},
				foreground_div: '.txt', //put div in the foreground. So images are "pushed" to the background.
				foreground_div_zindex: '20',
				content_align: 'center',
				cycle_cleartype_no_bg: false,
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);

			var css_div_foreground = {
					'overflow' : 'hidden',
					'position': 'absolute',
					'z-index': options.foreground_div_zindex,
					'left': '0px',
					'top':'0px'
				};

            return $(this).each(function(idx) {// Don't break the chain
            	var htmlelem = $(this);
				if(htmlelem.is('*'))
				{
					var hastxtDiv = false;
	            	if(options.foreground_div != "")
	            	{
	            		if(htmlelem.find(options.foreground_div).is('*'))
						{
		            		var txt = htmlelem.find(options.foreground_div).html();
	           				var divForeGround = $("<div/>").css(css_div_foreground).html(txt).WG_SizeElemAsElem(this);
	           				hastxtDiv = true;
						}
	            	}

					//Collect items for the rotator
					var all_available_items = $(this).WG_ImgLiCollect().find(".rot_item");
					/*
					$(all_available_items).each(function(idx) {
						$.log($(this).html());
					});
					*/
					var container_cycle_elements = $('<div/>')
														.addClass('container_cycle_elements').css('overflow','hidden')
														.WG_SizeElemAsElem(this).append(all_available_items.WG_SizeElemAsElem(this));



					switch(options.type)
					{
						case 'random':
							var rndItem = Math.ceil(Math.random() * all_available_items.length) - 1;
							htmlelem.html(all_available_items[rndItem]);
							break;
						case 'plugin_cycle':
							htmlelem.html(container_cycle_elements);

							var all_cycle_properties = {};
							var hasNavControls = false;
							if( ($(options.plugin_cycle_config.pause)).is('*') && ($(options.plugin_cycle_config.play)).is('*') && ($(options.plugin_cycle_config.next)).is('*') && ($(options.plugin_cycle_config.prev)).is('*')	)
							{
								hasNavControls = true;
							}
							$(container_cycle_elements).cycle(options.plugin_cycle_config);
							if(hasNavControls)
							{
							    $(options.plugin_cycle_config.pause + ", " + options.plugin_cycle_config.next + ", " + options.plugin_cycle_config.prev).click(function() { container_cycle_elements.cycle('pause'); return false; });
							    $(options.plugin_cycle_config.play).click(function() { container_cycle_elements.cycle('resume'); return false; });
							}
							break;
						case 'plugin_xxxx':
							htmlelem.html(container_cycle_elements);
							$(container_cycle_elements).cycle(options.pluginoptions);
							break;
					}

					//SET CSS ITEMS
					$(all_available_items).css({	//'background-color': 'transparent',//CYCLE PLUGIN SETS BG TO WHITE IN IE
										'margin': '0px',
										'padding' : '0px',
										'text-align' : options.content_align
					});

					if(hastxtDiv)
	            		htmlelem.prepend(divForeGround);

					options.cb.call();
				}
            });
		}
    });
})(jQuery);

(function($) {
   $.fn.WG_SizeElemAsElem = function(elem2imitate) {
		return this.each(function() {
			$(this).css({
						'height': $(elem2imitate).height() + 'px',
						'width': $(elem2imitate).width() + 'px'
					})
		});
   }
})(jQuery);
