isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
isWindows = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;

// No Active dialogs when site loads so set to nothing
activedialog = "";
dialogpassthru = "";

function isArabic() {
	u = window.location.toString();
	if(u.indexOf('arabic') == -1) {
		return false;
	} else {
		return true;
	}
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie= c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function findPosition(obj) {
	var curLeft = curTop = 0;
	if(obj.offsetParent) {
		curLeft = obj.offsetLeft;
		curTop = obj.offsetTop;
		
		while(obj = obj.offsetParent) {
			curLeft += obj.offsetLeft;
			curTop += obj.offsetTop;
		}
	}
	return [curLeft,curTop];
}

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

function bodyClickListener() {
	if(!document.getElementById) return false;
	om = document.getElementById("optionsmenu");
	if(om.style.display == "block") {
		om.style.display = "none";
	}
	document.onclick = null;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function emailprompt() {
	if(!document.getElementById) return false;
	
	p = document.getElementById('regPromptHolder');
	p.style.display = 'block';
	
	for(i = 0; i <= 10; i++) {
		setTimeout("setOpacity('regPromptHolder'," + i + ")",i*20);
	}
}

function setOpacity(obj,val) {
	if(!document.getElementById) return false;
	
	obj = document.getElementById(obj);

	obj.style.filter = 'alpha(opacity=' + val*10 + ')';
	obj.style.MozOpacity = val/10;
	obj.style.opacity = val/10;
}

function addSHPL() {
	if(!document.getElementById("mylinks")) return false;
	
	var obj = document.getElementById("mylinks");
	var liNode = document.createElement('li');
	var aNode = document.createElement('a');
	
	aNode.setAttribute("href","Javascript:void(0);");
	
	en = "Make ADNEC your homepage";
	ar = "إجعل أدنيك صفحتك الرئيسية";
	
	if(!isArabic()) {
		aNode.innerHTML = en;
	} else {
		aNode.innerHTML = ar;
	}

	aNode.setAttribute("title","Set your homepage to www.adnec.ae");
	
	if(isIE) {
		aNode.onclick = function() {
			this.style.behavior='url(#default#homepage)';
			this.setHomePage('http://www.adnec.ae');
		}
	} else {
		aNode.setAttribute("onclick","alert('To set your browsers homepage please use the options menu in your browser')");
	}
	
	liNode.appendChild(aNode);
	
	//Example Link
	//<li><a href="" title="Set your homepage to www.adnec.ae">Make ADNEC your homepage</a></li>

	//obj.appendChild(liNode);
	obj.insertBefore(liNode,document.getElementById('nlsub'));
}

// Do specific things depending on page
function addPageFunctions() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	
	bodyObj = document.getElementsByTagName('body');
	bodyID = bodyObj[0].id.toString();
	
	// What's On function
	if(bodyID.indexOf('whatson') > -1) {
		// Now find the print button and alter it.
		po = document.getElementById('printlink');
		
		if(po) {
			po_link = po.href;
		
			if(isWindows) {
				po.href = "javascript:window.print()";
			} else {
				po.href = "javascript:window.open('" + po_link + "','printwin','width=770,height=500,menubar=yes'); void(0);";
			}
		}
	}
}

function $(o) {
	if(!document.getElementById) return false;
	return document.getElementById(o);
}

// Dialog Functions

function getDialogError() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	dialog = $(activedialog);
	dialog_divs = dialog.getElementsByTagName('div');
	error_div = "";
	
	for(i = 0; i < dialog_divs.length; i++) {
		if(dialog_divs[i].className == "dialog_error") {
			error_div = dialog_divs[i];
		}
	}
	
	if(error_div == "") {
		return false;
	} else {
		return error_div;
	}
}

function errorDialog(sError) {
	errorObj = getDialogError();
	
	if(!errorObj) {
		return false;
	} else {
		// Good, now put something in it.
		errorObj.innerHTML = sError;
		errorObj.style.display = 'block';
	}
}

function clearDialogError() {
	errorObj = getDialogError();
	
	if(!errorObj) {
		return false;
	} else {
		// Good, now put something in it.
		errorObj.innerHTML = "";
		errorObj.style.display = 'none';
	}
}

function cancelDialog() {
	if(!document.getElementById) return false;
	
	if(activedialog != "") {
		dialog = $(activedialog);
		dialog.style.display = 'none';
		
		clearDialogError();
		activedialog = "";
		dialogpassthru = "";
	} else {
		return false;
	}
}

function confirmDialog() {
	if(!document.getElementById) return false;
	
	if(activedialog != "") {
		eval(activedialog + "_confirm()");
	} else {
		return false;
	}
}

function showDialog(d,passthru) {
	if(!document.getElementById) return false;
	
	if(activedialog != "") {
		cancelDialog();
	}

	dialog = $(d);
	dialogpassthru = passthru;
	activedialog = d;
	
	if(dialog.style.display == '' || dialog.style.display == 'none') {
		dialog.style.display = 'block';
	}
}

function vcalendar_confirm() {
	eventID = dialogpassthru;
	window.location = "vcalendar.php?id=" + eventID;	
	cancelDialog();
}

function checkEmail(e) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(e);
}


function sendtoemail_confirm() {
	if(!document.getElementById) return false;
	
	oForm = $('ste_form');
	
	name = oForm.your_name.value;
	email_to = oForm.sendto_email.value;
	
	if(name == "") {
		errorDialog('Please enter your name');
		return false;
	}
	else if(email_to == "") {
		errorDialog('Please enter an email address');
		return false;
	}
	else if(!checkEmail(email_to)) {
		errorDialog('Please enter a valid email address');
		return false;
	}
	
	oForm.submit();
}

function confirmlink_confirm() {
 	websitelink = dialogpassthru;
	window.location = websitelink;	
	cancelDialog();
}

function convertLinks() {
	if(!document.getElementsByTagName || !document.createElement || !document.createTextNode) return false;
	// load the A tags
	
	var aTags = document.getElementsByTagName("a");
	//return if no A tags on page
	if(aTags.length < 1) return false;
	
	for(var i = 0; i < aTags.length; i++) {
		var curA = aTags[i];

		cn = (curA.getAttribute("className") != null) ? curA.getAttribute("className") : curA.getAttribute("class");

		switch(cn) {
			case "external":
				curA.setAttribute("target","_blank");
				curA.target = "_blank";
				break;
			case "gallery_link":
				curLink = curA.href;
				curA.href = "javascript:showImage('" + curLink + "');"
				break;
		}
	}
}

// Galleries
available_galleries = Array();

function showImage(s) {
	if(!document.getElementById) return false;
	
	var big_image = $('gallery_big_image');
	big_image.innerHTML = "<img src=\"" + s + "\" /><div class=\"image_bar\"><a href=\"javascript:hideBigImage();\">Hide Image</a></div>";
	
	big_image.style.display = "block";
}

function hideBigImage() {
	if(!document.getElementById) return false;

	var big_image = $('gallery_big_image');
	big_image.style.display = "none";
}

function gallery_init() {
	if(!document.getElementById || !document.getElementsByTagName) return false;
	
	navUL = document.createElement('ul');
	navUL.id = "gallery_selector";
	
	// Gallery DIV
	g = document.getElementById('gallery');
	divs = g.getElementsByTagName('div');
	for(i = 0; i < divs.length; i++) {
		active = (i == 0) ? 1 : 0;
		display = (i == 0) ? "block" : "none";
		available_galleries.push({id: divs[i].id, active: active});
		divs[i].style.display = display;
		
		init_css_style = (i == 0) ? " style=\"background-color: #fff;\"" : "";
		
		navLI = document.createElement('li');
		navLI.innerHTML = "<a href=\"javascript:toggle_gallery('" + divs[i].id + "')\" id=\"" + divs[i].id + "_link\"" + init_css_style + ">" + divs[i].title + "</a>";
		navUL.appendChild(navLI);
	}

	g.insertBefore(navUL,document.getElementById(available_galleries[0].id));
}

function toggle_gallery(gID) {
	if(!document.getElementById) return false;
	
	ag = available_galleries;
	for(i = 0; i < ag.length; i++) {
		if(gID == ag[i].id) {
			ag[i].active = 1;
			document.getElementById(ag[i].id + "_link").style.backgroundColor = "#FFF";
			document.getElementById(ag[i].id).style.display = "block";
		} else {
			if(ag[i].active) {
				ag[i].active = 0;
				document.getElementById(ag[i].id + "_link").style.backgroundColor = "#EEE";
				document.getElementById(ag[i].id).style.display = "none";
			}
		}
	}
}

function move_gallery(galleryID,dir) {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	
	//Image Width
	iWidth = 120; // Pixels wide.
	
	g = document.getElementById(galleryID);
	ph = g.getElementsByTagName('ul')[0];
	
	if(!g) return false;
	if(!ph) return false;
			
	if(!g.curIndex) {
		g.curIndex = 0;
	}
	
	if(ph.style.left == "" && ph.style.top == "") {
		ph.style.left = ph.style.top = 0;
	}
	
	//Find out how many pictures present.
	totalPics = g.getElementsByTagName('li').length - 1;

	if(totalPics == 0) return false;
	
	if(dir == "prev") {
		nextpic = g.curIndex - 1;
		if(nextpic < 0) {
			nextpic = 0;
		}
		stylePos = -(nextpic * iWidth) + "px";
		g.curIndex = nextpic;
		ph.style.left = stylePos;
	} else {
		prevpic = g.curIndex + 1;
		if(prevpic > totalPics) {
			prevpic = totalPics;
		}
		stylePos = -(prevpic * iWidth) + "px";
		g.curIndex = prevpic;
		ph.style.left = stylePos;
	}
}

function showMailing(path) {
	window.open('viewEmail.php?path='+path,'emailwin','width=600,height=500,scrollbars=yes,menubar=yes');
}


rotation_text = Array(
	'International Venue of the Year (AEO Awards 2010)',
	'Best International Venue (Exhibition News Awards 2010)',
	'Green Award (Middle East Event Awards 2010)',
	'Best International Venue (CONFEX Awards 2009)',
	'Best Venue (Middle East Event Awards 2009)',
	'Middle East\'s Leading Exhibition Centre (World Travel Awards 2009)',
	'Future Commercial & Retail Development Award (Cityscape Awards 2009)',
	'Best Exhibition - for IDEX 2009 (Middle East Event Awards 2009)',
	'Construction Project of the Year (Construct Magazine 2008)'
);

rotation_text_arabic = Array(
	'جائزة &quot;أفضل مركز    دولي للعام&quot; (جوائز إتحاد    منظمي المعارض 2010)',
	'جائزة &quot;أفضل مركز    دولي&quot; (جوائز    اكزيبشن نيوز 2010)',
	'&quot;الجائزة الخضراء&quot; (جوائز فعاليات    الشرق الأوسط 2010)',
	'جائزة &quot;أفضل مركز    عالمي&quot; (جوائز كونفكس    2009)',
	'جائزة &quot;أفضل مركز للفعاليات&quot; (جوائز فعاليات الشرق الأوسط 2009)',
	'جائزة &quot;أفضل معرض&quot;    لآيدكس 2009 (جوائز فعاليات الشرق الأوسط 2009)',
	'جائزة &quot;مركز    المعارض الرائد في الشرق الأوسط&quot; (جوائز السفر    العالمية 2009)',
	'جائزة &quot;أفضل مشروع    تجاري مستقبلي&quot; (جوائز سيتي سكيب 2009)',
	'جائزة &quot;أفضل مشروع تطويري&quot; (جوائز مجلة كونستركشن ويك 2008)'
);

rotation_pointer = 0;

function rotateH2() {
	if(!document.getElementById) return false;
	ar = document.getElementById('AwardRotater');
	
	rotation_array = (LANG == "arabic") ? rotation_text_arabic : rotation_text;
	
	holder = rotation_array[rotation_pointer].split(" (");
	award = holder[0];
	awarded_by = holder[1].replace(")","");
	
	ar.innerHTML = "<em>" + award + "<br /><span>" + awarded_by + "</span></em>";
	
	rotation_pointer++;
	
	if(rotation_pointer == rotation_array.length) {
		rotation_pointer = 0;
	}
	
	setTimeout('rotateH2()',5000);
}
		
addLoadEvent(createSearchOptions);
addLoadEvent(convertLinks);
addLoadEvent(applyToolTips);
addLoadEvent(addSHPL);
addLoadEvent(addPageFunctions);