/**
 * Script de exibição de banner e campanhas publicitárias para 
 * o sistema de guias Guia SJC.
 * 
 * @author Fernando S. Rodrigues
 * @date Qua, 3 de Ago de 2011
 * 
 * @package guiads_banner
 * 
 * <head>
 *  <script type="text/javascript" 
 *          src="http://www2.guiasjc.com.br/guiads/guiads.js?rev=20110703:1.0.0"></script>
 * </head>
 * 
 * <script type="text/javascript">
 *      guiads_fill_slot("Ex_HomePage_Banner_Top_728x90")
 * </script>
 *
 */ 
(function(window, undefined){
    var win = window,
    
        doc = document,
        
        regmetrics = /(px|pt|em|\%)/,
                        
        s = {            
            
            slot: null,
            
            publisher: null,
            
            width: '0px',
            
            height: '0px',
            
            src: "",
            
            query_string: null,
            
            protocol: null,
            
            sub: null,
            
            domain: null,
            
            path: null,
            
            service: null
        };      
        
    win._guiads_protocol = "http",

    win._guiads_sub_domain = "www2",

    win._guiads_domain = window.location.host.replace(/www2\.|www\./,''),

    win._guiads_path = "wp-content/themes/guiachannels/guiads",

    win._guiads_service = "guiads.php";
        
    /**
     * Função de Carregamento do banner ads para o local específico 
     * 
     */
    function gfx(slot, width, height, publisher){
        var body = doc.getElementsByTagName('body');
        var scripts = body[0].getElementsByTagName('script');        
        var regex = new RegExp("/(" + slot + ")/");         
        var frame = "";
        var src = "";
        
        s.slot = encodeURIComponent(slot);
        s.width = regmetrics.test("" + width) ? width : (width + "px");
        s.height = regmetrics.test("" + height) ? height : (height + "px");
        s.publisher = publisher;
        
        // Protocolo HTTP | HTTPS        
        s.protocol = win._guiads_protocol;        
        
        // Sub Domínio
        s.sub = win._guiads_sub_domain;
                
        // Domínio
        s.domain = win._guiads_domain;
        
        // Path
        s.path = win._guiads_path;
        
        // Serviço
        s.service = win._guiads_service;
        
        // Query String
        s.query_string = 'slot='  + s.slot +
                         '&width=' + s.width +
                         '&height=' + s.height +
                         '&publisher=' + s.publisher;
                       
        // Endereço para o <iframe />
        s.src = s.protocol + '://' + 
                s.sub + '.' + 
                s.domain + '/' + 
                s.path + '/' + 
                s.service + '?' + 
                s.query_string;
        
        if(regex.test(scripts[(scripts.length - 1)].text) != null){
            var tmp = doc.createElement('div');
            
            frame += '<ins style="width: ' + s.width + '; ' +
                               'height: ' + s.height+ '; ' + '; ' +
                               'display: inline-table; position: relative; border: 0pt none;">';
                    
            frame += '<ins style="width: ' + s.width + '; ' + 
                                   'height: ' + s.height + '; ' + 
                                   'display: block; position: relative; border: 0pt none;">';

            frame += '<iframe id="guiads_banner_frame_' + s.slot + '" ' + 
                               'name="guiads_banner_frame_' + s.slot + '"' +
                               'src="' + s.src + '" ' +
                               'width="' + s.width + '" ' +
                               'scrolling="no" ' + 
                               'height="' + s.height + '" ' + 
                               'frameborder="0" ' + 
                               'marginwidth="0" ' + 
                               'marginheight="0" ' + 
                               'style="border: 0pt none; position: absolute; top: 0pt; left: 0pt;">';

            frame += '</iframe>';
            frame += '</ins>';
            frame += '</ins>';
                            
            tmp.innerHTML = frame;
            
            var p = scripts[(scripts.length - 1)].parentNode;
            
            if(p.nodeType == 1){                
                p.appendChild(tmp.childNodes[0]);
            }            
            
            delete tmp;
        }
        
        delete frame;
    }
        
    win.GuiadsFillSlot = win.guiads_fill_slot = gfx;
    
})(window);
