	 /////// Global extensions on strings///////////////
         String.prototype.trim = function() {
	      return this.replace(/^\s+|\s+$/g,"");
         }
         String.prototype.ltrim = function() {
	      return this.replace(/^\s+/,"");
         }
        String.prototype.rtrim = function() {
         	return this.replace(/\s+$/,"");
          }
     ///////////////////////////////////

	  liveGuru=function(){
	    return {  
	   ///////////////////////// default settings///////////////////////////////////////////// 
	      settings: {
		          timeout: 2000,
				  counter: 0,
				  extract_map: new Array(),
				  target:"",
				  addEvent:"",
				  eventCallbackParams:["title"],
				  wait_response:0,
				  maxItems:3,                    /// max items that can be viewed
				  showMore:3,             /// expandes the list to view more items
				  slideItems:1,          /// slides in the items history
				  attach_position: "bottom",    //////////// top or bottom
				  url: "source.php",
				  history_offset:0,
				  openLinkMethod:"_blank", /// _blank and _self
				  interface:"wordpress",   
				  feed_link:"http://localhost/catman_nou/blog/feed",  /// rss feed link 
				  order: "ASC",  /// ASC and DESC
				  automaticSlide:0,
				  custom_action_flag:0,
				  stop_slide_up:0,        //a flag that disables the Slide up functionality 
				  stop_slide_down:0,  //a flag that disables the Slide down functionality
				  contract:0,   // stops the current request for no updating conflicts when press "less"
				  params: {
					         id:"title",  //// identifies a feed, to remove duplicates
							 print_text: ["title","description"],  //// the text that will appear on the list, you can set multiple options at once
							 extract_text: ["title","description"],  //// the data that should retrieve from the feed
				            link_text: ['link'],    /// the link attached to the selected tag
				            link_attached_to: ['title'],    /// attach the link to specified tags
							 attach_link:true  /// if you want the link to be attashed or not
					   }
		  },
	  ///////////////////////////////////////////////////////////////////////////////////////	  
		  
		 ////////////////// starts the liveGuru "process" /////////////////////
	      init : function(rewrite_default){
				var handler=this;
			  if(rewrite_default!=null)
			   {
				    if(rewrite_default.target!=null) this.settings.target=rewrite_default.target;
					 if(rewrite_default.eventCallbackParams!=null) this.settings.eventCallbackParams=rewrite_default.eventCallbackParams;
					if(rewrite_default.addEvent!=null) this.settings.addEvent=rewrite_default.addEvent;
                    if(rewrite_default.automaticSlide!=null) this.settings.automaticSlide=rewrite_default.automaticSlide;
					if(rewrite_default.slideItems!=null) this.settings.slideItems=rewrite_default.slideItems;
					if(rewrite_default.history_offset!=null) this.settings.history_offset=rewrite_default.history_offset;
					if(rewrite_default.attach_position!=null) this.settings.attach_position=rewrite_default.attach_position;
					if(rewrite_default.url!=null) this.settings.url=rewrite_default.url;
					if(rewrite_default.id!=null) this.settings.params.id=rewrite_default.id;
					if(rewrite_default.showMore!=null) this.settings.showMore=rewrite_default.showMore;
				    if(rewrite_default.maxItems!=null) this.settings.maxItems=rewrite_default.maxItems;
				    if(rewrite_default.timeout!=null) this.settings.timeout=rewrite_default.timeout;
			        if(rewrite_default.openLinkMethod!=null) this.settings.openLinkMethod=rewrite_default.openLinkMethod;
					if(rewrite_default.interface!=null) this.settings.interface=rewrite_default.interface;
					if(rewrite_default.feed_link!=null) this.settings.feed_link=rewrite_default.feed_link;
					if(rewrite_default.order!=null) this.settings.order=rewrite_default.order;
					if(rewrite_default.print_text!=null) this.settings.params.print_text=rewrite_default.print_text;
					if(rewrite_default.extract_text!=null) this.settings.params.extract_text=rewrite_default.extract_text;
					if(rewrite_default.link_text!=null) this.settings.params.link_text=rewrite_default.link_text;
					if(rewrite_default.link_attached_to!=null) this.settings.params.link_attached_to=rewrite_default.link_attached_to;
					if(rewrite_default.attach_link!=null) this.settings.params.attach_link=rewrite_default.attach_link;

				   }  
				   if($("#"+this.settings.target).attr("id")!=undefined)
		            { 
					  ////////// start the process if the target is valid/////////////////////////////	
			          $contor=0;
			           for(i=0;i<=this.settings.params.extract_text.length-1;i++)
				          {
						  this.settings.extract_map[$contor]=this.settings.params.extract_text[i];
						  $contor+=2;
						 }
			   			  this.theRequest();
	                    if(this.settings.attach_position=="top") this.settings.stop_slide_up=1;
			                                                else this.settings.stop_slide_up=1;
			             setInterval(function(){ handler.theRequest(); },this.settings.timeout)
						/////////////////////////////////////////////////////////////////////////////// 
					}
						 },
		 /////////////////////////////////////////////////////////////////////
		 
		 //////test if an element is array or not ///////////////////////////////////
		  isArray: function (testObject) {   
               return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
           },
		  /////////////////////////////////////////////////////////////////////////
		  
		 ////////////////////////////// creates the ajax request ///////////////////////////////// 
		  theRequest:function()
		      { 
			      
				  /////////// creates the request for data//////////////////
			      var requested_text=new String();
				   if(this.isArray(this.settings.params.extract_text))
				    {
						 for(var i=0;i<=this.settings.params.extract_text.length-1;i++)
						  {
						    if(i==0)
					    	    requested_text+=this.settings.params.extract_text[i];
							else
							    requested_text+=","+this.settings.params.extract_text[i];
						  }
						} 
					else requested_text=this.settings.params.extract_text;	
				  /////////////////////////////////////////////////////////////////
				  
					 if($("#"+this.settings.target).html().trim()=="")  this.preloader();
					 
					/////////////// creates the request link ///////////////////////////////////// 
					 var my_request="feed="+this.settings.interface+"&max="+this.settings.maxItems+"&feed_link="+this.settings.feed_link+"&id="+this.settings.params.id+"&title="+requested_text+"&link="+this.settings.params.link_text[0]+"&use_link="+this.settings.params.attach_link+"&order="+this.settings.order+"&offset="+this.settings.history_offset;
					 ////////////////////////////////////////////////////////////////////////////////
					
			if(this.settings.wait_response==0) {
			$.ajax({
                 type: "POST",
				  refer:this,
                  url: this.settings.url,
                 data: my_request,
              success:  this.handleTheResponce
             });
			
			}
				this.settings.wait_response=1;
			},
		///////////////////////////////////////////////////////////////////////////////////////////////// 	
			
			////////////////handle the server responce //////////////////////////////////////////////////////
		 handleTheResponce: function($msg) { 
			
		 //// stops the slide down functionality///
		  if($msg.indexOf("(****)")!=-1){
		   $msg=$msg.replace("(****)","");
		    this.refer.settings.stop_slide_down=1;
		  }
		  /////////////////////////////////////////
		  
		  ////// stops the slide up funtionality///////
		  if($msg.trim().indexOf("(*****)")!=-1){ 
		   $msg=$msg.replace("(*****)","");
		    this.refer.stop_slide_up=1; 
		 
		  }
		  /////////////////////////////////////////////
		  
		   if($msg.indexOf("(**)(*)")==-1){
			 $("#"+this.refer.settings.target).show("fast");
		       $msg=$msg.split("(***)"); 
			   var $start_link="",$end_link="";
		
		     // if there is a single element, the loop should go from 0 to 1////
			   if($msg.length==1)    $substract=0; 
				 else $substract=1;	 
				/////////////////////////////////////////////////////
	
			   for(var i=$msg.length-$substract;i>=0;i--)
			     {   	 
				    $duplicate_flag=0;
				    $data=$msg[i].split("(**)");
					
					////////search for duplicates /////////////////////////////
					$(" #"+this.refer.settings.target+" > li").each(function(index)
					  {
						if($(this).attr("className").toString().toLowerCase().indexOf("tag_"+$data[0].toString().toLowerCase())!=-1)
						   $duplicate_flag=1;
						 
					  }); 
					/////////////////////////////////////////////////////////////
				
		
					   
					   ////////// creates the html format and adds the entry to the list////////////
					      if($data[1]!=undefined){
					        $print_feed=$data[1].trim().split("(*)"); 
							$temp_feed=$print_feed;
						    $print_feed=this.refer.doPackage($print_feed,"print",$data[2]); 

						    $package='<li  id="item' +"_"+ this.refer.settings.target+"_"+ this.refer.settings.counter + '" class="tag_'+$data[0].trim()+'" '+this.refer.attachEvent(this.refer.doPackage($temp_feed,"event"))+'>'+$print_feed+ '</li>';                   
						      if($duplicate_flag==0  && this.refer.settings.contract==0 ) {  
							       this.refer.addItem($package); 
											   }
										
						  }
						////////////////////////////////////////////////////////////////////////////////
				 }
		 		} 
				
		  ///////// resets flags ////////////////////////////////////////		
		      if(this.refer.settings.contract==1){		
	 	          this.refer.settings.contract=0;
				  }
		  $("#loader_"+this.refer.settings.target).slideUp("fast").remove();		  
		  this.refer.settings.wait_response=0; 
		  if( this.refer.settings.custom_action_flag==1) this.refer.settings.custom_action_flag=0;
		  ////////////////////////////////////////////////////////////////////
		  
		  /////////// automatic slide -> next step //////////////// 
		  if(this.refer.settings.automaticSlide==1)
			   { 
			      this.refer.SlideDown(1);
				   }
		 ////////////////////////////////////////////////////////////////
		 
		 }	,
		 ///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	///// teste function /////////////////////////////////////////////////////////
		 dump:function(obj)
		   {     var text="";
			     for(o in obj)
				   {
					    text+="<br>"+o+" : "+obj[o];
					   }
					   alert(text);
			   },
	
	///////////////// Remove an element from the list ///////////////////////
	    forceRemove:function($type,$id) {
		   
		      switch($type)
			    {
					case "id": 
					      $full_class="tag_"+$id; 
						  $("."+$full_class).remove();
						  break;
					default: break;
					}
		   	},
	
	////////// attaches an event to the <li> tag //////////////////////
	   attachEvent: function (params)
	    { 
			if(this.settings.addEvent!="")
             {
				 this.settings.addEvent=this.settings.addEvent.toString().replace("{","");
				 this.settings.addEvent=this.settings.addEvent.toString().replace("}","");
				 this.settings.addEvent=this.settings.addEvent.split(","); 
				 $event=this.settings.addEvent[0]+"="+this.settings.addEvent[1]+"("+params+")";
				 return $event;
				  }
			  return "";
			},
	
	//////// adds the preloader in the wanted position//////////////////////////////
		 preloader: function ()
		   { 
		    
		       if(this.settings.attach_position=="bottom")
			    $("#"+this.settings.target).append("<div class='loader' id='loader_"+this.settings.target+"'>Loading...</div>");   
			   else
			    $("#"+this.settings.target).prepend("<div class='loader' id='loader_"+this.settings.target+"'>Loading...</div>");       
		  },
		  
	/////////// increases  the number of items shown in the list/////////////////////////// 		
		 More: function ()
		   { 
		   if(this.settings.stop_slide_down==0 && this.settings.custom_action_flag==0){
			   this.settings.maxItems+=this.settings.showMore; 
		     if(this.settings.custom_action_flag==0){
			   this.preloader();
			   this.theRequest();
			 }
			    this.settings.custom_action_flag=1; 
		   }
		     this.settings.counter_start+=this.settings.showMore;
			   },
	/////////// decreases  the number of items shown in the list/////////////////////////// 		   
		 Less: function ()
		   { 
		   if(this.settings.stop_slide_down==0 && this.settings.custom_action_flag==0){ 
		       this.preloader();
		       this.removeItem(1);
			   this.settings.contract=1;
		       this.settings.maxItems-=this.settings.showMore;
               this.settings.custom_action_flag=1; 
		   }
		   this.settings.counter_start-=this.settings.showMore;
			   },
			 
	///////////scrolls up through the feed /////////////////////////// 		 
		 SlideUp: function (no_request)
		   { 
			   if(this.settings.history_offset>0 && this.settings.stop_slide_up==0 && this.settings.custom_action_flag==0  ){
			   this.settings.history_offset-=this.settings.slideItems; 
    		    if(this.settings.stop_slide_down=1) this.settings.stop_slide_down=0;
     		   if(no_request!=1 ){
			     this.preloader();
			     this.theRequest();
			   }
			   this.settings.custom_action_flag=1; 
			   }

			   },
	
	///////////scrolls down thrue the feed /////////////////////////// 
		 SlideDown: function (no_request)
		   {   
		     if(this.settings.stop_slide_down==0 && this.settings.custom_action_flag==0){
			   this.settings.history_offset+=this.settings.slideItems; 
                if(this.settings.stop_slide_up=1)  this.settings.stop_slide_up=0;
			      if(no_request!=1  ){
			         this.preloader();
			         this.theRequest();
				  }
			  this.settings.custom_action_flag=1;  
			 }
	
			   },			   
	///////// check if a data is restricted or not by print_text or eventCallback/////////// 
		    restricted:function($zone,index)
			  {
				    temp=this.settings.extract_map[index]; 
				    if($zone=="print")
					 {
						for(i=0;i<=this.settings.params.print_text.length-1;i++)
						   {
							   if(this.settings.params.print_text[i]==temp)
							     return false;
							   }
						 }
				  else if($zone=="event")
				     {
					  for(i=0;i<=this.settings.eventCallbackParams.length-1;i++)
						   {
							   if(this.settings.eventCallbackParams[i]==temp)
							    return false;
							   }
					  }
					 else if($zone=="link")
				     {
					  for(i=0;i<=this.settings.params.link_attached_to.length-1;i++)
						   { 
							   if(this.settings.params.link_attached_to[i]==temp)
							    return false;
							   }
					  }  
					  return true;
				  },
         ////////////////////////////////////////////////////////////////////////////////////////////	
		 
		 ////// attaches a link to the specified tag ////////////////////////
		 attachLink:function($target,$link,index)
		  {						  
		  var $start_link=new String();
		  var $end_link=new String();
						  
			  if(!this.restricted("link",index))
					{
						  
					  if($link!=undefined && $link.trim()!="" && this.settings.params.attach_link==true)
					   { 
					      $start_link="<a href='"+$link.trim()+"' target='"+this.settings.openLinkMethod+"'>";
					      $end_link="</a>";
					   }  
		
					}
				return $start_link+$target+$end_link;	
			  },
		 ////////////////////////////////////////////////////////////////////
		 
		 ///// inserts each element in a separte <p> tag in the same <li>, for print_text or eventCallback/////////////////////////////////////
		 doPackage:function(entry,for_,$link)
		            { 
					  
					  if(for_=="print"){

						 ///////// for print text///////// 
						 var $return=new String();
					     for(var i=0;i<=entry.length-1;i++)
						  { 
						     if(entry[i].indexOf("live_class=")==-1)
							  {  
							    if(!this.restricted("print",i))
								 {
					        $return+="<p class='"+entry[i+1].replace("live_class=","")+"'>"+this.attachLink(entry[i],$link,i)+"</p>";
								 }
							     }
							  }
							  /////////////////////////////
					  }
					  if(for_=="event"){
						    /////////////// for the eventCallback function////////
							 var $return=new String();
							 $contor=0;
					     for(var i=0;i<=entry.length-1;i++)
						  { 
						     if(entry[i].indexOf("live_class=")==-1)
							  {  
							    if(!this.restricted("event",i)){ 
									if($contor==0)
							         $return+="'"+escape(entry[i])+"'";
									else
									 $return+=",'"+escape(entry[i])+"'";
									$contor++; 
								
								}
							  }
							  
							  }
							
							/////////////////////////////////////////////////////
						  }
						  return $return;	  
			           },
		 //////////////////////////////////////////////////////////////////////////////////////////////////////
		 
		 /////////////////////// add an item to the list /////////////////////////////////////////////////////// 
		  addItem: function ($package) { 
		       
			   $("#loader_"+this.settings.target).slideUp("fast"); 
				if (this.settings.counter>=this.settings.maxItems) this.removeItem();
				  if(this.settings.attach_position=="bottom")
				     $('#'+this.settings.target).append($package).fadeIn("slow");
				  else
				     $('#'+this.settings.target).prepend($package);
     		    $('#item'  +"_"+ this.settings.target+"_"+ this.settings.counter).fadeIn("fast");
      		    this.settings.counter += 1;	
			}	,		
		 ///////////////////////////////////////////////////////////////////////////////////////////
		 
		 ////////////////////// remove an item/items from the list ////////////////////////////////////////////////	
		 removeItem:	function ($contract) {	
       
			//  var pos=$('#item' +"_"+ this.settings.target+"_" + (this.settings.counter-this.settings.maxItems)).position().top;
			//  var height=$('#item' +"_"+ this.settings.target+"_" + (this.settings.counter-this.settings.maxItems)).css("height"); 
			//	$('#item' +"_"+ this.settings.target+"_" + (this.settings.counter-this.settings.maxItems+1)).css("position","relative").animate({top:-(parseInt(height))});
			if($contract==undefined)
				 $('#item' +"_"+ this.settings.target+"_" + (this.settings.counter-this.settings.maxItems)).fadeOut("fast").remove();
		    else {	
			    for(var i=0;i<=this.settings.showMore-1;i++)
				  {
			       $('#item' +"_"+ this.settings.target+"_" + (this.settings.counter-1)).fadeOut("fast").remove();
			       this.settings.counter--;
				  }
			  }
			}		
	    /////////////////////////////////////////////////////////////////////////////////////////////////////		
		}}
