/* AD POPUP LIB - MIKAEL RAMIREZ v 1.1 */
(function() {

    /**************************************************************************************/
    /* EDIT ONLY THIS CODE BELOW                                                          */
    /**************************************************************************************/

    var promoOverlay = {
        mobile: true,
        img: 'http://www.youngleafs.com/pvoverlay/images/youngleafs-overlay.png',
        top: 30,
        width: 256,
        height: 189,
        buttons: [
            {
                url: 'http://mobile.youngleafs.com',
                width: 256,
                height: 189,
                css: {'left':'0', 'top':'0'}
            },
            { 
                close: true,
                width: '100',
                height: '40',
                url: null,
                css: {'right':'-10px', 'bottom':'-10px'}
            }
        ]
    };
    
    /**************************************************************************************/
    /* DO NOT EDIT BELOW THIS LINE                                                        *
    /**************************************************************************************/










    // preload the overlay image
    if (promoOverlay !== undefined) {
        var img = new Image();
        img.src = promoOverlay['img'];
    }

    // http://dean.edwards.name/weblog/2006/06/again/
    function domReady() {
          // quit if this function has already been called
          if (arguments.callee.done) return;

          // flag this function so we don't do the same thing twice
          arguments.callee.done = true;

          main();
    };

    /* for Mozilla */
    if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);

    // for Internet Explorer (using conditional comments)
    /*@cc_on @*/
    /*@if (@_win32)
        document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
        var script = document.getElementById("__ie_onload");
        script.onreadystatechange = function() {
            if (this.readyState == "complete") {
                domReady(); // call the onload handler
            }
        };
    /*@end @*/

    // for safari
    if (/WebKit/i.test(navigator.userAgent)) { // sniff
        var _timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) {
                clearInterval(_timer);
                domReady(); // call the onload handler
            }
        }, 10);
    }
    /* for other browsers */
    window.onload = domReady;
    /**************************************************************************************/

    var ie6 = false; 
    var underlayCSS = {'position':'fixed','zIndex':'1000','top':'0','left':'0','width':'100%','background':'#000','opacity':'.75','filter':'alpha(opacity=75)'};
    var underlayID = '_pvPromoUnderlay';
    var promobgID = '_pvPromoBG';
    var buttonBoxID = '_pvPromoButtons';
    var blankImage = 'pvoverlay/images/blank.gif';

    function extend() {
        function ext(dest, src) {
            for(var p in src) dest[p] = src[p];
        }
        if(arguments.length == 2)
            ext(arguments[0], arguments[1])
        else {
            var l = arguments.length, src = arguments[l-1], i = l-1;
            while(i--) { ext(arguments[i], src); }
        }
    }
    
    function createEmptyDiv(name, dontAppend) {
        var div = document.createElement('div');
        div.setAttribute('id', name);
        div.style.display = 'none';
        if (dontAppend == undefined && !dontAppend)
            document.body.appendChild(div)
        return div;
    }

    function setEmbeds(prop) {
        var embeds = document.getElementsByTagName('embed');
        for(var l=embeds.length,i=0;i<l;i++)
            embeds[i].style.visibility = prop;

        var objects = document.getElementsByTagName('object');
        for(var l=objects.length,i=0;i<l;i++)
            objects[i].style.visibility = prop;
    }

    function sizeUnderlay(el) {
        var docWidth = 0;
        var docHeight = 0;

         // most browsers in strict or quirks mode
        if (window.innerHeight)
            docHeight = Math.max(document.body.clientHeight, document.body.scrollHeight, document.body.clientHeight);
        // ie6/7 strict
        else if (document.documentElement && document.documentElement.clientWidth) {
            if (ie6) {
                /* this thing is hacky-hackey. + 20 seems to be the default ie body margin.
                it only matters in the case of the height. the width is caculated correctly. */
                docHeight = document.documentElement.scrollHeight + 20;
                docWidth = document.documentElement.scrollWidth;
                el.style.position = 'absolute';
            }
            else docHeight = document.body.clientHeight;
        }
        // ie6/7 quirks mode
        else {
            docHeight = document.body.scrollHeight + 20;
            docWidth = document.body.scrollWidth;
            el.style.position = 'absolute';
        }
        extend(el.style, {
            'width': docWidth ? docWidth + 'px' : '100%',
            'height': docHeight + 'px',
            'display':'block'
        });
    }

    function createPromoBG(p, css) {
        var d = createEmptyDiv(promobgID);
        extend(d.style, css);
        extend(d.style, {'backgroundImage': 'url(' + p.img + ')', 'backgroundRepeat': 'no-repeat', 'zIndex': '1002'});
        if (ie6) extend(d.style, { 'backgroundImage': 'none', 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + p.img + "', sizingMethod='image')" });
    }
    function createPromoButtons(p, css) {
        var d = createEmptyDiv(buttonBoxID, true);
        var html = '';    
        css.zIndex = '1003';
        extend(d.style, css);
        
        for (var i=0, l=p.buttons.length; i<l; i++) {
            var o = p.buttons[i];
            var cssb = 'width:' + o.width + 'px;height:' + o.height + ';position:absolute;';
            for (var prop in o.css)    cssb += prop + ':' + o.css[prop] + ';';
            var url = (typeof o.url == 'string' ? o.url : 'javascript:void(0)');
            if (o.close == true) url = "javascript:window.pvClosePopup()";
            html += '<a style="' + cssb + '" href="' + url +'">' +
            '<img style="border:none" src="' + blankImage + '" width="' + o.width + '" height="' + o.height + '" /></a>\n';
        }
        d.innerHTML = html;
        document.body.appendChild(d);
    }
    function createPromo(p) {
        var css = {
            'margin':'0', 'padding':'0',
            'marginLeft': '-' + p.width/2 + 'px', 'top': p.top + 'px',
            'width': p.width + 'px','height': p.height + 'px',
            'position': 'absolute',    'left': '50%', 'display':'block'
        }
        createPromoBG(p, css);
        createPromoButtons(p, css);
    }
    function isSupportedMobile() {
        var a = navigator.userAgent;
        // support phones with webkit here
        var agents = ['Android','T-Mobile G1','Opera Mini','Opera Mobi','iPod','iPhone','Android 1.0;','T-Mobile myTouch 3G','webOS',' Pre\/'];
        for (var l=agents.length, i=0;i<l;i++) {
            if (a.match(agents[i]) != null)
                return true;
        }
        return false;
    }
    
    window.pvClosePopup = function() {
        var rem = [underlayID, promobgID, buttonBoxID];
        for (var i=0, l=rem.length;i<rem.length;i++) {
            var o = document.getElementById(rem[i]);
            if (o) {
                o.style.display = 'none';
                o.parentNode.removeChild(o);
            }
        }
        setEmbeds('visible');
        window.scrollTo(0, 1);
    }

    function main() {
        if (promoOverlay !== undefined) {
            ie6 = typeof document.body.style.maxHeight === "undefined";
            // if it's a mobile only popup and we are not on a supported mobile device, hide
            if (promoOverlay['mobile'] !== undefined && promoOverlay['mobile'] && !isSupportedMobile())
                return;

            setEmbeds('hidden');
            var underlay = createEmptyDiv(underlayID);
            extend(underlay.style, underlayCSS);
            sizeUnderlay(underlay);
            createPromo(promoOverlay);
        }
    }

})();

