
function vplayer(options) {
	//domain: 'jquery.com',
	registervplayer(options);

	var cookie_quality = $.cookie('video_quality');
	if(cookie_quality == null) {
		
		var element = $("#"+options.id);
		
		element.removeAttr("id");
		element.attr("id", options.id + "_container");
		element.html('<div style="position:relative;height:100%;width:100%;"><div style="height:100%;width:100%;" id="'+options.id+'"></div><div style="position:absolute;top:0px;height:100%;width:100%;" class="quality_chooser"></div></div>');
		
		var chooser_element = element.find(".quality_chooser");
		
		chooser_element.html('<div style="position:relative;width:100%;height:100%;"><div style="position:absolute;cursor:pointer;background-image:url(\'/medias/videos/images2/highBtn.jpg\');background-repeat:no-repeat;width:107px;height:54px;left:152px;top:106px;" class="high_res_button"></div><div style="position:absolute;cursor:pointer;background-image:url(\'/medias/videos/images2/lowBtn.jpg\');background-repeat:no-repeat;width:88px;height:54px;left:171px;top:170px;" class="low_res_button" ></div></div>');
		chooser_element.find(".low_res_button").click(function() {
			updateAllPlayerQualityAndDisplay(QUALITY_LOW_RES);
		});
		chooser_element.find(".high_res_button").click(function() {
			updateAllPlayerQualityAndDisplay(QUALITY_HIGH_RES);
		});
		
		
	} else {
		options.displayPlayer(cookie_quality);
	}
}


var videoBaseUrl = "/medias/videos/";
var videoLowResPath = videoBaseUrl+"lowRes/";
var videoHighResPath = videoBaseUrl+"hiRes/";

var QUALITY_HIGH_RES = "high_res";
var QUALITY_LOW_RES = "low_res";


var players = new Array();
function registervplayer(options) {
	
	options.displayPlayer = function (quality) {
		var path = videoLowResPath+options.url;
		if(quality == QUALITY_HIGH_RES) {
			path = videoHighResPath+options.url;
		}
		options.quality = quality;
		options.url = path;
		embedPlayer(options);
	}
	players.push(options);
}
function updateAllPlayerQualityAndDisplay(quality) {
	//alert("displayTheSWFPlayer");
	//domain: 'test2.com',
	//,  secure: false 
	$.cookie('video_quality', quality, { expires: 7, path: '/'});
	for(var i = 0;i<players.length;i++) {
		players[i].displayPlayer(quality);
	}
	$(".quality_chooser").hide();
	
}

function embedPlayer(options) {
	
	//alert("Embeding video player for tag:"+options.id);
	var controlHeight = 26;
	if(options.backgroundColor == undefined) {
		options.backgroundColor = "#000000";
	}
	
	/*var hiResSize = {'width':340, 'height':260};
	var hiResWideSize = {'width':340, 'height':260};
	var lowResSize = {'width':255, 'height':195};
	var lowResWideSize = {'width':255, 'height':195};*/
	var xywh = {
		"1":
		{
			"wide":
			{
				"high_res":
				{
					"width":380, 
					"height":260,
					"top":20, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":16, 
					"left":10
				}
			},
			"normal":
			{
				"high_res":
				{
					"width":340, 
					"height":260,
					"top":10, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":10, 
					"left":10
				}
			}
			
		},
		"2":
		{
			"wide":
			{
				"high_res":
				{
					"width":380, 
					"height":260,
					"top":20, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":16, 
					"left":10
				}
			},
			"normal":
			{
				"high_res":
				{
					"width":340, 
					"height":260,
					"top":10, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":10, 
					"left":10
				}
			}
			
		},
		"3":
		{
			"wide":
			{
				"high_res":
				{
					"width":380, 
					"height":260,
					"top":12, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":9, 
					"left":10
				}
			},
			"normal":
			{
				"high_res":
				{
					"width":340, 
					"height":260,
					"top":10, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":10, 
					"left":10
				}
			}
			
		},
		"4":
		{
			"wide":
			{
				"high_res":
				{
					"width":380, 
					"height":260,
					"top":7, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":7, 
					"left":10
				}
			},
			"normal":
			{
				"high_res":
				{
					"width":340, 
					"height":260,
					"top":10, 
					"left":10
				},
				"low_res":
				{
					"width":255, 
					"height":195,
					"top":10, 
					"left":10
				}
			}
			
		}
		
	};
	
	/*var size;
	if(options.quality == QUALITY_LOW_RES) {
		if(options.isWide) {
			size = lowResWideSize;
		} else {
			size = lowResSize;
		}
	} else {
		if(options.isWide) {
			size = hiResWideSize;
		} else {
			size = hiResSize;
		}
	}*/
	
	size = xywh[options.bg][(options.isWide?"wide":"normal")][options.quality];
	
	var divHeight = parseInt($("#"+options.id+"_container").css("height"));
	//alert("divHeight = "+divHeight);
	var top = ((divHeight-controlHeight - size.height)/2);
	//alert("top:"+top);
	
	//top = size.top;
	
	var backgrounImagePath = '/medias/videos/images2/player'+(options.isWide?'Wide':'')+'Bg'+(options.bg)+(options.quality==QUALITY_LOW_RES?'lr':'')+'.png';
	//alert("backgrounImagePath = "+backgrounImagePath);
	flowplayer(
			options.id, 
			{"src":"/medias/videos/flowplayer-3.2.4-0.swf","bgcolor":"#FFFFFF"}, 
			{
				"buffering": false,
				"clip":
				{
					"url":options.url,
					"autoPlay": false,
					"autoBuffering": true,
					"scaling":"orig"
					/*"scaling":"orig"*/
					/*"onFinish": function(){ this.getPlugin("play").hide(); }*/
			   	},
				"canvas":
				{
					'backgroundGradient': 'none', 
					'backgroundGradient': 'none',
					'backgroundImage': 'url('+backgrounImagePath+')'
					/*'border':'2px solid #FF0000',*/
					/*"backgroundColor":"#FFFF00", 'backgroundGradient': 'none'*/},
				"screen":
				{
					"height":size.height,
					"width":size.width,
					"top": size.top
					
					
					/*"height":"100pct",*/
					/*"top":0,*/
					/*"backgroundColor":"#0000FF",
					'backgroundGradient': 'none', */
					
				},
				"logo":
				{
					opacity:0.1
				},
				"play":
				{
					/* #FFEA00 */
					/*opacity:0,*/
					replayLabel: options.lang=="fr"?'Encore':'Play Again'
				},
				"plugins":
				{
					"controls":
					{
						"borderRadius":"0px",
						"timeColor":"#666699",
						"bufferGradient":"none",
						"slowForward":true,
						/*"backgroundColor":"rgba(0, 0, 0, 1)",*/
						"backgroundColor": options.backgroundColor,
						"volumeSliderGradient":"none",
						"slowBackward":false,
						"timeBorderRadius":20,
						"time":true,
						"progressGradient":"none",
						"height":controlHeight,
						"volumeColor":"#666699",
						"tooltips":
						{
							"marginBottom":5,
							"scrubber":true,
							"volume":true,
							"buttons":false
						},
						"opacity":1,
						"fastBackward":false,
						"timeFontSize":12,
						"bufferColor":"#ccccff",
						"border":"0px",
						"volumeSliderColor":"#666699",
						"buttonColor":"#666699",
						"mute":true,
						"autoHide":
						{
							"enabled":true,
							"hideDelay":500,
							"hideStyle":"fade",
							"mouseOutDelay":500,
							"hideDuration":400,
							"fullscreenOnly":true
						},
						"backgroundGradient":"none",
						"width":"100pct",
						"display":"block",
						"sliderBorder":"1px solid rgba(128, 128, 128, 0.7)",
						"buttonOverColor":"#666699",
						"fullscreen":false,
						"timeBgColor":"rgb(0, 0, 0, 0)",
						"scrubberBarHeightRatio":0.2,
						"bottom":0,
						"stop":false,
						"zIndex":1,
						"sliderColor":"#666699",
						"scrubberHeightRatio":0.6,
						"tooltipTextColor":"#666699",
						"sliderGradient":"none",
						"timeBgHeightRatio":0.8,
						"volumeSliderHeightRatio":0.6,
						"name":"controls",
						"timeSeparator":" ",
						"volumeBarHeightRatio":0.2,
						"left":"50pct",
						"tooltipColor":"666699",
						"playlist":false,
						"durationColor":"#666699",
						"play":true,
						/*"play":{opacity:0},*/
						"fastForward":true,
						"timeBorder":"0px solid rgba(0, 0, 0, 0)",
						"progressColor":"#666699",
						"volume":true,
						"scrubber":true,
						"volumeBorder":"1px solid rgba(128, 128, 128, 0.7)",
						"builtIn":false
					}
					
					/*"content":
					{
						url: 'flowplayer.content-3.2.0.swf',
						backgroundColor: '#FF0000', backgroundGradient: 'none', border: 0
					}*/
					/*"rtmp":
					{
						"netConnectionUrl":"rtmp://vod01.netdna.com/play",
						"url":"http://builds.flowplayer.netdna-cdn.com/flowplayer.rtmp-3.2.3.swf"
					}*/
				}
			}
		);
}

