(function($){

$.widget("dg.youTubePlayer", $.dg.intpopup, {
	options: {
		animTime: 300,
		href: "",
		title: ""
	},
	element: null,
	iframe: null,

	_getElementToShow: function(){
		this.element.addClass("dg-youtubeplayer");
	
		return this.iframe = $("<iframe>")
			.addClass("youtube-videoplayer")
			.attr("src", this.options.href)
			.attr("rel", "0")
			.attr("frameborder", "0")
			.attr("allowfullscreen", "")
		;
	},
	
	_onShow: function(){
		
		if($(this.iframe).length > 0 && this.iframe.attr("tagName") != "iframe")
			this.iframe.replaceWith(this._getElementToShow());
	},
	
	_onHide: function(){
		var a = $("<div>");
	
		this.iframe.replaceWith(a);
		this.iframe = a;
	}
});


$(document).ready(function(){
	$("a.youtubeplayer").each(function(){
		var href = $(this).attr("href");
		var title = $(this).attr("title");

		$(this).attr("href", "#");
		$(this).removeAttr("title");
		
		$(this).click(function(){
			if($(this).data("elem") !== undefined && $(this).data("elem").length > 1){
				$(this).data("elem").youTubePlayer("show")
			}else{
				var elm = $("<div>")
					.appendTo("body")
					.youTubePlayer({
						href: href,
						title: title
					})
				;
				$(this).data("elem", elm);
			}
		});
	});
});

}(jQuery))
