// ==UserScript==
// @name        QNAP Qdownload Patch
// @namespace   http://www.scss.com.au/family/andrew/opera/userjs/
// @version     1.0
// @date        2008-06-29
// @author      Andrew Gregory <andrew at scss dot com dot au>
// @description Patches horrible obsolete ActiveWidgets Grid control used by the
//              QNAP Qdownload application into something a bit more usable.
//              ** KNOWN ISSUES: Mouse clicks do not work on the grid list.
//                 Clicking on the list (to focus it), then using the up/down
//                 arrows on the keyboard is a workaround. **
// @include     *
// ==/UserScript==
// License:     http://creativecommons.org/licenses/by-nc-sa/2.0/
(function(opera){

if (location.pathname.indexOf("/cgi-bin/Qdownload/html/") != -1) {
  // NOTE: location of Grid files on QNAP:
  // http://qnap:8080/cgi-bin/Qdownload/html/runtime
  // /home/httpd/cgi-bin/Qdownload/html/runtime

  // copied from browser.js:
	// variables and utility functions
	if(!opera.postError)opera.postError=function(){}; // handling versions w/o error console
	// Storing function references
	var
	getElementsByTagName=Document.prototype.getElementsByTagName,
	createElement=Document.prototype.createElement,
	createTextNode=Document.prototype.createTextNode,
	insertBefore=Node.prototype.insertBefore,
	setAttribute=Element.prototype.setAttribute,
	appendChild=Node.prototype.appendChild,
	version=opera.version,
	call = Function.prototype.call;

  // copied from browser.js:
	function addCssToDocument(cssText, doc, mediaType){
		getElementsByTagName.call=addEventListener.call=createElement.call=createTextNode.call=insertBefore.call=setAttribute.call=appendChild.call=version.call=call;
		doc = doc||document;
		mediaType = mediaType||'';
		addCssToDocument.styleObj=addCssToDocument.styleObj||{};
		var styles = addCssToDocument.styleObj[mediaType];
		if(!styles){
			var head = getElementsByTagName.call(doc, "head")[0];
			if( !head ){
				var docEl = getElementsByTagName.call(doc, "html")[0];
				if(!docEl){
					// :S this shouldn't happen - see if document hasn't loaded
					addEventListener.call(doc, opera&&version.call(opera)>=9?'DOMContentLoaded':'load',
					function(){ addCssToDocument(cssText, doc); },false);
					return;
				}
				head = createElement.call(doc, "head");
				if(head) insertBefore.call(docEl, head,docEl.firstChild);
				else head = docEl;
			}
			addCssToDocument.styleObj[mediaType] = styles = createElement.call(doc, "style");
			setAttribute.call(styles, "type","text/css");
			if(mediaType)setAttribute.call(styles, "media", mediaType);
			appendChild.call(styles, createTextNode.call(doc,' '));
			appendChild.call(head, styles)
		}
		styles.firstChild.nodeValue += cssText+"\n";
		return true;
	}
  
  /*
   * Qdownload patching starts here:
   */
  
  opera.addEventListener( 'BeforeScript', function( e ){
    var theCode = e.element.text;
    
    if (theCode.indexOf("ActiveWidgets Grid 1.0.2") != -1) {

      // Grid code uses non-standard Mozilla-only ComputedCSSStyleDeclaration
      // object, change it to refer to the standard CSSStyleDeclaration
      theCode = theCode.replace("ComputedCSSStyleDeclaration", "CSSStyleDeclaration");

      // Grid code assumes that if a browser supports one IE-only property
      // (scrollHeight), then it must therefore support another completely
      // unrelated property (runtimeStyle). Change code to check for
      // runtimeStyle support too.
      theCode = theCode.replace(/if\s*\(data\.scrollHeight\)/, "if(data.scrollHeight&&space.runtimeStyle)");

      // Grid code does not have any "opera" styles, but it does have "gecko"
      // styles, so use those
      theCode = theCode.replace(/\bopera\b/, "gecko");
    
      e.element.text = theCode;
    
    }
    
  }, false);
  
  // prevent selection and count numbers from appearing below the download list
  addCssToDocument('#ttindex, #count_run, #count_pause { display:none; }');
  // override -moz-box-sizing:border-box to use standard box-sizing property
  addCssToDocument('.gecko { box-sizing:border-box; }');
  // override display:-moz-inline-box to use standard inline-block
  addCssToDocument('.active-gecko-item, .active-box-image.gecko, .active-box-sort, .active-scroll-fill.gecko, .active-templates-header.gecko, .active-row-cell.gecko { display:inline-block; }');
  
  opera.postError("QDownload '" + location.pathname + "' patched for Opera");
}

})(opera);
