/* 
-----------------------------------------------------------------
	NZA.2323 NZ NSCREEN
	css_helpers.js
	
	Description: 
 	COntains various CSS helper functions
	
	Created 14/01/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 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'.
*/
function css_imagereplace() {
	
	var tabClass = "h2fir"; // css classname of item we are looking for
	if ((!document.getElementsByTagName) && (!document.appendChild)) return false;
	
	// 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') {
				if (h2s[i].childNodes[0].childNodes[1].nodeType=='3') { // if the second node is text only
					h2textvalue = h2s[i].childNodes[0].childNodes[1].nodeValue;
					h2textvalue = (h2textvalue.toLowerCase()) // convert string to lowercase
					h2textvalue = h2textvalue.replace(/\n/g, "");
					h2textvalue = h2textvalue.replace(/ +/g, "-"); // replace spaces with -
					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++){
						spans[0].style.backgroundImage = 'url(/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
					h2textvalue = h2s[i].childNodes[1].nodeValue;
					h2textvalue = (h2textvalue.toLowerCase()) // convert string to lowercase
					h2textvalue = h2textvalue.replace(/\n/g, "");
					h2textvalue = h2textvalue.replace(/ +/g, "-"); // replace spaces with -
					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++){
						spans[0].style.backgroundImage = 'url(/resources/images/ui/text/h2-' + h2textvalue + '.gif)'; // insert the style into the first span
					}
				}
			}
	}
	// Process h3's
	var h3s=document.getElementsByTagName('h3');		
	for(var i=0;i<h3s.length;i++){
		if(!cssjs('check',h3s[i],tabClass)){continue;} // if css class exists
		
			var h3textvalue;
			if (h3s[i].childNodes[0].nodeName == 'A') {
				if (h3s[i].childNodes[0].childNodes[1].nodeType=='3') { // if the second node is text only
					h3textvalue = h3s[i].childNodes[0].childNodes[1].nodeValue;
					h3textvalue = (h3textvalue.toLowerCase()) // convert string to lowercase
					h3textvalue = h3textvalue.replace(/\n/g, "");
					h3textvalue = h3textvalue.replace(/ +/g, "-"); // replace spaces with -
					h3textvalue = h3textvalue.replace("?", ""); // replace ? with nothing
					var spans=h3s[i].getElementsByTagName('span');
					h3s[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
					for(var ii=0;ii<spans.length;ii++){
						spans[0].style.backgroundImage = 'url(/resources/images/ui/text/h2-' + h3textvalue + '-link.gif)'; // insert the style into the first span
					}
				}
				
			} else {
				if (h3s[i].childNodes[1].nodeType=='3') { // if the second node is text only
					h3textvalue = h3s[i].childNodes[1].nodeValue;
					h3textvalue = (h3textvalue.toLowerCase()) // convert string to lowercase
					h3textvalue = h3textvalue.replace(/\n/g, "");
					h3textvalue = h3textvalue.replace(/ +/g, "-"); // replace spaces with -
					h3textvalue = h3textvalue.replace("?", ""); // replace ? with nothing
					var spans=h3s[i].getElementsByTagName('span');
					h3s[i].style.overflow = "hidden"; // hide the overflow when the text size is increased
					for(var ii=0;ii<spans.length;ii++){
						spans[0].style.backgroundImage = 'url(/resources/images/ui/text/h3-' + h3textvalue + '.gif)'; // insert the style into the first span
					}
				}
			}
		}
}

/*  FUNCTION pnglinks - now deprecated (03-08-2008)
	-------------------------------------------------------------
	This script create hover effects that are more durable and faster than CSS. 
	Part of the filename must match the class name exactly to work. This is a MUST fix for IE bugs. 
*/
function pnglinks() {
	if (!document.getElementById) return false;
	
	// Secondary nav - Watch
	var navsecondary2 = document.getElementById('nav_secondary_2');
	if (navsecondary2) {
		var navelements = navsecondary2.getElementsByTagName("li");
		for (var i=0; i<navelements.length; i++) {
			element = navelements[i];
			if(cssjs('check',element.getElementsByTagName("span")[0],'active')){continue;} // if active class ignore
			element.onmouseover = function(evt) { 
				this.getElementsByTagName("a")[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/resources/images/ui/nav/nav-secondary3-" + this.className + "-hover.png\', sizingMethod=\"crop\")";
				return false;
			} 
			element.onmouseout = function(evt) { 
				this.getElementsByTagName("a")[0].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/resources/images/ui/nav/nav-secondary3-" + this.className + "-off.png\', sizingMethod=\"crop\")"; 
				return false;
			} 
		}
	} 
}

/* Form input(upload) nicifier */
var W3CDOM = (document.createElement && document.getElementsByTagName);
function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('span');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.src='/resources/images/ui/buttons/tool-browse-off.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

/* CSS safari hacks - use only for non-critical fixes */
function apply_browser_fixes_for_js_dependent_elements() { 
	if ((whichbrowser.isNS && (whichbrowser.versionMinor == 7.1)) || 
					(whichbrowser.isNS && (whichbrowser.versionMinor == 8.1)) || 					
					(whichbrowser.isFirefox && (whichbrowser.versionMinor == 1))) {
		document.body.className += ' phoenix';
	}	
	//if ((whichbrowser.isSafari) && (!whichbrowser.isChrome)) {
		if (whichbrowser.isSafari) {
		//alert(whichbrowser.versionMajor);
		var safariversion;
		if (whichbrowser.versionMajor >= '500') {
			safariversion = '3';
		} else {
			safariversion = '';
		}
		document.body.className += ' safari'+safariversion; 
	}
	if (whichbrowser.isOpera) {
		document.body.className += ' opera';
	}
}
	
	
/* OTHER HELPER FUNCTIONS */
// Events helper
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	}
}

// CSS helper
function cssjs(a,o,c1,c2) {
		switch (a){
			case 'swap':
				o.className=!domtab.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!domtab.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
}

	
//	Browser detect helper (for Safari, Opera, Mac and Linux oddities)	
var whichbrowser;
function detectbrowser() {			
	var ua = navigator.userAgent.toLowerCase(); 
	// browser engine name
	this.isGecko  = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);   
	// browser name
	this.isSafari = (ua.indexOf('safari') != - 1);
	this.isChrome = (ua.indexOf('chrome') != - 1);
	this.isMozilla = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
	this.isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
	this.isFirefox = (ua.indexOf('firefox/') != -1);
	this.isOpera       = (ua.indexOf('opera') != -1);
	this.isNS = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
	// browser version
	this.versionMinor = parseFloat(navigator.appVersion); 	
	// correct version number
	if (this.isGecko && !this.isMozilla) {
	  this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
	}
	else if (this.isMozilla) {
	  this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
	} else if (this.isSafari) {
		this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
	} else if (this.isIE && this.versionMinor >= 4) {
	  this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
	}
	this.versionMajor = parseInt(this.versionMinor); 
	// platform
	this.isWin = (ua.indexOf('win') != -1);
	this.isMac = (ua.indexOf('mac') != -1);
	// detect ns version
	this.isNS6x = (this.isNS && this.versionMajor == 6);
	this.isIE7x = (this.isIE && this.versionMajor == 7);
}
whichbrowser = new detectbrowser();	

addEvent(window, 'load', css_imagereplace, false);
addEvent(window, 'load', initFileUploads, false);
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
//if (isIE) {
//	addEvent(window, 'load', pnglinks, false);
//}
addEvent(window, 'load', apply_browser_fixes_for_js_dependent_elements, false);

// For Mac and Linux gecko Flash rendering z-index bug
function generateiframe(el,showhide) {
	if ((!document.createElement) && (!document.appendChild)) return false;
	if (!document.getElementById("ffflashzindexfix")) {
		var boxIframe = document.createElement("iframe");
		if (showhide=='show') {
			boxIframe.setAttribute("id","ffflashzindexfix");
			boxIframe.setAttribute("src","about:blank");
			boxIframe.setAttribute("scrolling","no");
			boxIframe.setAttribute("frameBorder","0");
			el.appendChild(boxIframe);
			var iframeheight = el.clientHeight - 12;
			boxIframe.style.height = iframeheight;
		} else if (showhide=='hide') {
			var boxIframe = document.getElementById("ffflashzindexfix");
			el.removeChild(boxIframe);
		}
	} else {
		if (showhide=='hide') {
			var boxIframe = document.getElementById("ffflashzindexfix");
			el.removeChild(boxIframe);
		}
	}
}

function trackShareItem(url, network, slug) {
  pageTracker._trackEvent('Share Tracking', network, slug);
  window.open(url, 'blank');
}