
var hasflowplayer=false;
var url2process_count=0;
(function($) {
	    
$.fn.publicitat = function(opt) {
	
	/**********************************************************************
	 * GET ITEM PUBLICITAT FUNCTIONS
	 *********************************************************************/	
	function get_item(data, index){
		var size = $("li", data).length;
		if (size == 0) return "";
		else if (index == undefined) index = random(size);

		return 	$("li:eq(" +index + ")",data);

	} 
	/**********************************************************************
	 * PAINT FUNCTIONS
	 *********************************************************************/	
	 	$.fn.extend({
	 		
	 		get_publicitat: function (theurl, index){
				var $this = $(this);
				countUrl(theurl);
				$.get(theurl, {}, function(data,textStatus){
					var item=get_item(data, index);
					$this.place_publicitat($(item).children());
					removeUrl(theurl);
				});
			},
			
			place_publicitat : function(htmlCode){
						$(this).empty();
						$(this).html(htmlCode);
			}
		});


	/**********************************************************************
	 * RELOAD FUNCTIONS
	 *********************************************************************/	
 	/**
	 * Reload the page. Change the Publicitat element shown
	 */
	function reload_page(){
		// show/hide the appropriate regions 

	}
	
		
	/**********************************************************************
	 * OTHER FUNCTIONS
	 *********************************************************************/
		function random(X) {
	    	return Math.floor(X * (Math.random() % 1));
		}		
		
		/**********************************************************************
		 * Video utils
		 *********************************************************************/
		function makeItVideo(item){
			if(!hasflowplayer){
				flowplayer("a.videoAdd", "/wb/system/modules/com.gmv.opencms.repunit.template/resources/flowplayer/flowplayer.commercial-3.1.5.swf"
					,
					{	
					key: '#$55cca231d447c7d42cd',							
					plugins: { 
						controls: {URL: '/wb/system/modules/com.gmv.opencms.repunit.template/resources/flowplayer/flowplayer.controls-3.1.5.swf',
								autoHide: 'always',
								timeColor: '#F44029',
								bufferColor: '#FEECEA',
								progressColor:'#F44029',
								sliderColor: '#F44029'
											} 
										}
					} );
					hasflowplayer=true;
				}
		}
		function countUrl(url){
			url2process_count=url2process_count+1;
		}
		function removeUrl(url){
			url2process_count=url2process_count-1;
			if(allUrlAreDone()){
				makeItVideo()
			}
		}
		function allUrlAreDone(){
			return url2process_count==0;
		}
	/**********************************************************************
	 * MAIN
	 *********************************************************************/	
	var options = $.extend({
		type: 'simple'
	}, opt || {});

	$(this).get_publicitat(options.url);

	return $(this);
	};

})(jQuery);
