/*
 * 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-04-02
 * Rev: 3
 */
 (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
				cycle_speed: 1500,
				cycle_timeout: 4000,
				cycle_random: 1,
				content_align: 'center',
				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({
														      			'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':
							$(html_elem).html(container_cycle_elements.cycle({
																			    fx: 		options.cycle_fx,
																			    speed:     	options.cycle_speed,
																			    timeout: 	options.cycle_timeout,
																			    random:  	options.cycle_random
																			}));
							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);