$.extend({
  jYoutube: function( url, size ){
    if(url === null){ return ""; }

    size = (size === null) ? "big" : size;
    var vid;
    var results;

    results = url.match("[\\?&]v=([^&#]*)");

    vid = ( results === null ) ? url : results[1];

    if(size == "small"){
      return "http://img.youtube.com/vi/"+vid+"/default.jpg";
    }else {
      return "http://img.youtube.com/vi/"+vid+"/0.jpg";
    }
  }
});



////////////////////////////////////////////////////////////////////
/* returns the youtube id from the link 						  */
////////////////////////////////////////////////////////////////////

$.extend({
	youtubeId: function(url){
		var ytid = url.match("[\\?&]v=([^&#]*)");
		ytid = ytid[1];
		return ytid;
	}
});

////////////////////////////////////////////////////////////////////
/* this method is used for stripping a string to a desired length */
////////////////////////////////////////////////////////////////////

$.extend({
	stripStr: function(str,n)
	{
		if(str.length >= n)
		{
			var s = str.substr(0, n);
			var words = s.split(' '); 
			words[words.length-1] = '';
			str = words.join(' ') + '&hellip;'
		}
		return str;
	} 
});


///////////////////////////////////////////////////////
/* this method is used for processing Video JSON data*/
///////////////////////////////////////////////////////

$.extend({
	processVideoData: function(data)
	{
		var video_data = data;
		var video_info = '';
		var id         = 0;
		
		if(data['doc'].constructor == Array)
		{
			$.each
			(
				data['doc'], 
				function(bb) 
				{
					
					var author 				= video_data['doc'][bb]['author'];
					var date	 			= video_data['doc'][bb]['date'];
					var description 		= $.stripStr(video_data['doc'][bb]['description'],150);
					var embedcode			= video_data['doc'][bb]['embedcode'];
					var title	 			= video_data['doc'][bb]['title'];
					var url					= video_data['doc'][bb]['url'];
					var viewcount			= video_data['doc'][bb]['viewCount'];
					var youtube_thumbnail 	= $.jYoutube(url, 'small');
					
					id = id + 1;
					
					video_info += "<div class=\"video-thumbnail\"><img src=\""+youtube_thumbnail+"\" align=\"left\" style=\"margin:0 0 10px 0\" width=\"85px\" height=\"75px\"></div><div class=\"video-description\"><span class=\"orange\"><a name =\""+title+"\" href=\""+url+"\" class=\"dialog_link\" id=\""+id+"\">"+title+"</a></span> </div>- By: "+author+"<br/>"+description+"<div class=\"clear\"></div>";
				}
			);
		}
		else
		{
			
			var author 				= video_data['doc']['author'];
			var date	 			= video_data['doc']['date'];
			var description 		= $.stripStr(video_data['doc']['description'],150);
			var embedcode			= video_data['doc']['embedcode'];
			var title	 			= video_data['doc']['title'];
			var url					= video_data['doc']['url'];
			var viewcount			= video_data['doc']['viewCount'];
			var youtube_thumbnail 	= $.jYoutube(url, 'small');
			
			id = id + 1;
			
			video_info += "<div class=\"video-thumbnail\"><img src=\""+youtube_thumbnail+"\" align=\"left\" style=\"margin:0 0 10px 0\" width=\"85px\" height=\"75px\"></div><div class=\"video-description\"><span class=\"orange\"><a name =\""+title+"\" href=\""+url+"\" class=\"dialog_link\" id=\""+id+"\">"+title+"</a></span> </div>- By: "+author+"<br/>"+description+"<div class=\"clear\"></div>";

		}
		
		return video_info;
	}
});



///////////////////////////////////////////////////////
/* this method is used for processing BLOG JSON data*/
///////////////////////////////////////////////////////

$.extend({
	processBlogData: function(data)
	{
		var blog_data = data;
		var blog_info = '';
		
		if(blog_data['doc'])
		{
			$.each
			(
				blog_data['doc'], 
				function(bb) 
				{
					var description 	= $.stripStr(blog_data['doc'][bb]['description'],150);
					var title	 		= blog_data['doc'][bb]['title'];
					var url 			= blog_data['doc'][bb]['url'];
					
					blog_info +=	"<div class=\"news-item\"> <span class=\"orange\"><a href=\""+url+"\" target=new>"+title+"</a></span></div><div class=\"clear-news-gray\"></div>";
				}
			);
						
			return blog_info;
		}
		else
		{
			return "<p>No record was found !!!</p>";
		}
	}
});


///////////////////////////////////////////////////////
/* this method is used for processing EDITORIAL JSON data*/
///////////////////////////////////////////////////////

$.extend({
	processEditorialData: function(data)
	{
		var editorial_data = data;
		var editorial_info = '';

		if(editorial_data['doc'])
		{
			
			$.each
			(
				editorial_data['doc'], 
				function(bb) 
				{
					var description 	= $.stripStr(editorial_data['doc'][bb]['description'],150);
					var title	 		= editorial_data['doc'][bb]['title'];
					var url 			= editorial_data['doc'][bb]['url'];
					
					editorial_info +=	"<tr><td><p><b><a href>"+title+"</a></b><br/>"+description+"</p></td></tr>";
				}
			);
			
			return editorial_info;
		}
		else
		{
			return "<p>No Editorial record was found !!!</p>";
		}
	}
});

$.extend({
	showModal:function() 
	{
	$("#dialog").dialog({
		bgiframe: true,
		height: 140,
		modal: true
	});
	}
	
});
