﻿function( opera ){
	addEventListener( opera && opera.version() >= 9 ? 'DOMContentLoaded' : 'load', function(){
	
	
		if( navigator.userAgent.indexOf('MSIE')<0 &&
			document.body &&
			document.body.firstChild &&
			document.body.firstChild.nodeType == Node.TEXT_NODE &&
			escape(document.body.firstChild.nodeValue).match(/%1F%(u[\dabcdef]{4}|[\dabcdef]{2})%08/i) ){
			
			
			var xmlhttp = new XMLHttpRequest();
			xmlhttp.open('GET',location,true);
			xmlhttp.setRequestHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; "+navigator.userLanguage+")");
			xmlhttp.setRequestHeader("Accept-Encoding","identity");
			
			xmlhttp.onreadystatechange = function(){
				if( this.readyState == 4 && (this.status == 200 || this.status == 304 ) ){
				
					if( this.responseXML && this.responseXML.documentElement ){
					
						document.removeChild(document.documentElement);
						document.appendChild(document.importNode(this.responseXML.documentElement,true));
						
					}
					else{
						//GRRRRRR !!! this seems to happen always ! :(
						//ok.. convert markup in document... but allas ! It's tag soup !
						//one can't use DOMParser or DOM LS, so play with outer/innerHTML
					
					//	too slow like this !
					//	var markup = this.responseText.replace(/<head([^>]*)>/,'<head$1><base href="'+location+'"/>');
					//	location.href = "data:text/html,"+encodeURIComponent(markup);
					
						var html = document.getElementsByTagName('html')[0];
						var head = document.getElementsByTagName('head')[0];
						var body = document.getElementsByTagName('body')[0];
						if( !head ){
							head = document.createElement("head");
							html.insertBefore(head,html.firstChild);
						}else while( head.firstChild )
							head.removeChild( head.firstChild );
							
						function indexOf(str,regexp){
							var m = str.match(regexp);
							return m ? str.indexOf(m[0]) : -1;
						};
												
						var hidx = indexOf(this.responseText, /<\s*head\b/i )
						var bidx = indexOf(this.responseText, /<\s*body\b/i )
						var fidx = indexOf(this.responseText, /<\s*frame(set)?\b/i )
						
						if(hidx==-1) hidx = this.responseText.length;
						if(bidx==-1) bidx = this.responseText.length;
						if(fidx==-1) fidx = this.responseText.length;
						
						var hlim = bidx>fidx?fidx:bidx;
						if(hlim==-1) hlim = this.responseText.length;
						var blim = bidx>fidx?bidx:fidx;
						if(blim==-1) blim = this.responseText.length;
						
						head.outerHTML = this.responseText.substring(hidx,hlim);
						body.innerHTML = this.responseText.substring(hlim,blim);//outerHTML is weird !
						
						//if( opera && opera.version()<9 ){
							//fix for styles and outerHTML - it doesn't hurt if it runs in 9
							var matches = this.responseText.match(/<style[^>]*>([^<]|<[^\/])*<\/style/ig);
							var styles = document.getElementsByTagName('style');
							for(var k=0, m, s; matches && k < matches.length && (s = styles[k]); k++){
								m = matches[k];
								var i = m.indexOf(">");
								var e = m.indexOf("</");
								s.appendChild( document.createTextNode( m.substring(i+1,e-1) ) );
								
							}
								
							var hlinks = document.getElementsByTagName('link');
							for(var k=0, l; l = hlinks[k]; k+=2){
								l.parentNode.replaceChild(l.cloneNode(true),l)
							}
							
							//and now js
							
							var matches = this.responseText.match(
								(/<script[^>]*>([^<]|<[^\/]|<\/[^s]|<\/s[^c]|<\/sc[^r]|<\/scr[^i]|<\/scri[^p]|<\/scrip[^t])*<\/script/ig));
							var scripts = document.getElementsByTagName('script');
							for(var k=0, m, s; matches && k < matches.length && (s = scripts[k]); k++){
								m = matches[k];
								var n_s = s.cloneNode(true);
								if(m && (!n_s.firstChild || !n_s.firstChild.nodeValue) ){
								
									var i = m.indexOf(">");
									var e = m.indexOf(m.match(/<\/script/i)[0]);
									n_s.appendChild( document.createTextNode( m.substring(i+1,e-1) ) );
								}
								s.parentNode.replaceChild(n_s,s);
							}
						//}
					}
				}
			};
			xmlhttp.send('');
			
			//warn the user !! because this script might not work well with pages generated
			//with a POST request (mind the xmlhttp GET), and it makes browsing slower !
			if( opera.version() >= 9 )
				alert("This webserver is misconfigured.\nPlease right click this page, select "+
				"Edit site preferences, go to the Network tab and on the Browser Identification "+
				"select box, choose 'Identify as Internet Explorer'. Then refresh this page.\n\nThis "+
				"misconfiguration will slow down your browsing.");
			else
				alert("This webserver is misconfigured.\nPlease press F12 and choose 'Identify "+
				"as Internet Explorer' from the dropdown menu. Then refresh this page.\n\nThis "+
				"misconfiguration will slow down your browsing.");
		}
			
	},false);
	
})( window.opera );
