(function($)
{
	var FourSeasonsVideo = function(container, video_id, options)
	{
		if(container.length)
		{
			if(!video_id)
				video_id = parseInt(container.attr("id").replace("fourseasonsvideo_", ""));
			
			this.container = container;
			this.videoId = video_id;

			$.extend(this.options, options);

			if(this.options.previewUrl == false)
			{
				var image = this.container.find("img");

				if(image.length > 0)
				{
					var url = image.eq(0).attr("src");
					this.options.previewUrl = url;
				}
			}

			if(this.options.width == false)
				this.options.width = this.container.width();

			if(this.options.height == false)
				this.options.height = this.container.height();

			this.init();
		}
		
	}
	
	FourSeasonsVideo.prototype.container = false;
	FourSeasonsVideo.prototype.videoId = false;
	
	FourSeasonsVideo.prototype.options = {
		
		swfVersionStr: "10.0.0",
		xiSwfUrlStr: "playerProductInstall.swf",
		streamer: "rtmp://fms.0F99.edgecastcdn.net/000F99/_definst_",
		basePath: "http://4-seasons.tv/sites/all/themes/fourseasons/player/",
		serverPath: "http://4-seasons.tv",
		autostart: false,
		controlbarPos: "bottom",
		width: false,
		height: false,
		
		stretching: "fill",
		quality: "high",
		bgcolor: "#ffffff",
		allowscriptaccess: "always",
		allowfullscreen: "true",
		previewUrl: false
	}
		
	FourSeasonsVideo.prototype.init = function()
	{
		if(typeof(swfobject) != "undefined")
		{
			var params = {
				quality: this.options.quality,
				bgcolor: this.options.bgcolor,
				allowscriptaccess: this.options.allowscriptaccess,
				allowfullscreen: this.options.allowfullscreen
			};

			var attributes = {
				id: "player",
				name: "player",
				align: "left"
			};
			
			var flashvars = this.getFlashVars();
			var player_width = this.options.width;
			var player_height = this.options.height;
			
			swfobject.embedSWF(
			    this.options.basePath+"player.swf", this.container.attr("id"), 
			    player_width, player_height, 
			    this.options.swfVersionStr, this.options.xiSwfUrlStr, 
			    flashvars, params, attributes);
		}
		else
		{
			console.log("SWFObject is not installed.");
		}
	}
	
	FourSeasonsVideo.prototype.getFlashVars = function()
	{
		var pathServer = this.options.serverPath;
		var pathTo = this.options.basePath;

		var flashvars = {};
		
		// URL AUF VIDEO-XML
		flashvars.playlistfile = pathServer + "/video/"+this.videoId+"/xml";

		flashvars.logo = {};
		flashvars.logo.file   		= pathTo+"cornerbug.png"; 
		flashvars.logo.margin   	= 0;
		flashvars.logo.out 	   		= 0.5; 
		flashvars.logo.over     	= 1; 
		flashvars.logo.hide     	= false; 
		flashvars.logo.timeout  	= 3 
		flashvars.logo.position 	= 'top-right'; 

		flashvars.autostart 		= this.options.autostart;
		flashvars.playerready 		= "onPlayerStart";
		flashvars.repeat 			= "list";
		flashvars.repeat 			= "list";
		flashvars.stretching 		= this.options.stretching;
		flashvars.controlbar 		= this.options.controlbarPos;

		var plugins = []
		plugins.push(pathTo+"FsTv.swf");
		plugins.push(pathTo+"FsProducts.swf");
		plugins.push(pathTo+"rtmpt.swf");

		flashvars["plugins"] = plugins.join(",");

		flashvars["FsTv.path"] = pathServer; 
		flashvars["FsTv.streamer"] = this.options.streamer;

		flashvars["FsTv.afterplaylist"] = "false";
		flashvars["FsTv.taf"] = "false";
		flashvars["FsTv.startimage"] = this.options.previewUrl;

		flashvars["FsProducts.path"] = pathServer; 
		// GA tracking für produkte 
		flashvars["FsProducts.tracking"] = "true"; 

		flashvars["rtmpt.module"] = pathTo+"RTMPTMediaProvider.swf";
		flashvars["rtmpt.debug"] = "false";

		flashvars["skin"] = pathTo+"/modieus.swf";
				
		return flashvars;
	}
	
	
	$.fn.fourSeasonsVideo = function(video_id, options)
	{
		new FourSeasonsVideo(this, video_id, options);
	}

})(jQuery);

