var maioraGlVars = {"web_path":"http:\/\/www.adsmaiora.it","logos_path":"_logos","thumbs_path":"_thumbs","ranks_path":"_ranks","network_web_path":"http:\/\/remote.adsmaiora.it","banners_path":"_banners","slots_path":"_slots"};
/*

ajaxCall(url, handler)
url: url loaded
handler: function called on response, with content passed as first parameter:
... handler(response) ...

*/

var xmlhttps = [];

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
  	// Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
  // Internet Explorer
    try {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
      	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
      	alert("Your browser does not support AJAX!");
      	return false;
      }
    }
  }
	return xmlHttp;
}

function ajaxCall2(method, url, params, handler) {

  //var params_query=obj2query(params);
  var params_query=params;
  if (method == "GET") url = url + "?" + params_query;
  
  var i = xmlhttps.length;
  xmlhttps[i] = GetXmlHttpObject();
  xmlhttps[i].onreadystatechange = function() {
    if (this.readyState==4 || this.readyState=="complete") {
      handler(this.responseText);
    }
  }
  
  xmlhttps[i].open(method,url,true);
  
  if (method == "GET") xmlhttps[i].send(null);
  else if (method == "POST") {
    xmlhttps[i].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttps[i].setRequestHeader("Content-length", params_query.length);
    xmlhttps[i].setRequestHeader("Connection", "close");
    xmlhttps[i].send(params_query);
  }

}

function ajaxCall(url, handler) {
  if(0) {
    $.ajax({
      url: url,
      context: this,
      success: handler
    });
    /*
    eventuali modifiche a method, data
    GET a POST
    form, files, contentType, etc...per per modi
    */
  } else {
    var i = xmlhttps.length;
    xmlhttps[i] = GetXmlHttpObject();
    xmlhttps[i].onreadystatechange = function() {
      if (this.readyState==4 || this.readyState=="complete") {
        handler(this.responseText);
      }
    }
    xmlhttps[i].open("GET",url,true);
    xmlhttps[i].send(null);
  }
}
var slots = [];

function maioraLoadSlot(slot_id, div_id) {

  if (div_id.substr(7) != slot_id) { alert(div_id.substr(7)); return }
  var div_id = 'amdiv_s' + slot_id;

  var i = slots.length;
  while (i--) if (slots[i] === slot_id) return;
  slots.push(slot_id);

  document.getElementById(div_id).innerHTML = '...';
  ajaxCall2("GET", maioraGlVars.network_web_path+'/'+maioraGlVars.slots_path+'/'+slot_id+'/slot.html', "", function (responseText) {
    document.getElementById(div_id).innerHTML = responseText;
    var scripts = document.getElementById(div_id).getElementsByTagName('script');
    for(var i = 0; i < scripts.length; i++) {
      eval(scripts[i].innerHTML);
    }
  });

}

function maioraClick(id_slot, id_banner, id_pubblicazione) {

  if(id_banner == null) id_banner = '';
  if(id_pubblicazione == null) id_pubblicazione = '';
  ajaxCall2("GET", maioraGlVars.network_web_path+'/click_count.php', 's='+id_slot+'&b='+id_banner+'&p='+id_pubblicazione, function (responseText) {if(responseText) alert(responseText)});
}

function maioraImpression(id_slot, id_banner, id_pubblicazione) {
  
  if(id_banner == null) id_banner = '';
  if(id_pubblicazione == null) id_pubblicazione = '';
  ajaxCall2("GET", maioraGlVars.network_web_path+'/impression_count.php', 's='+id_slot+'&b='+id_banner+'&p='+id_pubblicazione, function (responseText) {if(responseText) alert(responseText)});
}

