	var locales = ["pt-br"];
	var rebufferCount = 0;
	var players = [];
	
	function getVideoTitle(vidid){
		return $('#' + vidid).parents('#videoblock').find('.video-title').text();	
	}
	
	function resetCCData(){
		$("div.locale p.current").removeClass("current");
	}
	
	function getCCdata(t, lang){
		//where to start
		var ccout = ""
		var locale = $("div#captions div#" + lang)
		var next = ((locale.children("p.current").length) ? locale.children("p.current") : locale.children("p:first"));		
		
		if(next.attr("start") < t && next.attr("end") < t)
			for(next = next.next(); next.length > 0; next = next.next())
				if(next.attr("start") < t && next.attr("end") > t)
					break;
		
		if(next.attr("start") < t && next.attr("end") > t){
			next
				.addClass("current")
				.siblings(".current")
					.removeClass("current");
					
			if (next.children("span.st-capt").html() != null)
				 ccout = next.children("span.st-capt").html();
		}
		
		return ccout;
		
	}
	
	
	function processTimePosition(evt){
		var cc = ""	
		for(locale in locales){
			var ccdata = getCCdata(evt.position, locales[locale]);
			cc += ccdata;
			if (ccdata.length > 0)
				cc += "<br>";
		}
		
		if (cc.length > 0){
			$("#" + evt.id).get(0).sendEvent("CAPTION", cc);
		}else{
			$("#" + evt.id).get(0).sendEvent("CAPTION", "");
		}
	}
	
	var totalbuffers = 0;
	//watch the states and take any neccessary actions
	function processStateChange(evt){
		switch(evt.newstate){
			case "BUFFERING":
				if (evt.oldstate == "PLAYING"){
					totalbuffers++;
					if (totalbuffers > 1){
						domain = document.domain.substring(document.domain.indexOf('.'));
						var expire = new Date();
						expire.setDate(expire.getDate() + 1);
						document.cookie ='hswi_bufferdl=true; domain=' + domain + '; expires=' + expire.toString() + '; path=/';
					}
				}
				break;
			case "PLAYING":
				s.Media.play(getVideoTitle(evt.id),evt.currentpos)
				break;
			case "PAUSED":
				s.Media.stop(getVideoTitle(evt.id),evt.currentpos) 
				break;
			case "COMPLETED":
				s.Media.close(getVideoTitle(evt.id)) 
				resetCCData();
				break;	
			
		}	
		getVideoTitle(evt.id);	
	}
	
	//used to turn locales on and off in the subtitiles area
	function toggleLocale(locale){
		var pos = $.inArray(locale, locales);
		if ($.inArray(locale, locales) == -1){
			locales[locales.length] = locale;
		}else{
			$("div#captions div#" + locale).children(".current").removeClass("current")
			locales.splice(pos, 1);
		}	
	}
	
	//listen for the player and assign proccesing actions after it is ready
	function playerReady(obj) {
		var player = $("#" + obj['id']).get(0);
		player.addModelListener("TIME","processTimePosition");
		player.addModelListener("STATE","processStateChange");
		players[obj['id']] = true;
	}
	
	
	$(document).ready(function(){
		//create click events to switch between the languages
		$("#captionnav li a").click(function(){
				$(this)
					.parent()
					.addClass("current")
					.siblings()
						.removeClass("current")
				
				if ($(this).attr("rel"))
					$($(this).attr("rel")).siblings().hide().end().show();
			})
			
		//assign action to any playvideo buttons
		$(".playvideo").click(function(){
			
			video = "#video-block-" + $(this).parent().attr("rel");
			//$('#video-header').pngfix();
			//$('#video-callout').pngfix();
			//$('.videoplayer-float #videoblock').pngfix({ repeatMethod: "stretch" });
			if ($("#videomask").length == 0){
				maskh = ($(window).height() > $(document).height()) ? $(window).height() : $(document).height();
				maskw = ($(window).width() > $(document).width()) ? $(window).width() : $(document).width();
				$("<div></div>").attr("id", "videomask").height(maskh).width(maskw).hide().css("filter", "alpha(opacity=70)").appendTo("body");
			}else{
				
			}
			$("#videomask").show(1, function(){		
				//find the center of the window
				cx = ($(window).width()/2) - ($(video).width()/2) + $(document).scrollLeft();
				cy = ($(window).height()/2) - ($(video).height()/2) + $(document).scrollTop();
				
				if (cy < 0) cy = 10;
				
				
				$(video)
					.appendTo("body")
					.css("position", "absolute")
				 	.css("left",  cx + "px")
					.css("top", cy + "px")
					.slideDown("fast");
			});
			playVideoWhenReady($(video).find(".videoplayerposition").children("#object|#embed").attr('id'));
			return false;
			
		});
		
		
		//assign action to any close video buttons
		$(".closevideo").click(function(){
			stopVideoWhenReady( $(this).parent().parent().find("#videoblock .videoplayerposition").children("#object|#embed").attr("id") );
			$(this).parents(".videoplayer-float").slideUp("fast", function(){$("#videomask").hide()});
			return false;			
		});
	});
	
	function registerVideo(videoId){
		players[videoId] = false;	
	}
	
	function playVideoWhenReady(videoId){
		if(players[videoId] && typeof($("#" + videoId).get(0).sendEvent) == "function"){
			$("#" + videoId).get(0).sendEvent("PLAY", true);
		}else
			setTimeout("playVideoWhenReady('" + videoId + "')", 500)
	}
	function stopVideoWhenReady(videoId){
		if(players[videoId] && (typeof($("#" + videoId).get(0).sendEvent) == "function")){
			
			$("#" + videoId).get(0).sendEvent("STOP");
		}else
			setTimeout("stopVideoWhenReady('" + videoId + "')", 500)	
	}
	
	
	