if(typeof Array.prototype.unshift==='undefined') {
	// Add an element to the beginning of an array
	Array.prototype.unshift = function() {
		this.reverse();
		var a = arguments, i = a.length;
		while(i--) { this.push(a[i]); }
		this.reverse();
		return this.length;
	};
}

if(typeof Array.prototype.push==='undefined' ) {
	// Add an element to the end of an array, return the new length
	Array.prototype.push = function() {
		for( var i = 0, b = this.length, a = arguments, l = a.length; i<l; i++ ) {
			this[b+i] = a[i];
		}
		return this.length;
	};
}

String.prototype.stripHTML = function() {
	// Strip HTML tags from a string
	return this.replace(/<(?:.|\s)*?>/g,"");
};

String.prototype.stripWhitespace = function() {
	// Strip tabs and multiple spaces from a string
	var str = this.replace(/\t/g," ");
	return str.replace(/\s{2,}/g," ");	
};

String.prototype.normalise = function() {
//	Strips non-alphanumeric chars & multiple spaces, replaces single spaces with underscores, converts to lowercase
	return this.stripHTML().replace(/[^a-zA-Z0-9\s]/g, "").trim().replace(/\s{1,}/g, "_").toLowerCase();
};

String.prototype.trim = function() {
	var str = this;
	var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for (var i = 0; i < str.length; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	for (i = str.length - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
};

function in_array(sNeedle, aHaystack) {
	// Returns true if sNeedle is in array aHaystack
	for(var i=0; i<aHaystack.length; i++) {
		if(sNeedle == aHaystack[i]) {
			return true;	
		}
	}
	return false;
};

function addEventHandler(obj, evt, fnc) {
	if(typeof(obj.attachEvent) != 'undefined') {
		obj.attachEvent('on' + evt, fnc);
	}
	else {
		obj.addEventListener(evt, fnc, false);
	}
};

// addDOMLoadEvent :: http://www.thefutureoftheweb.com/blog/adddomloadevent
addDOMLoadEvent=(function(){var load_events=[],load_timer,script,done,exec,old_onload,init=function(){done=true;clearInterval(load_timer);while(exec=load_events.shift())
exec();if(script)script.onreadystatechange='';};return function(func){if(done)return func();if(!load_events[0]){if(document.addEventListener)
document.addEventListener("DOMContentLoaded",init,false);if(/WebKit/i.test(navigator.userAgent)){load_timer=setInterval(function(){if(/loaded|complete/.test(document.readyState))
init();},10);}
old_onload=window.onload;window.onload=function(){init();if(old_onload)old_onload();};}
load_events.push(func);}})();

function htmlEditorInit() {

	// If Publishing an FAQ, set up the editor content
	if(document.getElementById("publish_faq_condition_tid")) {
		var iTaxonomy = document.getElementById("publish_faq_condition_tid").innerHTML;
		var sQuestion = document.getElementById("publish_faq_question").innerHTML;
		var sAnswer = document.getElementById("publish_faq_expert_response").innerHTML;
		// Convert question to single line
		document.getElementById('edit-title').value = sQuestion.replace(new RegExp("\\n", "g"), " ");
		// convert linebreaks in answer to paragraphs
		document.getElementById('edit-body').value = "<p>" + sAnswer.replace(new RegExp("\\n", "g"), "</p><p>") + "</p>";
		
		// Select the taxonomy term (condition)
		var oTaxonomy = document.getElementById("edit-taxonomy-2");
		for(var i = 0; i < oTaxonomy.options.length; i++) {
			if(oTaxonomy.options[i].value == iTaxonomy) {
				oTaxonomy.options[i].selected = true;
			}
			else {
				oTaxonomy.options[i].selected = false;
			}
		}
	}

	// Initialise the editable elements
	if(document.getElementById('edit-body')) {
		var oFCKeditor = new FCKeditor('body');
		oFCKeditor.BasePath = baseURL + 'custom/fckeditor/';
		oFCKeditor.ReplaceTextarea();
	}
	if(document.getElementById('edit-field-level-2-content-0-value')) {
		var oFCKeditor = new FCKeditor('field_level_2_content[0][value]');
		oFCKeditor.BasePath = baseURL + 'custom/fckeditor/';
		oFCKeditor.ReplaceTextarea();
	}
	if(document.getElementById('edit-field-references-0-value')) {
		var oFCKeditor = new FCKeditor('field_references[0][value]');
		oFCKeditor.BasePath = baseURL + 'custom/fckeditor/';
		oFCKeditor.ReplaceTextarea();
	}

};

function toggleElements(sShow, sHide) {
	var aShow = sShow.split(",");
	for(var i=0; i<aShow.length; i++) {
		document.getElementById(aShow[i]).style.display = "block";
	}
	var aHide = sHide.split(",");
	for(var i=0; i<aHide.length; i++) {
		document.getElementById(aHide[i]).style.display = "none";
	}
};


sfHover = function() {
	// Suckerfish css dropdown menu ie fix.
	// Get sf dropdown UL - first UL element on page
	var sfEls = document.getElementsByTagName("ul")[0].getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
};
if(window.attachEvent) {
	window.attachEvent("onload", sfHover);
}

function findPos(obj) {
	// Returns absolute x/y coords of obj.
	var curleft = curtop = 0;
	if(obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return[curleft,curtop];
};

function ixTooltip(oElem, sMessage) {
	// Displays sMessage in div at absolute position of oElem (+/- x/y offset vals).
	var xOffset = 0;
	var yOffset = 0;
	if(oElem != null) {
		var elemPos = findPos(oElem);
		var ixTooltipDiv = document.createElement("DIV");
		ixTooltipDiv.id = "ixTooltip";
		ixTooltipDiv.className = "ixTooltip";
		ixTooltipDiv.innerHTML =   "<div class='ixTooltipTop'></div>"
							+ "<div class='ixTooltipMid'>" + sMessage + "</div>"
							+ "<div class='ixTooltipBot'></div>";
		document.body.appendChild(ixTooltipDiv);
		ixTooltipDiv.style.left = (elemPos[0] - xOffset) + "px";
		var ixTooltipDivHeight = ixTooltipDiv.offsetHeight;
		ixTooltipDiv.style.top = (elemPos[1] - (ixTooltipDivHeight + yOffset)) + "px";
		ixTooltipDiv.style.visibility = "visible";
		
	}
	else {
		var ixTooltipDiv = document.getElementById("ixTooltip");
		ixTooltipDiv.parentNode.removeChild(ixTooltipDiv);
	}
};

function cli_parseContentImages() {
	// Parse the content area for images, replace src with link to dynamic thumbnail & assign lytebox link
	if(!document.getElementById("cli_content_inner")) {
		return false;
	}
	
	var thmW = 400;	// Thumbnail width
	var thmQ = 70;	// Thumbnail quality (if jpeg)
	
	var contentImages = document.getElementById("cli_content_inner").getElementsByTagName("img");
	for(var i = 0; i < contentImages.length; i++) {
		
		var imgSRC = contentImages[i].src;
		
		if(contentImages[i].getAttribute("width") && contentImages[i].getAttribute("height")) {
			// If the image has height/width attributes, calculate what the thumbnail size will be
			var imgW = contentImages[i].width;
			var imgH = contentImages[i].height;
			var resizePerc = (thmW / imgW) * 100;
			var newW = Math.ceil((imgW * resizePerc) / 100);
			var newH = Math.ceil((imgH * resizePerc) / 100);
		}
		
		// Generate thumbnail url
		var imgURL = imgSRC.split('custom');
		var thmURL = "/custom/ixthm/?img=/custom" + imgURL[1] + "&w=" + thmW + "&q=" + thmQ;

		// Update the image
		contentImages[i].src = thmURL;
		if(typeof(imgW) != "undefined") {
			contentImages[i].width = newW;
			contentImages[i].height = newH;
		}

		// Create lytebox wrapper link to fullsize image
		var imgLink = document.createElement('a');
		imgLink.href = imgSRC;
		imgLink.rel = "lytebox[thumbnails]";
		imgLink.title = contentImages[i].alt;
		
		// Attach the link to the doc, append the thumbnail to the link
		var imgLink = contentImages[i].parentNode.insertBefore(imgLink, contentImages[i]);
		imgLink.appendChild(contentImages[i]);
	}
};

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

function eraseCookie(name) {
	createCookie(name,"",-1);
};
