//-----------------------------------------------------------------------
// Description : Vizky Javascript Header file
// By : Vizrt Thailand
// Usage : attach this file in the head area of html page using MPS file
// Version : 1.2.0
// Date : 3/13/2008
// Editor : pte@vizrt.com

//-----------------------------------------------------------------------
// Change Log
// 03/13/2008 : Create this file by pte@vizrt.com
// 03/31/2008 : Support Mac's Safari
// 07/07/2008 : Add CheckObject function


//-----------------------------------------------------------------------
// PREDEFINE FUNCTION BLOCK
//-----------------------------------------------------------------------
function OnVizkyInit(varControlObject){
//alert("Default Init")
}
function OnVizkyLMouseUp(varControlObject, x, y){}
function OnVizkyLMouseDown(varControlObject, x, y){}
function OnVizkyLMouseDbClick(varControlObject, x, y){}
function OnVizkyRMouseUp(varControlObject, x, y){}
function OnVizkyRMouseDown(varControlObject, x, y){}
function OnVizkyRMouseDbClick(varControlObject, x, y){}
function OnVizkyMouseMove(varControlObject, x, y){}
function OnVizkyVarChange(varControlObject, varStationId, varChannelId, varVarName, varVarValue){}
function OnVizkyUserScript(varControlObject, functionName, functionParam){ window[functionName](varControlObject, functionParam);}
function OnVizkyConnected(varControlObject, varStationId){}
function OnVizkyDisconnected(varControlObject, varStationId){}
function OnVizkyError(varControlObject, errorCode, errorDESC, errorDESC2){}
function OnVizkyStatus(varControlObject, statusCode, statusDESC){}
function OnVizkyServerCommand(varControlObject, varStationId, varChannelId, varCommand){}
function OnVizkyPublish(varControlObject, varStationId, varChannelId, varLayer, varURL, varParam){}
function OnVizkyVideoDecoderMetadata(varControlObject, varViewportId, varVideoId, varURL, varParam){}
function OnVizkyTimelineTrigger(varControlObject, varPlayableId, varDirectorName, varTagName, varParam){}

var pleaseInstall = "Please install"

//-----------------------------------------------------------------------
// BROWSER CHECKING
//-----------------------------------------------------------------------
	var navig_agt=navigator.userAgent.toLowerCase();
	var navig_fox=(navig_agt.indexOf("firefox")!=-1);
	var navig_safari=(navig_agt.indexOf("safari")!=-1 );
	var navig_ie=((navig_agt.indexOf("msie")!=-1) && (navig_agt.indexOf("opera")==-1));
//-----------------------------------------------------------------------
// FUNCTION BLOCK
//-----------------------------------------------------------------------
function MacInit(varControlIdName)
{
	var pluginObj = document.getElementById(varControlIdName);
	OnVizkyInit(pluginObj);
}
function MacVarChange(varControlIdName, varStationId, varChannelId, szVarName, szVarValue)
{
	var pluginObj = document.getElementById(varControlIdName);
	OnVizkyVarChange(pluginObj, varStationId, varChannelId, szVarName, szVarValue);
}
function MacUserScript(varControlIdName, funtionName, functionParam)
{
	var pluginObj = document.getElementById(varControlIdName);	
	OnVizkyUserScript(pluginObj, funtionName, functionParam);
}

function MacTimelineTrigger(varControlIdName, varPlayableId, varDirectorName, varTagName, varParam)
{
	var pluginObj = document.getElementById(varControlIdName);
	OnVizkyTimelineTrigger(pluginObj, varPlayableId, varDirectorName, varTagName, varParam);
}


//-----------------------------------------------------------------------
// Function : MacCallback(varControlIdName,varCallbackFunction, x, y)
// Result: Forward callback from plugin to Java script
function MacMouseCallback(varControlIdName,varCallbackFunction, x, y)
{
	var pluginObj = document.getElementById(varControlIdName);
	if( varCallbackFunction == "OnVizkyLMouseUp")
	{
		OnVizkyLMouseUp(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyLMouseDown")
	{
		OnVizkyLMouseDown(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyLMouseDbClick")
	{
		OnVizkyLMouseDbClick(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyRMouseUp")
	{
		OnVizkyRMouseUp(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyRMouseDown")
	{
		OnVizkyRMouseDown(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyRMouseDbClick")
	{
		OnVizkyRMouseDbClick(pluginObj,x,y);
	}
	if( varCallbackFunction == "OnVizkyMouseMove")
	{
		OnVizkyMouseMove(pluginObj,x,y);
	}
}
//-----------------------------------------------------------------------
// Function : detectSafariPlugin()
// Result : return the true if the plugin exist , use for Safari plugin

function detectSafariPlugin()
{
 	var pluginFound = false;
	if (navigator.mimeTypes['application/vizky']) {
 		pluginFound = true;
	} else 
	{
		pluginFound = false;
	}
	return pluginFound;

}
//-----------------------------------------------------------------------
// Function : detectFireFoxPlugin()
// Result : return the true if the plugin exist , use for Firefox plugin
//
function detectFireFoxPlugin()
{
    // allow for multiple checks in a single pass
    var daPlugins = detectFireFoxPlugin.arguments;
   // alert(daPlugins);
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0)
	{
		var pluginsArrayLength = navigator.plugins.length;
		// for each plugin...
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ )
		{
			// loop through all desired names and check each against the current plugin name
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++)
			{
				// if desired plugin name is found in either plugin name or description
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
					(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) )
				{
					// this name was found
					numFound++;
				}   
			}
			// now that we have checked all the required names against this one plugin,
			// if the number we found matches the total number provided then we were successful
			if(numFound == daPlugins.length)
			{
				pluginFound = true;
				// if we've found the plugin, we can stop looking through at the rest of the plugins
				break;
			}
		}
    }
    return pluginFound;
}

//-----------------------------------------------------------------------
// Function : CreateObject(varControlIdName, iWidth, iHeight)
// Result : Create and return the Control Ojbect for the activeX, print error to html page if cannot find plugin
//
function CreateVizkyObjectOld(varControlIdName, iWidth, iHeight)
{
    if(navig_safari)
    {
    	//alert("safari:"+ varControlIdName);
    	if(detectSafariPlugin())
    	{
			document.write("<embed id='" + varControlIdName + "' type='application/Vizky' width= " + iWidth + " height= " + iHeight + ">");
    	}
		else
		{
			//document.write(pleaseInstall);
		}
		
    }
	else if(navig_fox)
	{
		if(detectFireFoxPlugin("Vizky"))
		{
			document.write("<embed id='" + varControlIdName + "' type='application/Vizky' width= " + iWidth + " height= " + iHeight + ">");
		}
		else
		{
			document.write(pleaseInstall);
		}
	}
	else if(navig_ie)
	{
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyInit()">OnVizkyInit(document.getElementById("' + varControlIdName + '"))</SCRIPT>');

		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseUp(x, y)">OnVizkyLMouseUp(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseDown(x, y)">OnVizkyLMouseDown(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseDbClick(x, y)">OnVizkyLMouseDbClick(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseUp(x, y)">OnVizkyRMouseUp(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseDown(x, y)">OnVizkyRMouseDown(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseDbClick(x, y)">OnVizkyRMouseDbClick(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyMouseMove(x, y)">OnVizkyMouseMove(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyVarChange(varStationId, varChannelId, szVarName, szVarValue)"> OnVizkyVarChange(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, szVarName, szVarValue)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyUserScript(varFunctionName, varFunctionParam)">OnVizkyUserScript(document.getElementById("' + varControlIdName + '"), varFunctionName, varFunctionParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyConnected(varStationId)"> OnVizkyConnected(document.getElementById("' + varControlIdName + '"), varStationId)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyDisconnected(varStationId)"> OnVizkyDisconnected(document.getElementById("' + varControlIdName + '"), varStationId)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyStatus(statusCode, statusDESC)"> OnVizkyError(document.getElementById("' + varControlIdName + '"), statusCode, statusDESC)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyError(errorCode, errorDESC, errorDESC2)"> OnVizkyError(document.getElementById("' + varControlIdName + '"), errorCode, errorDESC, errorDESC2)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyServerCommand(varStationId, varChannelId, varCmdString)"> OnVizkyServerCommand(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, szCmdString)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyPublish(varStationId, varChannelId, varLayer, varURL, varParam)"> OnVizkyPublish(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, varLayer, szURL, szParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyVideoDecoderMetadata(varViewportId, varVideoId, varURL, varParam)"> OnVizkyVideoDecoderMetadata(document.getElementById("' + varControlIdName + '"), varViewportId, varVideoId, szURL, szParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyTimelineTrigger(varPlayableId, varDirectorName, varTagName, varParam)"> OnVizkyTimelineTrigger(document.getElementById("' + varControlIdName + '"), varPlayableId, varDirectorName, varTagName, varParam)</SCRIPT>');

		document.write('<OBJECT ID="' + varControlIdName + '" WIDTH=' + iWidth + ' HEIGHT=' + iHeight + ' CLASSID="CLSID:00A05948-567E-4AA1-86E9-A0374C175DE8" VIEWASTEXT>')
		document.write(pleaseInstall);
		document.write('</OBJECT>')
	}

	return document.getElementById(varControlIdName);
}

//--------------------------------------------------------------------------------------------------------------------------------------
//With Image showing as fallback
function CreateVizkyObject(varControlIdName, iWidth, iHeight, ImageURL)
{
	if(ImageURL == "")
	{
		alert("test")
	}
	
    if(navig_safari)
    {
    	//alert("safari:"+ varControlIdName);
    	if(detectSafariPlugin())
    	{
			document.write("<embed id='" + varControlIdName + "' type='application/Vizky' width= " + iWidth + " height= " + iHeight + ">");
    	}
		else
		{
			if(ImageURL == null)
			{
				document.write(pleaseInstall);
			}
			else
			{
				document.write('<img src="' +  ImageURL  + '">');
			}
		}
		
    }
	else if(navig_fox)
	{
		if(detectFireFoxPlugin("Vizky"))
		{
			document.write("<embed id='" + varControlIdName + "' type='application/Vizky' width= " + iWidth + " height= " + iHeight + ">");
		}
		else
		{
			if(ImageURL == null)
			{
				document.write(pleaseInstall);
			}
			else
			{
				document.write('<img src="' +  ImageURL  + '">');
			}
		}
	}
	else if(navig_ie)
	{
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyInit()">OnVizkyInit(document.getElementById("' + varControlIdName + '"))</SCRIPT>');

		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseUp(x, y)">OnVizkyLMouseUp(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseDown(x, y)">OnVizkyLMouseDown(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyLMouseDbClick(x, y)">OnVizkyLMouseDbClick(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseUp(x, y)">OnVizkyRMouseUp(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseDown(x, y)">OnVizkyRMouseDown(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyRMouseDbClick(x, y)">OnVizkyRMouseDbClick(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyMouseMove(x, y)">OnVizkyMouseMove(document.getElementById("' + varControlIdName + '"), x, y)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyVarChange(varStationId, varChannelId, szVarName, szVarValue)"> OnVizkyVarChange(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, szVarName, szVarValue)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyUserScript(varFunctionName, varFunctionParam)">OnVizkyUserScript(document.getElementById("' + varControlIdName + '"), varFunctionName, varFunctionParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyConnected(varStationId)"> OnVizkyConnected(document.getElementById("' + varControlIdName + '"), varStationId)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyDisconnected(varStationId)"> OnVizkyDisconnected(document.getElementById("' + varControlIdName + '"), varStationId)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyError(errorCode, errorDESC, errorDESC2)"> OnVizkyError(document.getElementById("' + varControlIdName + '"), errorCode, errorDESC, errorDESC2)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyStatus(statusCode, statusDESC)"> OnVizkyError(document.getElementById("' + varControlIdName + '"), statusCode, statusDESC)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyServerCommand(varStationId, varChannelId, szCmdString)"> OnVizkyServerCommand(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, szCmdString)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyPublish(varStationId, varChannelId, varLayer, szURL, szParam)"> OnVizkyPublish(document.getElementById("' + varControlIdName + '"), varStationId, varChannelId, varLayer, szURL, szParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyVideoDecoderMetadata(varViewportId, varVideoId, szURL, szParam)"> OnVizkyVideoDecoderMetadata(document.getElementById("' + varControlIdName + '"), varViewportId, varVideoId, szURL, szParam)</SCRIPT>');
		document.write('<SCRIPT FOR="' + varControlIdName + '" EVENT="OnVizkyTimelineTrigger(varPlayableId, varDirectorName, varTagName, varParam)"> OnVizkyTimelineTrigger(document.getElementById("' + varControlIdName + '"), varPlayableId, varDirectorName, varTagName, varParam)</SCRIPT>');
		

		document.write('<OBJECT ID="' + varControlIdName + '" WIDTH=' + iWidth + ' HEIGHT=' + iHeight + ' CLASSID="CLSID:00A05948-567E-4AA1-86E9-A0374C175DE8" VIEWASTEXT>')
		if(ImageURL == null)
		{
			document.write(pleaseInstall);
		}
		else
		{
			document.write('<img src="' +  ImageURL  + '">');
		}
		document.write('</OBJECT>')
	}

	return document.getElementById(varControlIdName);
}

//-----------------------------------------------------------------------
// Function : getCurrentPath()
// Result : return the path of the current folder where html page is located
//
function getCurrentPath()
{
	var fullPathWithName = "";
	fullPathWithName += window.location;

	var i= fullPathWithName.length - 1

	for(i; i > 0; i--)
	{
		if(fullPathWithName.charAt(i) == '/')
			break;
	}
	
	var filePrefix = "file:///";
	var startIdx = fullPathWithName.lastIndexOf(filePrefix);

	// -1 mean this is not from local disk
	if(startIdx == -1)
		startIdx = 0;
	else
		startIdx += filePrefix.length;

	var currentPath = fullPathWithName.substring(startIdx,i+1);

	// Replace %20 with space bar
	return currentPath.replace(/%20/g, " ");
}

//-----------------------------------------------------------------------
// Function : getCurrentPath()
// Result : return full path
//
 function VizkyGetPathOfHtmlPage() 
 {
	var fullPathWithName = "";
	fullPathWithName += window.location;
	
	var i= fullPathWithName.length - 1

	for(i; i > 0; i--)
	{
		if(fullPathWithName.charAt(i) == '/')
			break;
	}
	
	var currentPath = fullPathWithName.substring(0,i+1);
	
	// Replace %20 with space bar
	return currentPath.replace(/%20/g, " ");
 }


//-----------------------------------------------------------------------
// Function : CheckObject(Object, ObjectName)
// Result : return true if the object match with given object name, false if not
//
function CheckObject(Object, ObjectName)
{
		var navig_agt=navigator.userAgent.toLowerCase();
		var navig_fox=(navig_agt.indexOf("firefox")!=-1);
	   if(navig_fox)
	   {
			if(document.getElementById(ObjectName).id == Object.id)
				return true;
			else
				return false;
	   }
	   else
	   {
			if(Object.id == ObjectName)
				return true;
			else
				return false;
	   }
}

//-----------------------------------------------------------------------
// PRESET VARIABLE BLOCK
//-----------------------------------------------------------------------

//-----------------------------------------------------------------------
// Variable : CURRENT_PATH
// Result : store the current path of html file
//
var CURRENT_PATH = getCurrentPath();

