jQuery.fn.youtube = function(data)
{
    var config = {
                  type              : null,          // allowed values: 'playlist', 'search','user'
		          keyword           : null,          //A search query term. Searches for the specified string in all video metadata, such as titles, tags, and descriptions.
		          url               : null,
                  users             : null,          // videos uploaded by a user
		          alt               : null,          //The format of feed to return, such as atom (the default), rss, or json.
                  orderby           : null,          //The order in which to list entries, such as relevance (the default for the videos feed) or viewCount.
                  start_index       : null,          //The 1-based index of the first result to be retrieved (for paging). 
	              max_results       : 0,          //The maximum number of entries to return at one time default : 10
	              categories        : null,          //The categories and/or tags to use in filtering the feed results. 
		                                             //  For example, feedURL/-/fritz/laurie returns all entries 
									                 // that are tagged with both of the user-defined tags fritz and laurie.
		
                  format            : null,          //A specific video format. For example, format=1 restricts search results to videos for mobile devices.		
		          most_viewed       : null,
		          top_rated         : null,
		          recently_featured : null,
		          top_favorites     : null,
		          most_discussed    : null,
		          most_linked       : null,
		          most_responded    : null,
		   
		          recently_featured : null,
		          playlist_id       : null,          //playlists feed contains a list of public playlists defined by a user.
		          div               : this,
		          
		          cleanReturn       : 1,             //do you want a full youtube return, or just an image list
		          inlineVideo       : 1,             //do you want to redirect to youtube, or play inlinevideo
		          callback          : null,
                  api_key           : null
                  //blockUI           : true           // boolean, if true requires jquery.litebox.js
	            };//end of config
		 
		if(data) 
		{
		   $.extend(config, data);
		}
		
		/**/
		return this.each(function()
            		    {
            		    	$('#youtubelist').remove();
            		    	$(this).append('<ul id="#youtubelist">');
            		    	
            		    	var url = $.youtube.getURL(config);
            		    	$.youtube.request(url);
            		    }
		);//end of each 
}
/*end of youtube function*/
	
/** 
 *extend the youtube function

 */

$.youtube = 
{
	config   :{},
	
	/**
	  * genereate the url 
	  * according to the configaation
	  *
	  *
	  
	  */
	
	getURL: function(config)
	{
	    var url='';
		this.config = config;
		config.type = config.type;
		
		if( (config.type =='search') || (config.type=='tag') || (config.type=='title') || (config.type =='description') )
		{
		    config.type='search';
		}
			
        if (config.url) return config.url;
        if (!config.callback) config.callback = 'jQuery.youtube.response';
		
		var url = 'http://gdata.youtube.com/feeds/';
		
		switch (config.type){
			case 'users':    url +='users/'+config.keyword+'/uploads?alt=json-in-script&callback='+config.callback;
			                 break;
			    
            case 'search':   url  +='videos?alt=json-in-script&callback='+config.callback;
				             url  += '&vq='+config.keyword;   
			                 break;
			              
			case 'playlist': url +='playlists/'+config.keyword;
			                 break;
			                 
			case 'category': break;
			                 
            default        : url ='http://gdata.youtube.com/feeds/videos';
				
        }
        
		if (config.start_index) 
		{
		   url +='&start-index='+ config.start_index;
		}
		
		
		if (config.max_results) 
		{
		    url +='&max-results='+ config.max_results;
		}
		
		if (config.orderby) 
		{
		    url +='&orderby='+ config.orderby;
		}		
	   return url;
	},
	
	/* 
	*request the url in the head and get the jsondata
	*@param url string
	*@return null,
	*/
	request: function(url)
	{
		var script  = document.createElement('script');
        script.type = 'text/javascript';
        script.src  = url;
        
        document.documentElement.firstChild.appendChild(script); //add into <head>
		
		//$("head").append(script);
		//$>getjson ()url,jsondata);
    },
	
	/**
	* this function process the jsondata
	* and display into the div 
	*
	*@param jsonData jsondata
	* Modified By Khalil Akashah
	*/
	response : function (jsonData)
	{
		var inlineVideo =this.config.inlineVideo
        
        if(jsonData.feed.entry)
        {
	         var html='';
		     $.each( jsonData.feed.entry, function(i, item) 
		           { 
		                for( var k = 0; k <item.link.length; k++ ) 
			              {
				               if( item.link[k].rel == 'alternate' ) 
				                {
				      	          url = item.link[k].href;
				       	          break;
				                }
			              }
			              
			              var thumb = item.media$group.media$thumbnail[1].url;
			              var title = item.media$group.media$title.$t;
			              var descriptions = item.media$group.media$description.$t;
                          
                          if (inlineVideo)
                          {
			                  var videoId  = $.youtube.getVideoId(url);
			                  html        += '<li class="clearfix vidThumb"><a href="javascript:$.youtube.playVideo(\''+videoId+'\',true);">';
					  html        += '<img src="'+thumb+'"  id="youtubethumb" alt="'+item.title.$t+'"></a><br /><a';
					  html        += ' href="javascript:$.youtube.playVideo(\''+videoId+'\',true);">'+title+'</a>';
					  html        += ' <div class="vidDesc">'+$.youtube.processDesc(descriptions)+'</div> </li>'; 
			                  
			              }
			              else 
			              {   			
			                  html        += '<a href="'+url+'"><img src="'+thumb+'" id="youtubethumb" alt="'+item.title.$t+'"></a>';
			              }
		           }
		     );
		     
		     $(this.config.div).html(html);
		     //'<a href="javascript:videoOverlay(\''+getVideoId(url)+'\');"><img src="'+thumb+'" id="youtubethumb" alt="'+entry.title.$t+'"  onmouseout="clearTimeout(timer)" onmouseover="mousOverImage(this,\''+getVideoId(url)+'\',2)"></a>';
		     //var thumb = entry['media$group']['media$thumbnail'][1].url;
	    }	  
	},
	
    /** 
     * @param url 
     * @return video id   
     */	
	getVideoId: function (url)
	{
	   var arrayURL= url.split("=");
	   if (arrayURL)
	    {
		   return arrayURL[1];
		}
	},
	
    /** 
     * @param desc 
     * @return description   
     */	
	processDesc: function (desc)
	{
	   if (desc.length>20)
	   {
	      return desc.substring(0,20) + "...";
	   }
	   else
	      return desc;
	},	
	
	/** 
	 * Play the video  
	 *
	 *@param id videoid
	 */
	playVideo : function (id,autoplay)
    {
    	   var autoNum = 0;
	   var html  = '';
	   html     += '<div id="youtubecontent">';
	   
	   
	   html += '<object width="425" height="344" >';
	   html += '<param name="movie" value="http://www.youtube.com/v/'+id+'"></param>';
	   
	   if(autoplay)
	   {
	   	html += '<param name="autoplay" value="1">';
	   	autoNum = 1;
	   }

	   html += '<param name="wmode" value="transparent"></param>';
	   html += '<embed src="http://www.youtube.com/v/'+id+'&autoplay='+autoNum+'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344" ></embed>';
	   html += '</object>';
	   html += '</div>';

	   $.youtube.showPlayer(html);
	},
    
    /** 
      *Show Player
      */
    showPlayer : function (htmls)
    {
	$('#video_player').html(htmls);
    }	
	
    	  
};