// Copyright (C) 2007 Cognos Incorporated. All Rights Reserved.
// Cognos and the Cognos logo are trademarks of Cognos Incorporated.

// These are the languages (English, French, Japanese, Germany, Swedish, Spanish, Russian and Simplified Chinese) that are supported by Cognos 8.
var sSupportedLangs = " en fr ja de sv es ru zh-cn ";
var sDefaultLang = "en";

var sDefaultFeaturesHelp = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=680,height=500";

var sDefaultFeaturesQuickTours = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=800,height=600";

function getSupportedLang(sProductLocale)
{
	// Use the locale string to construct the path to the localized folder.  
	// Make sure they are converted to lowercase.
	var sLang = sProductLocale;
	sLang = sLang.toLowerCase();

	if ( sSupportedLangs.indexOf(" " + sLang + " ") < 0 )
		sLang = sDefaultLang;
	
	return sLang;
}

// A common API to fire up help documentation
// 	@sProductLocale:	the target product locale
// 	@sStartupFile:		the start up file
// 	@sContext:		the web context line
function gotoHelp( sProductLocale, sStartupFile, sContext )
{	
	var sLang = getSupportedLang(sProductLocale);

	var sURL = "../documentation/" + sLang + "/" + sStartupFile;

	sURL += (sContext == "") ? ".html" : ".html?helpid=" + sContext;
	
	helpWindow = window.open(sURL, "helpWindow", sDefaultFeaturesHelp);
	helpWindow.focus();
}

// A common API to fire up quick tours documentation
// 	@sProductLocale:	the target product locale
// 	@sStartupFile:		the start up file under tours folder
//	@sOptions:			the options
// 	Note: As WO-2163, the sStartupFile and sOptions arguments would be obsolete now.
//			For backward compatibility, just leave it as it was before.
function gotoTours( sProductLocale, sStartupFile, sOptions )
{
	var sLang = getSupportedLang(sProductLocale);
	
	var sURL = "../documentation/" + sLang + "/tours/" + "crntours.html?sStartupFile=" + sStartupFile;

	if ( sOptions != null && sOptions != "" ) {
		sURL += "&" + sOptions;
	}

	var toursWindow = window.open(sURL, "toursWindow", sDefaultFeaturesQuickTours);
	toursWindow.focus();
}


// The following two APIs will help ReportStudio & CMM etc. for redirecting purpose.
function initDocs()
{
	var query = parseHash(window.location.href); // read URL for topic/section info

	var sLang = sDefaultLang;
	var sBook = "wig_cr_a"; // Drfault to "Get Started" documentation
	var sHelpID = "";
	var sCurrentTopic = "";
	var sStartupFile = ""; // when using crntours menu, pass the app info to highlight

	if( query['lang'] && (query['lang'] != "") ) {
		sLang = getSupportedLang(query['lang']);
	}

	if( query['book'] && (query['book'] != "") ) {
		sBook = query['book'];
	}

	if( query['helpid'] && (query['helpid'] != "") ) {
		sHelpID = query['helpid'];
	}

	if( query['sStartupFile'] && (query['sStartupFile'] != "") ) {
		sStartupFile = query['sStartupFile'];
	}
	
	sCurrentTopic = sLang + "/" + sBook + ".html";

	if( sHelpID != "" ) {
		sCurrentTopic += "?helpid=" + sHelpID;
	}
	else if( sStartupFile != "" ) {
		sCurrentTopic += "?sStartupFile=" + sStartupFile;
	}

	// determine full path to html
	var sDirTopics = "";
	var sDirString = window.location.href.substring(0, window.location.href.indexOf('?') );
	var nPos = sDirString.lastIndexOf('/');
	sDirTopics = (nPos != -1) ? window.location.href.substring(0, nPos + 1) : "";

	sCurrentTopic = sDirTopics + sCurrentTopic;

	window.location.href = sCurrentTopic;
}

function parseHash(s)
{
	var query = new Array();
	var nPos = s.indexOf("?");

	if (nPos != -1)
	{
		s = s.substring(nPos + 1, s.length);
		var sPair = "";
		while (s != "")
		{
			nPos = s.indexOf("&");
			if (nPos == -1) { sPair = s; s = ""; }
			else {	sPair = s.substring(0, nPos); s = s.substring(nPos + 1, s.length); }
			nPos = sPair.indexOf("=");
			if (nPos != -1) query[sPair.substring(0, nPos)] = unescape(sPair.substring(nPos + 1, sPair.length));
		}
	}

	return query;
}


