// This files contains javascript functions related to the Octoshape Plug-In.
// The file is included by all broadcasters. 

var install_link;
var group   = "";   // holds the group

var error1 = "Octoshape Streaming Services is currently not plugged into your system or\nis not plugged in correctly, please re-install\nIf you just installed it, you may have to wait a few seconds for it to start up."

function isDisabled(theChannel){
	var disabled = new Array();
	
	// DISABLELING CHANNELS ON THE BROADCASTERS WEB PAGE (use the full uniqe channel name):
	//disabled[0] =  "WMSLUK.wmsluk";
	//disabled[1] =  ".100fm";
	//disabled[2] =  ".voice";
	//disabled[3] =  "HHUK.hhuk_mp3";
	//disabled[4] =  "MPLUSM.mplus_mp3";
	//disabled[5] =  "DW.dw";
	//disabled[6] =  "RISE.rise_mp3";

	for (i=0;i<disabled.length;i++){
		if (theChannel == disabled[i]){
			alert("The stream is temporarily disabled.\n   Try again later.");
			return true;
		}
	}
	return false;
}

// Call this function to play
// for instance call it in the 'onclick' of a button element.
function octoshape_play(lnk){
	var variant = lnk.substring(lnk.indexOf(":")+1, lnk.indexOf("."));
	install_link = 'http://www.octoshape.com/download/external_install.asp?variant='+variant;

	octoshape_play_custom(lnk, error1, install_link, 400, 520, 'http://www.octoshape.com/play/startup_popup.asp', 300, 400);
}


// Call this function to play
// for instance call it in the 'onclick' of a button element.
// width and height are in pixels
function octoshape_play_custom(lnk, alertTextNoPlugIn, 
						webPageGetPlugInAddress, webPageGetPlugInHeight, webPageGetPlugInWidth, 
						webPageStartupAddress, webPageStartupHeight, webPageStartupWidth){

	install_link_size = "height="+webPageGetPlugInHeight+",width="+webPageGetPlugInWidth;
	startup_link_size = "height="+webPageStartupHeight+",width="+webPageStartupWidth;

	octoshape_play_custom2(lnk, alertTextNoPlugIn, webPageGetPlugInAddress, install_link_size, webPageStartupAddress, startup_link_size);
}

// Call this function to play
// for instance call it in the 'onclick' of a button element.
// width and height are in pixels
function octoshape_play_custom2(lnk, alertTextNoPlugIn, 
						webPageGetPlugInAddress, webPageGetPlugInWindowFeatures, 
						webPageStartupAddress, webPageStartupWindowFeatures){

	var channel = lnk.substring(lnk.indexOf(":")+1, lnk.length);
	if (isDisabled(channel)) return;

	var result = -1;
	
	// if the octoclient is installed we play, else we promt for install
	if (get_octoclient()){
		result = client.getStatus(true);	
		if (result == 2) { // everything is fine
			window.location.href = lnk;
		}else { // client is not started up. 
			// configuring the startup object
			sub_startup_object = config_startup_obj_external(lnk, 
															webPageGetPlugInAddress, webPageGetPlugInWindowFeatures, 
															webPageStartupAddress, webPageStartupAddress, webPageStartupWindowFeatures,
															alertTextNoPlugIn);
			// starting up
			sub_startup_link_rec();
		}
	}
	// client has not been set, that is: octoshape is not installed
	else {
		window.open(webPageGetPlugInAddress, "octoinstall", webPageGetPlugInWindowFeatures);
	}
}


function octoshape_play_relay(lnk, httplnk){
	var variant = lnk.substring(lnk.indexOf(":")+1, lnk.indexOf("."));
	install_link = 'http://www.octoshape.com/download/external_install.asp?variant='+variant;
	var result = -1;
	
	// if the octoclient is installed we play, else we promt for install
	if (get_octoclient()){
		result = client.getStatus(true);
		changeDiv('octo_headline', 'Automaticly Checking for Octoshape Plug-In ... ');
				
		if (result == 2) { // everything is fine
			window.location.href = "OCTOSHAPE:"+lnk;
			setTimeout("window.location.href = '"+httplnk+"';", 500);
		}else { // client is not started up. 
			// setting the object sub_startup_object for use in the function sub_startup_link_rec
			sub_startup_object = config_startup_obj_inplayer("ERROR", lnk, install_link);
			sub_startup_object.playlink = function (){
				obj.message("Beginning to play in high quality ... ");
				window.location.href = "OCTOSHAPE:"+lnk;
				setTimeout("window.location.href = '"+httplnk+"';", 1000);
			};
			sub_startup_link_rec();
		}
	}
	else{
		sub_play_in_mplayer_install(install_link);
	}
}


// the page we are on is loacated inside a media player and we wish to stay there
function octoshape_play_relay_inside_mplayer(lnk, httplnk){
	var variant = lnk.substring(lnk.indexOf(":")+1, lnk.indexOf("."));
	install_link = 'http://www.octoshape.com/download/external_install.asp?variant='+variant;
	var prelink = "http://www.octoshape.com/broadcasters/asx.asp?httplink=" + httplnk +"&localplaylink=";
	var result = -1;
	
	// if the octoclient is installed we play, else we promt for install
	if (get_octoclient()){
		result = client.getStatus(true);
		changeDiv('octo_headline', 'Automaticly Checking for Octoshape Plug-In ... ');
		
		if (result == 2) { // everything is fine
			window.location.href = prelink + client.getLink(lnk, false);
		}else { // client is not started up. 
			// setting the object sub_startup_object for use in the function sub_startup_link_rec
			sub_startup_object = config_startup_obj_inplayer(prelink, lnk, install_link);
			sub_startup_link_rec();
		}
	}
	else{
		sub_play_in_mplayer_install(install_link);
	}
}
// this is set on the relay page
var play_in_mplayer_install_not_completed = false;
function sub_play_in_mplayer_install_not_completed_open(){
	if (play_in_mplayer_install_not_completed){
		window.location.reload();
	}
	else{
		window.location.href = window.location.href + "&installnotcompleted=true";	
	}
}

// if no_install_popup=true then we do not make the popup
function sub_play_in_mplayer_install(install_link){
	var message = "Get the Octoshape Plug-In to play: <a href=\""+install_link+"\" target=\"_blank\">Get Plug-In</a>."+
		"<BR>When the Plug-In is in place press here to play: <a href='#' onclick='javascript: sub_play_in_mplayer_install_not_completed_open();'>PLAY</a>.";

	if (play_in_mplayer_install_not_completed){
		changeDiv("octo_message", message + "<BR>If you just plugged in Octoshape, you may have to wait a few seconds for the plug-in to start up");
	}
	else {
		changeDiv("octo_message", message);
		window.open(install_link, "octoinstall", "height=400, width=520");
	}
}

function get_octoclient(){
	if (sub_get_octoclient()){ 
		//alert("octoclient called, returned true");
		return true;
	}
	else {
		var text = navigator.userAgent.toLowerCase();
		if (text.indexOf("firefox") > 0){ 
			//alert("refreashing plugins");
			navigator.plugins.refresh(false);
			return (sub_get_octoclient());
		}
		//alert("octoclient called, returned false");
		return false;
	}
}
	
// if broadcaster= false, then install_link is not used
// after_link is some (optional) web page where we move to when we are up and playing
function config_startup_obj_inplayer(prelink, lnk, install_link){
	var obj = new Object();
	
	obj.error = function(withStandard, text){ 
		if (withStandard)	{ changeDiv('octo_error',error1 + text);  	}
		else				{ changeDiv('octo_error',text);  			}
	}
	
	obj.standard_error = function(){ changeDiv('octo_error',error1); }
	obj.message =  function(text){ changeDiv('octo_message',text); }
	
	//var obj.popup;
	obj.playlink = function (){
		obj.message("Beginning to play in high quality ... ");
		window.location.href = prelink + client.getLink(lnk, false);
	};
	
	obj.force_install = function (){
		sub_play_in_mplayer_install(install_link);
	}

	obj.force_install_kill_player = obj.force_install;

	obj.starting_up = function(){
		obj.message('The Octoshape Plug-In is starting up. The high quality stream will begin in a few seconds.');
	}
	
	obj.starting_up_wait = function(){
		obj.message('The Octoshape Plug-In is starting up. The high quality stream will begin in a few seconds.');
	}

	return obj;
}

// if install_link = "stay" then ...
function config_startup_obj_external(lnk, install_link, install_link_size ,startup_link, startup_link_wait, startup_link_size, standard_error_text){
	var obj = new Object();
	
	obj.standard_error = function(){ alert(standard_error_text); }
	obj.error = function(withStandard, text){ 
		if (withStandard)	{ alert(standard_error_text + text); }
		else 				{ alert(text); }
	}
		
	//var obj.popup;
	obj.playlink = function (){
		if (obj.popup != undefined){
			if(!obj.popup.closed){
				obj.popup.close();
			}
			obj.popup = undefined;
		}
		window.location.href = lnk;	
	};

	if (install_link == "stay"){
		obj.force_install = function(){
			if (!install) { window.location.href = appendQuerystring("install=true"); }
		}
	} else {
		obj.force_install = function(){
			window.open(install_link, "octoinstall", install_link_size);
		}
	}
	
	obj.force_install_kill_player = obj.force_install;

	obj.starting_up = function(){
		if (obj.popup == undefined){
 			obj.popup = window.open(startup_link, "startup_popup", startup_link_size);
		}
	}
	
	obj.starting_up_wait = function(){
		obj.popup = window.open(startup_link_wait, "startup_popup", startup_link_size);
			if (obj.popup.blur){
				obj.popup.focus();
			}
	}
	return obj;
}



// helper function for sub_get_octoclient
function sub_get_octoclient_mimetype() {
	var octomime = "application/x-octoshapeplugin"+group+"-client";

	if (navigator && navigator.mimeTypes && navigator.mimeTypes.length) {
		var mimetype = navigator.mimeTypes[octomime];

		if (mimetype) {
		var oDiv = document.createElement("DIV");
			document.body.appendChild(oDiv);
			oDiv.innerHTML = "<object id=clientobject type=\""+octomime+"\" hidden=\"true\"></object>";
			client = document.getElementById('clientobject');
			if (typeof(client) != "undefined" && typeof(client.getStatus) != "undefined") {
				//alert("Client "+group+" loaded using mimeTypes:" + octomime);
				return true;				
			}
		} 
	}
	return false;
}


// helper function for play_with_octoshape
// checks to se if the Octoshape Plug-In is installed
// works for both IE and firefox
function sub_get_octoclient(){
	//alert("sub_get_octoclient called");

	if (typeof (client) != "undefined") {
		//alert("Client is already defined");
		return true;
	}	
	try {
		if (window.ActiveXObject) {
			try {
				client = new ActiveXObject("octoshapeplugin"+group+".client");
				if (typeof(client) != "undefined") {
					//alert("Client loaded using ActiveXObject");
					return true;
				}
			} catch(exception) {}
		}
		
		if (window.GeckoActiveXObject) {
			try {
				client = new GeckoActiveXObject("octoshapeplugin"+group+".client");
				if (typeof(client) != "undefined") {
					//alert("Client loaded using GeckoActiveXObject");
					return true;
				}
			} catch(exception) {}
		}

		// Crashes opera
		if (false && navigator && navigator.plugins && navigator.plugins.length) {
			client = navigator.plugins["application/x-octoshapeplugin"+group+".client"];
			if (typeof(client) != "undefined") {
				//alert("Client loaded using navigator plugins");
				return true;
			}
		}		
		
		if (navigator && navigator.mimeTypes && navigator.mimeTypes.length) {
			if (sub_get_octoclient_mimetype("")) {
				return true;
			}
		}

		//alert(" Failed all together. ");		
		client = undefined;
		return false;
	} catch (exception) {
		if (sub_get_octoclient_mimetype("")) {
			return true;
		}
	
		client = undefined;

		return false;
	}
}

// These 2 global variables are needed because javascript can not recurs on the arguments
// which we want to do in the following; wait for the octoclient to start.
// the argument link is an octolink (before client.getlink)
var sub_startup_object;  
var sub_startup_error_count = 0;
var sub_startup_wait_count = 0;

function sub_startup_link_rec(){
	var result = -500;
	var step = 0;
	try {
		step = 1;
		result = sub_get_octoclient();
		step = 2;
		//alert(show_props(client,"client"));
		if (!result) {
	 			sub_startup_object.standard_error();
				client = undefined;
				sub_startup_object.force_install();
	 			return;
		}
		step=3;

		if (typeof(client.getStatus) == "undefined") {
			sub_startup_object.error(false, "The octocient plugin is missing a getStatus function. Aborting.");
			sub_startup_object.force_install();
			return;	
		}
		step=4;

		result = client.getStatus(true);
		//alert("client?:"+result);
		step=5;
		switch (result){
	 		case 0:
				step=6;
				sub_startup_object.error(false, "Octoshape is not running");
	 			break;
	 		case 1:
				step=7;
				sub_startup_wait_count = sub_startup_wait_count + 1;
				if (sub_startup_wait_count > 10){
		 			sub_startup_object.starting_up_wait();
					sub_startup_wait_count = -20;
				}
				else{
		 			sub_startup_object.starting_up();
		 		}
				setTimeout("sub_startup_link_rec()",2000);
	 			break;
			case 2:
				step=8;
				sub_startup_object.playlink();
	 			break;
	 		case -1:
				step=9;
				sub_startup_object.standard_error();
				client = undefined;
				sub_startup_object.force_install_kill_player();
	 			break;
	 		default:
				step=10;
	 			sub_startup_object.error(true, "\n\nError no.: " + result + "/"+step);
				client = undefined;
				sub_startup_object.force_install();
		 }
	}
	catch (exception) {
		sub_startup_error_count = sub_startup_error_count + 1;
		if (sub_startup_error_count < 3){
		sub_startup_object.error(false, "An un-expected error occured while trying to start up the Octoshape plug-in. \n"+
				"This is attempt "+sub_startup_error_count+" out of 3\n"+
				"We try to proceed in 5 seconds, please wait ... "+
				"\n\n\nStartup link Exception (result is: " +result+"/ "+step+"): " + exception.description);
				setTimeout("sub_startup_link_rec()",5000);
		}
		else {
			sub_startup_error_count = 0;
			client = undefined;
			sub_startup_object.error(false, "An un-expected error occured while trying to start up the Octoshape plug-in. \n"+
				"Please try to shut down the plug-in and then press play again. \n" + 
				"To shut down: right click the system tray icon and choose exit. \n" + 
				"\n\n\nStartup link Exception (result is: " +result+"/ "+step+"): " + exception.description);						
		}
	}
}




// ************************ OLD *********************************************

/*

// Call this function to play
// for instance call it in the 'onclick' of a button element.
function octoshape_play_old(lnk){
	var variant = lnk.substring(lnk.indexOf(":")+1, lnk.indexOf("."));
	install_link = 'http://www.octoshape.com/download/external_install.asp?variant='+variant;
	
	// if the octoclient is installed we play, else we promt for install
	if (!octocheck(lnk)){
		navigator.plugins.refresh(true);
		if (!octocheck(lnk)){
			window.open(install_link, "octoinstall", "height=400, width=520");
		}
	}
}



function octocheck(lnk){
	var result = -1;
	if (sub_get_octoclient()){
		result = client.getStatus(true);
		
		if (result == 2) { // everything is fine
			try	{ 
				text = navigator.userAgent.toLowerCase();
			
				// firefox will turn the button-page into a blank 'stopped' page
				// so we reload the original href in the end.
				if (text.indexOf("firefox") > 0){
					var tmp = window.location.href; 
					window.location.href = lnk; 
					window.location.href = tmp;
				}
				else {
					window.location.href = lnk; 				
				}
			}
			catch (err){ alert("Octoshape: argument to 'play_with_octoshape' is not valid.\n\nError message: "+err); }
			return true;
		}else { // client is not started up.
			sub_startup_object = get_sub_startup_object_broadcaster(lnk, install_link);
			sub_startup_link_rec();			

			return true;
		}
	}
	else {return false;}
}
*/