/* 
-----------------------------------------------------------------
	BRANZ
	BRA.2417
	css_helpers.js
	
	Description: 
 	Contains various CSS helper functions
	
	Created 20.05.2008 by AK
	Last Updated: See SVN	
-----------------------------------------------------------------
*/


/*  FUNCTION css_imagereplace
	-------------------------------------------------------------
	This script will read the first instance of
 	a header 2 tag, note the contents and display
 	the matching filename (or pass through to the PHP image generator) as a background image via css.
	Filenames for background images must match exactly 
	the text inside the <h2> node, and the <h2> must have 
	the CSS class 'h2fir'.
*/

//var server = '/bra_2417'; // stage2.chrometoaster.com
var server = ''; // local dev
 
function css_imagereplace() {
	var tabClass = "h2fir"; // css classname of item we are looking for
	var parentClass = "sectionliner"; // css classname of item we are looking for
	if ((!document.getElementsByTagName) && (!document.appendChild)) return false;
	
	// Process H1's - piped through image PHP generator
	var h1s=document.getElementsByTagName('h1');		
	for(var i=0;i<h1s.length;i++){
		if(!cssjs('check',h1s[i].parentNode,parentClass)){continue;} // if css class exists
			var h1textvalue;
			if (h1s[i].childNodes[1]) {
					var nodenumber = 1;
			} else if (h1s[i].childNodes[0]) {
					var nodenumber = 0;
			}
			if (h1s[i].childNodes[nodenumber].nodeType=='3') { // if the second node is text only
				//alert (h1s[i].childNodes[1].nodeValue);
				h1textvalue = h1s[i].childNodes[nodenumber].nodeValue;
				h1textvalue = (h1textvalue.toUpperCase()) // convert string to uppercase
				h1textvalue = h1textvalue.replace(/\n/g, "");
				//h1textvalue = h1textvalue.replace(/ +/g, "-"); // replace spaces with -
				//h1textvalue = h1textvalue.replace("?", ""); // replace ? with nothing
				//h1textvalue = h1textvalue.replace("\'", ""); // replace ' with nothing
				var spans=h1s[i].getElementsByTagName('span');
				h1s[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
				// read in colours
				var foregroundColor = getStyle(h1s[i],'color');
				foregroundColor = foregroundColor.replace("#", "");
				var backgroundColor = getStyle(h1s[i],'backgroundColor');
				backgroundColor = backgroundColor.replace("#", "");
				for(var ii=0;ii<spans.length;ii++){
					//var h1textvalue = 'Hot Topics'; //testing
					if (document.getElementById('search_results')) {
						spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font=GothaBol.ttf&font_size=18&text=' + escape(h1textvalue) + '&fg=' + foregroundColor + '&bg=' + backgroundColor + ')'; // insert the style into the first span						
					} else {
						spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font_size=22&text=' + escape(h1textvalue) + '&fg=' + foregroundColor + '&bg=' + backgroundColor + ')'; // insert the style into the first span						
					}
				}
			}
	}
	
	// Process H2's
	var h2s=document.getElementsByTagName('h2');		
	for(var i=0;i<h2s.length;i++){
		if(!cssjs('check',h2s[i],tabClass)){continue;} // if css class exists
		
			var h2textvalue;
			if (h2s[i].childNodes[0].nodeName == 'A') {
				//alert(h2s[i].childNodes[0].nodeName);
				//alert (h2s[i].childNodes[0].childNodes[1].nodeValue);
				if (h2s[i].childNodes[0].childNodes[1].nodeType=='3') { // if the second node is text only
					h2textvalue = h2s[i].childNodes[0].childNodes[1].nodeValue;
					if (h2s[i].parentNode.parentNode.parentNode.parentNode.id == "related") {
						h2textvalue = (h2textvalue.toUpperCase()) // convert string to uppercase
					} else {
						h2textvalue = (h2textvalue.toLowerCase()) // convert string to lowercase
						h2textvalue = h2textvalue.replace(/ +/g, "-"); // replace spaces with -
						h2textvalue = h2textvalue.replace("?", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace(")", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace("(", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace("\'", ""); // replace ' with nothing
					}
					h2textvalue = h2textvalue.replace(/\n/g, "");
					var spans=h2s[i].getElementsByTagName('span');
					h2s[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
					for(var ii=0;ii<spans.length;ii++){
					//	var h2textvalue = 'credits'; //testing
						if (h2s[i].parentNode.parentNode.parentNode.parentNode.id == "related") {
							//spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font_size=9&text=' + escape(h2textvalue) + '&fg=934925&bg=fafafa&align=center)'; 
							spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font_size=18&text=' + escape(h2textvalue) + '&fg=934925&bg=fafafa&align=center)'; 
						} else {
							spans[0].style.backgroundImage = 'url('+server+'/resources/images/ui/text/h2-' + h2textvalue + '-link.gif)'; // insert the style into the first span
						}
					}
				}
				
			} else {
				if (h2s[i].childNodes[1].nodeType=='3') { // if the second node is text only
					//alert (h2s[i].childNodes[1].nodeValue);
					h2textvalue = h2s[i].childNodes[1].nodeValue;
					if (h2s[i].parentNode.parentNode.parentNode.parentNode.id == "related") {
						h2textvalue = (h2textvalue.toUpperCase()) // convert string to uppercase
					} else {
						h2textvalue = (h2textvalue.toLowerCase()) // convert string to lowercase
						h2textvalue = h2textvalue.replace(/ +/g, "-"); // replace spaces with -
						h2textvalue = h2textvalue.replace("?", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace(")", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace("(", ""); // replace ? with nothing
						h2textvalue = h2textvalue.replace("\'", ""); // replace ' with nothing
					}
					var spans=h2s[i].getElementsByTagName('span');
					h2s[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
					for(var ii=0;ii<spans.length;ii++){
					//	var h2textvalue = 'credits'; //testing
						if (h2s[i].parentNode.parentNode.parentNode.parentNode.id == "related") {
							//spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font_size=9&text=' + escape(h2textvalue) + '&fg=934925&bg=fafafa&centeralign=1)'; 
							spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font_size=18&text=' + escape(h2textvalue) + '&fg=934925&bg=fafafa&centeralign=1)'; 
						} else {
							spans[0].style.backgroundImage = 'url('+server+'/resources/images/ui/text/h2-' + h2textvalue + '.gif)'; // insert the style into the first span
						}
					}
				}
			}
	}
	// Process Shop prices - piped through image PHP generator
	var prices = document.getElementById("zone_maincontent").getElementsByTagName("span");
	if (prices.length > 0) {
		for (var i=0;i<prices.length;i++) {
			if(!cssjs('check',prices[i],"price")) { continue; } // if css class exists
			var pricetextvalue = "";
			if (prices[i].childNodes[0].nodeName == 'A') { // price is inside an <a> element
				if (prices[i].childNodes[0].childNodes[1].nodeType=='3') { // if the node is text only
					pricetextvalue = prices[i].childNodes[0].childNodes[1].nodeValue;
				}
			} else {
				if (prices[i].childNodes[2].nodeType=='3') { // if the node is text only
					pricetextvalue = prices[i].childNodes[2].nodeValue;
				}
			}
			if (pricetextvalue.length >0 ) {
				pricetextvalue = (pricetextvalue.toUpperCase()) // convert string to lowercase
				pricetextvalue = pricetextvalue.replace(/\n/g, "");
				var spans=prices[i].getElementsByTagName('span');
				prices[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
				for(var ii=0;ii<spans.length;ii++){
					if (cssjs('check',spans[ii],"price2")) { // if grey bg
						spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font=GothaBoo.ttf&font_size=24&text=' + escape(pricetextvalue) + '&fg=444444&bg=f1f1f1)'; // insert the style into the first span	
					} else { // if white bg
						spans[0].style.backgroundImage = 'url('+server+'/resources/scripts/text2image.php?font=GothaBoo.ttf&font_size=24&text=' + escape(pricetextvalue) + '&fg=444444&bg=ffffff)'; // insert the style into the first span	
					}
				}
			}
		}
	}
	
}

/* Javascript menu support */
function MenuHoverPseudo() {
	if (!document.getElementsByTagName) return false;
	
	// Main nav
	if (!document.getElementById("nav_main")) return false;
	
	/* preload nav images & submenu */
	for (var i=0; i<8; i++) {
		var menuname = "menu" + i;
		menuname = newImage('/resources/images/ui/nav/nav-main-' + (i+1) + '-off.gif');
		var submenuname = "submenu" + i;
		submenuname = newImage('/resources/images/ui/nav/nav-main-dropdown-bg-' + (i+1) + '.gif');
	}
	
	// loop through each menu list element
	var navitem = document.getElementById("nav_main").getElementsByTagName("li");
	for (var i=0; i<navitem.length; i++) {
		if(cssjs('check',navitem[i],'parent')) {	
			navitem[i].onmouseover=function() { 
				cssjs('add',this,'over'); 
				cssjs('add',this.childNodes[1],'over'); 
				generateiframe(this.childNodes[3], 'show'); 
				// Bug fix - hide breadcrumbs underneath primarynav in IE
				if (!document.getElementById("nav_secondary")) return false;
				var navsecitem = document.getElementById("nav_secondary").getElementsByTagName("a");
				for (var a=0; a<navsecitem.length; a++) {
					if(cssjs('check',navsecitem[a].parentNode,'over')) {	
						cssjs('remove',navsecitem[a].parentNode,'over'); 
					}
				}
			}
			navitem[i].onmouseout = function() { 
				cssjs('remove',this,'over'); 
				cssjs('remove',this.childNodes[1],'over'); 
				generateiframe(this.childNodes[3], 'hide');
			}
		}
	}
	
	// Breadcrumb nav 
	if (!document.getElementById("nav_secondary")) return false;
	// loop through each menu link element
	var navitem = document.getElementById("nav_secondary").getElementsByTagName("a");
	for (var i=0; i<navitem.length; i++) {
		if(cssjs('check',navitem[i].parentNode,'parent')) {	
			navitem[i].onmouseover=function() { 
				cssjs('add',this.parentNode,'over'); 
			}
			navitem[i].onmouseout=function() { 
				cssjs('remove',this.parentNode,'over'); // IE bug fix
			}
			navitem[i].parentNode.getElementsByTagName("div")[0].onmouseover=function() { 
				cssjs('add',this.parentNode,'over'); 
			}
			navitem[i].parentNode.getElementsByTagName("div")[0].onmouseout=function() { 
				cssjs('remove',this.parentNode,'over'); 
			}
		}
	}
}

/* IE Select bug fix -generates an Iframe to place on top of the select box in the BRANZ Shop pages */
function generateiframe(el,showhide) {
	if (whichbrowser.isIE && whichbrowser.versionMinor <= 6 ) { // do this if lower than IE 7
		if ((!document.createElement) && (!document.appendChild)) return false;
		var boxIframe = document.createElement("iframe");
		if (showhide=='show') {
			boxIframe.setAttribute("id","ieselectfix");
			boxIframe.setAttribute("src","about:blank");
			boxIframe.setAttribute("scrolling","no");
			boxIframe.setAttribute("frameBorder","0");
			el.insertBefore(boxIframe,el.childNodes[0]);
			var iframeheight = el.clientHeight - 12;
			boxIframe.style.height = iframeheight;
		} else if (showhide=='hide') {
			var boxIframe = document.getElementById("ieselectfix");
			el.removeChild(boxIframe);
		}
	}
}

/* Table helper - applies a 'last' class to the last cell/rows to get around IE or Moz not being able to style table rows */
function tablerowhelper() {
		if (!document.getElementById) return false;
		var tables = document.getElementsByTagName("table"); // get tables
		for (var i=0; i<tables.length; i++) {
			if(!cssjs('check',tables[i],'layout_table')) {	// don't act on layout tables
				var tablerows = tables[i].getElementsByTagName("tr");
				//cssjs('add',tablerows[1],'first');  // add the 'first' class
				cssjs('add',tablerows[tablerows.length-1],'last');  // add the 'last' class
				for (var ii=0; ii<tablerows.length; ii++) { // get rows
					var tableheaders = tablerows[ii].getElementsByTagName("th");
					for (var iii=0; iii<tableheaders.length; iii++) {
						cssjs('add',tableheaders[tableheaders.length-1],'last');  // add the 'last' class
						cssjs('add',tableheaders[0],'first');  // add the 'first' class
					}
					var tablecells = tablerows[ii].getElementsByTagName("td");
					for (var iii=0; iii<tablecells.length; iii++) {
						cssjs('add',tablecells[tablecells.length-1],'last');  // add the 'last' class
						cssjs('add',tablecells[0],'first');  // add the 'first' class
					}
				}
			}
		}
}

/* Get the CSS style for a node */
function getStyle(el,styleProp) {
	if (el.currentStyle) {
		var y = el.currentStyle[styleProp];
	} else if (window.getComputedStyle) {
		// Note: Mozilla returns rgb values and not HEX values and thus needs conversion
		if (styleProp=='backgroundColor') {
			styleProp = 'background-color';
		}
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
		y = rgbConvert(y);
	}
	return y;
}

/* RGB to HEX convertor for Mozilla */
function rgbConvert(str) {
   str = str.replace(/rgb\(|\)/g, "").split(",");
   str[0] = parseInt(str[0], 10).toString(16).toLowerCase();
   str[1] = parseInt(str[1], 10).toString(16).toLowerCase();
   str[2] = parseInt(str[2], 10).toString(16).toLowerCase();
   str[0] = (str[0].length == 1) ? '0' + str[0] : str[0];
   str[1] = (str[1].length == 1) ? '0' + str[1] : str[1];
   str[2] = (str[2].length == 1) ? '0' + str[2] : str[2];
   return ('#' + str.join(""));
}

/* Preloader */
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

/* Supporting inline PNGs in IE */
function fnLoadPngs() {
	if (whichbrowser.isIE && whichbrowser.versionMinor <= 6 ) { // do this if lower than IE 7
		if (!document.getElementById) return false;
		var el = document.getElementById("zone_maincontent"); // limit to a specific area
		if (el) {
			var images = el.getElementsByTagName("img"); // get images
			var shim			= '/resources/images/ui/pixel.gif';
			for (var i=0; i<images.length; i++) {
				if (images[i].src.match(/\.png$/i) !== null) {
					var src = images[i].src;
					images[i].style.width = images[i].width + "px";
					images[i].style.height = images[i].height + "px";
					images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
					images[i].src = shim;
				}
				images[i].style.visibility = "visible"; /* turning all images back on again. */
			}
		}
	} 
}