/*
 * jQuery WG Build Collection Images Listitems 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: 1
 */
jQuery.fn.WG_ImgLiCollect = function(options) {
    var defaults = {
    	emptysource: true,
    	itemclass: 'rot_item',
		cb: function(){}
    };
    var options = $.extend(defaults, options);

	if($(this).is('*'))
	{
		//SEARCH FOR LIST ITEMS
		var counted_li = $(this).find("li").get().length; 			//$.log("counted_li:" + counted_li);

		//FIND ALL IMAGES AND LIST ITEMS (NOT IMAGES IN LIST ITEMS)
		var all_available_items =  $(this).find("img, li").not('li img').get();				//$.log("all_available_items:" + "[" + all_available_items.length + "]" + all_available_items);

		if(options.emptysource){$(this).empty()}

		var has_listitems = counted_li > 0;

		//li ITEMS HAVE PRIORITY OVER IMAGES, SO DISCARD IMAGES WHEN LISTITEM AVAILABLE, BUT USE IMAGES IF THERE IS NO LIST
		var collection_items = has_listitems ? $(all_available_items).filter('li') : all_available_items;


		var temp_holder_items = $('<div/>');
		$.each(collection_items, function () {
			//cleaned html of items, so no width height etc
			var item_html = has_listitems 	?
												($.trim($(this).html())).length > 0   ? $(this).html() : null
											:
												$('<img/>').attr({
																	'src': $(this).attr('src'),
																	'border' : '0'
																})
					      					;
			if(item_html != null)
			{
				//add to container with new wrapper
		  		temp_holder_items.append($('<div/>').addClass(options.itemclass).append(item_html));
			}
		});

		//$.log((has_listitems ? "LI" : "IMG") + ": " + temp_holder_items.find("."+options.itemclass).length + " = " + temp_holder_items.html());



		//SELECT ROTATING ITEM ITEMS
		//all_available_items = temp_holder_items.find(options.itemclass); //$.log(holder_items.html());
		//$.log("length:"+cleaned_collection.length);

		//if (temp_holder_items.find("."+options.itemclass).length <= 1)	{$.log('Items found: ' + temp_holder_items.find("."+options.itemclass).length + " items");}

		options.cb.call();

		return temp_holder_items;
	}
};