/*
 * 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-05
 * Rev: 5
 */
 (function($){
     $.fn.extend({
         WG_Rotate: function(options) {
	        var defaults = {
				type: 'cycle', 							//cycle, random  //cycling needs the cycling plugin http://malsup.com/jquery/cycle/
				cycle_fx: 'scrollUp', 						//fade,scrollDown,scrollUp,scrollLeft,scrollRight //scrollHorz,scrollVert (use when nav controls are available)
				cycle_direction: 'up',				//left,right,up,down
				cycle_speed: 1500,
				cycle_timeout: 4000,
				cycle_random: 1,
				cycle_cleartype: false,
				cycle_cleartype_no_bg: false,
				content_align: 'center',
				nav_next:'', //EX: img#nav_next
				nav_prev:'',
				nav_play:'',
				nav_pause:'',
				cb: function(){}
	        };
	        var options = $.extend(defaults, options);

            return $(this).each(function(idx) {// Don't break the chain
				if($(this).is('*'))
				{
					//SEARCH FOR LIST ITEMS
					var counted_li = $(this).find("li").get().length; //$.log("counted_li:" + counted_li);

					//FIND ALL ITEMS FOR ROTATING
					var all_available_items =  $(this).find("img, li").not('li img').get(); //$.log("all_available_items:" + "[" + all_available_items.length + "]" + all_available_items);

					$(this).empty();

					var holder_items = $('<div/>');

					//LI items have priority over images
					var collection_items = counted_li > 0 ? $(all_available_items).filter('li') : all_available_items; //USE IMAGES IF THERE IS NO LIST

					$.each(collection_items, function () {
						var item_html = counted_li > 0 	?
															($.trim($(this).html())).length > 0   ? $(this).html() : null
														:
															$('<img/>').attr({
																				'src': $(this).attr('src'),
																				'border' : '0'
																			})
								      					;
						if(item_html != null)
						{
					  		holder_items.append($('<div/>').addClass('rot_item').append(item_html));
						}
					});


					//SELECT ROTATING ITEM ITEMS
					all_available_items = holder_items.find('div.rot_item'); //$.log(holder_items.html());
					//if (all_available_items.length <= 1)	{$.log('Too few items ' + options.source_container + "=" + all_available_items.length + " items");}
					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 'cycle':
							var all_cycle_properties = {};
							var hasNavControls = false;
							if( ($(options.nav_pause)).is('*') && ($(options.nav_play)).is('*') && ($(options.nav_next)).is('*') && ($(options.nav_prev)).is('*')	)
							{
								hasNavControls = true;
							}


							$(this).html(container_cycle_elements);

							var cycle_properties = {	fx:			options.cycle_fx,
													    speed:		options.cycle_speed,
													    timeout:	options.cycle_timeout,
													    random:		hasNavControls ? 0 : options.cycle_random,//DEACTIVATE RANDOM IF NAV CONTROLS AVAILABLE
												        next:		options.nav_next,
												        prev:		options.nav_prev,
														prevNextClick: onPrevNextClickCovering,
												        cleartype: 	options.cycle_cleartype,
												        cleartypeNoBg: options.cycle_cleartype_no_bg
							}

							//ADD PROPERTIES FOR SPECIAL TRANSITION DEFINITIONS
							var cycle_properties_new;
							switch(options.cycle_fx)
							{
								case 'scrollRight':
								case 'scrollLeft':
									cycle_properties_new = {fx: hasNavControls ? "scrollHorz" : options.cycle_fx};
									break;
								case 'scrollDown':
								case 'scrollUp':
									cycle_properties_new = {fx: hasNavControls ? "scrollVert" : options.cycle_fx};
									break;
								case 'cover':
								case 'uncover':
									cycle_properties_new = {direction: options.cycle_direction};
									break;
							}
							$.extend(all_cycle_properties, cycle_properties, cycle_properties_new);
							$(container_cycle_elements).cycle(all_cycle_properties);

							//NAV CONTROLS EVENTS
							if(hasNavControls)
							{
							    $(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':
							var rndItem = Math.ceil(Math.random() * all_available_items.length) - 1;
							$(this).html(all_available_items[rndItem]);
							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();
				}
            });

	        function onPrevNextClickCovering()
	        {
	        	//alert('z');
	        	//$(container_cycle_elements).cycle.rev = 1;
	        };


		}
    });
})(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);




