function module_alphanav(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		animateTo: true,
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
	
	$(elem).find('dl.list-item a').each(function () {
		$(this).closest('dl.list-item').addClass('link');
	});
	
	$(elem).find('.alpha-index a').bind('click', function () {
		// Get actual hash (because IE adds domain as well)
		anchor = $(this).attr('href').substr($(this).attr('href').indexOf('#'));
		$(elem).find('.content')[0].scrollTo(anchor);
		return false;
	});
	
};

function module_conferences(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
};

function module_firmnews(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
};

function module_form(elem)
{
	this.elem = elem;
	var obj = this;
	$(this.elem).find('input[type="submit"]').bind('click', function () {		
		obj.submit();
		return false;
	});
};

module_form.prototype.submit = function()
{
	var f = $(this.elem).find('form.validate');
	var v = f.validate({
		errorElement: 'span',
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().prev().children() );
		}
	});

	if(f.valid()){
		$.post(f.attr('action'),f.serialize(),this.handle,"html");
		var obj = $(f).find('input[type="submit"]');
		obj.attr('value','Sending');
		obj.attr('disabled','disabled');
	}
};

module_form.prototype.handle = function(data,textStatus)
{
	var f = $(this.elem).find('form.validate');
	if(textStatus == "success"){
		$(f).slideUp();
		$(this.elem).find('div.response').append(data);
	}
};

function module_gallery(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
	var obj = this;
	// Thumbnails
	obj.layout();
	$('#localnav a[href$="#'+ $(elem).closest('.section')[0].id +'"]').click(function () {
		$(elem).children('.thumbnails').hide();
		setTimeout(function() {
			$(elem).children('.thumbnails').fadeIn(400, function(){
				obj.layout();
			});
		}, 1000);
	});
	
	// Detail Image
	$(elem).find('.thumbnails li a').click(function () {
		$(elem).find('.detail_img').fadeIn(400).html('<img alt="" src="'+ $(this).attr('href') +'" />');
		return false;
	});
	$(elem).find('.detail_img').click(function () {
		$(this).fadeOut(400);
	});
	
};

module_gallery.prototype.layout = function(params)
{
	// Get train width
	var obj = $(this.elem).children('.thumbnails');
	width = 0;
	$(obj).find('li').each(function(img){
		width += $(this).width()+10;
	});
	if (width < 950) width = 950;
	$(obj).children('ul').css({'width': width+'px'});
	width -= 10;

	// Scroll train
	if (width > 950) {
		$(obj).mousemove(function(e){
			m = ($(window).width()/2)-470;
			if (((m-e.pageX) < -180) && ((m-e.pageX) > -760)) {
				$(obj).children('ul').css({left: (((width-940)/580)*(m-e.pageX+180))+'px'});
			} else if ((m-e.pageX) > -760) {
				$(obj).children('ul').css({left: '0px'});
			} else {
				$(obj).children('ul').css({left: -(width-940)+'px'});
			}			
		});
	}	
};

function module_leader(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).find('.content').jScrollPane({
		scrollbarWidth: 10,
		dragMaxHeight: 200
	});
};

function module_multicolumn(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		animateTo: true,
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 130
		// showArrows:true
	});
	
	$(elem).find('.alpha-index a').bind('click', function () {
		// Get actual hash (because IE adds domain as well)
		anchor = $(this).attr('href').substr($(this).attr('href').indexOf('#'));
		$(elem).find('.content')[0].scrollTo(anchor);
		return false;
	});
	
};

function module_office(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		animateTo: true,
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 130
	});
};

function module_projects(elem)
{
	this.elem = elem;
	var obj = this;
	obj.searchInterval = 0;
	obj.searchInterval2 = 0;
	
	$('#catalog_filters').bind('mouseenter',function(e) {
		clearInterval(obj.searchInterval);
		clearInterval(obj.searchInterval2);
		obj.searchInterval = setInterval(function(){ obj.controls.apply(obj,[true]); },300);
	});

	$('#catalog_filters').bind('mouseleave',function(e) {
		clearInterval(obj.searchInterval);
		clearInterval(obj.searchInterval2);
		obj.searchInterval = setInterval(function(){ obj.controls.apply(obj,[false]); },300);
	});
	
	$('#catalog_filters input[type="checkbox"]').checkbox({empty:'/assets/img/x.gif'});
	
	// Reset
	$('.form_reset').bind('click',function(){
		obj.reset();
		return false;
	});
	
	// Search
	$('#search').bind('keypress change',function(e) {
		if(e.type == 'keypress' && e.which == 13){
			obj.search(true,'key');
			return false;
		}
		var tv = $(this).val();
		if(tv != 'Search…'){
			//$('#search_clear').css('display','block');
			$('#search_clear').show();
			if(tv.length >= 2){
				//$('#search_submit').show();
			}else{
				//$('#search_submit').hide();
			}
		}else{
			//reset it
			$('#search_clear').hide();
			//$('#search_submit').hide();
		}
	}).inputHint();
	
	if($('#search').val() == 'Search…'){
		$('#search_clear').hide();
	}
	
	$('#search_clear').bind('click',function(){
		obj.clear();
		return false;		
	});
	
	$('#search_submit').bind('click',function(){
		obj.search(true,'submit');
		return false;
	});	
		
	this.form_value = $('#form_search').serialize();
	
	// Sort
	$('#catalog_browse .header li').bind('click', function() {
		$('#sort_column').val($(this)[0].id.substr(4));
		if ($(this).hasClass('selected')) {
			if ($(this).hasClass('desc')) {
				$('#sort_direction').val('');
			} else {
				$('#sort_direction').val('desc');
			}
		} else {
			$('#sort_direction').val('');
		}
		obj.search(true,'head click');
	});
	
	// Links to project details
	$('#catalog_browse tbody').bind('click', function(e) {
		var url = ($($(e.target).parent().find('a')[0]).attr('href'));
		if(url){
			custom.addHistory(url);
		}
	});
	
	//only safari can handle now - ie6 actually does ok
	if ($.browser.safari) {
		if ($('html:not(.ie6)')[0]) $('#catalog_browse .content').jScrollPane({		
			scrollbarWidth: 10,
			scrollbarMargin: 10,
			dragMaxHeight: 200
		});
	}
	
};

module_projects.prototype.controls = function(mode)
{
	clearInterval(this.searchInterval);
	var obj = $('#catalog_filters');
	if(mode){		
		$(obj).animate({'height': 160},250,'');
    // $(obj).find('.controls').animate({opacity: 1.0},100);
    $(obj).find('.controls').fadeIn(250);
    $('#label').hide();
	}else{
		$(obj).animate({'height': 37},250,'');
    // $(obj).find('.controls').animate({opacity: 0.0},100);
    $(obj).find('.controls').fadeOut(250);
    $('#label').show();
		if(this.form_value != $('#form_search').serialize()){
			var obj = this;
			clearInterval(obj.searchInterval2);
			obj.searchInterval2 = setInterval(function(){ obj.search.apply(obj,[true,'controls']); },300);
		}
	}
};

module_projects.prototype.clear = function()
{
	$('#search').val('');
	$('#search').focus();
	$('#search_clear').hide();
};

module_projects.prototype.reset = function()
{		
	this.form_value = $('#form_search').serialize();
	custom.addHistory('/expertise/project_catalog');
	//var obj = this;
	//clearInterval(obj.searchInterval2);
	//this.projects_controls(false);
	//obj.searchInterval2 = setInterval(function(){ obj.projects__reset.apply(obj,[true]); },300);
	//$('#form_search').reset();
};

module_projects.prototype._reset = function()
{
	clearInterval(this.searchInterval2);
	custom.addHistory('/expertise/project_catalog');
};

module_projects.prototype.render = function(json,textStatus)
{
	if (json.status == "success") {
		// Update column headers
		
		// Update datagrid
		$('#project_catalog .content').html(json.content.main);
		// Update filter result count
		$('#info_match').html(json.data.projects.length);
	} else {
		// Display error
		alert(json.status.toUpperCase() + ': ' + json.message);
	}
};

module_projects.prototype.sort = function(column,dir)
{
	$('#sort_column').val(column);
	$('#sort_direction').val(dir);
	this.search(true,'sort');
};

module_projects.prototype.search = function(reset,s)
{
	//alert(s);
	clearInterval(this.searchInterval);
	clearInterval(this.searchInterval2);
	var f = $('#form_search');
	
	//format multi checkboxes in same style as dropdowns - woot
	var qs = [];
	qs.push({name:'sort_column',value:$('#sort_column').val()});
	qs.push({name:'sort_direction',value:$('#sort_direction').val()});
	var tv = $('#search').val();
	if(tv != 'Search…' && tv.length >= 2){
		qs.push({name:'search',value:$('#search').val()});
	}
	
	var tA = $(f).find('input[type="checkbox"]');
	var tObj = {};
	var imax = tA.length;
	for(var i=0;i<imax;i++){
		var obj = tA[i];
		if($(obj).attr('checked')){
			var dA = $(obj).attr('id').split('_');
			if(tObj[dA[1]] == undefined){
				tObj[dA[1]] = {name:dA[1],value:[dA[2]]};
			}else{
				tObj[dA[1]].value.push(dA[2]);
			}
		}
	}
	for(var i in tObj){		
		qs.push({name:tObj[i].name,value:(tObj[i].value).join()});
	}	
	var query_string = 'filter';
	var imax = qs.length;
	for(var i=0;i<imax;i++){		
		if(qs[i].value != ''){			
			query_string += '/' + qs[i].name + '/' + qs[i].value;
		}
	}
	if(query_string == 'filter'){
		query_string = '';
	}
	url = '/expertise/project_catalog/' + query_string;
	custom.addHistory(url);
};

function module_solutions(elem)
{
	this.elem = elem;
	this.ind = -1;
	this.len = $(this.elem).find('.solution').length;
	this.solution = null;
	this.lastsolution = null;
	this.fadeInterval = 0;
	var obj = this;
	
	if ($('html:not(.ie6)')[0]) $(elem).find('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
		
	//local nav
	$(this.elem).find('.nav .prev').bind('click',function(){
		obj.nav.apply(obj,[-1]);
		return false;
	});
	$(this.elem).find('.nav .next').bind('click',function(){
		obj.nav.apply(obj,[1]);
		return false;
	});
	
	//image nav
	
	this.nav(1);
};

module_solutions.prototype.nav = function(dir)
{
	switch(true){
		case (this.ind + dir) > (this.len - 1):
			this.ind = 0;
		break;
		
		case (this.ind + dir) < 0:
			this.ind = this.len - 1;
		break;
		
		default :
			this.ind += dir;
		break;
	}
	
	//update nav
	$(this.elem).find('.nav .location').text((this.ind+1)+'/'+this.len);
	this.view();
};

module_solutions.prototype.view = function()
{
	var obj = this;
	var tA = $(this.elem).find('.solution');
	for(var i=0;i<tA.length;i++){
		if(i == this.ind){
			this.solution = $(tA[i]);
			$(tA[i]).fadeIn(400);
		}else{
			$(tA[i]).hide();
		}
	}
	
	//image mouseover
	$(this.solution).find('.imagenav img').each( function(ind) {
		/*
		$(this).bind('click',function(){
			obj.image.apply(obj,[ind]);
		});
		*/
		$(this).bind('mouseover',function(){
			obj.image.apply(obj,[true,ind]);
		});
		$(this).bind('mouseout',function(){
			clearInterval(obj.fadeInterval);
			obj.fadeInterval = setInterval(function(){ obj.image.apply(obj,[false,ind]); },150);
			//obj.image.apply(obj,[false,ind]);
		});
	});
	
	if(this.lastsolution){
		$(this.lastsolution).find('.imagenav img').each( function() {
			$(this).unbind();
		});	
	}
	
	this.lastsolution = this.solution;
	
};

module_solutions.prototype.image = function(mode,id)
{
	clearInterval(this.fadeInterval);
	var tA = $(this.solution).find('.images img');
	for(var i=0;i<tA.length;i++){
		if(i == id){
			if(mode == true){
				$(tA[i]).fadeIn(300);
			}else{
				$(tA[i]).fadeOut(150);
				//
				$(this.solution).find('.description').fadeIn(300);
			}
		}else{
			$(tA[i]).hide();
		}
	}
	if(mode == true){
		$(this.solution).find('.description').hide();
	}
	
};

function module_text(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
};

function module_thumbs(elem)
{
	this.elem = elem;
	// Add styled scrollbars
	if ($('html:not(.ie6)')[0]) $(elem).children('.content').jScrollPane({
		scrollbarWidth: 10,
		scrollbarMargin: 10,
		dragMaxHeight: 200
	});
	
	$(elem).find('dl.list-item a').each(function () {
		$(this).closest('dl.list-item').addClass('link');
	});
};