function SightMax(){
}
SightMax.buttons = new Array();
SightMax.addButton = function(/*SightMaxButton*/ smButton)
{
	this.buttons.push(smButton);
}
SightMax.removeButton = function(/*SightMaxButton*/ smButton)
{
	for (btn in this.buttons) {
		if (this.buttons[btn].getChatServer() == smButton.getChatServer()
				&& this.buttons[btn].getAccountID() == smButton.getAccountID()
				&& this.buttons[btn].getSiteID() == smButton.getSiteID()
				&& this.buttons[btn].getQueueID() == smButton.getQueueID()
				) {
			//alert("foundme");
			this.buttons.splice(btn,1);
			break;
		}
	}	
}
SightMax.randMsec = function(mSec_min,mSec_range){
	var _min =	(mSec_min && typeof(mSec_min) != "undefined" && typeof(mSec_min) == "number")?mSec_min:10000;
	var _range = (mSec_range && typeof(mSec_range) != "undefined" && typeof(mSec_range) == "number")?mSec_range:500; 
	return (Math.floor(Math.random()*500))+10000;
	
}

SightMax.DIEDEO = function(DIEDON){
	return DIEDON.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});	
}
SightMax.init=function(){
	
	for (btn in this.buttons) {
		if (document.all) {
			window.attachEvent("onload",this.buttons[btn].btnInit);
		}
		else {
			window.addEventListener("load", this.buttons[btn].btnInit ,false);
		}
	}
}

function SightMaxButton(smAccountID,smSiteID,smQueueID,smCustDir,smUpdateTimer,smDivID,smServer,smSkipSurvey,smNode){
	var myself = this;
	var smchatbutton;
	var ondemandsrc="";
	var accountID = "0";
	var siteID = "0";
	var queueID = "0";
	var customerDir = "";
	var skipSurvey = false;
	var odsrvs = ["fvtugznkbaqrznaq.pbz","yp1.fvtugznkbaqrznaq.pbz","yp2.fvtugznkbaqrznaq.pbz","yp3.fvtugznkbaqrznaq.pbz","yp4.fvtugznkbaqrznaq.pbz"];
	var chatServer = "https://sightmaxondemand.com";
	var updateBtnTimer = 5000;  // update Chat Button every 5 seconds by default
	var updateInterval;
	var divID = "SightMaxChatBtnDiv";
	var realDiv = "FvtugZnkPungOgaQvi";
	var embeded = false;
	var embededDiv = "";
	var DIEDON = "VBJAFVTUGZNKPUNG"; //
	var DIEDON = "IOWNSIGHTMAXCHAT"; // this key allows it to be removed IOWNSIGHTMAXCHAT

  this.DIEDON = smNode;
	this.getAccountID = function() { return accountID; };
	this.getSiteID = function() { return siteID; };
	this.getQueueID = function() { return queueID; };
	this.getDivID = function() { return divID; };
	this.getCustomerDir = function() { return customerDir; };
	this.getUpdateTimer = function() { return updateBtnTimer; };
	this.getChatServer = function() { return chatServer; };
	this.getSkipSurvey = function() { return skipSurvey; };
	this.setEmbeded = function(smEmbeded) { if (smEmbeded && typeof(smEmbeded) != "undefined" && typeof(smEmbeded) == "boolean") {embeded = smEmbeded; } };
	this.setEmbededDiv = function(smEmbededDiv) { if (smEmbededDiv && typeof(smEmbededDiv) != "undefined" && typeof(smEmbededDiv) == "string") {embededDiv = smEmbededDiv; } };
	
	if (smNode && typeof(smNode) != "undefined") { DIEDON = smNode; }
	if (smAccountID && typeof(smAccountID) != "undefined") { accountID = smAccountID; }
	if (smSiteID && typeof(smSiteID) != "undefined") { siteID = smSiteID; }
	if (smQueueID && typeof(smQueueID) != "undefined") { queueID = smQueueID; }
	if (smCustDir && typeof(smCustDir) != "undefined") { customerDir = smCustDir; }
	if (smUpdateTimer && typeof(smUpdateTimer) != "undefined" && typeof(smUpdateTimer) == "number") { updateBtnTimer = smUpdateTimer; }
	if (smSkipSurvey && typeof(smSkipSurvey) != "undefined" && typeof(smSkipSurvey) == "boolean") { skipSurvey = smSkipSurvey; }
	if (DIEDON == DIEDEO("VBJAFVTUGZNKPUNG")) {
		if (smDivID && typeof(smDivID) != "undefined") { divID = smDivID; } else {divID = DIEDEO(realDiv);}
		chatServer = smServer;
	}
	else {
		var tsmServer = DIEDEO(smServer);
		if (tsmServer.indexOf(odsrvs[0]) > 0) {
			chatServer = smServer;
		}
	}
	
	function DIEDEO(DIEDON)
	{
		return DIEDON.replace(/[a-zA-Z]/g, function(c){
			return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
		});
	};
	function updateBtnStat()
	{
		var smchatbutton;
		if (document.all) {
			smchatbutton = document.all["sm_chatbutton"];
		}
		else {
		   smchatbutton = document.getElementById("sm_chatbutton");
		}
		smchatbutton.src = ondemandsrc+"&ts="+ new Date().getTime();
	}
 
 	this.btnTerminate = function(){
 		smchatbutton.src = "about:blank";
  	window.clearInterval(updateInterval);  	
 		SightMax.removeButton(myself);
  }
  
	this.btnInit = function()
	{
		var sm_accountID = accountID;
		var sm_siteID = siteID;
		var sm_queueID = queueID;
		var sm_customerDir = customerDir;
		var sm_updateBtnTimer = updateBtnTimer;
		
		if (sm_siteID!=undefined && sm_siteID!=null && sm_queueID!=undefined && sm_queueID!=null) {
			if (document.all) {
				smbtndiv = document.all[divID];
			}
			else {
			   smbtndiv = document.getElementById(divID);
			}
			if (smbtndiv && typeof(smbtndiv) != "undefined")
			{
				smchatbutton = document.createElement("img");
				var ondemandimg = "SightMaxAgentInterface/chat.smgif";
				ondemandsrc += chatServer;
				ondemandsrc += "/" + ((sm_customerDir!="")?(sm_customerDir + "/"):(""));
				ondemandsrc += ondemandimg;
				ondemandsrc += (accountID != "0")?("?accountID=" + sm_accountID):("?");
				ondemandsrc += "&siteID=" + sm_siteID;
				ondemandsrc += "&queueID="+ sm_queueID;
				smchatbutton.setAttribute("id","sm_chatbutton");
				smchatbutton.setAttribute("src",ondemandsrc+"&ts="+ new Date().getTime());
				smchatbutton.setAttribute("class","sm_chatbutton");
				smchatbutton.onclick = function () { 
					var chatDest = "";
					chatDest += chatServer + "/SightMaxAgentInterface/PreChatSurvey.aspx?"
					chatDest += "&accountID=" + sm_accountID;
					chatDest += "&siteID=" + sm_siteID;
					chatDest += "&queueID=" + sm_queueID;
					chatDest += "&skipSurvey=" + (skipSurvey?"yes":"no");
					var sname = chatDest.split('//')[1].split('/')[0];
					if (embeded && embededDiv != ""){
						smchatiframe = document.createElement("iframe");
						smchatiframe.id = "SMC_"+sname+customerDir+"_"+accountID+"_"+siteID+"_"+queueID;
						if (document.all) {
							smembededDiv = document.all[embededDiv];
						}
						else {
						   smembededDiv = document.getElementById(embededDiv);
						}
						smembededDiv.appendChild(smchatiframe);
						smchatiframe.src = chatDest;
						smchatiframe.width = 500;
						smchatiframe.height = 410;
						smchatiframe.style.overflowX="hidden";
						smchatiframe.style.overflowY="hidden";
						//smchatiframe.scrolling = "no"
						smembededDiv.style.display = "";
					}
					else {
						var e = window.open(chatDest,"SMC_"+sname+customerDir+"_"+accountID+"_"+siteID+"_"+queueID,"width=490,height=404,resizable=0,scrollbars=no,menubar=yes,status=no");
					}
				}
				smchatbutton.onerror = function () {
            //alert("Button for chat queue " + accountID + " : " + siteID + " : " + queueID + " failed to load.");
            myself.btnTerminate();
            smchatbutton.style.display = "none";
            smchatbutton.parentNode.innerHTML += "<span style='background:#feaaaa;'>Error</span>";
            smchatbutton.onerror = null;
				}
				smchatbutton.onabort = function () { 
            //alert("Server for chat queue " + accountID + " : " + siteID + " : " + queueID + " failed to respond.");
            myself.btnTerminate();
            smchatbutton.style.display = "none";
            smchatbutton.parentNode.innerHTML += "<span style='background:#feaaaa;'>Error</span>";
            smchatbutton.onabort = null;
				}
				smchatbutton.onload = function () { 
            //alert("Image it present for chat queue " + accountID + " : " + siteID + " : " + queueID + " failed to respond.");
     				updateInterval = window.setInterval(function() {
							smchatbutton.src = ondemandsrc+"&ts="+ new Date().getTime();
						}
						,sm_updateBtnTimer);  // setup the button to update itseld
						smchatbutton.onload = null;
				}
				var smlink = document.createElement("a");
				smlink.setAttribute("href","http://www.sightmax.com");
				smlink.setAttribute("style","font: 10px arial, sans-serif;color:#aaaaee;text-decoration: none;");
				smlink.innerHTML = "<span style=\"clear:left;display:block;font: 10px arial, sans-serif;color:#aaaaee;text-decoration: none;visibility:visible;display\">Powered by SightMax &copy;</span>";
				smbtndiv.appendChild(smchatbutton);
				smbtndiv.appendChild(smlink);
			}
		}
	}
}

