/*
LP @ 26-10-2009

REQUIRES: jQuery

Objective:
  Load any kind of zahir advertisement at bottom.
  Reload any kind of zahir ads, granting a minimal time for them to be visible.
	Call for adicional netscope counts on reload.

WARNING:
	Limited to reload only 1 ad at a time!
*/
var mrecMainID = mrecMainID || 1507; 

//to be sure!
//var zahirdir = zahirdir || '';
//var zahirscreen = zahirscreen || '';
//var zahirand = zahirand || '';

//get and show an ad
function showAd(id,local)
{
    if(typeof local == "undefined") 
    {
        local = 1;
    }
    
    if(jQuery("#zahirad"+id+"html").size()<=0 && local == 1)
    {
        return false;
    }

    var zahirand = Math.random().toString();
    var zahirscreen = jQuery(window).width();
    var zahirurl = "http://zahirx.aeiou.pt/pl/serve.pl?S="+id+"&X=nio&F=javascript&D="+zahirdir+"&resol="+zahirscreen+"&ord="+zahirand;

    if ($.browser.msie) {
        document.write('<script type="text/javascript" src="'+zahirurl+'"></script>');
    } else {
        document.write('<script defer="defer" type="text/javascript" src="'+zahirurl+'"></script>');
    }    

    if (local == 1)
    {
        jQuery("#zahirad"+id).ready(function() {
            jQuery("#zahirad"+id).children().not(jQuery("script")).each(function() {
                jQuery("#zahirad"+id+"html").append(jQuery(this));
            });
            jQuery("#zahirad"+id).attr("id","zahirad"+id+"old");
            jQuery("#zahirad"+id+"html").attr("id", "zahirad"+id);
        });
    }
}

function showAdd(){
    try{
	    reloadAd();
	    countWebscope('Comentários','Comentários','Comentario Expandido','/Comentario');
	    if(window.console)console.log('Oppened a comment');
    }
    
    catch(e){
	    if(console && console.log)console.log(e.message);
    }
}


function showMrec(id){
    
    if(typeof(id)=='undefined'){
	var id=mrecMainID;
    }
    
    
    if(typeof(m3_u)=='undefined'){
	var m3_u = (location.protocol=='https:'?'https://pub.sapo.pt/ajs.php':'http://pub.sapo.pt/ajs.php');
    }
    
    if(typeof(m3_r)=='undefined'){
	var m3_r = Math.floor(Math.random()*99999999999);
    }
    
    if (!document.MAX_used) document.MAX_used = ',';
    
    document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
    document.write ("?zoneid="+id);
    document.write ('&cb=' + m3_r);
    if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
    document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
    document.write ("&loc=" + escape(window.location));
    if (document.referrer) document.write ("&referer=" + escape(document.referrer));
    if (document.context) document.write ("&context=" + escape(document.context));
    if (document.mmm_fo) document.write ("&;mmm_fo=1");
    document.write ("'><\/scr"+"ipt>");

}

//reload ad with netscope integration.
function reloadAd(id, param)
{
    param = param || {};

    var m3_r = Math.floor(Math.random()*99999999999);
    var mrec_html_element = param.html_element || '#mrec';
    $(mrec_html_element).empty().html('<iframe width="300" height="250" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" src="'+baseurl+'reloadmrec.html?c='+m3_r+'"></iframe>');
    if(window.console)console.log('Reloaded ad');
}

function countWebscope(section,subsection,content,analytics){
	if(typeof(wreport_ok)!='undefined'){
		if(wreport_ok == 1){
			if ((!section) || (section == null)) section = WRP_SECTION;
			if ((!subsection) || (subsection == null)) subsection = WRP_SUBSECTION;
			if ((!content) || (content == null)) content = WRP_CONTENT;
	
			counter = new wreport_counter(section, subsection, WRP_ID, WRP_ACC, WRP_CHANNEL, WRP_SECTION_GRP, WRP_SUBSECTION_GRP);
			counter.add_content(content);
			if(window.console)console.log('Netscope');
			counter.count();
		}
	}
	
	if(_gat._getTrackerByName()){
	    if(window.console)console.log('Analytics');
	    _gat._getTrackerByName()._trackPageview(location.href+analytics);
	}
}

function chamadaAoNetscope(){
    if(window.console)console.log('Clicked a flash gallery');
    reloadAd();
    countWebscope(null,null,WRP_CONTENT,'/Flash');
}

