var g_sOwcPath = owcGetCommonerPath();

owcBindWebComponentsSDK(g_sOwcPath);

function owcBindWebComponentsSDK(sOwcHRef) 
{
	if(typeof(isRelease) == "undefined" || isRelease == false)
	{
        owcBindWebSdkScript(sOwcHRef, "JScriptCore.js");
        owcBindWebSdkScript(sOwcHRef, "DomCore.js");
        owcBindWebSdkScript(sOwcHRef, "OwcDHTML.js");
        owcBindWebSdkScript(sOwcHRef, "OwcGlobals.js");
        owcBindWebSdkScript(sOwcHRef, "OwcXHTTP.js");
        owcBindWebSdkScript(sOwcHRef, "OwcCommon.js");
        owcBindWebSdkScript(sOwcHRef, "OwcControl.js");
        owcBindWebSdkScript(sOwcHRef, "OwcDataExchange.js");
        owcBindWebSdkScript(sOwcHRef, "OwcUiElements.js");
        owcBindWebSdkScript(sOwcHRef, "OwcDataBound.js");
        owcBindWebSdkScript(sOwcHRef, "OwcWindow.js");	
        owcBindWebSdkScript(sOwcHRef, "OwcPage.js");
        owcBindWebSdkScript(sOwcHRef, "OwcControlCommon.js");
        owcBindWebSdkScript(sOwcHRef, "OwcAppBase.js");
        owcBindWebSdkScript(sOwcHRef, "OwcImageViewer.js");
        owcBindWebSdkScript(sOwcHRef, "OwcMenu.js");
        owcBindWebSdkScript(sOwcHRef, "OwcPageBind.js");
        owcBindWebSdkScript(sOwcHRef, "OwcPanes.js");
        owcBindWebSdkScript(sOwcHRef, "OwcInformationComponentViewer.js");
    }
} // owcBindWebComponentsSDK()

function owcBindWebSdkScript(sOwcHRef, sScriptFile)
{
	if (document.readyState != "complete") // && document.readyState != "interactive")// causes script loading error on IE7 after clearing cache and restarting browser
		document.writeln('<script language="javascript" type="text/javascript" src="' + sOwcHRef + sScriptFile + '"></script>');
	else if (!owcIsWebSdkScriptIncluded(sOwcHRef, sScriptFile))
		owcAddScript(sOwcHRef + sScriptFile);
}

function owcIsWebSdkScriptIncluded(sOwcHRef, sScriptFile)
{
	return (owcFindScriptPosition(sOwcHRef + sScriptFile) >= 0);
}

function owcGetCommonerPath() 
{
	var oScript = null;
	var sPath = "/Olive/WebComponents/";

	// Find <script src=".../OwcBind_LandingPage.js"> and use script's path
	var arrScripts = document.scripts;
	if (!document.scripts)
		arrScripts = owcGetElementsByTagName("script");
	for (var iScript=0; iScript < arrScripts.length; ++iScript)
	{
		if (arrScripts[iScript].src.indexOf("OwcBind_LandingPage.js") >= 0)
		{
			sPath = arrScripts[iScript].src;
			sPath = sPath.replace("OwcBind_LandingPage.js", "");
			break;
		}
	}

	return sPath;
} // owcGetCommonerPath()

///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                            Navigating DOM tree                            //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////
function owcAddScript(uri, afterIndex)
{
	var oScriptElem = document.createElement("script");
	oScriptElem.language = "javascript";
	oScriptElem.type = "text/javascript";
	oScriptElem.src = uri;
	
	var headElem = owcGetElementsByTagName("head")[0];
	return headElem.appendChild(oScriptElem);
}

function owcFindScriptPosition(uri, relativeMatch)
{
	var sMatchRef = uri.toLowerCase().replace(/\\/, "/");

	// Find <script> element matching file source
	var arrScripts = document.scripts;
	if (!document.scripts)
		arrScripts = owcGetElementsByTagName("script");
	for (var iScript=0; iScript < arrScripts.length; ++iScript)
	{
		var scriptSrc = arrScripts[iScript].src.toLowerCase().replace(/\\/, "/");
		if (relativeMatch)
		{
			var pos = scriptSrc.indexOf(sMatchRef);
			if (pos >= 0 && ((pos + sMatchRef.length) == scriptSrc.length))
				return iScript;
		}
		else if (scriptSrc == sMatchRef)
			return iScript;
	}
	
	return -1;
}

function owcGetElementsByTagName(name)
{
	if (!name || name == "")
		return null;
	if (document.getElementsByTagName)
		return document.getElementsByTagName(name);
	else if (document.all.tags)
		return document.all.tags(name);
	return null;
} // owcGetElementsByTagName()