var URL_UPLOAD_ROOT = 'http://www.netsabe.com.br/uploads/'

var  BANNER_TARGETS = {
    SBT:{
        "target":'#SBT',
        "width":730,
        "height":90
    },
    HBT:{
        "target":"#HBT",
        "width":220,
        "height":90
    },
    HBD:{
        "target":"#HBD",
        "width":300,
        "height":70
    },
    BXB:{
        "target":"#BXB",
        "width":300,
        "height":250
    },
    HB1:{
        "target":"#HB1",
        "width":300,
        "height":70
    },
    HB2:{
        "target":"#HB2",
        "width":300,
        "height":70
    },
    PU:{
        "target":"#PU",
        "width":200,
        "height":200,
        "left":400,
        "top":350,
        "timeout":10
    }
};

var fecharpopup = function(nomeCookie){
    $.cookie(nomeCookie,1,{
        expires:30,
        path:"/"
    });
    $("#PU").css("display","none");
}


var advertise = {
   
    banner:{},
    properties:{
    },
    flashId: null,
    
    init:function(current_banner){
        if(typeof(current_banner) == 'object'){
            this.banner = {}, this.flashId = null;
            this.properties = {
                width:null,
                height:null,
                target:null,
                target_width:null,
                target_height:null,
                target_left:null,
                target_top:null,
                timeout:null,
                innerHTML:null
            };
            this.banner = current_banner;
            this.getTargetLocalization();
            this.defineAdvertise();
            
        }
    },
    
    defineAdvertise:function(){
        if(this.properties && typeof(this.properties.target) != 'undefined'){
            var nomeCookie = "bitHidePopup"+this.banner.pageTag;
            var localization = $(this.properties.target);
            if(localization && localization.length > 0){
                var properties = this.properties;


           
                localization.css({
                    'width':properties.target_width+"px",
                    'height':properties.target_height+"px",                    
                    'overflow':'hidden'
                });
                
                
                if(properties.target == "#PU"){
                    localization.css({
                        "left":properties.target_left+ "px",
                        "top":properties.target_top+ "px",
                        "width":properties.width+"px",
                        "height": properties.height+"px"
                    });
                    var timeout = properties.timeout;
                    setTimeout(function(){
                        fecharpopup(nomeCookie);
                    },timeout*1000);
                    if($.cookie(nomeCookie) == 1){
                        localization.css("display","none");
                    }
                }else{
                    
                    localization.mouseover(function(){
                        $(this).css({
                            'overflow':'none',
                            'height':properties.height
                        });
                    }).mouseout(function(){
                        $(this).css({
                            'overflow':'hidden',
                            'height':properties.target_height
                        });
                    });
                }
                

                var content = this.getAdvContent();
                var innerHTML = advertise.banner.innerHTML;

                if(typeof(innerHTML) != "undefined" && innerHTML != "" && innerHTML!=null){
                    jQuery(function(){
                        var content_dyn = '<scr'+'ipt type="text/javascript" src="'+innerHTML+'"></script>';
                        localization.writeCapture().html(content_dyn);
                    });
                }else{
                    localization.html(content);
                    localization.find("object embed").css('visibility', 'visible');
                    if(this.flashId!=null && typeof(this.flashId) != 'undefined'){
                        if(swfobject && typeof(swfobject) == 'object'){
                            swfobject.registerObject(this.flashId, "9.0.115", "expressInstall.swf");
                        }
                    }
                }
            }
        }
    },
    getAdvContent:function(){
        var file = URL_UPLOAD_ROOT+this.banner.path;
        var link = this.getLink();
        var width = (this.banner.width) ? this.banner.width : this.properties.width;
        var height = (this.banner.height) ? this.banner.height : this.properties.height;
        var id = this.banner.id;
        var titulo = this.banner.description;
        var posX = this.banner.posX;
        var posY = this.banner.posY;
        
        var localization = this.banner.localization;
        var page = this.banner.pageTag;
        var title = this.banner.title;
        
        this.flashId = null;
        var content =  "";
        if(/\.swf/.test(this.banner.path)){            
            this.flashId = "pub"+Math.floor(Math.random()*99999)+"id"+Math.floor(Math.random()*99999);
            content+= "<object id=\""+this.flashId+"\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+width+"\" height=\""+height+"\">";
            content+= "<param name=\"movie\" value=\""+file+"\" />";
            content+= "<param name=\"wmode\" value=\"transparent\" />";
            content+= "<param name=\"allowScriptAccess\" value=\"always\" />";
            content+= "<param name=\"flashvars\" value=\"idBanner="+id+"&titulo="+titulo+"\" />";
            content+="<!--[if !IE]>-->";
            content+="<!--<![endif]-->";
            //            content+= "<embed src=\""+file+"\" type=\"application/x-shockwave-flash\" width=\""+width+"\" height=\""+height+"\" allowscriptaccess=\"always\" allowfullscreen=\"false\" wmode=\"transparent\" />";
            content+= "<embed src=\""+file+"\" width=\""+width+"\" height=\""+height+"\" flashvars=\"idBanner="+id+"&titulo="+titulo+"\" menu=\"false\" wmode=\"transparent\" allowscriptaccess=\"always\" quality=\"high\" bgcolor=\"#ffffff\" name=\""+this.flashId+"\" id=\""+this.flashId+"\" type=\"application/x-shockwave-flash\">";
            content+="</object>";                
        }else{
            content+= "<a href=\""+link+"\" target=\"_blank\"><img src=\""+file+"\" /></a>";
        }
        
        //console.log(localization +" - "+ description);
        
        if(typeof(_gaq) != 'undefined'){
            _gaq.push(['_trackEvent', 'Impressao', page, localization +" - "+ title]);
        }
        
        if(typeof(this.banner.innerHTML) != "undefined" && this.banner.innerHTML!=null){
            content = this.banner.innerHTML;
        }
        return content;
    },
    getTargetLocalization:function(){
        var obj = this.banner;
        var instance = this;
        var localization = obj.localization;
        if(obj){
            $.each(BANNER_TARGETS,function(objName,objProperties){
                if(objName == localization){
                    var width = obj.width;
                    var height = obj.height;
                    var left = typeof(obj.posX) == "undefined" ? objProperties.left  : obj.posX;
                    var top = typeof(obj.posY) == "undefined" ? objProperties.top  : obj.posY;
                    var timeout = typeof(obj.timeout) == "undefined" ? objProperties.timeout : obj.timeout;
                    
                    
                    if(width && width > 0){
                        instance.properties.width = width;
                    }else{
                        if(objProperties.width){
                            instance.properties.width = objProperties.width;
                        }
                    }
                    if(height && height > 0){
                        instance.properties.height=height;
                    }else{
                        instance.properties.height = objProperties.height;
                    }
                    
                    instance.properties.target = objProperties.target;
                    instance.properties.target_width = objProperties.width;
                    instance.properties.target_height = objProperties.height;
                    instance.properties.target_left = left;
                    instance.properties.target_top = top;
                    instance.properties.timeout = timeout;
                }
            });
        }
    },    
    getLink:function(){
        var link = $.trim(this.banner.link);
        if(link!=""){
            if(/\http\:\/\//.test(link)){
                return link;
            }else{
                if(/\@/.test(link)){
                    return "mailto:"+link;
                }
                return "http://"+link;
            }
        }
    }
};



