/*
	Blackout List Library
	
	This library contains the code for handling list events
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

var classHolder = '';
var selectListItemIds = new Array();

function onListItem(obj,type){
	var classStr = obj.className;
	if(classStr.indexOf('Selected')==-1){
		classHolder = classStr;
		if(type){
			obj.className = 'list list'+type+'On';
		} else {
			obj.className = 'list listOn';
		}
	}
}

function offListItem(obj){
	var classStr = obj.className;
	if(classStr.indexOf('Selected')==-1){
		obj.className = classHolder;
	}
}

function downListItem(obj,type){
	var classStr = obj.className;
	if(classStr.indexOf('Selected')==-1){
		if(type){
			obj.className = 'list list'+type+'Down';
		} else {
			obj.className = 'list listDown';
		}
	}
}

function upListItem(obj,action,type,subtype){
	if(!obj){
		if(subtype){
			deselect(type,subtype);
		} else {
			deselect(type);
		}
		return;
	}
	var classStr = obj.className;
	if(classStr.indexOf('Selected')==-1){
		if(type){
			var extraClassStr = classStr.length > String('list list'+type).length ? classStr.substr(String('list list'+type).length) : '';
			if(subtype){
				deselect(type,subtype);
				selectListItemIds['list'+type+subtype+'Status'] = obj.id;
				obj.className = 'list list'+type+'Selected'+extraClassStr;
			} else {
				deselect(type);
				selectListItemIds['list'+type+'Status'] = obj.id;
				obj.className = 'list list'+type+'Selected'+extraClassStr;
			}
		} else {
			deselect();
			selectListItemIds['listStatus'] = obj.id;
			obj.className = 'list listSelected';
		}
		eval(action);
	}
}

function deselect(type,subtype){
	if(type){
		if(subtype){
			if(obj=document.getElementById(selectListItemIds['list'+type+subtype+'Status'])){
				if(obj.className==obj.getAttribute("deselectedClass")) obj=null;
			}
			if(!obj) if(obj=document.getElementById('selectedListItem'+type+subtype)){ if(obj=document.getElementById(obj.getAttribute('value'))){} }
			if(obj){
				obj.className = obj.getAttribute("deselectedClass");
				selectListItemIds['list'+type+subtype+'Status'] = '';
			}
		} else {
			if(obj=document.getElementById(selectListItemIds['list'+type+'Status'])){
				if(obj.className==obj.getAttribute("deselectedClass")) obj=null;
			}
			if(!obj) if(obj=document.getElementById('selectedListItem'+type)){ if(obj=document.getElementById(obj.getAttribute('value'))){} }
			if(obj){
				obj.className = obj.getAttribute("deselectedClass");
				selectListItemIds['list'+type+'Status'] = '';
			}
		}
	} else {
		if(obj=document.getElementById(selectListItemIds['listStatus'])){
			if(obj.className==obj.getAttribute("deselectedClass")) obj=null;
		}
		if(!obj) if(obj=document.getElementById('selectedListItem')){ if(obj=document.getElementById(obj.getAttribute('value'))){} }
		if(obj){
			obj.className = obj.getAttribute("deselectedClass");
			selectListItemIds['listStatus'] = '';
		}
	}
}

/*
	Stuff to do with widths and heights and positions of page elements
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function updateViewer(){
	// Get window height
	height = getWindowHeight();
	
	// Get height of non-content elements
	var lessHeight = 24; // Top and bottom margins of the content area
	lessHeight += document.getElementById("header") ? document.getElementById("header").offsetHeight : 0;
	lessHeight += document.getElementById("mainmenu")? document.getElementById("mainmenu").offsetHeight : 0;
	lessHeight += document.getElementById("globalMessageHolder") ? document.getElementById("globalMessageHolder").offsetHeight : 0;
	lessHeight += document.getElementById("titleContainer")? document.getElementById("titleContainer").offsetHeight : 0;
	lessHeight += document.getElementById("footerContainer") ? document.getElementById("footerContainer").offsetHeight : 0;
	lessHeight = lessHeight>height ? height : lessHeight;
	
	// Resize content area
	document.getElementById("contentViewer").style.height = (height-lessHeight)+'px';
	if(document.getElementById("sidebarViewer")) document.getElementById("sidebarViewer").style.height = (height-lessHeight)+'px';
	
	// If sidebar adjustments should be made, make them
	if(window.updateViewerPublic) updateViewerPublic();
}

function getWindowHeight(){
	return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
}

function getMousePosition(e){
	if (!e) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	// show the position values in the form named Show
	// in the text fields named MouseX and MouseY
	return {x:tempX,y:tempY};
}

function getElementPosition(obj){
	var x=0,y=0;
	if(obj){
		var objp = obj;
		while(1){
			if(!objp.parentNode){
				break;
			}
			objp = objp.parentNode;
			x -= objp.scrollLeft?objp.scrollLeft:0;
			y -= objp.scrollTop?objp.scrollTop:0;
		}
		if(obj.offsetParent){
			while(1){
				x += obj.offsetLeft;
				y += obj.offsetTop;
				if(!obj.offsetParent){
					break;
				}
				obj = obj.offsetParent;
			}
		} else {
			if(obj.x) x += obj.x;
			if(obj.y) y += obj.y;
		}
	}
	return {x:x,y:y};
}

function getElementWidth(obj){
	return obj.offsetWidth;
}

function getElementHeight(obj){
	return obj.offsetHeight;
}

function getElementNetWidth(obj){
	return obj.offsetWidth-(obj.style.borderLeftWidth&&!isNaN(parseInt(obj.style.borderLeftWidth))?parseInt(obj.style.borderLeftWidth):0)-(obj.style.borderRightWidth&&!isNaN(parseInt(obj.style.borderRightWidth))?parseInt(obj.style.borderRightWidth):0)-(obj.style.paddingLeft&&!isNaN(parseInt(obj.style.paddingLeft))?parseInt(obj.style.paddingLeft):0)-(obj.style.paddingRight&&!isNaN(parseInt(obj.style.paddingRight))?parseInt(obj.style.paddingRight):0);
}

function getElementNetHeight(obj){
	return obj.offsetHeight-(obj.style.borderTopWidth&&!isNaN(parseInt(obj.style.borderTopWidth))?parseInt(obj.style.borderTopWidth):0)-(obj.style.borderBottomWidth&&!isNaN(parseInt(obj.style.borderBottomWidth))?parseInt(obj.style.borderBottomWidth):0)-(obj.style.paddingTop&&!isNaN(parseInt(obj.style.paddingTop))?parseInt(obj.style.paddingTop):0)-(obj.style.paddingBottom&&!isNaN(parseInt(obj.style.paddingBottom))?parseInt(obj.style.paddingBottom):0);
}

/*
	Stuff to do with the page loader, to use this, set $loader to true in PHP at the top of the page
	then run this function when the page has loaded.
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

var loaderstore = '';
function hidePageLoader(){
	document.getElementById('pageLoader').style.display = 'none';
	document.getElementById('contentHolder').style.display = 'block';
	// These two lines are related to IE6 and png fix
	document.getElementById('contentHolder').innerHTML = loaderstore;
	loaderstore = '';
	updateViewer();
	if(window.fixpng) fixpng();
}

/*
	Stuff to do with Advertising
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function hideAds(speed){
	var initialSpeed = 20;
	var friction = 0.8;
	var currentHeight = parseInt(document.getElementById('adsContainer').style.height);
	var finalHeight = document.getElementById('adsBar').offsetHeight;
	var show = document.getElementById('adsShowLink');
	var hide = document.getElementById('adsHideLink');
	
	speed = (speed) ? speed : initialSpeed;
	if(currentHeight<=finalHeight){
		document.getElementById('adsContainer').style.height = finalHeight+'px';
		// Change link
		hide.style.display = 'none';
		show.style.display = 'inline';
	} else {
		document.getElementById('adsContainer').style.height = (currentHeight-speed)+'px';
		updateViewer();
		setTimeout("hideAds("+(speed*friction)+");",20);
	}
}

function showAds(speed){
	var initialSpeed = 20;
	var friction = 0.8;
	var currentHeight = parseInt(document.getElementById('adsContainer').style.height);
	var finalHeight = document.getElementById('adsBar').offsetHeight+document.getElementById('adsContent').offsetHeight;
	var show = document.getElementById('adsShowLink');
	var hide = document.getElementById('adsHideLink');
	
	speed = (speed) ? speed : initialSpeed;
	if(currentHeight>=finalHeight){
		document.getElementById('adsContainer').style.height = finalHeight+'px';
		// Change link
		show.style.display = 'none';
		hide.style.display = 'inline';
	} else {
		document.getElementById('adsContainer').style.height = (currentHeight+speed)+'px';
		updateViewer();
		setTimeout("showAds("+(speed*friction)+");",20);
	}
}

/*
	Stuff to do with shutters
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

shutterelements = Array();
shutterdirections = Array();
shutterdestinations = Array();
shuttercallbacks = Array();
shutterintervals = Array();
shutterindex = 0;
shutterfriction = 0.6;
shuttermindistance = 2;

function startShutter(element,direction,destination,callback){
	shutterelements[shutterindex] = element;
	shutterdirections[shutterindex] = direction;
	shutterdestinations[shutterindex] = destination;
	shuttercallbacks[shutterindex] = callback;
	shutterintervals[shutterindex] = window.setInterval("shutter("+shutterindex+");",5);
	shutterindex++;
}

function shutter(index){
	switch(shutterdirections[index]){
		case 'close up':
		case 'close down':
			current = parseInt(shutterelements[index].style.height);
			if(current<=shutterdestinations[index]){
				shutterelements[index].style.height = shutterdestinations[index]+'px';
				finished = true;
				action = 'close';
			} else {
				distance = Math.round(Math.abs(shutterdestinations[index]-current)*shutterfriction);
				distance = distance<shuttermindistance ? shuttermindistance : distance;
				shutterelements[index].style.height = (current-distance)+'px';
				finished = false;
			}
			break;
		case 'open down':
		case 'open up':
			current = parseInt(shutterelements[index].style.height);
			if(current>=shutterdestinations[index]){
				shutterelements[index].style.height = shutterdestinations[index]+'px';
				finished = true;
				action = 'open';
			} else {
				distance = Math.round(Math.abs(shutterdestinations[index]-current)*shutterfriction);
				distance = distance<shuttermindistance ? shuttermindistance : distance;
				shutterelements[index].style.height = (current+distance)+'px';
				finished = false;
			}
			break;
		case 'close left':
		case 'close right':
			current = parseInt(shutterelements[index].style.width);
			if(current<=shutterdestinations[index]){
				shutterelements[index].style.width = shutterdestinations[index]+'px';
				finished = true;
				action = 'close';
			} else {
				distance = Math.round(Math.abs(shutterdestinations[index]-current)*shutterfriction);
				distance = distance<shuttermindistance ? shuttermindistance : distance;
				shutterelements[index].style.width = (current-distance)+'px';
				finished = false;
			}
			break;
		case 'open right':
		case 'open left':
			current = parseInt(shutterelements[index].style.width);
			if(current>=shutterdestinations[index]){
				shutterelements[index].style.width = shutterdestinations[index]+'px';
				finished = true;
				action = 'open';
			} else {
				distance = Math.round(Math.abs(shutterdestinations[index]-current)*shutterfriction);
				distance = distance<shuttermindistance ? shuttermindistance : distance;
				shutterelements[index].style.width = (current+distance)+'px';
				finished = false;
			}

			break;
		default:
			alert("Unknown shutter direction");
			return;
	}
	if(isMSIE()) updateViewer();
	if(finished){
		window.clearInterval(shutterintervals[index]);
		callback = shuttercallbacks[index];
		callback(action);
	}
}

function headerShutter(action){
	obj = document.getElementById('headerShutterLink');
	if(action=='open'){
		obj.innerHTML = '<a href="javascript:headerShutter(\'hide\')">'+obj.getAttribute('hide')+'</a>';
	} else if(action=='close'){
		if(!isMSIE()) updateViewer();
		obj.innerHTML = '<a href="javascript:headerShutter(\'show\')">'+obj.getAttribute('show')+'</a>';
	} else if(action=='show'){
		obj.innerHTML = obj.getAttribute('show');
		obj = document.getElementById('headerclock');
		obj.style.display = 'block';
		obj = document.getElementById('header');
		if(obj.getAttribute("dev")=="true"){
			if(obj.getAttribute("xmas")=="true"){
				obj.style.background = 'url(../images/page.header.dev.xmas.jpg) top left no-repeat';
			} else {
				obj.style.background = 'url(../images/page.header.dev.jpg) top left no-repeat';
			}
		} else {
			if(obj.getAttribute("xmas")=="true"){
				obj.style.background = 'url(../images/page.header.xmas.jpg) top left no-repeat';
			} else {
				obj.style.background = 'url(../images/page.header.jpg) top left no-repeat';
			}
		}
		getDataReturnText('../servlets/header.php',nothing,nothing,false);
		startShutter(obj,'open down',115,headerShutter);
		if(!isMSIE()){
			obj = document.getElementById('contentViewer');
			currentheight = parseInt(obj.style.height);
			obj.style.height = (currentheight-(115-22))+'px';
			if(document.getElementById("sidebarViewer")) document.getElementById("sidebarViewer").style.height = obj.style.height;
		}
	} else if(action=='hide'){
		obj.innerHTML = obj.getAttribute('hide');
		obj = document.getElementById('headerclock');
		obj.style.display = 'none';
		obj = document.getElementById('header');
		obj.style.background = 'url(../images/page.header.min.jpg) top left no-repeat';
		getDataReturnText('../servlets/header.php?minimized=1',nothing,nothing,false);
		startShutter(obj,'close up',22,headerShutter);
	}
}

function nothing(){}

/*
	Stuff to do with displaying and hiding text or other page elements
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function expand(box,icon,inline){
	if(icon){
		// Big or small
		size = icon.src.indexOf('.small')>=0 ? 'small' : 'big';
		if(box.style.display=='none'){
			box.style.display='block';
			icon.alt = 'Compress';
			icon.src=imageFolder+'images/icon.compress.'+size+'.gif';
		} else {
			box.style.display='none';
			icon.alt = 'Expand';
			icon.src=imageFolder+'images/icon.expand.'+size+'.gif';
		}
	} else {
		if(box.style.display=='none'){
			box.style.display=inline?'inline':'block';
		} else {
			box.style.display='none';
		}
	}
}

function fadeTransition(oldObjID,newObjID){
	$('#'+oldObjID).fadeOut('fast',function(){
		$('#'+newObjID).fadeIn('fast');
	});
}

/*
	Stuff to do with AJAX
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function parseForKeyValuePairs(str){
	if(str&&str.length>0){
		var pairsObj = new Object();
		var pairs = new Array();
		var keyvalue = new Array();
		if(str.substr(0,1)=='&') str = str.substr(1);
		pairs = str.split('&');
		for(i=0;i<pairs.length;i++){
			keyvalue = pairs[i].split('=');
			if(keyvalue.length!=2) {
				alert("Key or Value missing.\n\nInitial string => "+str);
				return false;
			} else {
				eval("pairsObj."+keyvalue[0]+" = '"+keyvalue[1].replace(/'/g,"\\'")+"';");
			}
		}
		return pairsObj;
	}
	return false;
}

function executeEmbeddedScripts(node){
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;
	
  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
	var scripts = new Array();
	
	for(i in st){
		if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }
		
		if(strExec!=null&&strExec!="") scripts.push(strExec);
	}
	
	for(i in scripts){
		strExec = scripts[i];
		
		// Remove html comment syntax
		strExec = strExec.replace("<!--","");
		strExec = strExec.replace("//-->","");
		strExec = strExec.replace("// -->","");
		
    try {
      var x = document.createElement("script");
      x.type = "text/javascript";
			
			if(strExec){
				/* In IE we must use .text! */
				if ((bSaf) || (bOpera) || (bMoz))
					x.innerHTML = strExec;
				else x.text = strExec;
			} else {
				x.src = strSrc;
			}
			
      document.getElementsByTagName("head")[0].appendChild(x);
    } catch(e) {
      alert("Script execution error: "+e);
    }
  }
}

function getNextScriptChar(str,startingpos,char){
	var insideDoubleQuote = false;
	var insideSingleQuote = false;
	var escaped = false;
	for(j=startingpos;j<str.length;j++){
		
		if(str.charAt(j)==char&&!insideDoubleQuote&&!insideSingleQuote){ // Look for the char
			return j;
		}
		
		if(str.charAt(j)=="'"&&!escaped&&insideSingleQuote){ // Look for closing single quote
			insideSingleQuote = false;
		} else if(str.charAt(j)=="'"&&!escaped&&!insideDoubleQuote){ // Look for opening single quotes
			insideSingleQuote = true;
		}
		
		if(str.charAt(j)=='"'&&!escaped&&insideDoubleQuote){ // Look for closing double quote
			insideDoubleQuote = false;
		} else if(str.charAt(j)=='"'&&!escaped&&!insideSingleQuote){ // Look for opening double quotes
			insideDoubleQuote = true;
		}
		
		if(str.charAt(j)=='\\'&&!escaped){ // Look for escape
			escaped = true;
		} else {
			escaped = false;
		}
		
	}
	return -1;
}

/*
	Stuff which can be used almost anywhere
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function isMSIE(){
	var userAgent = navigator.userAgent.toLowerCase();
	return document.all && userAgent.indexOf('msie')!=-1 ? true : false;
}

function getElementByAttributeValue(att,val){
	var allElements = document.getElementsByTagName('*');
	for(a=0;a<allElements.length;a++){
		if(allElements[a].getAttribute(att)&&allElements[a].getAttribute(att)==val) return allElements[a];
	}
	return false;
}

function belongsToClass(obj,targetcls,alerts){
	while(1){
		cls = obj.className?obj.className:(obj.getAttribute?obj.getAttribute('className'):"");
		if(cls){
			if(targetcls.substr(0,1)=='%'){
				tmpcls = targetcls.substr(1);
				if(cls.substr(cls.length-tmpcls.length)==tmpcls||cls.indexOf(tmpcls+" ")>=0){
					return obj;
				}
			} else if(targetcls.substr(targetcls.length-1)=='%'&&targetcls.substr(targetcls.length-2,1)!='\\'){
				tmpcls = targetcls.substr(0,targetcls.length-1);
				if(cls.substr(0,tmpcls.length)==tmpcls||cls.indexOf(" "+tmpcls)>=0){
					return obj;
				}
			} else {
				if(cls==targetcls||cls.substr(0,5)==targetcls+" "||cls.substr(cls.length-5)==" "+targetcls||cls.indexOf(" "+targetcls+" ")>=0){
					return obj;
				}
			}
		}
		if(!obj.parentNode){
			break;
		}
		obj = obj.parentNode;
	}
	return false;
}

// By Richard Rutter
// http://clagnut.com/sandbox/imagefades/
function setOpacity(obj,opacity) {
  //opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter = "alpha(opacity:"+opacity+")";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
}

function removeOpacity(obj){
	obj.style.filter = "";
  obj.style.KHTMLOpacity = "";
  obj.style.MozOpacity = "";
  obj.style.opacity = "";
}

/*
	HTML menu stuff
	
	Copyright (C) 2007 Jeremy Bell
	jeremy@blackoutwebdesign.com
	Blackout Entertainment Limited
*/

function changeMenu(label){
	if(label!='me') deselectMenu('me');
	if(label!='club') deselectMenu('club');
	if(label!='global') deselectMenu('global');
	if(label!='tools') deselectMenu('tools');
	if(label!='help') deselectMenu('help');
	buttonwidth = label=='help' ? 153 : (label=='global' ? 155 : 154);
	obj = document.getElementById('menu'+label);
	obj.innerHTML = '<img src="../images/menu/'+label+'.jpg" alt="" width="'+buttonwidth+'" height="24" border="0" />';
	objc = document.getElementById('menu'+label+'content');
	objc.style.display = 'block';
}

function deselectMenu(label){
	buttonwidth = label=='help' ? 153 : (label=='global' ? 155 : 154);
	obj = document.getElementById('menu'+label);
	obj.innerHTML = '<a href="javascript:changeMenu(\''+label+'\');"><img src="../images/menu/'+label+'.off.jpg" alt="" width="'+buttonwidth+'" height="24" border="0" onmouseover="javascript:swapImage(this,\'../images/menu/'+label+'.on.jpg\');" onmouseout="javascript:swapImage(this,\'../images/menu/'+label+'.off.jpg\');" /></a>';
	objc = document.getElementById('menu'+label+'content');
	objc.style.display = 'none';
}

function escapeBlackout(str){
	str = encodeURIComponent(str);
	return str;
}

/*
	XML Updates
*/
var xmlUpdateInProgress=false;
var xmlUpdateIntervalID=false;

$('document').ready(function(){
	xmlUpdate();
	xmlUpdateIntervalID = window.setInterval('xmlUpdate();',60000);
});

function xmlUpdate(force){
	if(!xmlUpdateInProgress||force){
		xmlUpdateInProgress = true;
		getDataReturnXml('../servlets/newmail.php', xmlUpdateResponse, xmlUpdateError, false, null, null, null, true);
	}
}
function xmlUpdateResponse(x){
	if(x.firstChild){
		newmail = parseInt(x.getElementsByTagName('newmail')[0].firstChild.nodeValue);
		if(newmail>0){
			title = messages.youHaveXNewMessages;
			title = title.replace('[number_of_unread_messages]',newmail);
			title = title.replace('[messages]',(newmail==1?messages.messageWord:messages.messagesWord));
			$('#newmail_Img').attr('title',title);
			$('#newmail_Btn').css('display','block');
		} else {
			$('#newmail_Btn').css('display','none');
			if(newmail==-1){
				window.clearInterval(xmlUpdateIntervalID);
			}
		}
	}
	xmlUpdateInProgress = false;
}
function xmlUpdateError(){
	xmlUpdateInProgress = false;
}

/*
	Commentary related functions
*/
function fixTeamName(name){
	if(name.length>=4){
		name = trim(name);
		str = strtolower(name.substr(0,4));
		if(str=='the ' || str=='teh '){
			name = name.substr(4);
		}
	}
	return ucwords(name);
}

/*
	Translation related functions
*/

var languageClickHander = function(e) {
	if($(e.target).closest('#languagesDropdown').length == 0) {
		hideLanguagesDropdown();
		$('body').unbind('click',languageClickHander);
	}
}
function toggleLanguagesDropdown(){
	if($('#languagesDropdown').css('display')=='none'){
		showLanguagesDropdown();
	} else {
		hideLanguagesDropdown();
	}
}
function showLanguagesDropdown(){
	var iconPosition = $('#currentLanguageIcon').position();
	$('#languagesDropdown').css('top',(iconPosition.top+11)+'px');
	$('#languagesDropdown').css('left',(iconPosition.left-95)+'px');
	$('#languagesDropdown').stop(true).fadeTo('fast',1);
	var middleHeight = $('#languagesContent').height()-50;
	$('#languagesMiddle').height(middleHeight);
	$('body').bind('click',languageClickHander);
}
function hideLanguagesDropdown(){
	$('#languagesDropdown').stop(true).fadeOut('fast');
}


/*
php.js functions (http://phpjs.org)
*/
function ucwords (str) {
	return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
		return $1.toUpperCase();
	});
}
function trim (str, charlist) {
	var whitespace, l = 0, i = 0;
	str += '';
	
	if (!charlist) {
		// default list
		whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
	} else {
		// preg_quote custom list
		charlist += '';
		whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
	}
	
	l = str.length;
	for (i = 0; i < l; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}
	
	l = str.length;
	for (i = l - 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 strtolower (str) {
   return (str+'').toLowerCase();
}

/*
Floating content windows
*/
var floatingWindowWidth = 0;
var floatingWindowHeight = 0;
var floatingWindowBorderWidth = 0;
function openFloatingWindow(w,h,bw){
	$('body').append('<div style="background-color:#000; position:absolute; top:0; left:0; z-index:97;" id="floatingWindowBG"></div><div style="background-color:#000; position:absolute; top:0; left:0; display:none; z-index:98; width:'+(w+bw*2)+'px; height:'+(h+bw*2)+'px;" id="floatingWindowBorder"></div><div style="background:url(../images/languages.bg.png) repeat-x 0px '+(h-115)+'px #FFF; position:absolute; z-index:99; width:'+w+'px; height:'+h+'px;" id="floatingWindow"></div><div style="position:absolute; z-index:100; width:16px; height:16px;" id="floatingWindowCloseButton"><a href="javascript:closeFloatingWindow();"><img src="../images/close.png" width="16" height="16" border="0" alt="Close" /></a></div>');
	floatingWindowWidth = w;
	floatingWindowHeight = h;
	floatingWindowBorderWidth = bw;
	$('#floatingWindowBG').fadeTo(0,0.6);
	$('#floatingWindowBorder').fadeTo(0,0.3);
	updateFloatingWindow();
	$(window).bind('resize',updateFloatingWindow);
	$('body').bind('click',closeFloatingWindow);
	return $('#floatingWindow');
}

function updateFloatingWindow(){
	w = floatingWindowWidth;
	h = floatingWindowHeight;
	bw = floatingWindowBorderWidth;
	// Determine position and height
	var oldOverflow = $('body').css('overflow');
	$('body').css('overflow','hidden');
	var ww = $(window).width();
	var wh = $(window).height();
	$('body').css('overflow',oldOverflow);
	var l = ww/2 - w/2;
	var t = wh/2 - h/2;
	
	// Resize darkened background
	$('#floatingWindowBG').css('width',ww+'px');
	$('#floatingWindowBG').css('height',wh+'px');

	// Move darkened border
	$('#floatingWindowBorder').css('top',(t-bw)+'px');
	$('#floatingWindowBorder').css('left',(l-bw)+'px');
	
	// Move lightbox to a more centered location
	$('#floatingWindow').css('left',l);
	$('#floatingWindow').css('top',t);
	
	// Move close button
	$('#floatingWindowCloseButton').css('top',(t+7)+'px');
	$('#floatingWindowCloseButton').css('left',(l+w-23)+'px');
}

function closeFloatingWindow(e){
	if(!e || $(e.target).closest('#floatingWindow').length == 0) {
		$('#floatingWindowBG').remove();
		$('#floatingWindowBorder').remove();
		$('#floatingWindow').remove();
		$('#floatingWindowCloseButton').remove();
	}
}

