// define FlashPhone main namespace
var FlashPhone = {};

// constants
FlashPhone.constants = {};
FlashPhone.constants.defaultFlashPhoneDivID = "flashphoneContainer";
FlashPhone.constants.swfurl = "https://www.newetone.com/flex/fxphone.swf";//production environment


if (document.domain == "www.ctezone.com") {
	FlashPhone.constants.swfurl = "http://www.ctezone.com/cte_flex/fxphone.swf";
}

//FlashPhone.constants.swfurl = "fxphone.swf";//local environment
FlashPhone.constants.defaultBgcolor = "#869ca7";

/********************** begin of phone  language class *****************************/
FlashPhone.Language = function(language)
{
    this.itemSeparatorChar = "|";
    this.keyvalueSeparatorChar = ":";
    
    this.language = language;
}

// public method setLanguageObject
FlashPhone.Language.prototype.setLanguageObject = function(language){
    this.language = language;
}

// public method toString
FlashPhone.Language.prototype.toString = function(){
     var langStr = "";
     for(var key in this.language)
     {
        langStr += key + this.keyvalueSeparatorChar
                + this.language[key] + this.itemSeparatorChar;
     }
     return langStr;
}
/********************** end of phone  language class *****************************/

/********************** begin of phone web ui adjustment class *****************************/
// define FlashPhone ui namespace
FlashPhone.ui = {};

FlashPhone.ui.WebUIUpdater = function()
{
    this.flashphoneContainer = undefined;
    this.flashphoneContent = undefined;
    this.defaultDivWidth = undefined;
    this.defaultDivHeight = undefined;
}

FlashPhone.ui.WebUIUpdater.prototype.setFlashphoneContainer = function(flashphoneContainerID)
{
    if (typeof flashphoneContainerID == "string")
        this.flashphoneContainer = document.getElementById(flashphoneContainerID);
    else
        this.flashphoneContainer = flashphoneContainerID;
    
    if (this.flashphoneContainer == null || this.flashphoneContainer == undefined)
        throw new Error("Can't find flash phone html div id by " + flashphoneContainerID);
}

FlashPhone.ui.WebUIUpdater.prototype.show = function(htmlContent)
{
    //this.flashphoneContainer.innerHTML = htmlContent;
    var flashphoneContent = document.createElement("div"); 
    flashphoneContent.id = "flashphoneContent";
    flashphoneContent.style.width = this.defaultDivWidth;
    flashphoneContent.style.height = this.defaultDivHeight;
    flashphoneContent.innerHTML = htmlContent; 
    
    this.flashphoneContainer.appendChild(flashphoneContent); 
    this.flashphoneContent = flashphoneContent;
}

FlashPhone.ui.WebUIUpdater.prototype.update = function(){}

FlashPhone.ui.WebUIUpdater.prototype.restore = function(){}

// begin of resize phone  web ui adjustment class extends WebUIUpdater
FlashPhone.ui.ResizeWebUIUpdater = function(flashphoneContainerID
                                                , defaultDivWidth, defaultDivHeight
                                                , resizeDivWidth, resizeDivHeight)
{
    this.setFlashphoneContainer(flashphoneContainerID);
    this.defaultDivWidth = defaultDivWidth;
    this.defaultDivHeight = defaultDivHeight;
    this.resizeDivWidth = resizeDivWidth;
    this.resizeDivHeight = resizeDivHeight;
}

FlashPhone.ui.ResizeWebUIUpdater.prototype = new FlashPhone.ui.WebUIUpdater();

FlashPhone.ui.ResizeWebUIUpdater.prototype.update = function()
{
    this.flashphoneContent.style.width = this.resizeDivWidth;
    this.flashphoneContent.style.height = this.resizeDivHeight;
}

FlashPhone.ui.ResizeWebUIUpdater.prototype.restore = function()
{
    this.flashphoneContainer.style.width = this.defaultDivWidth;
    this.flashphoneContainer.style.height = this.defaultDivHeight;
    this.flashphoneContent.style.width = this.defaultDivWidth;
    this.flashphoneContent.style.height = this.defaultDivHeight;
}
// end of resize phone  web ui adjustment class

// begin of popup phone  web ui adjustment class extends WebUIUpdater
FlashPhone.ui.PopUpWebUIUpdater = function(flashphoneContainerID, defaultDivWidth, defaultDivHeight)
{
    this.setFlashphoneContainer(flashphoneContainerID);
    this.defaultDivWidth = defaultDivWidth;
    this.defaultDivHeight = defaultDivHeight;
}

FlashPhone.ui.PopUpWebUIUpdater.prototype = new FlashPhone.ui.WebUIUpdater();

FlashPhone.ui.PopUpWebUIUpdater.prototype.update = function()
{
    this.flashphoneContent.style.zIndex = 9999;
}

FlashPhone.ui.PopUpWebUIUpdater.prototype.restore = function()
{
    this.flashphoneContent.style.zIndex = 1;
}
// end of popup phone  web ui adjustment class
/********************** end of phone  web ui adjustment class *****************************/

//
FlashPhone.SwfPhone = function(swfui, width, height, urlprefix, calledprefix, sipserver, defaultCalled, sipUsernamePrefix, dialSoundUrl, lang)
{
    //this.uiUpdater = updater;
    this.swfui = swfui;
    this.urlprefix = urlprefix;
    this.calledprefix = calledprefix;
    this.sipserver = sipserver;
    this.defaultCalled = defaultCalled;
    this.dialSoundUrl = dialSoundUrl;
    this.width = width;
    this.height = height;
    this.sipUsernamePrefix = sipUsernamePrefix;
    
    this.flashvars = "ui=" + this.swfui
                    + "&width=" + this.width
                    + "&height=" + this.height
                    + "&urlprefix=" + this.urlprefix
                    + "&calledNumberPrefix=" + this.calledprefix
                    + "&sipserver=" + this.sipserver
                    + "&called=" + this.defaultCalled
                    + "&dialsoundurl=" + this.dialSoundUrl
                    + "&sipUsernamePrefix=" + this.sipUsernamePrefix
                    + "&lang=" + lang;
    
    this.flashContent = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\""
			+ "id=\"fxphone\" width=\"100%\" height=\"100%\""
			+ "codebase=\"https://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab\">"
			+ "<param name=\"movie\" value=\"" + FlashPhone.constants.swfurl + "\" />"
			+ "<param name=\"quality\" value=\"high\" />"
			+ "<param name=\"bgcolor\" value=\"" + FlashPhone.constants.defaultBgcolor + "\" />"
			+ "<param name=\"allowScriptAccess\" value=\"always\" />"
			+ "<param name=\"wmode\" value=\"transparent\" />"
			+ "<param name=\"flashvars\" value=\"" + this.flashvars + "\" />"
			+ "<embed src=\"" + FlashPhone.constants.swfurl + "\" quality=\"high\" bgcolor=\"" + FlashPhone.constants.defaultBgcolor + "\""
			+ "width=\"100%\" height=\"100%\" name=\"fxphone\" align=\"middle\" "
			+ "play=\"true\" "
			+ "loop=\"false\" "
			+ "quality=\"high\" "
			+ "allowScriptAccess=\"always\" "
			+ "type=\"application/x-shockwave-flash\" "
			+ "wmode=\"transparent\""
			+ "FlashVars=\"" + this.flashvars + "\""
			+ "pluginspage=\"https://www.adobe.com/go/getflashplayer\">"
			+ "</embed>"
			+ "</object>";
}

FlashPhone.SwfPhone.prototype.show = function()
{
    this.uiUpdater.show(this.flashContent);
}

FlashPhone.SwfPhone.prototype.setUIUpdater = function(uiUpdater)
{
    this.uiUpdater = uiUpdater;
}

FlashPhone.SwfPhone.prototype.update = function()
{
    this.uiUpdater.update();
}

FlashPhone.SwfPhone.prototype.restore = function()
{
    this.uiUpdater.restore();
}

