//handle popup closing by the end user, stop streaming before closing the popup, will prevent browser crashing
window.onbeforeunload = beforeUnload;

function beforeUnload() {
		if (getFlashMovie("swfPlayer") != undefined) stopVideo();
}

//get a flash applet instance within the DOM
function getFlashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) { 
    	return window[movieName];  
	}
	else {
		if (document[movieName] != undefined) {
			if (document[movieName].length != undefined) {
				return document[movieName][1];
			}
			return document[movieName];
		}
		else
			return undefined;
	}
}

//resize player popup to the correct player size
function adjustWindowSize(iWidth, iHeight){
	iWidth = Number(iWidth);
	iHeight = Number(iHeight);
    var resizeRef = document.getElementById('resizeReference');
	
    if (resizeRef) {
		var iPreWidth = resizeRef.offsetWidth;
        var iPreHeight = resizeRef.offsetHeight;
		
		if ( iWidth > 0.9 * iPreWidth && iWidth < 1.1 * iPreWidth &&
			iHeight > 0.9 * iPreHeight && iHeight < 1.1 * iPreHeight)  {
			//nothing
		}
		else {		       
	        window.resizeTo(iPreWidth,iPreHeight);
    	    var iPostWidth = resizeRef.offsetWidth;
        	var iPostHeight = resizeRef.offsetHeight;

	        iOuterWidth = iWidth + (iPreWidth-iPostWidth);
    	    iOuterHeight = iHeight + (iPreHeight-iPostHeight);
		
			var x = (screen.width/2)-(iOuterWidth/2);
			var y = (screen.height/2)-(iOuterHeight/2);
			window.moveTo(x, y);	

		    window.resizeTo(iOuterWidth, iOuterHeight);
		}
    }
	
	//remove preloader table
	if (document.getElementById("preloader"))
		document.getElementById("preloader").style.display = "none";
}

function maximize() {
	window.moveTo(0,0);
	window.resizeTo(screen.availWidth, screen.availHeight);
}

function configXMLError(error) {
	var msg = '';
	
	switch (error) {
		case "file_not_found":
						msg = "configuration file not found";
						break;
		case "invalid_data":
						msg = "invalid configuration file";
						break;
	}
	var divConfig = document.getElementById("config");
	if (divConfig) divConfig.innerHTML = "Configuring player ...<br>" + msg;
}

//DS3 PLAYBACK FUNCTIONS
function playVideo() {
	getFlashMovie("swfPlayer").playVideo();
}

function pauseVideo() {
	getFlashMovie("swfPlayer").pauseVideo();
}

function stopVideo() {
	getFlashMovie("swfPlayer").stopVideo();
}

function resumeVideo() {
	getFlashMovie("swfPlayer").playVideo();
}

//to be implemented
function seekVideo(pos) {
	getFlashMovie("swfPlayer").seekVideo();
}

//ENSURE DS2 COMPATIBILITY AND ADMIN AREA EXISTING JS FUNCTIONS COMPATIBILITY
function pause() {
	pauseVideo();
}
function stop() {
	stopVideo();
}
function play() {
	playVideo();
}
function resume() {
	resumeVideo();
}
function closeAndResumePlay() {
	resumeVideo();
}
function seek(pos) {
	seekVideo(pos);
}
function closeAndStartPlay(pos) {
	seekVideo(pos);
}



//BROWSER IDENTIFICATION FUNCTIONS


//RIGHT CLICK DISABLE
//disable right click flash contextmenu
var message = "Copyright Point Marketing";
function NoRightClick(b) {
   if(((navigator.appName=="Microsoft Internet Explorer")&&(event.button > 1))
   ||((navigator.appName=="Netscape")&&(b.which > 1))){
   alert(message);
   return false;
   }
}
document.onmousedown = NoRightClick;
