var liveintent = null;
(function () {
    var ua = navigator.userAgent.toLowerCase(),
        d = document,
        b = {
        ie6: /msie 6.0/.test(ua),
        opr: /opera/.test(ua),
        ie7: /msie 7.0/.test(ua),
        msi: (/msie/.test(ua)) && !(/opera/.test(ua))
    },
        w = window,
        de = d.documentElement,
        db = d.body;
    liveintent = {
        li_conf: {
                "project"    : 'liveintent',
                "offset_x"   : 0,
                "offset_y"   : 0,
                "win_pos"    : '',
                "mout_tm"    : '1000',
                "designs"    : ['h'],
                "P1" : '196',
                "exclude_p1" : '0'
        },
        li_pwd: 'http://c.liveintent.com/',
        li_exfr: false,
        li_exlo: false,
        li_btn: null,
        li_mout: true,
        li_page: '',
        li_frm: 'liveintent_app_exfrm',
        li_tmr: null,
        did: 'h',
        li_css: function (ph) {
            try {
                var l = d.ce("link");
                l.rel = "stylesheet";
                l.type = "text/css";
                l.href = ph;
                l.media = "all";
                d.gn("head")[0].appendChild(l);
            } catch(e) {}
        },
        li_setc: function (name, value, expires) {
            if (expires) {
                var date = new Date();
                date.setTime(date.getTime() + (expires * 86400000));
            }
            d.cookie = name + "=" + value + ((expires) ? ";expires=" + date.toGMTString() : "") + ";path=/";
        },
        li_getc: function (name) {
            var nameEQ = name + "=",
                ca = d.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);
            }
            return false;
        },
        li_delc: function (name) {
            if (this.li_getc(name)) {
                this.li_setc(name, '', -1);
            }
        },
        li_getd : function() {
            var dc = this.li_getc('did'), ds = this.li_conf['designs'];
            if(dc) {
                for(key in ds) {
                    if(ds[key] == dc) {
                        return dc;
                    }
                }
            }
            var rand = Math.floor((Math.random()*ds.length));
            return ds[rand];
        },
        li_setd : function() {
            var did =  this.did || this.li_getd();
            this.li_delc('did');
            this.li_setc('did', did, 364);
            this.did = did;
        },
        li_list: function () {
            var or = w.onresize,
                os = w.onscroll;
            if (!b.msi) {
                w.addEventListener('load', function () {
                    liveintent.li_pos();
                    liveintent.li_adp();
                }, false);
            } else {
                w.attachEvent('onload', function () {
                    liveintent.li_pos();
                    liveintent.li_adp();
                });
            };
            w.onresize = function () {
                if (or) {
                    or();
                };
                liveintent.li_pos();
            };
            w.onscroll = function () {
                if (os) {
                    os();
                };
                if (liveintent.li_exlo) {
                    liveintent.li_pos();
                }
            };
        },
        li_spos: function () {
            if (typeof(w.pageYOffset) == "number") {
                return [w.pageXOffset, w.pageYOffset];
            } else {
                if (db && (db.scrollLeft || db.scrollTop)) {
                    return [db.scrollLeft, db.scrollTop];
                } else {
                    if (de && (de.scrollLeft || de.scrollTop)) {
                        return [de.scrollLeft, de.scrollTop];
                    } else {
                        return [0, 0];
                    }
                }
            }
        },
        li_area: function (cs) {
            var xs = 0,
                ys = 0,
                ww = 0,
                wh = 0;
            if (cs) {
                if (w.innerHeight && w.scrollMaxY) {
                    xs = db.scrollWidth;
                    ys = w.innerHeight + w.scrollMaxY;
                } else {
                    if (db.scrollHeight > db.offsetHeight) {
                        xs = db.scrollWidth;
                        ys = db.scrollHeight;
                    } else {
                        xs = db.offsetWidth;
                        ys = db.offsetHeight;
                    }
                }
            }
            if (self.innerHeight) {
                ww = self.innerWidth;
                wh = self.innerHeight;
            } else {
                if (de && de.clientHeight) {
                    ww = de.clientWidth;
                    wh = de.clientHeight;
                } else {
                    if (db && (db.clientWidth || db.clientHeight)) {
                        ww = db.clientWidth;
                        wh = db.clientHeight;
                    } else {
                        if (db) {
                            ww = db.clientWidth;
                            wh = db.clientHeight;
                        }
                    }
                }
            }
            return [(cs !== true || xs < ww ? ww : xs), (cs !== true || ys < wh ? wh : ys)];
        },
        li_pos: function () {
            if (this.li_btn == null) {
                return;
            }
            var cn = d.gi('liveintent_app_container'),
                cnts = cn.style,
                pos = this.li_xy(this.li_btn),
                spos = this.li_spos(),
                area = this.li_area(),
                btn = [this.li_btn.clientWidth ? this.li_btn.clientWidth : this.li_btn.offsetWidth, this.li_btn.clientHeight ? this.li_btn.clientHeight : this.li_btn.offsetHeight],
                cnt = [cn.clientWidth, cn.clientHeight],
                wpos = (this.li_conf['win_pos'] != '') ? this.li_conf['win_pos'] : this.li_btn.getAttribute('li_window_pos') ? this.li_btn.getAttribute('li_window_pos') : "left",
            sd = wpos.indexOf("side") != -1 ? 1 : 0,
            dr = wpos.indexOf("right") != -1 ? 1 : 0,
            df = [3, 2],
            v = new Array(4),
            po = new Array(12);
            for (i = 0; i < 4; i++) {
                v[i] = new Array(4);
                for (j = 0; j < 4; j++) {
                    v[i][j] = 0;
                }
            }
            for (var j = 0; j < 12; j++) {
                po[j] = new Array(2);
            }
            for (var i = 0; i < 2; i++) {
                var l = new Array(4);
                l[0] = pos[i] - spos[i] > cnt[i] ? 1 : 0;
                l[1] = pos[i] + btn[i] - spos[i] > cnt[i] && pos[i] + btn[i] < spos[i] + area[i] ? 1 : 0;
                l[2] = spos[i] + area[i] - pos[i] > cnt[i] && pos[i] > spos[i] ? 1 : 0;
                l[3] = spos[i] + area[i] - pos[i] - btn[i] > cnt[i] ? 1 : 0;
                if (! (l[0] || l[1] || l[2] || l[3])) {
                    if (pos[i] + btn[i] - spos[i] > spos[i] + area[i] - pos[i]) {
                        if (pos[i] + btn[i] > spos[i] + area[i]) {
                            l[0] = 1;
                        } else {
                            l[1] = 1;
                        }
                    } else {
                        if (pos[i] > spos[i]) {
                            l[2] = 1;
                        } else {
                            l[3] = 1;
                        }
                    }
                }
                for (j = 0; j < 4; j++) {
                    for (t = 0; t < 4; t++) {
                        if (i == 0) {
                            v[j][t] += l[t];
                        } else {
                            v[t][j] += l[t];
                        }
                    }
                }
            }
            if (sd) {
                if (dr) {
                    po = [
                        [2, 0],
                        [1, 0],
                        [2, 3],
                        [1, 3],
                        [0, 1],
                        [0, 2],
                        [3, 1],
                        [3, 2],
                        [3, 0],
                        [0, 0],
                        [0, 3],
                        [3, 3]
                    ];
                } else {
                    po = [
                        [2, 3],
                        [1, 3],
                        [2, 0],
                        [1, 0],
                        [3, 2],
                        [3, 1],
                        [0, 2],
                        [0, 1],
                        [3, 0],
                        [0, 0],
                        [0, 3],
                        [3, 3]
                    ];
                }
            } else {
                if (dr) {
                    po = [
                        [3, 1],
                        [3, 2],
                        [0, 1],
                        [0, 2],
                        [1, 0],
                        [2, 0],
                        [1, 3],
                        [2, 3],
                        [3, 0],
                        [0, 0],
                        [0, 3],
                        [3, 3]
                    ];
                } else {
                    po = [
                        [3, 2],
                        [3, 1],
                        [0, 2],
                        [0, 1],
                        [2, 3],
                        [1, 3],
                        [2, 0],
                        [1, 0],
                        [3, 0],
                        [0, 0],
                        [0, 3],
                        [3, 3]
                    ];
                }
            }

            for(var key in po) {
                if (!!po[key].push) {
                    if (v[po[key][0]][po[key][1]] == 2) {
                        df[0] = po[key][0];
                        df[1] = po[key][1];
                        break;
                    }
                }
            }
            for (i = 0; i < 2; i++) {
                j = i ^ 1;
                switch (df[i]) {
                case 0:
                    pos[j] -= cnt[j];
                    if (j == 1 && !b.ie6) {
                        pos[j] -= 2;
                    }
                    break;
                case 1:
                    pos[j] -= cnt[j] - btn[j];
                    if (j == 1 && b.ie6) {
                        pos[1] += 2;
                    }
                    break;
                case 2:
                    pos[j] = pos[j];
                    break;
                case 3:
                    pos[j] += btn[j];
                    if (j == 1) {
                        pos[1] += 2;
                    }
                    break;
                }
            }
            pos[0] += parseInt(this.li_conf['offset_x']);
            pos[1] += parseInt(this.li_conf['offset_y']);
            cnts.left = pos[0] + "px";
            cnts.top = pos[1] + "px";
        },
        li_clo: function (ob) {
            d.gi(ob).style.display = 'none';
        },
        li_ope: function (ob) {
            d.gi(ob).style.display = 'block';
        },
        li_app_load: function () {
           liveintent.li_clo('liveintent_app_preloader');
           liveintent.li_ope('liveintent_app_place');
        },
        li_psn_path: function () {
            var ms=window.document.getElementsByTagName('meta'),kw='';
            for(var i in ms){
                if((ms[i].name+'').toLowerCase()=='keywords'||(ms[i].name+'').toLowerCase()=='author') {
                    if(kw!=''){
                        kw+=',';
                    }
                    kw += ms[i].content.replace(/\s*,\s*/g,',');
                }
            }
            var p = location.pathname.toString()+'';
            if (p.length > 1) {
                if (p.indexOf('.') != -1) {
                    var d = p.lastIndexOf('/');
                    p = p.substr(0,d);
                }
                kw += p.replace(/\//g,',');
            }
            kw = kw.replace(/\s*,\s*,\s*/g,',');
            if(kw!='') {
                kw = encodeURIComponent(kw).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
            }
            return this.li_pwd + 'html/' + this.did + '.html?p='+ this.li_conf['project'] + '&kw=' + kw;
        },
        li_cll: function (atn) {
            var ifr = d.gi(this.li_frm),
                cnt = d.gi('liveintent_app_container'),
                sz = '0px';
            this.li_cto();
            if (atn) {
                this.li_clo('liveintent_close_btn');
                if (ifr != null && cnt != null) {
                    if (b.msi) {
                        sz = '1px';
                    }
                    ifr.height = cnt.style.height = sz;
                    ifr.width = cnt.style.width = sz;
                }
                this.li_exlo = false;
            } else {
                this.li_ope('liveintent_close_btn');
                if (ifr != null && cnt != null) {
                    ifr.height = cnt.style.height = '350px';
                    ifr.width = cnt.style.width = '300px';
                }
                this.li_exlo = true;
            }
        },
        li_show_app: function () {
            var path = this.li_psn_path();
            this.li_cll(false);
            this.li_ope("liveintent_app_container");
            if (b.msi) {
                var el = window.event.srcElement,
                    es = el;
                while (!el.getAttribute('onmouseover') || !new String(el.getAttribute('onmouseover')).match(/liveintent\.li_show_app/)) {
                    el = el.parentNode;
                    if (new String(el.nodeName).toLowerCase() == 'body') {
                        el = es;
                        break;
                    }
                }
                if (el != null) {
                    el.onmouseout = function () {
                        liveintent.li_stm();
                    };
                    this.li_btn = el;
                }
            } else {
                var ev = liveintent.li_show_app.caller.arguments[0];
                if (ev != null) {
                    ev.currentTarget.onmouseout = function () {
                        liveintent.li_stm();
                    };
                    this.li_btn = ev.currentTarget;
                }
            }
            for (var i = 0; i < this.li_btn.childNodes.length; i++) {
                if (this.li_btn.childNodes[i].nodeType == d.ELEMENT_NODE) {
                    if (this.li_btn.childNodes[i].getAttribute('li_window_pos') != null || this.li_btn.childNodes[i].getAttribute('id') == 'liveintent_follow_us_btn') {
                        this.li_btn = this.li_btn.childNodes[i];
                        break;
                    }
                }
            }
            if (!this.li_exfr) {
                d.gi('liveintent_app_place').innerHTML = '<iframe id="' + this.li_frm + '" name="' + this.li_frm + '" onload="return liveintent.li_app_load();" src="' + path + '" width="300px" height="350px" frameborder="0" scrolling="no" align="top" ALLOWTRANSPARENCY="true"></iframe>';
                this.li_exfr = true;
            }
            if (this.li_btn != null) {
                this.li_pos();
            }
        },
        li_adp: function () {
            if (parseInt(this.li_conf['exclude_p1']) == 0) {
                this.li_cr('<img id="liveintent_pixel_p1" width="1" height="1" alt="" src="http://l1v.ly/' + this.li_conf['P1'] + '?aid=' + this.li_conf['project'] +'&did=' + this.did + '&st=bv&ua=un&k=ex"/>', 10003);
            }
        },
        li_xy: function (a) {
            var e = document.documentElement,
                x = 0,
                y = 0,
                o = 0,
                p = 0,
                r = /fixed/;
            do {
                o = r.test(a.style.position);
                p |= o;
                y += a.offsetTop || 0;
                x += a.offsetLeft || 0;
                if (o && a) {
                    y += a.scrollTop;
                    x += a.scrollLeft;
                }
                a = a.offsetParent;
            } while (a);
            if (!b.ie6 && e.scrollTop && p) {
                y += e.scrollTop;
                x += e.scrollLeft;
            }
            return [x, y];
        },
        li_cr: function (html, index) {
            var el = d.createElement("div"),
                es = el.style;
            es.position = "absolute";
            es.zIndex = index;
            es.top = "0";
            es.left = "0";
            d.body.insertBefore(el, d.body.firstChild);
            el.innerHTML += html;
        },
        li_cto: function () {
            if (this.li_tmr !== null) {
                clearTimeout(this.li_tmr);
            }
        },
        li_stm: function () {
            this.li_tmr = setTimeout('liveintent.li_cll(true)', parseInt(this.li_conf['mout_tm']));
        },
        li_on: function () {
            this.li_setd();
            this.li_css(this.li_pwd + 'css/exwin.css');
            var i = 0;
            if (b.opr) {
                i_0 = 33554431;
            } else {
                i_0 = 2147483647;
            }
            this.li_cr('<div id="liveintent_app_container" onmouseout="liveintent.li_stm()" onmouseover="liveintent.li_cto()" style="display:none; overflow-y: hidden;"><div id="liveintent_close_btn" onClick="liveintent.li_cll(true);"><img alt="x" src="'+this.li_pwd+'img/close_v8c.gif" title="Close window"/></div><div id="liveintent_app_preloader"><div id="liveintent_app_loading"></div></div><div id="liveintent_app_place" style="display:none;"></div></div>', i_0);
            this.li_list();
        }
    };
    d.ce = d.createElement;
    d.gn = d.getElementsByTagName;
    d.gi = d.getElementById;
    if (d.ELEMENT_NODE == null) {
        d.ELEMENT_NODE = 1;
        d.TEXT_NODE = 3;
    }
    liveintent.li_on();
})();
