function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", hideFormText, false);
else if (window.attachEvent)
	window.attachEvent("onload", hideFormText);
	
var transparentImage = "images/none.gif";

function fixTrans()
{
	if (typeof document.body.style.maxHeight == 'undefined') {

	var imgs = document.getElementsByTagName("img");
	
	for (i = 0; i < imgs.length; i++)
	{	
		if (imgs[i].src.indexOf(transparentImage) != -1)
		{
			return;
		}

		if (imgs[i].src.indexOf(".png") != -1)
			{
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "block";
			}
		}	
	}
}

if (document.all && !window.opera)
	attachEvent("onload", fixTrans);

var myTextExtraction = function(node) {
	//console.log(node.innerHTML);
	var sorter_handle = $(node).find(".sorter-handle");
	if(sorter_handle.length > 0){
		//console.log($(sorter_handle[0]).text());
		return $(sorter_handle[0]).text();
	}
	return node.innerHTML;
}

var table_backup = '';
var default_page_length = 10;
var all_items_in_table = 0;
function initNewsTable(){
    var all_items = $('h2 a');
    var sort_column = 0;
    var direction = 0;
        direction = 1;
        $('#greed').tablesorter({
                //sortList: [[sort_column,direction]],
                sortList:[],
		textExtraction: myTextExtraction
        }).tablesorterPager({
                container: $("#paging"),
                size: all_items.length
        });

        var hash = (window.location.hash);
        window.location.hash = '';
    
        var target_index = 0;
        $('h2 a').each(function(i,e){
            e = $(e);
            if(e.attr('name') == hash.substr(1)){
                target_index = i;
            }
        });
        var target_page = parseInt((target_index)/default_page_length);
        //console.log(target_page);
        $('#greed').trigger('destroy'); //destory table, ignore pager
		
		$('#greed').html(table_backup);
		//remove items no in the filter
			//rebuild table with pager	
		$('#greed').tablesorter({
                //sortList: [[sort_column,direction]],
            sortList:[],
			textExtraction: myTextExtraction
		}).tablesorterPager({
                	container: $("#paging"),
	                size: default_page_length
        	});
        //console.log($($('#paging li a')[target_page+1]));
        $($('#paging li a')[target_page+1]).trigger('click');
        
        window.location.hash = hash;

}
function initTableSorter(){
	table_backup = $('#greed').html();
    if($('#greed').hasClass("table-news")){
        initNewsTable();
        return;
    }
	all_items_in_table = $('#greed tbody tr').length;
	//console.log(all_items_in_table);
    var sort_column = 0;
    var direction = 0;
    if($('#greed').hasClass("table-events")){
        sort_column = 3;
        direction = 1;
    }
    else if($('#greed').hasClass("table-news")){
        direction = 1;
    }
        $('#greed').tablesorter({
                sortList: [[sort_column,direction]],
		textExtraction: myTextExtraction
        }).tablesorterPager({
                container: $("#paging"),
                size: default_page_length
        });
}
/*
	Due to the limitation of tablesort and tablesorterPager, for example the pager
	plugin was written by someone else, the update trigger doesn't update the pager, 
	the solution is to update the pager first by changing the 'size' option to the 
	full length, perform filtering, then resize it.
*/
function initListFilter(){
	//console.log('initListFilter');
	$('.filter-select select').change(function(){
		//console.log("filter select changed to:");
		//console.log($(this).val());
		var filterby = $(this).val();
		$('#greed').trigger('destroy'); //destory table, ignore pager
		
		$('#greed').html(table_backup);
		//remove items no in the filter
		if(filterby != '0'){
			$('#greed tbody tr').each(function(i,e){
				if(!$(e).hasClass(filterby)){
					$(e).remove();
				}
			});
		}
		//rebuild table with pager	
		$('#greed').tablesorter({
			sortList: [[0,1]],
			textExtraction: myTextExtraction
		}).tablesorterPager({
                	container: $("#paging"),
	                size: default_page_length
        	});
	});
	
}
function initSearchBar(){
	$('.search-box .search input.submit').click(function(){
		if( $('.search-box .search .tfield #search').val() == "Search"){
			$('.search-box .search .tfield #search').val("");
		}
        return true;
	});
}

function initPopupWindows(){
	$('a.target-blank').each(function(index, element){popWindow(element);});
}

function externalLinkInit(){
	$('a:not(.target-blank)').each(function(index, element){
		var temp_str = $(element).attr('href');
		if(temp_str == undefined){return;}
		temp_str = jQuery.trim(temp_str);
		if(temp_str.charAt(0)!= "#" && !temp_str.match(location.host) && temp_str.charAt(0)!="/" && !temp_str.match("mailto:")){
			//$(element).css("target-name", "new"); //not support as of yet
			//$(element).css("target-new", "tab"); //not supported as of yet 
		$(element).click(function(){window.open(this.href); return false;});
		}
	});
}

var locations= ['london','newyork','saopaulo','wdc','joburg','dubai', 'riyadh' ,'zurich'];
function initMap(){
    $('.map-arrow').each(function(i,e){
        var loc = locations[i];
        $('.map-'+loc).mouseover(function(){
            $('.link-'+loc).addClass("underline");
        });
        $('.map-'+loc).mouseleave(function(){
            $('.link-'+loc).removeClass("underline");
        });


    })
}

$( document ).ready( function(){
	//$('table#greed').tablesorter();
	initTableSorter();
	initListFilter();
	initSearchBar();
	initPopupWindows();
	externalLinkInit();
    initMap();
});

