//---------------------------------------------------------------------------
// SaaS KanaboWeb2.0 JavaScript for Client
//
// (C)Copyright WAC.com inc. All Rights Reserved.
//---------------------------------------------------------------------------
// KanaboWeb.execCommand( command, hostid, params )
//		command : on / off / toggle
//		hostid : xxxxx.xxxxx
//		params : 'kb_lv=1&kb_md=0&kb_sz=60%'
// KanaboWeb.execCommand('on','xxxxx.xxxxx')
// KanaboWeb.execCommand('on','xxxxx.xxxxx','kb_lv=3&kb_md=2')
// KanaboWeb.execCommand('off')
// KanaboWeb.execCommand('toggle','xxxxx.xxxxx')
// KanaboWeb.execCommand('toggle','xxxxx.xxxxx','kb_lv=3&kb_md=2')
// KanaboWeb.isTranslated() : true/false

function KanaboWebClass()
{
	//==================================================================//
	//     !!!! Please set host_url. "abc.xyz" is for example. !!!!     //
	this.host_url = "http://furigana.abc.xyz.com/saas/default.ashx?";
	this.host_id = "abc.xyz";
	//==================================================================//

	//-----------------------------------------------------------------
	// Public methods
	//-----------------------------------------------------------------
	
	// Request to get web page with furigana
	this.execCommand = function( cmd, params )
	{
		switch( cmd )
		{	
			case "toggle" : 
			{
				if( this.isTranslated() == true )
				{
					this.reset();
				}
				else
				{
					this.exec( params );
				}
			} break;
			case "off" :
			{
				this.reset();
			} break;
			case "on" :
			{
				this.exec( params );
			} break;
			default :
			{
				this.exec( params );
			} break;
		}
	}
	
	// get a web page with furigana
	this.exec = function ( params )
	{
		var url = this._createSaaSURL( params );
		window.location.href = url;
	}

	// get a original web page
	this.reset = function()
	{
		try
		{
			window.location.href = __kanaboweb.url;
		}
		catch( e )
		{
		}
	}

	// check translated
	this.isTranslated = function()
	{
		try
		{
			return __kanaboweb.translated;
		}
		catch( e )
		{
		}
		return false;
	}
	
	
	//-----------------------------------------------------------------
	// private methods
	//-----------------------------------------------------------------	
	
	this._createSaaSURL = function( params )
	{
		var url = this.host_url;
	
		url = url + "kb_id=" + this.host_id + "&";
	
		if( params != undefined )
		{
			url = url + params;
			url = url + "&";
		}
	
		url = url + "kb_url=" + this._getTargetURL();

		return url;
	}

	this._getTargetURL = function()
	{
		var url = location.href;
		try
		{
			url = __kanaboweb.url;
		}
		catch( e )
		{
		}
		return url;
	}

}

var KanaboWeb = new KanaboWebClass();