/*
 * 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: 2009-08-04
 * Rev: 4
 */
 (function($){
     $.fn.extend({
         WG_Rotate: function(options) {
	        var defaults = {
				source_container: '',
				type: 'cycle', 							//cycle, random  //cycling needs the cycling plugin http://malsup.com/jquery/cycle/
				cycle_fx: 'fade', 						//fade,scrollDown,scrollUp,scrollLeft,scrollRight //scrollHorz,scrollVert (use when nav controls are available)
				cycle_speed: 1500,
				cycle_timeout: 4000,
				cycle_random: 1,
				cycle_cleartype: 0,
				content_align: 'center',
				nav_next:'img#nav_next',
				nav_prev:'img#nav_prev',
				nav_play:'img#nav_play',
				nav_pause:'img#nav_pause',
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);

            return $(this).each(function(idx) {// Don't break the chain
				html_elem = this;

				if((($(html_elem)).is('*')) && ($(options.source_container).is('*')))
				{
					//$.log($(options.source_container).html());
					//search for list items
					var counted_li = $(options.source_container + ' li').length;

					//find all items for rotating
					var rot_items = $(options.source_container + ' img,' + options.source_container + ' li').not('li img');

					var holder_items = $('<div/>');

					if(counted_li < 1) //only use images if there is no list
					{
						rot_items.each(function () {
							var item = $('<img/>').attr({
									      			'src': $(this).attr('src'),
									      			'border' : '0'
									      			})
						  	holder_items.append($('<div/>').addClass('rot_item').append(item)); //$.log($.trim($(this).html()));
						});
					}


					rot_items.siblings('li').each(function () {
					  	if(($.trim($(this).html())).length > 0)
					  	{
							holder_items.append($('<div/>').addClass('rot_item').append($(this).html())); //$.log($.trim($(this).html()));
						}
					});

					rot_items = holder_items.find('div.rot_item').css({
																		'background-color': 'transparent',
														      			'width': '100%',
														      			'margin': '0px',
														      			'padding' : '0px',
														      			'text-align' : options.content_align
														      			}); //$.log(holder_items.html());

					if (rot_items.length <= 1)	{$.log('Too few items ' + options.source_container + "=" + rot_items.length + " items");}


					var rndItem = Math.ceil(Math.random() * rot_items.length) - 1;
					var container_cycle_elements = $('<div/>')
														.addClass('container_cycle_elements')
														.css('overflow','hidden')
														.append(rot_items)
														.WG_SizeElemAsElem(html_elem);

					switch(options.type)
					{
						case 'cycle':
							if(
								($(options.nav_pause)).is('*') &&
								($(options.nav_play)).is('*') &&
								($(options.nav_next)).is('*') &&
								($(options.nav_prev)).is('*')
							)
							{
								options.cycle_random = 0;//DEACTIVATE RANDOM IF NAV CONTROLS AVAILABLE
							}
							$(html_elem).html(container_cycle_elements);
							$(container_cycle_elements).cycle({					fx:			options.cycle_fx,
																			    speed:		options.cycle_speed,
																			    timeout:	options.cycle_timeout,
																			    random:		options.cycle_random,
																		        next:		options.nav_next,
																		        prev:		options.nav_prev,
																		        cleartype: 	options.cycle_cleartype
																			})

						    $(options.nav_pause + ", " + options.nav_next + ", " + options.nav_prev).click(function() { container_cycle_elements.cycle('pause'); return false; });
						    $(options.nav_play).click(function() { container_cycle_elements.cycle('resume'); return false; });

							break;
						case 'random':
							$(html_elem).html(rot_items[rndItem]);
							break;

					}
					options.cb.call();
				}


            });
		}
    });
})(jQuery);


(function($) {

   $.fn.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
      return this;
   }
})(jQuery);

jQuery.log = function(msg) {
      if (typeof (console) == "undefined") {
         console = { log: function() { } };
      }
      if (console) {
         console.log("%s: %o", msg, this);
      }
};


(function($) {
   $.fn.WG_SizeElemAsElem = function(elem2imitate) {
		return this.each(function() {
			$(this).css({
						'height': $(elem2imitate).height() + 'px',
						'width': $(elem2imitate).width() + 'px'
					})
		});
   }
})(jQuery);

(function($) {
   $.fn.WG_CenterText = function() {
		return this.each(function() {
			var pos_top_txt = ($(elem).height() - $(this).height())/2;
			$(this).css({
						'text-align': 'center',
						'top': pos_top_txt + 'px',
						'position': 'absolute',
						'left': '0px',
						'width': $(this).width() + 'px'
					})
		});
   }
})(jQuery);
