// ==UserScript==
// @name Fix unstyled fullscreen
// @author Scipio 
// @namespace http://home.wanadoo.nl/sipke.reina/opera/index.html 
// @version 1.0
// @description  Fixes pages that appear to be unstyled in fullscreen
//			mode. This script is deprecated since Opera 9.
// @ujs:category browser: fixes
// @ujs:published 2005-06-10 13:35
// @ujs:modified 2005-10-26 10:03
// @ujs:documentation http://userjs.org/scripts/browser/fixes/fix-unstyled-fullscreen 
// @ujs:download http://userjs.org/scripts/download/browser/fixes/fix-unstyled-fullscreen.js
// ==/UserScript==


/* 
 * This script is granted to the public domain.
 */

document.addEventListener(
	'load',
	function () {

		// scs collects all <style>s and <link rel="(foo )?stylesheet( bar)?"> with media="...,screen,..."
		// as soon as we get one with media="...,all,..." or media="...,projection,..." we know we can stop immediately
		var scs = [], oGood = /projection|all/, hasMedia = /^<[^<]+\smedia=[\"\']?\w/, oEl, oMed, oTgs = ['style','link'];

		for ( var n = 0, oTg; oTg = oTgs[n]; n++ ) {
			//check style tags first, as they are less likely to have media attributes, and we can stop sooner
			oTg = document.getElementsByTagName(oTg);

			for ( var i = 0; oEl = oTg[i]; i++ ) {
				if ( !n || oEl.getAttribute('rel').match(/\bstylesheet\b/) ) {

					//bug - always "screen" if not specified; if ( oMed = oEl.getAttribute('media') ) {
					if ( oEl.outerHTML.match(hasMedia) ) {
						oMed = oEl.getAttribute('media');
						if ( oMed.match(oGood) ) { return; } //no need to waste time checking the rest
						if ( oMed.indexOf('screen') > -1 ) { scs[scs.length] = oEl; }
					} else { return; } //no need to waste time checking the rest

				}
			}
		}

		// apply media="screen" to Fullscreen mode, too (by adding "projection" 
		// to the value of the media attribute of style declarations written for screen media). 
		for ( var i = 0; oEl = scs[i]; i++ ) {
			oEl.setAttribute('media',oEl.getAttribute('media')+',projection');
		}
	},
	false
);