/**
 * -----------------------------------------------
 * Scriptfile for WiQet project
 * -----------------------------------------------
 * Programmers:        Sequential Media
 * Company:            Sequential Media
 * Created:            2007-11-21
 * Updated:            -
 * Version:            1.0
 *
 * Build:
 *   Tested IE (6 + 7), FireFox (2.0) and Safari (3.0)
 *
 * Description:
 *   This file displays the WiQet editor and player
 *
 * Comments:
 *   'Cloned' from current Introvoice application renamed to the WiQet project
 *
 */

var IVSubmitForm = false;
var IVForm;

var flash2Installed 	= false;
var flash3Installed 	= false;
var flash4Installed 	= false;
var flash5Installed 	= false;
var flash6Installed 	= false;
var flash7Installed 	= false;
var flash8Installed 	= false;
var flash9Installed 	= false;
var flash10Installed 	= false;
var maxVersion			= 10;
var actualVersion 		= 0;
var hasRightVersion 	= false;
var requiredVersion 	= 7;
var isIE				= (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin				= (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var FormName			= "";
var IVplayerUrl			= "";
var InfoString			= "";
var params				= "";
var TheTags				= "";
/**
 * -----------------------------------------------
 * function: play_wiqet
 * -----------------------------------------------
 *
 * Description:
 *   Function to calculate how nice a colleague is.
 *
 * Input:
 *	 IVDisplayUrl		String	->  Url of page with player
 *	 IVplayerUrl		String	->	Url of swf file, such as player.swf or editor.swf
 *	 IVmodus			String	->	Modus of swf element, possible values 'editor' or 'player'
 *	 IVwiqetCode		String	->	Code of introvoice
 *	 IVcustomerId		String	->	Hashed customerid
 *	 IVuniqueId			String	->	Unique id which can be send while adding a introvoice
 *	 IVwidth			String	->	Width of swf element (with afterfix 'px')
 *	 IVheight			String	->	Height of swf element (with afterfix 'px')
 *	 IValign			String	->	Alignment of swf file
 *	 IVbgColor			String	->	Background color of swf file (hexadecimal value)
 *	 IVdivLink			String	->	Div element which wil show a link to player with current values
 *	 IVdivForm			String	->	Div element which shows form with data from the editor (e.g. wiqetCode)
 *	 IVdivPlayer		String	->	Div element which shows the actual flash file (player / editor)
 *	 IVFormName			String	->	Name of the submit form
 *	 IVFormType			String	->	The type of input fields, possible values 'hidden' or 'text'
 *	 IVdeleteCode		String	->	The delete code
 *	 IVdeletePinCode	String	->	The delete pincode
 *
 * Output:
 *   Returns a string for displaying the editor or player
 *	 Editor modus: returns the values of the introvoice in input fields
 */
function play_wiqet(IVDisplayUrl, IVplayerUrl, IVmodus,
					IVwiqetCode, IVcustomerId, IVuniqueId,
					IVwidth, IVheight, IValign,
					IVbgColor, IVdivLink, IVdivForm,
					IVdivPlayer, IVFormName, IVFormType,
					IVdeleteCode, IVdeletePinCode){

	// Generate the infostring for debug output
	InfoString += "\n-----------------------------------------------------\nValues:\n-----------------------------------------------------\n";
	InfoString += "IVDisplayUrl-->"+IVDisplayUrl+"\n";
	InfoString += "IVplayerUrl-->"+IVplayerUrl+"\n";
	InfoString += "IVmodus-->"+IVmodus+"\n";
	InfoString += "IVwiqetCode-->"+IVwiqetCode+"\n";
	InfoString += "IVcustomerId-->"+IVcustomerId+"\n";
	InfoString += "IVuniqueId-->"+IVuniqueId+"\n";
	InfoString += "IVwidth-->"+IVwidth+"\n";
	InfoString += "IVheight-->"+IVheight+"\n";
	InfoString += "IValign-->"+IValign+"\n";
	InfoString += "IVbgColor-->"+IVbgColor+"\n";
	InfoString += "IVdivLink-->"+IVdivLink+"\n";
	InfoString += "IVdivForm-->"+IVdivForm+"\n";
	InfoString += "IVdivPlayer="+IVdivPlayer+"\n";
	InfoString += "IVFormName-->"+IVFormName+"\n";
	InfoString += "IVFormType-->"+IVFormType+"\n";
	InfoString += "IVdeleteCode-->"+IVdeleteCode+"\n";
	InfoString += "IVdeletePinCode-->"+IVdeletePinCode+"\n-----------------------------------------------------\n";

	// The editor has a higher required flash version
	if (IVmodus == "editor")
	{

		// Check if current version is compatible
		requiredVersion = 8;
		hasRightVersion = f_detectFlash(requiredVersion);

		// Add params to the string
		params += "&uniqueId=" + IVuniqueId;
		params += "&deleteCode=" + IVdeleteCode;
		params += "&deletePinCode=" + IVdeletePinCode;
	}
	else
	{

		// Check the default version set in var 'requiredVersion'
		hasRightVersion = f_detectFlash(requiredVersion);
	}

	// Continue if we have the correct version or higher
	if(hasRightVersion)
	{
		// Add params to the string
		params += "&modus=" + IVmodus;
		params += "&introvoiceCode=" + IVwiqetCode;
		params += "&customerId=" + IVcustomerId;

		// Add params to the infostring for debug output
		InfoString += "\n-----------------------------------------------------\nparameters:\n-----------------------------------------------------\n";
		InfoString += params +"\n-----------------------------------------------------\n";

		// Generate the code to display the wiqet swf element
		TheTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
				+ ' width = "'+IVwidth+'" height = "'+IVheight+'" align = "'+IValign+'" '
				+ 'id = "movie" application="yes" trusted="yes" '
				+ 'codebase = "https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
				+ '<param name = "movie" value = "' + IVplayerUrl + '?' + params +'" />'
				+ '<param name = "quality" value = "high" />'
				+ '<param name = "wmode" value = "transparent" />'
				+ '<param name = "bgcolor" value = "'+IVbgColor+'" />'
				+ '<param name = "application" value = "yes" />'
				+ '<param name = "trusted" value = "yes" />'
				+ '<embed src = "' + IVplayerUrl +'?' + params + '" quality = "high" bgcolor = "'+IVbgColor+'" application="yes" trusted="yes" '
				+ 'width = "'+IVwidth+'" height = '+IVheight+' name = "movie" align = "'+IValign+'" '
				+ 'play = "true" '
				+ 'loop = "false" '
				+ 'quality = "high" '
				+ 'wmode = "transparent" '
				+ 'allowScriptAccess = "always" '
				+ 'type = "application/x-shockwave-flash" '
				+ 'pluginspage = "http://www.macromedia.com/go/getflashplayer">'
				+ '<\/embed>'
				+ '<\/object>';

		// Add params to the infostring for debug output
		InfoString += "The Swf Tag:\n-----------------------------------------------------\n";
		InfoString += TheTags +"\n-----------------------------------------------------\n";

		// Check if we are in the editor modus
		if (IVmodus == "editor")
		{

			// Define the input fields for in the form
			var	TheFormTags = ''
				+  '<input type = "'+IVFormType+'" id = "'+IVFormName+'[wiqetCode]" name = "'+IVFormName+'[wiqetCode]" value = "">'
				+  '<input type = "'+IVFormType+'" id = "'+IVFormName+'[playerUrl]" name = "'+IVFormName+'[playerUrl]" value = "">'

			// Check if we have a WiQet code
			if (IVwiqetCode == '')
			{
				TheFormTags += '<input type = "'+IVFormType+'" id = "'+IVFormName+'[deletePinCode]" name = "'+IVFormName+'[deletePinCode]" value = "">'
								+  '<input type = "'+IVFormType+'" id = "'+IVFormName+'[deleteCode]" name = "'+IVFormName+'[deleteCode]" value = "">'
								+  '<input type = "'+IVFormType+'" id = "'+IVFormName+'[playerUrl]" name = "'+IVFormName+'[playerUrl]" value = "">';
			}

			// Add params to the infostring for debug output
			InfoString += "The Form Tag:\n-----------------------------------------------------\n";
			InfoString += TheFormTags +"\n";

			// Check if we can find the div element, then fill it with the form elements
			if (document.getElementById(IVdivForm)) document.getElementById(IVdivForm).innerHTML = TheFormTags;
		}

		// Check if we can find the div for the player, then fill it with the swf code
		if (document.getElementById(IVdivPlayer)) document.getElementById(IVdivPlayer).innerHTML = (TheTags);

		// Check if we can find the debug div, then fill it with the debug output
		if (document.getElementById("infoWiqet"))	document.getElementById("infoWiqet").innerHTML = '<textarea  name=info ' +
																									 'style="width:450px;height:300px">' +
																									 (InfoString) +
																									 '</textarea>';

		// Place return
		return false;

	// Correct version is not available
	}
	else
	{

		// Check if the div is available
		if (document.getElementById("infoWiqet"))
		{

			// Define error string
			InfoString += "The Flash version you installed is: " + actualVersion+"\nYou need flashplayer"+requiredVersion+"\n";

			// Place error string in div element
			document.getElementById("infoWiqet").innerHTML = '<textarea name=info style="width:700px;height:300px">' +
															  (InfoString) +
															  '</textarea>';
		}

		// Define the return
		return "The Flash version you installed is: " + actualVersion + "\nYou need flashplayer: " + requiredVersion ;
	}
}

/**
 * -----------------------------------------------
 * function: IV_FillForm
 * -----------------------------------------------
 *
 * Description:
 *   Function that fills the form with data from editor
 *
 * Input:
 *	 wiqetCode			String	->  Code of wiqet
 *	 deleteCode			String	->	Delete code for the wiqet
 *	 deletePinCode		String	->	Delete pincode for the wiqet
 *
 * Output:
 *   The code for the wiqet, url to player, delete code, delete pincode
 *	 Will be filled in the input fields and div element (if defined)
 */
function IV_FillForm(wiqetCode, deleteCode, deletePinCode){

	// Define the link to the player
	var ivlink = IVDisplayUrl + '?wiqetCode=' + wiqetCode + '&customerId=' + IVcustomerId;

	// Check if we have a wiqet code
	if(wiqetCode != ''){

		// Check if div exists in document
		if (document.getElementById(IVdivForm)){

			// Fill input field with wiqet code given from the editor
			document.getElementById(IVFormName+'[wiqetCode]').value = wiqetCode;

			// Fill input field with player url and wiqet code from flash
			document.getElementById(IVFormName+'[playerUrl]').value= IVDisplayUrl + '?wiqetCode=' + wiqetCode;
		}

		// Check if div is set for displaying player link
		if (document.getElementById(IVdivLink))
		{

			// Fill the div
			document.getElementById(IVdivLink).innerHTML="<a target=_blank href='"+ivlink+"'>"+ivlink+"</a>";
		}
	}

	// Check if div exists in document
	if (document.getElementById(IVdivForm)){

		// Check for delete code, if exist fill the input field
		if((deleteCode != '') && (document.getElementById(IVFormName + '[deleteCode]') != undefined)){
			document.getElementById(IVFormName + '[deleteCode]').value = deleteCode;
		}

		// Check for delete pincode, if exist fill the input field
		if((deletePinCode != '') && (document.getElementById(IVFormName + '[deletePinCode]') != undefined)){
			document.getElementById(IVFormName+'[deletePinCode]').value = deletePinCode;
		}
	}
	if(IVSubmitForm == true){
		IVForm.submit();
	}
}

/**
 * -----------------------------------------------
 * function: f_detectFlash
 * -----------------------------------------------
 *
 * Description:
 *   Function that checks if we have a correct flash version to view the
 *	 editor or player
 *
 * Input:
 *	 p_iRequiredVersion		Integer	->  Minimum version for flash player
 *
 * Output:
 *   Will return a boolean if we have a flash version that is similar to the
 *	 required version (or higher) to view the editor or player.
 */
function f_detectFlash(p_iRequiredVersion){

	// Check for IE and Windows
	if(isIE && isWin)
	{
		// Write vbscript detection on ie win. IE on Windows doesn't support regular JavaScript plugins array detection.
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
		document.write('on error resume next \n');
		document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
		document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
		document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
		document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
		document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
		document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
		document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
		document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
		document.write('flash10Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.10"))) \n');
		document.write('<\/SCR' + 'IPT\> \n');
	}


	// Do plugin check
	if(navigator.plugins && (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) )
	{
		var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
		var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
		var flashVersion = parseInt(flashDescription.substring(16));

		flash2Installed = flashVersion == 2;
		flash3Installed = flashVersion == 3;
		flash4Installed = flashVersion == 4;
		flash5Installed = flashVersion == 5;
		flash6Installed = flashVersion == 6;
		flash7Installed = flashVersion == 7;
		flash8Installed = flashVersion == 8;
		flash9Installed = flashVersion == 9;
		flash10Installed = flashVersion >= 10;
	}

	// Set vars to true while the counter is < then the currentversion
	for(var i = 2; i <= maxVersion; i++) if(eval("flash" + i + "Installed") == true) actualVersion = i;

	// Check for webtv
	if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 4;

	// Check if we have the right version
	if(actualVersion >= p_iRequiredVersion) hasRightVersion = true; else hasRightVersion = false;

	// Do a return with de boolean hasRightVersion
	return hasRightVersion;
}

/**
 * -----------------------------------------------
 * function: alertWiqet
 * -----------------------------------------------
 *
 * Description:
 *   Function that fills the form with data from editor
 *
 * Input:
 *	 p_sForm	String	->  Link to form on the page
 *
 * Output:
 *   Gives a JavaScript alert with information from the form with input fields
 */
function alertWiqet(p_sForm){
	mes = 'wiqet[wiqetCode]->'+ p_sForm['wiqet[wiqetCode]'].value + '\n';
	mes += 'wiqet[playerUrl]->'+ p_sForm['wiqet[playerUrl]'].value + '\n';
	mes += '-------------------------------------------------------------------------\n'
	mes += 'Delete codes are only filled when Introvoice is made for ther first time!\n';
	mes += 'wiqet[deletePinCode]->'+ p_sForm['wiqet[deletePinCode]'].value + '\n';
	mes += 'wiqet[deleteCode]->'+ p_sForm['wiqet[deleteCode]'].value + '\n';
	alert (mes);
}

function publish(){
	if(window.movie){
		var IVFlashElement = window.movie;
	}
	else{
		var IVFlashElement = window.document.movie;
	}

    // hack by LiTr
    //var IVFlashElement = document.getElementById('movie');

	if(IVFlashElement.f_publishWiqet != null){
		IVFlashElement.f_publishWiqet();
	}
	else{
		alert('Version Wiqet isn\'t competible with external publish');
	}
}

function publishAndSubmit(p_oForm){
	if(window.movie){
		var IVFlashElement = window.movie;
	}
	else{
		var IVFlashElement = window.document.movie;
	}

    // hack by LiTr
    //var IVFlashElement = document.getElementById('movie');

	if(IVFlashElement.f_publishWiqet != null){
		IVFlashElement.f_publishWiqet();
		IVSubmitForm = true;
		IVForm = p_oForm;
	}
	else{
		alert('Version Wiqet isn\'t compatible with external publish');
	}
}