﻿//---------------------------------------------------------------------------
// SaaS KanaboVoice JavaScript for Client
//
// (C)Copyright WAC.com inc. All Rights Reserved.
//---------------------------------------------------------------------------
// KanaboVoice.openPopup( contentns_id, params )
// KanaboVoice.embedVoice( contentns_id, embed_id, params )
// KanaboVoice.playVoice( contentns_id, embed_id, embed_pos, params )
// KanaboVoice.stopVoice( contentns_id )

// Voice Request Function

// Open Play in New Window

function KanaboVoiceObject()
{
	this.host_url = "http://voice.kanabo.com/saas/default.ashx?";
	this.host_id = "kanabo.com";
	
	function PopupWindow()
	{
		this.left = 0;
		this.top = 0;
		this.width = 150;
		this.height = 150;
		this.toolbar = 'no';
		this.menubar = 'no';
		this.scrollbars = 'yes';
		this.resizable = 'yes';	
		this.open = function (name, url)
		{
			window.open(url, name, 
						'left='+this.left+',top='+this.top+',width='+this.width+',height='+this.height+',toolbar='+this.toolbar+',menubar='+this.menubar+',status='+this.statusbar+',scrollbars='+this.scrollbars+',resizable='+this.resizable);
		}
	}
	this.Popup = new PopupWindow();
	
	
	this.openPopup = function( contents_id, params )
	{
		var url = this._createSaaSURL( "player", contents_id, params );
		this.Popup.open("KBVoicePlayer", url, 0, 0, 150, 150, 0, 0, 0, 1, 1);
	}

	this.embedVoice = function( contents_id, embed_id, params )
	{
		var elem = null;
		if( embed_id == "" || embed_id == undefined )
		{
			elem = document.getElementById( contents_id );
		}
		else
		{
			var elem = document.getElementById( embed_id );
			if( elem == null )
			{
				alert( "iframe for embeded voice is not found!" );
				return;
			}	
			//elem.style.display = "block";
			elem.style.display = "inline";
			//elem.innerHTML = "";
		}
	
		var ifrm = null;
		frms = elem.getElementsByTagName( "IFRAME" );
		if( frms.length > 0 )
		{
			ifrm = frms.item(0);
			ifrm.src = "";
			elem.removeChild( ifrm );
			return;
		}

		ifrm = document.createElement( "iframe" );
		try
		{
			ifrm.frameBorder = 0; 
			ifrm.scrolling = "no"; 
			ifrm.style.width = "200px";
			ifrm.style.height = "50px";
			ifrm.style.verticalAlign = "middle";
			ifrm.allowTransparency="true";
			ifrm.src = this._createSaaSURL( "embed", contents_id, params );
		}
		catch( e )
		{
		}
	 	elem.appendChild( ifrm );  
	}

	this.playVoice = function( contents_id, embed_id, embed_pos, params )
	{
		var player = document.getElementById( this._makePlayerID( contents_id ) );
		if( player != null )
		{
			try
			{
				player.Play();
			}
			catch( e )
			{
			}			
		}
		else
		{
			var s = new String();
			if( embed_pos == null || embed_pos == undefined || embed_pos == "" )
				embed_pos = "AfterBegin";
			
			if( embed_id == null || embed_id == undefined || embed_id == "" )
				embed_id = contents_id;
	
			var s = "";
			doc = this._getKanaboVoiceHolder().document;
			s = "<SCRIPT language=javascript> var contents_id = '__CONTENTS_ID_VALUE'; var player_id = '__PLAYER_ID_VALUE'; var player_embed_id = '__PLAYER_EMBED_ID_VALUE'; var player_embed_pos = '__PLAYER_EMBED_POS_VALUE';</script>";
			s = s.replace( /__CONTENTS_ID_VALUE/g, contents_id );
			s = s.replace( /__PLAYER_ID_VALUE/g, this._makePlayerID( contents_id ) );
			s = s.replace( /__PLAYER_EMBED_ID_VALUE/g, embed_id );
			s = s.replace( /__PLAYER_EMBED_POS_VALUE/g, embed_pos );
			doc.write( s );
			s = "<SCRIPT language=javascript src='__SAAS_URL_VALUE'></SCRIPT>"; 
			s = s.replace( /__SAAS_URL_VALUE/g, this._createSaaSURL( "dynamic", contents_id, params ) );
			doc.write( s );
			doc.close();
		}
	}
	
	this.stopVoice = function( contents_id )
	{
		var player = document.getElementById( this._makePlayerID( contents_id ) );
		if( player != null )
		{
			try
			{
				player.Stop();
			}
			catch( e )
			{
			}
			var p = player.parentElement;
			if( p == undefined )
			{
				p = player.parentNode;
			}
			p.removeChild( player );
		}
	}
	

	//-----------------------------------------------------------------
	// private methods
	//-----------------------------------------------------------------

	this._createSaaSURL = function( mode, contents_id, params )
	{
		var url = this.host_url;
		if( url == undefined || url == "" )
			alert( "undefined host ID! KanaboVoice.url = <SAAS URL>" );
	
		if( contents_id != undefined && contents_id != "" )
			url = url + "kbv_area=" + contents_id + "&";
	
		if( mode == undefined || mode == "" )
			url = url + "kbv_dispMode=player&";
		else
			url = url + "kbv_dispMode=" + mode + "&";
	
		if( params != undefined && params != "")
		{
			url = url + params;
			url = url + "&";
		}
	
		url = url + "kbv_id=" + this.host_id + "&kbv_url=" + this._getTargetURL();
		url = encodeURI(url);
		return url;
	}	
		
	this._getTargetURL = function()
	{
		var url = location.href;
		try
		{
			url = __kanaboweb.url;
		}
		catch( e )
		{
		}
		return url;
	}

	
	this._getKanaboVoiceHolder = function()
	{
		if( document.getElementById( "KANABO_VOICE_HOLDER") == null )
		{
			var s = "<iframe id='KANABO_VOICE_HOLDER' width=200 height=100 frameborder=no ></iframe>";
	 		document.body.insertAdjacentHTML( "BeforeEnd", s ); 
		}
		return document.getElementById( "KANABO_VOICE_HOLDER" ).contentWindow;
	}

	this._makePlayerID = function( contents_id )
	{
		return "PLAYER_OBJECT_" + contents_id;
	}

}

//-------------------------------------------
// prototype
//-------------------------------------------

if(self.Node&&self.Node.prototype){
	Element.prototype.insertAdjacentHTML=insertAdj_HTML;
	Element.prototype.insert__Adj=insert__Adj;
}

function insertAdj_HTML(a1,a2){
	var r=document.createRange();
	r.selectNode(this);
	var t=r.createContextualFragment(a2);
	this.insert__Adj(a1,t);
}
function insert__Adj(a1,a2){
	var p=this.parentNode;
	var s=a1.toLowerCase();
	if(s=="beforebegin"){p.insertBefore(a2,this)}
	if(s=="afterend"){p.insertBefore(a2,this.nextSibling)}
	if(s=="afterbegin"){this.insertBefore(a2,this.childNodes[0])}
	if(s=="beforeend"){this.appendChild(a2)}
}


//-------------------------------------------
// Create KanaboObject
//-------------------------------------------
var KanaboVoice = new KanaboVoiceObject();

//-------------------------------------------
// EventHandler
//-------------------------------------------

function onPlayVoice(elem)
{
	var holder = KanaboVoice._getKanaboVoiceHolder();
	/***
	var voice_url = KanaboVoice._getKanaboVoiceHolder().voice_url;
	var area_id = KanaboVoice._getKanaboVoiceHolder().area_id;
	var player_id = KanaboVoice._getKanaboVoiceHolder().player_id;
	var player_embed_id = KanaboVoice._getKanaboVoiceHolder().player_embed_id;
	var player_embed_pos = KanaboVoice._getKanaboVoiceHolder().player_embed_pos;
***/
	var player = document.getElementById( holder.player_id );
	if( player != null )
	{
		var p = player.parentElement;
		p.removeChild( player );
	}


	var s = "";
	if( navigator.platform == "Win32" )
	{
		s = s + "<object id='__PLAYER_ID_VALUE' data='__VOICE_URL_VALUE' type='audio/x-ms-wma' style='__PLAYER_STYLE_VALUE'>";
		s = s +		"<param name='src' value='__VOICE_URL_VALUE' valuetype='ref' />";
		s = s + 	"<param name='autostart' value='1' valuetype='data' />";
		s = s + 	"<param name='showcontrols' value='1' valuetype='data' />";
		s = s + 	"<param name='showpositioncontrols' value='0' valuetype='data' />";
		s = s + 	"<param name='showstatusbar' value='0' valuetype='data' />";
		s = s + 	"<param name='showdisplay' value='0' valuetype='data' />";
		s = s + 	"<param name='PlayCount' value='1' valuetype='data' />";
		s = s + "</object>";
	}
	else
	{
		s = "<embed id='__PLAYER_ID_VALUE' width='147' height='27' loop='0' autostart='true' src='__VOICE_URL_VALUE' ";
		s = s + "AUTOREWIND='0' SHOWCAPTIONING='0' SHOWGOTOBAR='0' AUTOSIZE='0' SHOWPOSITIONCONTROLS='0' SHOWDISPLAY='0' ";
		s = s + "SHOWTRACKER='1' SHOWCONTROLS='1' SHOWSTATUSBAR='0' ></embed>";
	}
	
	s = s.replace( /__PLAYER_ID_VALUE/g, holder.player_id );
	s = s.replace( /__VOICE_URL_VALUE/g, holder.voice_url );

	if( holder.player_embed_pos == "hidden" )
	{
		s = s.replace( /__PLAYER_STYLE_VALUE/g, "width:0px;height:0px;" );
		document.body.insertAdjacentHTML( "BeforeEnd", s );
	}
	else
	{
		s = s.replace( /__PLAYER_STYLE_VALUE/g, "width:147px;height:27px;" );
		var elem = document.getElementById( holder.player_embed_id );
		elem.insertAdjacentHTML( holder.player_embed_pos, s );
	}

}

//-------------------------------------------
//-------------------------------------------


