// GuideChat  - ver. 2.0
// Work with cookies
// Copyright(c) 2008 WaveAccess, http://wave-access.com

var sLiveHelpCookie = 'LiveHelpCookie';
var iMaxClicks = 2;
//var sGuestJid = 'guest_b8b72de80229fae59384a8';
//var sJabberServer = 'mihail-pc';
var sContactFormURL = 'ContactChat.aspx';
var sWindowParams = 'width=400,height=370,toolbar=0,left=300,top=400';

var sGuestJid = 'guest_b7b345e99c9fc067b89242';
var sJabberServer = '70.38.112.42';

function clickCounter(){    
    renderButton(presence, jid);
    if (iMaxClicks <= 0)
        return;
	var iCookie = readCookie(sLiveHelpCookie);	
	if (!iCookie){
	    iCookie = 0;
	    setCookie(0);
	}
	if(iCookie && iCookie!='-1'){
		iCookie++;
		setCookie(iCookie);
	}
	if ((iCookie > iMaxClicks - 1) && presence == 'online')
	    openDIV();
}

function supportOnline()
{
    openDIV();    
}

function setSupportJID()
{
     openDIV();
}
function renderButton(status, jid){
    if (jid != sGuestJid) 
        status='offline';    
    var obj = document.getElementById("chatOnlineButtonDiv");
    var chatBtn = document.createElement('IMG');
    try{    
        chatBtn.id = 'chatBtn';    
        //chatBtn.src = 'http://mihail-pc/wave-access/i/live_chat_' + status + '.gif';
        chatBtn.src = 'http://wave-access.com/i/live_chat_' + status + '.gif';
        if (status == 'online'){
            chatBtn.onclick = Chat;
            chatBtn.style.cssText = "cursor:pointer;";
        }
        else{            
                if (typeof(sContactFormURL) != 'undefined'){
                    if (sContactFormURL.length > 0){                                                
                        chatBtn.style.cssText = "cursor:pointer;";
                        chatBtn.onclick = openContactForm;
                    }
                    else
                        chatBtn.style.cssText = "cursor:default;";                        
                }
                else
                    chatBtn.style.cssText = "cursor:default;";                    
        }   
    obj.appendChild(chatBtn);
    }
    catch(err){}
}
function openContactForm()
{
    window.open(sContactFormURL,'new', sWindowParams);
}
function openDIV()
{
	try{
	    if (readCookie(sLiveHelpCookie)!= '-1')
		{
		    var chatDIV = document.createElement('DIV');
		    chatDIV.id='chatDIV';
		    chatDIV.style.cssText = 'border:1px solid #808080; background:#ffffff; padding:5px 5px 20px 20px; position:absolute; z-index:98854;top:0px;left:0px;width:300px;text-align:center';
		    chatDIV.innerHTML = '<img src="http://wave-access.com/i/wa_close.gif" onClick="CloseChatDiv()" title="Close" style="cursor:pointer;float:right;margin:0 0 10px 0;clear:both"><div style="text-align:left;padding-right:15px;margin-top:20px;">' + divMessage + '</div><div class="questionOnlineMenuLink" style="float:none;width:100px;margin:20px auto 0 auto;"><a onClick="Chat()"><span>Enter Chat</span></a></div>';
		    document.body.appendChild(chatDIV);
		    document.getElementById('chatDIV').style.top = (screen.availHeight - document.getElementById('chatDIV').offsetHeight)/2 + "px";
		    document.getElementById('chatDIV').style.left = (screen.availWidth - document.getElementById('chatDIV').offsetWidth)/2 + "px";
		    setCookie(-1);
		}
	}
	catch(err){}
}
function CloseChatDiv()
{
	try{
		setCookie(-1);
		if( document.getElementById('chatDIV') != null )
			document.getElementById('chatDIV').style.display = "none";
	}
	catch(err){}
}

function Chat(){
	try{
		setCookie(-1);
		CloseChatDiv();
		window.open('../GuideChat/gc.html','new','width=460,height=450,toolbar=0, left=300,top=400');
	}
 	catch(err){}
}
/*
function embedObject(){
    var sResult = '';
    try{
	    sResult  = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="rosterConnector" width="0" height="0" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">\n';
        sResult += '<param name="movie" value="../GuideChat/rosterConnector.swf" />\n';
        sResult += '<param name="quality" value="high" />\n';
        sResult += '<param name="bgcolor" value="#ffffff" />\n';
        sResult += '<param name="allowScriptAccess" value="always" />\n';        
        sResult += '<param name="flashVars" value="jabber_server=' + sJabberServer + '&guest_jid=' + sGuestJid + '"/>\n';
        sResult += '<embed src="../GuideChat/rosterConnector.swf" quality="high" bgcolor="#ffffff" width="0" height="0" name="GuideChat" align="middle" play="true" loop="false" quality="high" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" flashVars="jabber_server=' + sJabberServer + '&guest_jid=' + sGuestJid + '">\n';
        sResult += '</embed>\n';
        sResult += '</object>';
        
        var swfGuideChatDIV = document.createElement('DIV');
		swfGuideChatDIV.id='swfGuideChatDIV';
		swfGuideChatDIV.style.cssText = "position:absolute;top:-10px;left:-10px;z-index:98853width:1px;height:1px";
		swfGuideChatDIV.innerHTML = sResult;
		
		document.body.appendChild(swfGuideChatDIV);
    }
    catch(err){}
}
*/
function readCookie(name){
    try{    
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++){
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	}
	catch(err){}
	
	return null;
}

function setCookie(number){
    try{
        document.cookie = sLiveHelpCookie + '=' + number + ';path=/';
    }
    catch(err){} 
}

//Following code checks if DOM is loaded. 
///////////////////////////////////////////////////////////////////////
// thanks!:) original from http://dean.edwards.name/weblog/2005/10/add-event/
// with input from Tino Zijdel, Matthias Miller, Diego Perini, Tanny O'Haley, Hedger Wang
// Author: Dr. Dirk Fischer, Keln/Cologne, Germany : Tested with www.browsershots.org : thanks!
// Version 1.2
function addEvent(o, t, h) {
    var s = /chrome/i.test(navigator.userAgent) ? false
: window.execScript ? "i"
: /safari/i.test(navigator.userAgent) ? "s"
: document.clientWidth || window.opera && !window.getSelection ? "k"
: document.addEventListener ? false : "o",
d = "DOMContentLoaded";
    if (t.toLowerCase() == "load") o = initLoad(o, s);
    if (!o) return false;
    if (o.addEventListener && !(s && t == d)) o.addEventListener(t, h, false);
    else {
        if (!h.$$guid) h.$$guid = addEvent.guid++;
        if (!o.evs) o.evs = {};
        var hs = o.evs[t];
        if (!hs) { hs = o.evs[t] = {}; if (o['on' + t]) hs[0] = o['on' + t]; }
        hs[h.$$guid] = h; o['on' + t] = handleEvent;
        if (t == d && addEvent.waitDOM == -1) initDOMLoad(o, h, s);
    }
    function initLoad(o, s) {
        if (o && (o == window || o == document || o == document.body)) {
            if (s == "k") o = document;
            else element = window;
            if (!addEvent.waitLoad && s && s != "k" && s != "s") {
                if (document.ids) document.tags.body.position = "absolute";
                addEvent.waitLoad = true;
                addEvent(o, 'load', function() { addEvent.waitLoad = false; });
                (function() {
                    if (addEvent.waitLoad) {
                        var o = document.body;
                        if (!testLoad(o) && !testLoad((o = document)) & !testLoad((o = window)))
                            setTimeout(arguments.callee, 10); else { addEvent(o, 'load', o.onload); o.onload = handleEvent; } 
                    }
                })();
            } 
        } return o;
    }
    function testLoad(o) { return !o || typeof o.onload != "function" || o.onload == handleEvent ? false : true; }
    function initDOMLoad(o, h, s) {
        addEvent.waitDOM = true;
        if (s == "s" || s == "k")
            (function() {
                if (addEvent.waitDOM) {
                    if (/loaded|complete/i.test(document.readyState))
                        doDOMLoad(); else setTimeout(arguments.callee, 10);
                } 
            })();
        else if (s == "i")
            (function() {
                if (addEvent.waitDOM) {
                    /*@cc_on@*//*@if (@_win32 || @_win64)
                    var e = document.createElement('doc:rdy');
                    try { e.doScroll('left'); e = null; doDOMLoad(); } catch (err) { setTimeout(arguments.callee, 10); }
                    @end@*/
                } 
            })();
        else addEvent(o, "load", h);
    }
    function doDOMLoad() { var t = "DOMContentLoaded"; addEvent.waitDOM = false; (window["on" + t])({ "type": t }); }
}
addEvent.guid = 1;
addEvent.waitDOM = -1;
function removeEvent(o, t, h) {
    if (o.removeEventListener) o.removeEventListener(t, h, false);
    else if (o.evs && o.evs[t] && h.$$guid) delete o.evs[t][h.$$guid];
}
function handleEvent(ev) {
    var r = true;
    ev = ev || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
    var hs = this.evs[ev.type];
    for (var i in hs) {
        this.$$h = hs[i];
        if (this.$$h(ev) === false) r = false;
    }
    if (this.$$h) this.$$h = null;
    return r;
}
function fixEvent(ev) {
    if (ev) { ev.preventDefault = fixEvent.preventDefault; ev.stopPropagation = fixEvent.stopPropagation; }
    return ev;
}
fixEvent.preventDefault = function() { this.returnValue = false; }
fixEvent.stopPropagation = function() { this.cancelBubble = true; }
///////////////////////////////////////////////////////////////////////

addEvent(window, 'DOMContentLoaded', clickCounter);