/*
LP @ 21-05-2009
REQUIRES jQuery!

Objective:
  load any kind of zahir advertisement at bottom.
  reload normal MREC ads (all ads are considered normal until they do document.write somewhere)
	call for adicional netscope counts (requires reloaded ad)
*/

var mrecMainID = mrecMainID || 1522,
    htmlreload="reloadmrec.html";

//get and show an ad
function showAd(id,local)
{
	console.log(id);
    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);
        });
    }
	if(window.console)console.log('showAd');
}

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>");
	if(window.console)console.log('Loaded ad');
}

//reload ad with netscope integration.
function reloadAd()
{
    var m3_r = Math.floor(Math.random()*99999999999);
    $('#mrec').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);
			counter.count();
			if(window.console)console.log('Netscope');	
		}
	}
	
	if(_gat._getTrackerByName()){
		_gat._getTrackerByName()._trackPageview(location.href+analytics);
		if(window.console)console.log('Analytics');	
	}
}

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