/*
 * 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: 2010-01-07
 * Rev: 8
 *
 *
 * 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: {},
				/*
				cycle_fx: 'fade', 						//fade,scrollDown,scrollUp,scrollLeft,scrollRight //scrollHorz,scrollVert (use when nav controls are available)
				cycle_direction: 'right',				//left,right,up,down
				cycle_speed: 1500,
				cycle_timeout: 4000,
				cycle_random: 1,
				cycle_cleartype: false,
				nav_next:'', //EX: img#nav_next
				nav_prev:'',
				nav_play:'',
				nav_pause:'',
				nav_paging_div: '',
				nav_paging_event: 'click',
				pluginoptions : null,
				*/
				content_align: 'center',
				cycle_cleartype_no_bg: false,
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);

            return $(this).each(function(idx) {// Don't break the chain
            	var htmlelem = $(this);
				if($(this).is('*'))
				{
					//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;
							$(this).html(all_available_items[rndItem]);
							break;
						case 'plugin_cycle':
							$(this).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':
							$(this).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
					});

					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);