/**
 * --------------------------------------------------------------------
 * preloadimages.js
 * Version: 0.1, 07.10.2010
 * by Gáspár Lajos, gaspar.lajos@glsys.eu
 *                      http://glsys.eu/
 *
 * Copyright (c) 2010 Gáspár Lajos
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * --------------------------------------------------------------------
 * Changelog:
 * ver 0.1: CSS preloader
 * --------------------------------------------------------------------
 */

var img;

function processStyleSheet(StyleSheet){
//Protocol
 var proto='';
 var host='';
 var path='';
 var pos=-1;
 if(StyleSheet.href){
  var href=StyleSheet.href;
  var lowhref=href.toLowerCase();
  proto=lowhref.indexOf('http://')==0?'http://':(lowhref.indexOf('https://')==0?'https://':'');
  host=lowhref.substr(href.indexOf('://')+3,href.length);
  pos=host.indexOf('/');
  if(pos!=-1){
   path=host.substr(pos,host.length);
   host=host.substr(0,pos);
   }
  }else{
  proto=location.protocol+'//';
  host=location.hostname;
  path=location.pathname;
  }
 if(host==location.hostname){
  pos=path.lastIndexOf("/");
  path=(pos!=-1)?path.substring(0,pos+1):'';
  var rules=(typeof(StyleSheet.cssRules)=='object'?StyleSheet.cssRules:(typeof(StyleSheet.rules)=='object'?StyleSheet.rules:''));
  if(typeof(rules)=='object')for(var j=0;j<rules.length;j++)if(typeof(rules[i])=='object'){
   var style=rules[j].style;
   if(typeof(style)!='undefined'&&style.backgroundImage.toLowerCase().substr(0,4)=="url("){
    var filename=style.backgroundImage.substring(4,style.backgroundImage.length-1);
    if(filename.substr(0,1)=='"')filename=filename.substring(1,filename.length);
    if(filename.substr(filename.length-1,1)=='"')filename=filename.substring(0,filename.length-1);
    if(filename.indexOf('http://')==-1&&filename.indexOf('https://')==-1){
     if(filename.indexOf('/')!=0)filename=path+filename;
     filename=proto+host+filename;
     }
    img=new Image();
    img.src=filename;
    }
   }
  }
 }

for(var i=0;i<document.styleSheets.length;i++){
 var StyleSheet=document.styleSheets[i];
 processStyleSheet(StyleSheet);
 if((StyleSheet.ownerNode?StyleSheet.ownerNode:StyleSheet.owningElement).tagName=='STYLE'&&StyleSheet.imports)for(var j=0;i<StyleSheet.imports.length;j++)processStyleSheet(StyleSheet.imports[j]);
 }

/*
TODO: lowercase !!!
TODO: test @import
*/

