var WBM_global = 
{
	addEvent: function(elm, evType, fn, useCapture)
	// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
	// By Scott Andrew
	{
		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;
		}
	}, 

	getFunction: function(obj, func)
	{
		var _f = obj + "." + func;
		
		_f += "(e";

		for(var i = 2; i < arguments.length; i++)
		{
			_f += ",\"" + arguments[i] + "\"";
		}
		
		_f += ")";
		
		return function(e) { eval(_f); }
	}
}

var WBM_Menu = 
{	
	init: function(e)
	{
		if(!document.getElementById || !document.getElementsByTagName)
			return;
		
		_menu = document.getElementById("nav");
		if(_menu)
		{
			for(var i = 0; i < _menu.childNodes.length; ++i)
			{
				if(_menu.childNodes[i].nodeName.toLowerCase() == "li")
				{
					for(var j = 0; j < _menu.childNodes[i].childNodes.length; ++j)
					{
						if(_menu.childNodes[i].childNodes[j].nodeName.toLowerCase() == "a")
						{
							WBM_global.addEvent(_menu.childNodes[i].childNodes[j], 'click', WBM_Menu.getFunction(_menu.childNodes[i].childNodes[j], "click"), false);
						}
					}
				}
			}
		}
	}, 

	getFunction: function(target, func)
	{
		return function(e){ eval("WBM_Menu." + func)(e, target) }
	}, 
	
	toggle: function(targetElement)
	{
		if(targetElement && targetElement.parentNode)
		{
			for(var i = 0; i < targetElement.parentNode.childNodes.length; ++i)
			{
				if(targetElement.parentNode.childNodes[i].nodeName.toLowerCase() == "ul" && targetElement.parentNode.childNodes[i].className == "sub-nav")
				{
					if(targetElement.parentNode.childNodes[i].style.display == "none")
					{
						targetElement.parentNode.childNodes[i].style.display = "";
					}
					else
					{
						targetElement.parentNode.childNodes[i].style.display = "none";
					}

					for(var j = 0; j < targetElement.parentNode.childNodes[i].childNodes.length; ++j)
					{
						if(targetElement.parentNode.childNodes[i].childNodes[j].nodeName.toLowerCase() == "li")
						{
							for(var k = 0; k < targetElement.parentNode.childNodes[i].childNodes[j].childNodes.length; ++k)
							{
								if(targetElement.parentNode.childNodes[i].childNodes[j].childNodes[k].nodeName.toLowerCase() == "a")
								{
									WBM_global.addEvent(targetElement.parentNode.childNodes[i].childNodes[j].childNodes[k], 'click', WBM_Menu.getFunction(targetElement.parentNode.childNodes[i].childNodes[j].childNodes[k], "click"), false);
								}
							}
						}
					}
				}
			}
		}
		
	}, 
	
	click: function(e, targetElement)
	{
		var el = window.event ? targetElement : e ? e.currentTarget : null;
		if (!el) return;
		
		var _id = "";
		if(el.href != "#")
		{
			if(el.href)
			{
				var _obj1 = parseUrl(el.href);
				
				if(_obj1.file)
				{
					var _obj2 = xtractFile(_obj1.file);
					_id = _obj2.filename;
				}
			}
		}

		WBM_Menu.toggle(el);
		
		if(_id)
		{
			WBM_displayPageContent(_id);
		}
		
		if(window.event)
		{
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault)
		{
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

function WBM_displayPageContent(_page)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			WBM_toggleCenterBox("open");
			
			var nEntity = obj.responseXML.getElementsByTagName("entity");
			
			for(var i = 0; i < nEntity.length; i++)
			{
				var _contentHTML = nEntity.item(i).getAttribute("contentHTML").toString();
				var _titleHTML = nEntity.item(i).getAttribute("titleHTML").toString();
				var _descriptionHTML = nEntity.item(i).getAttribute("descriptionHTML").toString();
				var _keywordsHTML = nEntity.item(i).getAttribute("keywordsHTML").toString();
			}
			
			document.title = _titleHTML;
			
			if(document.getElementsByTagName)
			{
				var _arr_meta = document.getElementsByTagName("meta");
				for(var i = 0; i < _arr_meta.length; i++)
				{
					if(_arr_meta[i] && _arr_meta[i].name == "title")
					{
						_arr_meta[i].content = _titleHTML;
					}
					if(_arr_meta[i] && _arr_meta[i].name == "description")
					{
						_arr_meta[i].content = _descriptionHTML;
					}
					if(_arr_meta[i] && _arr_meta[i].name == "keywords")
					{
						_arr_meta[i].content = _keywordsHTML;
					}
				}
			}
			
			if(document.getElementById('page-content'))
			{
				document.getElementById("page-content").innerHTML = _contentHTML;
				initScrollbars();
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}

	if(_page == "send_friend") 
	{
		var currentLocation64 = Base64.encode(currentLocation);
		WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/misc_action.php?action=display_page_content&page=' + _page + '&hash=' + currentLocation64, callback);
	}
	else
	{

		currentLocation = "http://www.blackglass.com.au/" + _page + ".php";	
		WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/misc_action.php?action=display_page_content&page=' + _page, callback);
	}
}


function WBM_displayEventShortDescription(_event_id)
{

	
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('left-event-content'))
			{
				document.getElementById("left-event-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}

			setTimeout('', 3000);

		},
		failure : function(obj){ }
	}

	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/event_action.php?action=display_left_event_details&event_id=' + _event_id, callback);



	
}

function WBM_displayEventLongDescription(_event_id)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('event-details-content'))
			{
				document.getElementById("event-details-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}
	currentLocation = "http://www.blackglass.com.au/news_articles.php?event_id=" + _event_id;
	
	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/event_action.php?action=display_event_details&event_id=' + _event_id, callback);
}

function WBM_displayEvent(_event_id)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}
	
	var _page = "news_articles_details";

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			WBM_toggleCenterBox("open");
			
			var nEntity = obj.responseXML.getElementsByTagName("entity");
			
			for(var i = 0; i < nEntity.length; i++)
			{
				var _contentHTML = nEntity.item(i).getAttribute("contentHTML").toString();
				var _titleHTML = nEntity.item(i).getAttribute("titleHTML").toString();
				var _descriptionHTML = nEntity.item(i).getAttribute("descriptionHTML").toString();
				var _keywordsHTML = nEntity.item(i).getAttribute("keywordsHTML").toString();
			}
			
			document.title = _titleHTML;
			
			if(document.getElementsByTagName)
			{
				var _arr_meta = document.getElementsByTagName("meta");
				for(var i = 0; i < _arr_meta.length; i++)
				{
					if(_arr_meta[i] && _arr_meta[i].name == "title")
					{
						_arr_meta[i].content = _titleHTML;
					}
					if(_arr_meta[i] && _arr_meta[i].name == "description")
					{
						_arr_meta[i].content = _descriptionHTML;
					}
					if(_arr_meta[i] && _arr_meta[i].name == "keywords")
					{
						_arr_meta[i].content = _keywordsHTML;
					}
				}
			}
			
			if(document.getElementById('page-content'))
			{
				document.getElementById("page-content").innerHTML = _contentHTML;
				initScrollbars();
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}

	currentLocation = "http://www.blackglass.com.au/news_articles_details.php?event_id=" + _event_id;

	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/misc_action.php?action=display_page_content&page=' + _page + '&event_id=' + _event_id, callback);
}

function WBM_displayEventList(_start)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('event-list-content'))
			{
				document.getElementById("event-list-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}
	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/event_action.php?action=display_event_list&start=' + _start, callback);
}

function WBM_displayMemberDetails(_member_id, _ord)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('team-content'))
			{
				document.getElementById("team-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}

	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/team_action.php?action=display_team_details&member_id=' + _member_id + '&ord=' + _ord, callback);
}


function WBM_displayCareerDetails(_career_id, _ord)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('career-content'))
			{
				document.getElementById("career-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}

	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/career_action.php?action=display_career_details&career_id=' + _career_id , callback);
}





function WBM_displayCaseStudyDetails(_case_study_id, _ord)
{
	if(document.getElementById('loading-screen'))
	{
		document.getElementById('loading-screen').style.display = '';
	}

	// get HTML
	var callback =
	{
		success : function(obj)
		{
			if(document.getElementById('case-study-content'))
			{
				document.getElementById("case-study-content").innerHTML = obj.responseText;
			}
			if(document.getElementById('loading-screen'))
			{
				document.getElementById('loading-screen').style.display = 'none';
			}
		},
		failure : function(obj){ }
	}

	currentLocation = "http://www.blackglass.com.au/case_studies_details.php?case_study_id=" + _case_study_id;
	WBM_XHR.asyncRequest('GET', 'http://www.blackglass.com.au/public_panel/includes/case_study_action.php?action=display_case_studies_details&case_study_id=' + _case_study_id + '&ord=' + _ord, callback);
}

function WBM_toggleCenterBox(_action)
{
	if(document.getElementById('center-box'))
	{
		if(_action == "close")
		{
			document.getElementById('center-box').style.display = "none";
		}
		else
		{
			document.getElementById('center-box').style.display = "";
		}
	}
}

function parseUrl(data)
{
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

    if (data.match(e))
    {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else
    {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}

function xtractFile(data)
{
    data = data.replace(/^\s|\s$/g, ""); //trims string

    if (data.match(/([^\/\\]+)\.(asp|html|htm|shtml|php)$/i) )
        return {filename: RegExp.$1, ext: RegExp.$2};
    else
        return {filename: "", ext: null};
}

WBM_global.addEvent(window, 'load', WBM_Menu.init, false);

function fix_bg() {
	var bg = document.getElementById('background');
	//alert(document.body.scrollTop);
	//alert(navigator.userAgent);
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		//alert(document.documentElement.scrollTop);
		bg.style.top = document.documentElement.scrollTop+"px";
	} else {
		bg.style.top = window.pageYOffset+"px";
	}
	
}



WBM_global.addEvent(window, 'scroll', fix_bg, false);


function WBM_popupWindow(url, PageWidth, PageHeight, _scrollbars, _status)
{
	var popleft = ((document.body.clientWidth - PageWidth) / 2) + window.screenLeft;
	var poptop = ((( document.body.clientHeight - PageHeight) / 2)) + window.screenTop-40;
		
	str = "" + Math.random() + "";
	_unique_id = str.substr(2, str.length);
		
	window.open(url, _unique_id ,"status=" + _status + ",resizable=1,scrollbars=" + _scrollbars + ",width=" + PageWidth + ",height=" + PageHeight + ",left=" + popleft + ",top=" + poptop);
}





/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}