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"    : 'ugo',
                "offset_x"   : '0' ? '0' : typeof(liveintent_offset_x) !='undefined' ? liveintent_offset_x : 0,
                "offset_y"   : '0' ? '0' : typeof(liveintent_offset_y) !='undefined' ? liveintent_offset_y : 0,
                "win_pos"    : '' ? '' : typeof(liveintent_position) !='undefined' ? liveintent_position : '',
                "mout_tm"    : '1000'  ? '1000 ' : typeof(liveintent_timeout)  !='undefined' ? liveintent_timeout : '1000',
                "designs"    : ['h', 'j'],
		"d_sizes"  : { "h" : { w : 300, h : 350}, "j" : { w : 300, h : 435}}
        },
        li_pwd: 'http://c.liveintent.com',
        li_exfr: false,
        li_exlo: false,
        li_btn: null,
        li_mout: true,
        li_frm: 'liveintent_app_exfrm',
        li_tmr: null,
        did: "j",
        li_iframes : null,
        li_curent_frame : 0,
        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'];
            dc = false;
            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);
            var auto_pos = 0;
            if (this.li_conf['win_pos'] == '') {
                auto_pos = 1;
            }
            if (auto_pos) {
            var ixl = area[0] - pos[0], ixr = pos[0] + btn[0],
                iyl = area[1] - pos[1], iyr = pos[1] + btn[1];
            if (ixl > ixr) {
                dr = 0;
            } else {
                dr = 1;
            }
            }
            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) {
                var u = key;
                if (auto_pos) {
                if (iyl < iyr) {
                    u = u^2;
                }
                }
                if (!!po[u].push) {
                    if (v[po[u][0]][po[u][1]] == 2) {
                        df[0] = po[u][0];
                        df[1] = po[u][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 + '&did=' + this.did;
        },
        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) {
                    ifr.height = this.li_conf['d_sizes'][this.did].h +'px';
                    ifr.width = this.li_conf['d_sizes'][this.did].w +'px';
                }
                if (cnt != null) {
                    cnt.style.height = this.li_conf['d_sizes'][this.did].h +'px';
                    cnt.style.width = this.li_conf['d_sizes'][this.did].w +'px';
                }
                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 (!new String(el.getAttribute('class')).match(/liveintent_follow_us_btn/) || !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;
                }
            }
            if (this.li_btn.childNodes != null) {
            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;
                    }
                }
            }
            }

            var frame_id = this.li_btn.getAttribute('frame_id');
            if (frame_id == null) {
                var id = this.li_iframes != null ? this.li_iframes.length : 0;
                this.li_btn.setAttribute('frame_id', id);
                frame_id = id;

            }

            this.li_frm =  'liveintent_frame' + frame_id;

            if (!this.li_exfr || this.li_curent_frame != frame_id) {
                for (var k in this.li_iframes) {
                    if (d.gi('liveintent_frame_cnt' + k) != null)  {
                        d.gi('liveintent_frame_cnt' + k).style.display = 'none';
                    }
                }
                if (this.li_iframes == null || this.li_iframes[frame_id] == null) {
                    liveintent.li_ope('liveintent_app_preloader');
                    liveintent.li_clo('liveintent_app_place');
                    var liveintent_frame = d.ce('iframe');
                    liveintent_frame.name = liveintent_frame.id = this.li_frm;

                    var src = liveintent.li_psn_path(this.li_btn);
                    liveintent_frame.src = src;

                    liveintent_frame.frameBorder = "0";
                    liveintent_frame.scrolling = "no";
                    liveintent_frame.width = this.li_conf['d_sizes'][this.did].w + "px";
                    liveintent_frame.height = this.li_conf['d_sizes'][this.did].h + "px";
                    liveintent_frame.align = "top";
                    liveintent_frame.setAttribute('allowTransparency', 'true');

                    if (!b.msi) {
                        liveintent_frame.addEventListener('load', function () {
                            liveintent.li_app_load();
                        }, false);
                    } else {
                        liveintent_frame.attachEvent('onload', function () {
                            liveintent.li_app_load();
                        });
                    };


                    var liveintent_frame_cnt = d.ce('div');
                    liveintent_frame_cnt.id = 'liveintent_frame_cnt' + frame_id;
                    liveintent_frame_cnt.appendChild(liveintent_frame);
                    d.gi('liveintent_app_place').appendChild(liveintent_frame_cnt);

                    if (this.li_iframes == null) {
                        this.li_iframes = [];
                    }
                    this.li_iframes[frame_id] = this.li_frm;
                } else {
                    d.gi('liveintent_frame_cnt' + frame_id).style.display = 'block';
                }

                this.li_curent_frame = frame_id;
                this.li_exfr = true;
            }
            if (this.li_btn != null) {
                this.li_pos();
            }
        },
        li_adp: function () {
            if ('http://p.l1v.ly/button?aid='+this.li_conf["project"]) {
                this.li_cr('<img id="liveintent_pixel_p1" width="1" height="1" alt="" src="http://p.l1v.ly/button?aid='+this.li_conf["project"]+'&did='+this.did+'&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 () {
            var classname = 'liveintent_follow_us_btn', re = new RegExp('\\b' + classname + '\\b'), els = d.gn("*");
            for(var i=0,j=els.length; i<j; i++) {
                if(re.test(els[i].className)) {
                    var e = els[i];

                    if (!b.msi) {
                        e.addEventListener('mouseover', function () {
                                               liveintent.li_show_app();
                        }, false);
                    } else {
                         e.attachEvent('onmouseover', function () {
                                          liveintent.li_show_app();
                        });
                    };

                }
            }
            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();
})();
