/*
To utilize add this to your page somewhere:
        <script language="javascript" src="http://www.stam.com/beforeYouGo/camilyo_byg.js"></script>
        <script language="javascript">
            cambyg_init('<YOUR-CAMILYO-BEFORE-YOU-GO-PAGE-URL>');
        </script>
*/
var cambyg_ignoreMouseLeave = false;



function cambyg_getCookie(cname) {
    var name = cname + "=";
    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);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

function cambyg_setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; ";// + expires;
}

function cambyg_init(byg_page_url, type, timeToStart) {
    //document.addEventListener("DOMContentLoaded", function(event) {
    jQuery('document').ready(function () {

        if (byg_page_url && byg_page_url != '') {
            if (type == 'wye') {
                camwye_onready(byg_page_url, timeToStart?timeToStart:1);
                addMessageEvent('wye');
            } else {
                cambyg_onready(byg_page_url);
                addMessageEvent('byg');
            }
            return;
        }

        jQuery.ajax({
            type: 'GET',
            url: decodeURIComponent(location.pathname) + "?__get_byg_url__=true",
            async: true,
            success: function (res) {
                if (res.trim()) {
//                    if (res.trim() == 'isLeadBooster') {
//                        $('document').ready(function(){
//                            setTimeout(function(){
//                                var height = $('.MainColumns_wp_outer').height();
//                                parent.postMessage('height:'+height, '*');
//                                var width = $('.MainColumns_wp_outer').width();
//                                parent.postMessage('width:'+width, '*');
//                            }, 100);
//                            return;
//                        });
//
//                    }
                    res = JSON.parse(res);
                    var bygUrl = res.bygUrl.trim();
                    var wyeUrl = res.wyeUrl.trim();
                    if (bygUrl && bygUrl != '' && res.platform == "3") {
                        
                        cambyg_onready(bygUrl);
                        addMessageEvent('byg');
                    }
                    if (wyeUrl && wyeUrl != '') {

                        camwye_onready(wyeUrl, res.wyeDelayToStart, res.platform);
                        addMessageEvent('wye');
                    }
                    
                }
            }
        });
    });
}
function addMessageEvent(type) {
    jQuery(window).on('message', function (e) {

        var data = e.originalEvent.data;

        if (data.indexOf('height') == 0) {
            var heightStr = '';
            if (data.indexOf('height:') == 0) {
                heightStr = data.replace('height:', '');
            } else {
                var d = data.replace('height_', '').split(':');
                if (d[0] != type ) {
                    return;
                }
                heightStr = d[d.length - 1];
            }
            if (heightStr < jQuery(window).height()) {
                jQuery('#camilyo_' + type + '_iframe_div').css('height', heightStr + 'px');
            }
            return;
        }

        if (data.indexOf('width') == 0) {
            var widthStr = '';
            if (data.indexOf('width:') == 0) {
                widthStr = data.replace('width:', '');
            } else {
                var d = data.replace('width_', '').split(':');
                if (d[0] != type) {
                    return;
                }
                widthStr = d[d.length - 1];
            }
            if (widthStr < jQuery(window).width()) {
                jQuery('#camilyo_' + type + '_iframe_div').css('width', widthStr + 'px');
            }
            return;
        }

        if (data != 'close_camilyo_byg') {
            return;
        }

        var prevDiv = document.querySelector('#camilyo_' + type + '_bg');

        if (prevDiv) {
            prevDiv.style.visibility = 'hidden';
            prevDiv.style.zIndex = -10000;
        }
    });

}
function cambyg_onready(byg_page_url) {

    var newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'camilyo_byg_bg');
    newDiv.setAttribute('style', 'visibility: hidden;z-index:-10000;height: 100%;width: 100%;position: fixed;top: 0;left: 0;');
    newDiv.innerHTML =
        '<div style="height: 100%;width: 100%;position: absolute;top: 0;left: 0;background-color: #000;opacity: 0.7;"></div>' +
        '<div style="height: 100%;overflow: hidden;width: 100%;display: table;position: absolute;top: 0;left: 0;">' +
            '<div style="position: absolute;top: 50%;width: 100%;text-align: center;display: table-cell;vertical-align: middle;position: static;">' +
                '<div id="camilyo_byg_iframe_div" style="opacity: 1; position: relative;top: -50%;text-align: left;width: 1200px;height: 1000px;margin-left: auto;margin-right: auto;">' +
                    '<iframe style="width:100%;height: 100%; border:none; box-sizing: content-box;" src="' + byg_page_url + '"></iframe>' +
                '</div>' +
            '</div>' +
        '</div>';
    document.querySelector('body').appendChild(newDiv);

    jQuery('#camilyo_byg_bg').click(function () {
        var prevDiv = document.querySelector('#camilyo_byg_bg');

        if (prevDiv) {
            prevDiv.style.visibility = 'hidden';
            prevDiv.style.zIndex = -10000;
        }
    });

    //window.addEventListener("message", cambyg_receiveMessage, false);
   
    //document.addEventListener("mouseenter", function (event) {
    jQuery('body').mouseenter(function (event) {
        cambyg_ignoreMouseLeave = true;
        setTimeout(function () {
            cambyg_ignoreMouseLeave = false;
        }, 50);
    });



    //document.querySelector('body').addEventListener("mouseleave", function (event) {
    jQuery('body').mouseleave(function (event) {


        if (jQuery('#camilyo_wye_bg').css('visibility') == 'visible') {
            return;
        }
        if (cambyg_ignoreMouseLeave) {
            return;
        }

        var prevDiv = document.querySelector('#camilyo_byg_bg');

        var c1 = cambyg_getCookie('c1');

        if (c1 == 'yes') {
            return;
        }

        if (event.clientY > 30 &&
            event.clientY < window.innerHeight - 30 &&
            event.clientX > 30 &&
            event.clientX < window.innerWidth - 30) {
            return;
        }

        if (prevDiv) {
            prevDiv.style.visibility = 'visible';
            prevDiv.style.zIndex = 300000;

            cambyg_setCookie('c1', 'yes', 10);
        }
        return;
    });
}



function camwye_onready(wye_page_url, wyeDelayToStart,platform) {

    var newDiv = document.createElement('div');
    newDiv.setAttribute('id', 'camilyo_wye_bg');
    newDiv.setAttribute('style', 'visibility: hidden;z-index:-10000;height: 100%;width: 100%;position: fixed;top: 0;left: 0;');

    var whStyles = platform == "3" ? 'width:1200px; height:1000px;' : 'width:80%; height:70%;';
    if (platform == "4") {
        whStyles += "padding-right:30px;padding-left:30px;padding-top:30px;padding-bottom:30px;box-sizeing:border-box;";
    }
    newDiv.innerHTML =
        '<div style="height: 100%;width: 100%;position: absolute;top: 0;left: 0;background-color: #000;opacity: 0.7;"></div>' +
        '<div style="height: 100%;overflow: hidden;width: 100%;display: table;position: absolute;top: 0;left: 0;">' +
            '<div style="position: absolute;top: 50%;width: 100%;text-align: center;display: table-cell;vertical-align: middle;position: static;">' +
                '<div id="camilyo_wye_iframe_div" style="opacity: 1; position: relative;top: -50%;text-align: left;'+whStyles+'margin-left: auto;margin-right: auto;">' +
                    '<iframe scrolling="no" style="width:100%;height: 100%;box-sizing: content-box;border:none" src="' + wye_page_url + '"></iframe>' +
                '</div>' +
            '</div>' +
        '</div>';
    document.querySelector('body').appendChild(newDiv);

    jQuery('#camilyo_wye_bg').click(function () {
        var prevDiv = jQuery('#camilyo_wye_bg');
        if (prevDiv.length>0) {
            prevDiv.css('visibility', 'hidden').css('z-index',-10000);
        }
    });
    setTimeout(function() {
        var prevDiv = jQuery('#camilyo_wye_bg');

        var c1 = cambyg_getCookie('wye1');

        if (c1 == 'yes') {
            return;
        }

        if (prevDiv.length>0) {
            prevDiv.css('visibility', 'visible').css('z-index', 300000);
            cambyg_setCookie('wye1', 'yes', 10);
        }
        return;

    }, wyeDelayToStart ? parseFloat(wyeDelayToStart) * 1000 : 1000);
  
}
