/**
 * Theme related Javascript 
 *
 */

// Make sure accidental debug does not crash IE 
if(!window.console) {
	console = {};
	console.log = function() {};
}


/** Whitespace-agnostic Cufon text converter */
function cufonize(selector, fontFamily, opts) {
        jQuery(selector).each( function() {
               var elem = jQuery(this);
               var text = jQuery.trim(elem.text());
               elem.text(text);

        });
	
        Cufon.replace(selector);
}

jQuery(document).ready(function() {
	
	// These all will be replaced with Myriad Pro
	var selectors = [
		"#language-selector a",
		"#menu li a", 
		".content h1", 
		".front-page-footer .links a", 
		".front-page-footer h2", 
		"ul.wizard-steps span",
		".front-page-splash-button a",
		".body-splash-button span"
	];
	
	var jQuerySelector = selectors.join(", ");
	
    cufonize(jQuerySelector, 'Myriad Pro');

    //Cufon.replace("h2");
    preloadCssImages();
    
});

preloadCssImages = function() {
        var allImgs = [];//new array for all the image urls  
        var k = 0; //iterator for adding images
        var sheets = document.styleSheets;//array of stylesheets
        
        for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
                var cssPile = '';//create large string of all css rules in sheet
                var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
                var baseURLarr = csshref.split('/');//split href at / to make array
                baseURLarr.pop();//remove file path from baseURL array
                var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
                if(baseURL!="") baseURL+='/'; //tack on a / if needed
                if(document.styleSheets[i].cssRules){//w3
                        var thisSheetRules = document.styleSheets[i].cssRules; //w3
                        for(var j = 0; j<thisSheetRules.length; j++){
                                cssPile+= thisSheetRules[j].cssText;
                        }
                }
                else {
                        cssPile+= document.styleSheets[i].cssText;
                }
                
                //parse cssPile for image urls and load them into the DOM
                var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
                if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
                        var arr = jQuery.makeArray(imgUrls);//create array from regex obj        
                        
						jQuery(arr).each(function(){
                                allImgs[k] = new Image(); //new img obj
                                console.log("Preloading image url:" + this);
								console.log("Preloading base:" + baseURL);
								
								var url = this;
								
								// Bugfix to CSS
								if(url[0] == '"') {
									url = url.substring(1);
								}
								
                                //Why strip it here after first adding it? This also breaks urls wihtout / in the beginning. example spinner image urls
								//if(baseURL[baseURL.length-1] == '/') {
									// Handle ending slash
								//	console.log("Stripping ending slash");
								//	baseURL = baseURL.substring(0, baseURL.length-1);
								//}
								
								// Relative to the server root
								if(url[0] == '/') {
									// http://
									var serverRoot = baseURL.indexOf("/", 8);
									baseURL = baseURL.substring(0, serverRoot);
								}
								
								var imgURL = (url == '/' || url.match('http://')) ? url : baseURL + url;     //set src either absolute or rel to css dir

								console.log("Fixed base:" + baseURL);
								
								console.log("Image full URL:" + imgURL);
								
                                allImgs[k].src = imgURL;
								
                                k++;
                        });
                }
        }//loop
	
	if(window.console) {
		console.log("Preloading images");
	}
	
        return allImgs;
}

 

