//Hover function ie
sfHover = function() {
	//Trigger only for IE
	if (document.all && !window.opera){		
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		if (sfEls){
			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);

var siteBaseUrl = '/';
var onloadEvents = new Array();

addToOnload('searchFormEvents()');

function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() { 
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}


function setBaseUrl(baseUrl){
	siteBaseUrl = baseUrl;
}

//searchBox
searchFormEvents = function (){	
	keywordE = document.getElementById("searchKeyword");	
	if (keywordE){
		keywordE.value = "Type Keywords";
		keywordE.onclick = function (){
			this.value = '';
		}
		keywordE.onblur = function (){
			if (this.value == ''){
				this.value = 'Type Keywords';
			}
		}
	}
}

function changeOnOther(){
	if (this.value == 'Venue Hire'){
		loadUrl('?enquiryType=Venue_Hire#Form');
	}
}

//Mailing List Form - auto clear
function mailingListFormAutoClear(formId){
	//MailingListUser[0][country]
	var fieldNames = ['firstName','lastName','email','street','suburb','state','postcode','country','phone'];
	var fieldValues = ['First Name','Last Name','Email','Street','Suburb','State','Post Code','Country','Phone'];
	myForm = document.getElementById(formId);
	//Need to add behaviour to each of these fields
	for (ii=0; ii < fieldNames.length ; ii++){
		var inputName = 'MailingListUser[0]['+fieldNames[ii]+']';
		
		myE = myForm.elements[inputName]; 
		myE.onclick = function (){		
			clearField(this,'remove');
		};
		
		/*
		myE.onblur = function (){				
			clearField(this,'restore');
		};
		*/
	}
	
	
}

//Clear the values from a field
function clearField(myInput,action){	
	var fieldNames = ['firstName','lastName','email','street','suburb','state','postcode','country','phone'];
	var fieldValues = ['First Name','Last Name','Email','Street','Suburb','State','Post Code','Country','Phone'];
	
	for (y =0; y < fieldNames.length; y++){
		var inputName = 'MailingListUser[0]['+fieldNames[y]+']';
		if (myInput.name == inputName){
			defaultVal = fieldValues[y];
			break;
		}
	}
	
	if (action == 'remove'){
		if (myInput.value == defaultVal) myInput.value = '';			
	} else { 
		if (myInput.value == '') myInput.value = defaultVal;	
	}
}

//Generic select option url jump
function jumpTo(s){
    var url = s.value;
	if (url !='') loadUrl(url);    
}

//Attaches an onclick to an element with id
function attachOnClickHref(id, link, eventParams){			
    element = document.getElementById(id);    
	element.style.cursor = 'pointer';
    element.onclick = function(){ loadUrl(link);}
}

function loadUrl(url){ window.location.href = url;}

function disableField(element){ document.getElementById(element).style.display = 'none';}

function closePopup() {
	e = document.getElementById('imgPopup');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function attachOnClickImage(id, imgPath , altText, imgWidth, imgHeight){
	element = document.getElementById(id);
	element.style.cursor = 'pointer';
	var imageText = altText || '';		
	var imgWidth = Number(imgWidth);
	var imgHeight = Number(imgHeight);		
	element.removeAttribute("href");
	element.onclick = function (){
		//check if this element exists.
		e = document.getElementById('imgPopup');
		if (!e){
			popupKeyListener();
			var myDiv = document.createElement("DIV");
			myDiv.id = "imgPopup";
			myIframe = document.createElement('iframe');
			myIframe.src = siteBaseUrl+'blank.php?image='+encodeURIComponent(imgPath)+'&altText='+encodeURIComponent(imageText);			
			myIframe.scrolling = 'no';
			myIframe.width = imgWidth;
			myIframe.height = imgHeight+45;					
			myIframe.frameBorder = "0";
			//Append
			myDiv.appendChild(myIframe);
			myDimensions = getWindowDimensions();
			myWidth = myDimensions[0];
			myDiv.style.width = imgWidth+'px';
			myDiv.style.left = (myWidth / 2) - (imgWidth / 2) + "px";
			document.body.appendChild(myDiv);
		}
		return false;
	}
}

/**
 * Caption Formater for the Gallery
 */
var CaptionFormat = {
	init : function(total){
		
		var styleChange = function (){this.parentNode.style.cursor ='default';};
		
		for(var i = 1; i <= total ; i++){
			var elementName = "caption"+ i;
			var ImageName = "Image"+ i;
			//First get the parent and cancel the onhover cursor
			img = document.getElementById(ImageName);
			img.parentNode.onmouseover  = styleChange;
			//Now format the caption if its length is > 30
			e = document.getElementById(elementName);
			if (e){
				if (e.innerHTML.length > 30) CaptionFormat.attachFormatting(e, i);				
			}
		}
	},
	/**
	 * @param element e - the element we wish to format
	 * @param int i - the id of that element
	 */
	attachFormatting : function (e, i){
		var caption = CaptionFormat.removeHtmlChar(e.innerHTML);
		e.innerHTML = '';
		
		startString = caption.slice(0,26) + '...';
		var mySpan = document.createElement('span');		
		mySpan.appendChild(document.createTextNode(startString));
		//mySpan.className = 'captionText';
		var toggle = document.createElement('img');
		toggle.src = siteBaseUrl + 'css/images/togglePlus.gif';
		toggle.setAttribute('align','right');
		toggle.className = 'togglePlus';
		
		toggle.onclick = CaptionFormat.getToggleFunc(caption);
		toggle.onmouseover = CaptionFormat.cursorPointerFunc();
		
		e.appendChild(toggle);
		e.appendChild(mySpan);
	},
	/**
	 * Returns the toogle func
	 * @param {Object} caption
	 */
	getToggleFunc :  function (caption){
		return(function (e){
			if (!e) var e = window.event;
				e.cancelBubble = true;
							
			if (e.stopPropagation) e.stopPropagation();
			CaptionFormat.toggleCaption(this, caption);
		});
	},
	cursorPointerFunc  : function (){
		return(function(){this.style.cursor = 'pointer';});
	},
	/**
	 * 
	 * @param {string} string
	 */
	removeHtmlChar : function (string){
		return string.replace(new RegExp("&amp;"),'&');		
	},
	
	/**
	 * 
	 * @param {element} e
	 */
	toggleCaption: function(e, caption){
		if (e.className == 'togglePlus'){
			e.src = siteBaseUrl + 'css/images/toggleMinus.gif';
			e.className = 'toggleMinus'; 
			//previousSibling  nextSibling
			e.nextSibling.innerHTML = caption;			
		} else {
			e.className = 'togglePlus';
			e.src = siteBaseUrl + 'css/images/togglePlus.gif';
			e.nextSibling.innerHTML = caption.slice(0,26)+ '...';
		}
	},
	fixIECaption : function (e){
		if (document.all && !window.opera && window.XMLHttpRequest){
			e.style.height = 'auto';
		}
	}
};

function processKeyPress(e){
	var e = window.event  || e;
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
}

function popupKeyListener() {document.onkeypress = processKeyPress; }

function getWindowDimensions(){
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

if (window.top.location != window.location) {
	window.top.location.href = window.document.location.href ;
}