var vtrigger;

function loadSWF(player_url, targetID, flashvars, params, attributes){
	//Check for existing SWF
	if(isObject(targetID)){
		//replace object/element with a new div
		replaceSwfWithEmptyDiv(targetID);
	}
	//Embed SWF
	var obj = swfobject.embedSWF(player_url, targetID, "100%", "100%", "9.0.0", "/static/swf/expressInstall.swf", flashvars, params, attributes);
}

function isObject(targetID){
	var isFound = false;
	var el = document.getElementById(targetID);
	if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")){
		isFound = true;
	}
	return isFound;
}

function replaceSwfWithEmptyDiv(targetID){
	var el = document.getElementById(targetID);
	if(el){
		var div = document.createElement("div");
		el.parentNode.insertBefore(div, el);
		//Remove the SWF
		swfobject.removeSWF(targetID);
		//Give the new DIV the old element's ID
		div.setAttribute("id", targetID);
	}
}

function close_video_overlay() {
	vtrigger.close();
}


jQuery(function(){
					 
	// VIDEO OVERLAY
	$("a.video-overlay").click(function(e) {
		
		e.preventDefault();
		var that = $(this);
		if (!$("#video-overlay").length) {
			var div = "<div id='video-overlay' style='margin:0 auto;display:none;width:100%;'></div>";
			$("body").append(div);
			vtrigger = $("#video-overlay").overlay({
    				expose: { 
        				color: '#111',
        				loadSpeed: 200,
        				opacity: 0.9
    				}, 
				top: "0", left: "0",
				api: true,
				closeOnClick: false,
				onClose: function() {
					var wrap = $("#video-overlay");
					replaceSwfWithEmptyDiv("VideoPlayer");
					wrap.children().remove();
				}
			});
		}
		vtrigger.onBeforeLoad = function() {
			var wrap = $("#video-overlay");
			wrap.show();
			wrap.load(that.attr("href"));
		};
		vtrigger.load();
		return false;

	});

});

