/*

	jQuery.bva_simpleyoutube
	
	Boy van Amstel - Copyright 2009
	http://www.boyvanamstel.nl

*/

jQuery.bva_simpleyoutube = 
{
	build : function(args)
	{		
	
		var obj = this;			

		var plugin = jQuery.bva_simpleyoutube;

		var settings = {
			url: "http://presents.3fm.nl/page/xml_youtube_loadmovie/",
			loadingImage: "loading-circle.gif",
			currentMovie: ".simpleyoutube_current",
			title: ".simpleyoutube_title",
			description: ".simpleyoutube_description",
			previews: ".simpleyoutube_previews a",
			width: "480px",
			height: "295px"
		};
		if(args) {
			jQuery.extend(settings, args);
		}
		
		jQuery(settings.previews, obj).unbind("click").click(function()
		{
			var titleHeight = $(settings.title, obj).innerHeight();
			jQuery(settings.title, obj).height(titleHeight);

			var descriptionHeight = $(settings.description, obj).innerHeight();
			jQuery(settings.description, obj).height(descriptionHeight);
			
			jQuery(settings.currentMovie, obj).html('<img src="' + settings.loadingImage + '" alt="" title="" />');
			jQuery(settings.title, obj).html('');
			jQuery(settings.description, obj).html('');
			
			var videoUrl = jQuery(this).attr("href");
			var urlSplit = videoUrl.split("=");
			
			var movieCode = urlSplit[1];
			
			jQuery.ajax({
				type: "GET",
				dataType: "xml",
				url: settings.url + movieCode,
				success: function (xml) {
					
					// Loopen door resultaten
					jQuery("movie",xml).each(function()
					{

						var title = jQuery(this).find("title").text();
						var description = jQuery(this).find("contents").text();
						var code = jQuery(this).find("code").text();
					
						var movie = '<object type="application/x-shockwave-flash" style="width: ' + settings.width + '; height:' + settings.height + ';" data="http://www.youtube.com/v/' + code +'&showsearch=0&autoplay=1&fs=1"><param name="movie" value="http://www.youtube.com/v/' + code +'&showsearch=0&autoplay=1&fs=1" /><param name="allowFullScreen" value="true" /></object>';
						
						jQuery(settings.title, obj).html(title);
						jQuery(settings.description, obj).html(description);
						jQuery(settings.currentMovie, obj).html(movie);
						
						jQuery(settings.title, obj).height("auto");
						jQuery(settings.description, obj).height("auto");

						window.location.href="#";
					});

				}
			});		
			
			return false;
			
		});

	}
};

jQuery.fn.extend
({
	bva_simpleyoutube : jQuery.bva_simpleyoutube.build
});
