/**
 * @author Hassan Kayani
 */

/**
 * This function grabs the correct css for the map controls based on hostname
 */
function switchCSS() {
	var hostName = location.hostname;
	//hostName = 'test';
	var newCSS = "css/getstyle.php?host=" + hostName;
	replaceCSS ("map-extended.css",newCSS);
}


/**
 * This function replaces stylesheet provided in oldFileName with newFileName
 * @param {String} oldFileName
 * @param {String} newFileName
 */
function replaceCSS (oldFileName, newFileName) {
	// We know css are included using 'link'
	var cssElement = "link";
	// We know the attribute we want to change
	var cssAttribute = "href";
	// Get all linked css
	var allCSS = document.getElementsByTagName (cssElement);
	
	// We need to go through all links and find the one to be replace (link to change is oldFileName)
	// We will do backward search
	for (var ind = allCSS.length - 1; ind >=0; ind--) {
		// Test if we have the file to replace
		if (allCSS[ind] && allCSS[ind].getAttribute(cssAttribute)!=null && allCSS[ind].getAttribute(cssAttribute).indexOf(oldFileName)!=-1) {
			// Create new css file
			var newCSS = createCSSFile(newFileName);
			// replace current css with newly created css
			allCSS[ind].parentNode.replaceChild(newCSS, allCSS[ind]);
		}
	}
}

/**
 * Creates new external CSS stylesheet using provided filename
 * @param {String} fileName
 */
function createCSSFile (fileName) {
	// Create DOM element (link)
	var newCSS = document.createElement("link");
	// Set different attributes
	newCSS.setAttribute("rel", "stylesheet");
	newCSS.setAttribute("type", "text/css");
	newCSS.setAttribute("href", fileName);
	newCSS.setAttribute("media", "screen");
	
	// Return this new DOM element
	return newCSS;
}

