var http_request = false;
var isIE8 = window.XDomainRequest ? true : false;
var invocation = createCrossDomainRequest();

function createCrossDomainRequest() {
	var request;
	if (isIE8) {
		request = new window.XDomainRequest();
	} else {
	    if (window.XMLHttpRequest) { // Mozilla, Safari
		   request = new XMLHttpRequest();
		   if (request.overrideMimeType) {
			   request.overrideMimeType('text/xml');
		   }
		} else if (window.ActiveXObject) { // IE 6 and below
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	}
	return request;
}
 
function makeAJAXRequest(url) {
	if (invocation) {
		if(isIE8) {
			invocation.onload = outputResult;
			invocation.onerror = aerreur;
			invocation.open("GET", url, true);
			invocation.send();
		} else {
			invocation.open('GET', url, true);
			invocation.onreadystatechange = handler;
			invocation.send();
		}
	} else {
		alert('There was a problem with the invocation code (Err: 99)');
	}
}

function aerreur() {
	alert('Message derreur');
}
 
function handler(evtXHR) {
	if (invocation.readyState == 4) {
		if (invocation.status == 200) {
			outputResult();
		} else {
		   //alert('There was a problem with the request.(Code: ' + invocation.status + ')');
		}
	}
}
 
function outputResult() {
	//if (return_xml) {
	if(isIE8) {
		// XDomainRequest doesn't provide responseXml, so if you need it:
		alert(invocation.responseText);
		var dom = new ActiveXObject("Microsoft.XMLDOM");
		dom.async = false;
		dom.loadXML(invocation.responseText);
		eval('loadBanner' + '(dom)');
	} else {
		eval('loadBanner' + '(invocation.responseXML)');
	}
	//} else {
	//	eval(callback_function + '(invocation.responseText)');
	//}
}
	
function makeHttpRequest(url, callback_function, return_xml) {
   if (window.XMLHttpRequest) { // Mozilla, Safari, IE7
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }

   } else if (window.ActiveXObject) { // IE 6 and below
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
       }
   }

   if (!http_request) {
       alert('Unfortunatelly you browser doesn\'t support this feature.');
       return false;
   }
   http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
               } else {
                   eval(callback_function + '(http_request.responseText)');
               }
           } else {
               //alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}

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) {}
		}
	}
	return xmlHttp;
}

function loadAd(url) {
	var xmlHttp = GetXmlHttpObject( );

	if(xmlHttp == null) {
		window.alert("Votre navigateur ne supporte pas AJAX.");
		return false;
	}

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			var dom = new ActiveXObject("Microsoft.XMLDOM");
			dom.async = false;
			dom.loadXML(xmlHttp.responseText);
			loadBanner(dom);
		}
	}
	
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function nextAd(lang) {
    var now = new Date();
    var url = '/template/ajax/banner.php?ts=' + now.getTime() + '&l=' + lang;
	if (!isIE8) {
		makeHttpRequest(url, 'loadBanner', true);
	} else {
		//makeAJAXRequest(url);
		loadAd(url);
	}
}

function loadBanner(xml) {
    var html_content = xml.getElementsByTagName('content').item(0).firstChild.nodeValue;
    var reload_after = xml.getElementsByTagName('reload').item(0).firstChild.nodeValue;
    var lang = xml.getElementsByTagName('lang').item(0).firstChild.nodeValue;
	document.getElementById('pub').innerHTML = html_content;
	el = Ext.get('pub');
	//el.slideIn('r', {
	//    duration: .5
	//});
	el.show();

    try {
        clearTimeout(to);
    } catch (e) {}

    to = setTimeout('nextAd("'+lang+'")', parseInt(reload_after));
}

window.onload = nextAd;
