YouTubeWidget = function()
{	

	/*

YouTubeWidget.getUser = function(user) 
{
};
	getFriends = function getFriends(user) {};
	//getVideosByUser = function getVideosByUser(user, page) {};
	//getVideosByFavourite = function getVideosByFavourite(user) {};
	//getVideosByTag = function getVideosByTag(tag, page) {};
	//getVideosByFeatured = function getVideosByFeatured() {};
	//getVideosByPlaylist = function getVideosByPlaylist(playlist, page) {};
	//getVideosByPopular = function getVideosByPopular(time_range) {};
	//getVideosByCategoryTag = function getVideosByCategoryTag(category_id, tag, page) {};
	//getVideo = function getVideo(video) {};
	*/
}
YouTubeWidget.ajaxFunction = function(url, callbackFunction, callbackArgument)
{
	var xmlHttp;
	try
	{
	   // Firefox, Opera 8.0+, Safari
	   xmlHttp=new XMLHttpRequest();
	}

	catch (e)
	{
		// Internet Explorer

   		try
     	{
     		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     	}
   		catch (e)
   		{
     		try
       		{
       			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
       		}
     		catch (e)
       		{
       		//alert("Your browser does not support AJAX!");
		       return false;
       		}
     	}
   }

   
   if (callbackFunction == 'writeToDiv')
    {
	xmlHttp.onreadystatechange = function()
		{
			if(xmlHttp.readyState==4)
			{
				document.getElementById(callbackArgument).innerHTML = xmlHttp.responseText;
			}
		}
	}

	else
	{
		xmlHttp.onreadystatechange = callbackFunction;
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

YouTubeWidget.getNumberOfProperties = function(object)
{
	var count = 0;
	for(var i in object)
	{
		count++;
	}	
	return count;
}

YouTubeWidget.generateUrl = function(params) 
{
	var returnUrl = "/youtube.php?";	

	var totalParams = YouTubeWidget.getNumberOfProperties(params);	
	var count = 0;
	for(var i in params)
	{
		count++;
		returnUrl = returnUrl + i + "=" + encodeURI(params[i]);
		if (count<totalParams){ returnUrl = returnUrl + "&"; }
	}
 
	return returnUrl;
};

YouTubeWidget.reloadDivWithContent = function(url, divId)
{
	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);	
}

YouTubeWidget.displayVideosByTag = function(divId, jsCallback, tag, per_page, template, nutsusername)
{
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.list_by_tag',
			'per_page':  per_page,
			'jscallback':  jsCallback,			
			'tag':  tag,			
			'template':  template,
			'nutsusername': nutsusername	
		} 
	);		
		YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};


YouTubeWidget.displayVideosByUser = function(divId, jsCallback, user, per_page)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 

			'divid':  divId,		
			'method':  'youtube.videos.list_by_user',
			'per_page':  per_page,
			'jscallback':  jsCallback,			
			'user':  user

		} 

	);		

	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};



YouTubeWidget.displayVideosByFavourite = function(divId, jsCallback, user)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.users.list_favorite_videos',
			'jscallback':  jsCallback,			
			'user':  user
		} 

	);		

	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		

};



YouTubeWidget.displayVideosByFeatured = function(divId, jsCallback)

{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.list_featured',
			'jscallback':  jsCallback
		} 

	);		



	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		

};



YouTubeWidget.displayVideosByPlaylist = function(divId, jsCallback, id, per_page)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.list_by_playlist',
			'per_page':  per_page,
			'jscallback':  jsCallback,			
			'id':  id
		} 

	);		



	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		

};



YouTubeWidget.displayVideosByPopular = function(divId, jsCallback, time_range)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.list_popular',
			'jscallback':  jsCallback,			
			'time_range':  time_range
		} 
	);		
	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};



YouTubeWidget.displayVideosByCategoryTag = function(divId, jsCallback, category_id, tag, per_page)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.list_by_user',
			'per_page':  per_page,
			'jscallback':  jsCallback,			
			'category_id':  category_id,
			'tag':  tag		
		} 
	);		
	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};



YouTubeWidget.displayVideo = function(divId, jsCallback, video)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.videos.get_details',
			'jscallback':  jsCallback,			
			'video_id':  video
		} 
	);		

     YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};



YouTubeWidget.displayFriends = function(divId, jsCallback, user)
{	
	var url = YouTubeWidget.generateUrl( 
		{ 
			'divid':  divId,		
			'method':  'youtube.users.list_friends',
			'jscallback':  jsCallback,			
			'user':  user
		} 
	);		

	YouTubeWidget.ajaxFunction(url, 'writeToDiv', divId);		
};
