(function($) {	
	var pos = null;
	var yOffset = 0;
	var file = null;
	var baseHref, downloadDirectory;
	
	function startMovie() {
		$('#videoOverlay').remove();		
		file = $(this).attr('href');
		
		var aF = file.split("/");
		downloadDirectory = "/files/aa/downloads/"+aF[aF.length-2]+"/";
				
		file = file.substr(file.lastIndexOf('/')+1);
		pos = $(this).position();
		pos.width = $(this).width();
		$('#mask').remove();
		$('<div id="mask"></div>').appendTo('body').css({ visibility:'visible', opacity:0 }).fadeTo(400, .7, function() {
			var vo = $('<div id="videoOverlay"></div>').css({ zIndex:500, position:'absolute', top:0, left:0  }).appendTo('body');
			if(pos.top+437>$('body').height()) yOffset = $('body').height()-pos.top-337; else yOffset = 0;
			if($.browser.msie && $.browser.version<7) { var img = $('<img id="videoBackground" src="/styles/images/videoPlayerBackground.png" />').appendTo(vo).css({ height:'100px', position:'absolute', left:(970/2)-70, top:pos.top+130-yOffset}); startFlash(); }
			else $('<img id="videoBackground" src="/styles/images/videoPlayerBackground.png" />').css({ position:'absolute', left:(pos.width/1.5)+pos.left, top:pos.top+(170/2)+130, width:0, height:0  }).appendTo(vo).animate({ width:'527px', height:'337px', left:(970/2)-60, top:pos.top+130-yOffset }, 450, startFlash);
		}).click(endMovie);
		return false;	
	}
	
	function startFlash() {
		var bPos = $('#videoBackground').position();
		$('<div id="videoFlash"></div>').appendTo('#videoOverlay');	
		swfobject.embedSWF("/flash/videoPlayer.swf?v=1.2.0", "videoFlash", "543", "337", "9.0.0","/flash/expressInstall.swf", { filename:file, baseHref:baseHref, downloadDirectory:downloadDirectory }, { allowFullScreen:true, bgColor:"#000000", menu:"false", wmode:"transparent" }, { style:"position:absolute; top:"+(bPos.top-2)+"px; left:"+(bPos.left-4)+"px;" });	
		$('<img id="videoClose" src="/styles/images/videoPlayerClose.gif" alt="Close" />').css({ position:'absolute', left:(970/2)-75+490, top:pos.top+130-yOffset+3, cursor:'pointer', opacity:0, zIndex:1000 }).appendTo('#videoOverlay').click(endMovie).hover(function() { $(this).css('opacity', 1); }, function() { $(this).css('opacity', .7); }).fadeTo('normal',.7);
	}
	
	function endMovie() {
		if($.browser.msie && $.browser.version<7) { $('#videoOverlay').remove(); return true; }
		$('#videoClose').remove();
		$('#videoFlash').fadeOut(100, function() {
			$('#videoOverlay img').animate({ width:0, height:0, left:(pos.width/1.5)+pos.left, top:pos.top+(170/2)+130 }, 350, function() {
				$('#videoOverlay').remove();
				$('#mask').fadeTo(400, 0, function() { $('#mask').remove(); });	
			});
		});
	}
	
	function _escapePlayer() {
		return (navigator.userAgent.match(/iPad/i) || 
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/Android/i) || 
				navigator.userAgent.match(/GoogleTV/i));
	}
	
	$(document).ready(function() {
		if(_escapePlayer()) return;
		
		var loc = String(window.location).split("/");
		baseHref = loc[0]+"//"+loc[2]+"/";
		$('a[@href$=flv],a[@href$=mp4]').click(startMovie);
	});
})(jQuery);
