// GuideChat  - ver. 2.0
// Work with cookies
// Copyright(c) 2008 WaveAccess, http://wave-access.com

var sLiveHelpCookie = 'LiveHelpCookie';
var iMaxClicks = 2;

function clickCounter() {
    var iCookie = readCookie(sLiveHelpCookie);
    if (!iCookie) {
        iCookie = 0;
        setCookie(0);
    }
    if (iCookie && iCookie != '-1') {
        iCookie++;
        setCookie(iCookie);
    }
    if (iCookie > iMaxClicks - 1)
        embedObject();
}

function supportOnline() {
    openDIV("");
}

function setSupportJID(message) {
    var divMessage = message.split("divMessage=")[1];
    openDIV(divMessage);
}

function openDIV(message) {
    try {
        if (readCookie(sLiveHelpCookie) != '-1') {
            var chatDIV = document.createElement('DIV');
            chatDIV.id = 'chatDIV';
            chatDIV.style.cssText = 'border:1px solid #808080; background:#ffffff; padding:5px 7px 20px 20px; position:absolute; z-index:98854;top:0px;left:0px;width:300px;text-align:center';
            chatDIV.innerHTML = '<img src="GuideChat/images/wa_close.gif" onClick="CloseChatDiv()" title="Close" style="cursor:pointer;float:right;margin:0 0 10px 0;clear:both"><div style="FONT:12px Arial, Helvetica, Tahoma, Verdana, sans-serif;text-align:left;padding-right:15px;margin-top:20px;">' + message + '</div><div style="float:none;margin:20px auto 0 auto;background:url(GuideChat/images/btn_enter_chat.png);width:71px;height:18px"><a onClick="Chat()" style="color:#ffffff;FONT:12px/18px Arial, Helvetica, Tahoma, Verdana, sans-serif;background:url(GuideChat/images/btn_enter_chat.png);width:71px;height:18px;display:block;cursor:pointer">&nbsp;</a></div>';
            document.body.appendChild(chatDIV);
            document.getElementById('chatDIV').style.top = (document.body.offsetHeight - document.getElementById('chatDIV').offsetHeight) / 2 + "px";
            document.getElementById('chatDIV').style.left = (document.body.offsetWidth - 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=462,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=70.38.112.42&guest_jid=guest_a4a13c2d0fa2f67c830a56"/>\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=70.38.112.42&guest_jid=guest_a4a13c2d0fa2f67c830a56">\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) {
        alert(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, Köln/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);




