var flowHelper = new function()
{
	this.list = [];
	this.FLOWSEL = "#flowplayer";
	this.flowPlayerFound=false;
	this.flowPlayerAPI=null;
	this.developmentMode = false;
	this.cdmode = false;
	this.language = null;

	this.init = function()
	{
		$(document).ready(function(){
			
			try
			{
				$('.requestinfo,.sharethis,.popup-25-year,.content-page-navigation img').ifixpng();	
			}
			catch ($e)
			{
			}

			flowHelper.language  = $("html").attr("lang");

			//Hide Elements Not Necessary for CD
			if(flowHelper.cdmode)
			{
				$("#top-toolbar").remove();
				$(".popup-25-year").remove();
				$("#gooogle-analytics").remove();
				$("#requestmoreingoform").remove();
				$("#menuarea").css("margin-top","44px");
				$("#menuarea a[href='/en/brandit.html']").remove();
				$("#menuarea a[href='/medialibrary/']").remove();
				$("#menuarea a[href='http://www.i3onlinetraining.com']").remove();
				$("#menuarea a[href='/en/news.html']").remove();
				$("#menuarea a[href='/es/news.html']").remove();
			}
			else
			{
				//$("#requestmoreingoform").remove();
			}
			//
			
			flowHelper.flowPlayerFound = $(flowHelper.FLOWSEL).length>0;
			
			if(!flowHelper.flowPlayerFound)
			{
				flowHelper.initRequestInfo();
				return;
			}
			flowHelper.initPlayer();
		});
	}

	this.initRequestInfo = function()
	{
		$("a.requestinfo").click(function(){
			aweberform_url = "../fragments/aweberform.html";
			config = {width:640,height:700};
			$container = "<div id='request-form-wrapper' title='Request more information...'></div>";
			if($(this).parent().hasClass("livechatmenu"))
			{
				aweberform_url = "../fragments/aweberform_short.html";
				config = {width:400,height:380};
				$container = "<div id='request-form-wrapper' title='Request a callback...'></div>";
			}

			$(this).append($container);
			$("#request-form-wrapper").load(aweberform_url);
			$("#request-form-wrapper").dialog(config);
			return false;
		});
	}

	this.getURL = function($url)
	{
		if(this.cdmode)
		{
			$url = "../media/videos/local" + $url.substr($url.lastIndexOf("/"));
		}

		return $url;
	}

	this.initPlayer = function()
	{	
		//alert("test-initPlayer()");
	
		if(this.flowPlayerFound)
		{
			//alert("test-flowPlayer");

			$("#flowplayer").wrap("<div id=\"flowplayer-wrapper\"></div>");//.css("border","1px solid blue");

			this.addVideoTools("#flowplayer-wrapper");

			$(".resolution").click(function(){$(".resolution-picker").toggle()});

			$hrefsave = $href = $(this.FLOWSEL).attr("href");
			if($href.substr(0,1) == "#")
			{
				$href = $href.substr(1);
				videoArray = this.findVideo($href);

				$url = videoArray[0];
				if($url!="" && $url!=null)
				{
					$url = this.getURL($url);
					
					$(this.FLOWSEL).attr("href",$url);
					$(this.FLOWSEL).attr("title",$url);

					//Add Resolutions to resolution picker
					if(!this.cdmode)
					{
						$("#flowplayer-controls .resolution-picker").html('');
						for(ii=0; ii<videoArray[2].length; ii++)
						{
							$("#flowplayer-controls .resolution-picker").append(videoArray[2][ii]);
						}
						$("#flowplayer-controls .resolution").html(videoArray[1]);

						//resolution click event
						$("#flowplayer-controls .resolution-picker a[rel]").click(function(){
							$("#flowplayer-controls .resolution-picker a").removeClass("selected");
							$(this).addClass("selected");
							$(".resolution-picker").hide();
							$("#flowplayer-controls .resolution").html($(this).clone());
							if(flowHelper.flowPlayerAPI!=null)
							{
								flowHelper.flowPlayerAPI.play($(this).attr("rel"));
							}
						});
					}
				}
				else
				{
					$(this.FLOWSEL).html("<div style='border:1px solid red;padding:15px;xcolor:red;padding:5px'>VideoTag not found:["+$hrefsave+"]</div>");
				}
			}
			
			if(this.developmentMode)
			{
				$str = "<div style='border:2px solid red;padding:5px;width:380px;position:absolute;left:5px;top:40px;font-size:8pt'>";
				$str += "<b>Video Disabled:</b> [Development Mode]<br/>";
				$str += "<b>URL:</b>" + $url;
				$str += "</div>";
				$("#flowplayer").html($str);
			}
			else
			{
				$width = this.cdmode ? '100%':'82%';
				this.flowPlayerAPI = flowplayer("flowplayer",
					{
						src:"/media/swf/flowplayer-3.2.7.swf",
						wmode:'opaque'
					},
					{	
						plugins:{
							controls:{
								url: "flowplayer.controls-3.2.5.swf",
								left:0,
								width:$width,
								background: '#666666',
								autoHide:true,
								onShowed:function()
								{
									$("#flowplayer-controls").show();
								},
								onHidden:function()
								{
									$("#flowplayer-controls").hide();
								}
							}
						}
					}
				);
			}
		}
		else
		{
			alert("flowPlayer not found!");
		}
	}

	this.addVideoTools = function($target)
	{
		if(!this.cdmode)
		{
			$shareThis = "Share this";
			$requestInfo = "Request info";

			switch(this.language)
			{
				case "es":
					$shareThis = 'Compartir';
					$requestInfo = 'Solicitud de informaci&#0243;n';
					break;
				default:
					$shareThis = 'Share this';
					$requestInfo = 'Request info';
					break;
			}
			
			//$shareThisButton = "<a href='#' class='sharethis'><span class='st_sharethis' displayText='"+$shareThis+"'></span></a>";
			$shareThisButton = "<a href='#' class='st_sharethis_custom'>"+$shareThis+"</a>";
			//myShareThisButton = '<a class="sharethis">'+$shareThis+'</a>';
			$($target).append('<div id="flowplayer-controls"><div><div class="resolution-picker"><a>360p</a><a>240p</a></div><a class="resolution">240p</a></div></div>');
			$($target).append('<div id="share-controls"><div>'+$shareThisButton+'<a class="requestinfo" href="request_moreinfo.html" target="_blank">'+$requestInfo+'</a></div></div>');
		}
		//
	}

	this.findVideo = function(title)
	{
		_list = [];
		default_url = null;
		default_link = null;
		for(i=0; i<videoList.items.length; i++)
		{
			item_title = videoList.items[i][0].toLowerCase();
			item_res = videoList.items[i][1].toLowerCase();
			if((title.toLowerCase() == item_title))
			{
				
				list_url = this.getURL(videoList.items[i][3]);
				list_res = videoList.items[i][1].split("x")[1]+"p";
				list_bitrate = videoList.items[i][2];				
				
				if(item_res == "428x240")
				{
					list_link = '<a rel="'+list_url+'" class="selected">' + list_res + ' ' + list_bitrate + 'Kbs</a>';
					default_url = list_url;
					default_link = list_link;
				}
				else
				{
					list_link = '<a rel="'+list_url+'">' + list_res + ' ' + list_bitrate + 'Kbs</a>';
				}

				_list.push(list_link);
			}			
		}
		return [default_url,default_link, _list];
	}

	this.init();
}
