﻿//begin

var leads_BrowserDetect = {
                init: function () {
                    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
                    this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
                    this.OS = this.searchString(this.dataOS) || "an unknown OS";
                },
                searchString: function (data) {
                    for (var i = 0; i < data.length; i++) {
                        var dataString = data[i].string;
                        var dataProp = data[i].prop;
                        this.versionSearchString = data[i].versionSearch || data[i].identity;
                        if (dataString) {
                            if (dataString.indexOf(data[i].subString) != -1)
                                return data[i].identity;
                        }
                        else if (dataProp)
                            return data[i].identity;
                    }
                },
                searchVersion: function (dataString) {
                    var index = dataString.indexOf(this.versionSearchString);
                    if (index == -1) return;
                    return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
                },
                dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
                dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

            };
            leads_BrowserDetect.init();

function leads_isOpera() {
    return leads_BrowserDetect.dataBrowser.identity == 'Opera';
}

function leads_isFunction( obj ) {
    return Object.prototype.toString.call(obj) === "[object Function]";
}


    var lc_g, lc_d, lc_j = 1, lc_a, lc_b = this, lc_f = !1, lc_h = "postMessage", lc_e = "addEventListener", lc_c, lc_i = lc_b[lc_h] && !leads_isOpera();
    receiveMessage  = lc_c = function (l, m, k){ 
        if (lc_i) {   
                if (l){ 
                        lc_a && lc_c(); 
                        lc_a = function (n) { 
                            if ((typeof m === "string" && n.origin !== m) || (leads_isFunction(m) && m(n.origin) === lc_f)) { 
                            return lc_f 
                            } 
                            l(n) 
                        } 
                 } 
                 if (lc_b[lc_e]) { 
                    lc_b[l ? lc_e : "removeEventListener"]("message", lc_a, lc_f) 
                    } 
                 else { 
                    lc_b[l ? "attachEvent" : "detachEvent"]("onmessage", lc_a) 
                    } 
                 } 
                 else { 
                    lc_g && clearInterval(lc_g); 
                    lc_g = null; 
                    if (l) { 
                        k = typeof m === "number" ? m : typeof k === "number" ? k : 100; 
                        lc_g = setInterval(function () { 
                            var o = document.location.hash, n = /^#?\lc_d+&/; 
                            if (o !== lc_d && n.test(o)) { 
                                lc_d = o; 
                                l({ data: o.replace(n, "") }) 
                             } 
                     }, k) } } } 

//end


var leads_sitePath = 'http://leads.bursadereclama.ro';

//receiving message
receiveMessage(function (e) {
    var args = e.data.split(',');
    if (args[0] == 'close_leads') {
        if (args[1] == undefined) args[1] = '';
        if (args[2] == undefined) args[2] = '';
        leads_HideLoadCollectorOrder(args[1], args[2]);
    }
}, leads_sitePath);

function LeadCollector(idDiv, idLink) {
    leads_AddStyles();
    if (idDiv == undefined)
        idDiv = '';
    if (idLink == undefined)
        idLink = '';

    leads_AddElements(idDiv, idLink);

    return false;
}

function leads_AddStyles() {
    var cssStyles = document.getElementById('LeadCollectorStylesLink');
    if (cssStyles == null) {
        cssStyles = document.createElement('link');
        cssStyles.setAttribute('id', 'LeadCollectorStylesLink');
        cssStyles.rel = 'stylesheet';
        cssStyles.type = 'text/css';
        cssStyles.href = leads_sitePath + '/Content/css/Site.css';
        (document.getElementsByTagName('head')[0] ||
        document.getElementsByTagName('body')[0]).appendChild(cssStyles);
    }
}

function leads_AddOverlay() {
    var overlayDiv = document.createElement('div');
    overlayDiv.setAttribute('id', 'LeadCollectorOverlaySquare');
    overlayDiv.className = 'overlaySquare';
    if (!leads_checkIE6())
        overlayDiv.style.display = 'block';
    else {
        overlayDiv.style.position = 'absolute';
        overlayDiv.style.width = document.body.clientWidth;
        overlayDiv.style.height = document.body.clientWidth;
    }
    document.body.appendChild(overlayDiv);
}

function leads_checkIE6() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
        var ieversion = new Number(RegExp.$j1) // capture x.x portion and store as a number
        if (ieversion == 6) {
            return true;
        }
        return false;
    }
    return false;
}

function leads_RemoveOverlay() {
    var overlayDiv = document.getElementById('LeadCollectorOverlaySquare');
    if (overlayDiv != null) {
        document.body.removeChild(overlayDiv);
    }
}

function leads_HideLoadCollectorOrder(idDiv, idLink) {

    if (idDiv == undefined)
        idDiv = '';
    if (idLink == undefined)
        idLink = '';

    var iframe = document.getElementById('LeadCollectorIframe' + idDiv + idLink);
    if (iframe != null) {
        iframe.style.display = 'none';
        iframe.setAttribute('src', '');
        var imgCloseDiv = document.getElementById('LeadCollectorCloseImgDiv' + idDiv + idLink);
        imgCloseDiv.style.display = 'none';
        leads_RemoveOverlay();
        var wrapperContainer = document.getElementById('Widget_LeadCollectorWrapper' + idDiv + idLink);
        document.body.removeChild(wrapperContainer);
    }
}

function leads_GetWindowWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && document.body.clientWidth) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function leads_GetWindowHeight() {
    var myHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function leads_GetScrollWidth() {
    var scrOfX = 0;
    if (typeof (window.pageXOffset) == 'number') {
        //Netscape compliant
        scrOfX = window.pageXOffset;
    } else if (document.body && document.body.scrollLeft) {
        //DOM compliant
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && document.documentElement.scrollLeft) {
        //IE6 standards compliant mode
        scrOfX = document.documentElement.scrollLeft;
    }
    return scrOfX;
}

function leads_GetScrollHeight() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant           
        scrOfY = window.pageYOffset;
    } else if (document.body && document.body.scrollTop) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

function leads_AddElements(idDiv, idLink) {

    leads_AddOverlay();

    var container;
    if (idDiv == '')
        container = document.getElementById('Widget_LeadCollector');
    else
        container = document.getElementById(idDiv);

    var wrapperContainer = document.getElementById('Widget_LeadCollectorWrapper' + idDiv + idLink);
    if (wrapperContainer == null)
        wrapperContainer = document.createElement('div');
    wrapperContainer.setAttribute('id', 'Widget_LeadCollectorWrapper' + idDiv + idLink);
    document.body.appendChild(wrapperContainer);

    wrapperContainer.style.position = 'absolute';
    wrapperContainer.style.display = 'block';
    wrapperContainer.style.margin = '0';
    wrapperContainer.style.padding = '0';
    wrapperContainer.style.zIndex = '99999';
    wrapperContainer.style.overflow = 'hidden';
    wrapperContainer.style.borderColor = '#FFFFFF';

    wrapperContainer.style.width = '370px';
    wrapperContainer.style.height = '540px';

    var offsetRec = container;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetRec) {
        offsetLeft += offsetRec.offsetLeft;
        offsetTop += offsetRec.offsetTop;
        offsetRec = offsetRec.offsetParent;
    }

    wrapperContainer.style.left = Math.max(0, Math.min(offsetLeft, leads_GetScrollWidth() + leads_GetWindowWidth() - 360)) + 'px';
    wrapperContainer.style.top = Math.max(0, Math.min(offsetTop, leads_GetScrollHeight() + leads_GetWindowHeight() - 490)) + 'px';

    var clearDiv = document.getElementById('LeadCollectorClearDiv' + idDiv + idLink);
    if (clearDiv == null) {
        clearDiv = document.createElement('div');
        wrapperContainer.appendChild(clearDiv);
    }
    clearDiv.setAttribute('id', 'LeadCollectorClearDiv' + idDiv + idLink);
    clearDiv.style.clear = 'both';
    clearDiv.style.fontSize = 0;

    var imgCloseDiv = document.getElementById('LeadCollectorCloseImgDiv' + idDiv + idLink);
    if (imgCloseDiv == null)
        imgCloseDiv = document.createElement('div');
    imgCloseDiv.setAttribute('id', 'LeadCollectorCloseImgDiv' + idDiv + idLink);
    var imgClose = document.getElementById('LeadCollectorCloseImg' + idDiv + idLink);
    if (imgClose == null)
        imgClose = document.createElement('img');
    imgCloseDiv.style.display = 'inline';
    imgClose.style.width = '18px';
    imgClose.style.height = '18px';
    imgClose.style.position = 'absolute';
    imgClose.style.textAlign = 'right';
    imgClose.style.margin = '10px 0px 0pt 314px';
    imgClose.style.backgroundColor = '#FAFCFD';
    imgClose.style.border = '0';
    imgClose.style.padding = '0';
    imgClose.setAttribute('id', 'LeadCollectorCloseImg' + idDiv + idLink);
    imgClose.className = "imgclose";
    imgClose.setAttribute('src', leads_sitePath + '/Content/Images/alert-close.png');
    imgClose.setAttribute('title', 'Close');
    imgClose.setAttribute('alt', 'Close');
    imgClose.onclick = new Function('leads_HideLoadCollectorOrder(' + "'" + idDiv + "'" + ',' + "'" + idLink + "'" + ')');
    imgCloseDiv.appendChild(imgClose);
    wrapperContainer.appendChild(imgCloseDiv);

    var iframe = document.getElementById('LeadCollectorIframe' + idDiv + idLink);
    if (iframe == null)
        iframe = document.createElement('iframe');
    iframe.setAttribute('id', 'LeadCollectorIframe' + idDiv + idLink);
    iframe.allowTransparency = 'true';
    iframe.frameBorder = 0;
    iframe.setAttribute('src', leads_sitePath + '/Form/Create?idsite=undefined&referral=' + escape(location.href) + '&idDiv=' + idDiv + '&idLink=' + idLink);
    iframe.className = "leadiframe";
    wrapperContainer.appendChild(iframe);
}
