var newWin = null;
function openWin(url, w, h) {
	var props = "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,personalbar=no,copyhistory=no,resizable=1,scrollbars=yes";
	var handle = "oNewWin";
	if(newWin && !newWin.closed) {
		newWin.focus();
	}
	newWin = window.open(url, handle, props);
}

function writeSwf(src, width, height) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">');
	document.write('<param name="movie" value="' + src + '">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<embed src="' + src + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" wmode="transparent"></embed>');
	document.write('</object>');
}

function writeSwfToElement(target, src, width, height, id, prefix) {
	var file = src.substring( src.indexOf('?video=') + 7, src.length);
	var path = "http://www.eloyalty.com/video/320x180player.swf?video=" + file;
	//var path = "http://192.168.32.29/video/320x180player.swf?video=" + file;
		
	var sID = (id) ? ' id="' + id + '"' : '';
	var sPrefix = (prefix) ? prefix : ''
	var sHtml = sPrefix;
		sHtml += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '"' + sID + '>';
		sHtml += '<param name="movie" value="' + path + '">';
		sHtml += '<param name="quality" value="high">';
		sHtml += '<param name="wmode" value="transparent">';
		sHtml += '<embed src="' + path + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"' + sID + ' wmode="transparent"></embed>';
		sHtml += '</object>';
	document.getElementById(target).innerHTML = sHtml;
}

function launchVideo(src) {
	window.open(src, 'video', 'width=430,height=270,menubar=no,toolbar=no,personalbar=no,copyhistory=no');
	return false;
}

$(document).ready(
	function() {
		setupVideoPlayer();
	}
);


function setupVideoPlayer() {
	var bLocated = false;
	// loop over links in doc
	$('A').each(
		function() {
			// locate all links that point to the player swf and attach click event
			if($(this).attr('href') && $(this).attr('href').indexOf('320x180player.swf') != -1) {
				// indicate that an instance was found so that the container div #videoPlayer will be appended ot the document
				bLocated = true;
				$(this).bind('click',
					function() {
						// close link
						var htmlCloseLink = '<a href="#" onclick="emptyVideoPlayer(); return false;">CLOSE</a>';
						writeSwfToElement('videoPlayer', this.href, 320, 218, "player", htmlCloseLink);
						$('#videoPlayer').show();
						return false;
					}
				);
			}
		}
	);
	
	if(bLocated) {
		$('#main').prepend('<div id="videoPlayer"></div>');
	}
}

function emptyVideoPlayer() {
	$('#videoPlayer').empty().hide();
}