// JavaScript Document




var C = {
	
	makeAllInactive: function(){
		$('.index-link').each( function(){
			if( $(this).hasClass('first') ){
				$(this).removeClass('first');
				$(this).addClass('first-inactive');
			}else{
				$(this).removeClass('active-link');
			}
		});
	},
	
	makeActive: function(el){
		if( el.hasClass('first-inactive') ){
			el.removeClass('first-inactive');
			el.addClass('first');
		}else{
			el.addClass('active-link');
		}
	},
	
	parseUrl: function(url){
		if( url.indexOf('-clients') != -1 ){
			return url.replace( '-clients', '');	
		}else{
			return url + 'consumer';
		}
	},
	
	updateUrl: function(url){
		var bits = url.split('/');
		var last = bits[bits.length - 1];
		var n = window.location.href.replace(/#\S+?$/, '');
		window.location.href = n + '#' + last;
	},
	
	handleClick: function(e){
		e.preventDefault()
		if( $(this).hasClass('active') || $(this).hasClass('first-active') ) return;
		C.makeAllInactive();
		C.makeActive( $(this) );
		var url = C.parseUrl( this.href );
		C.updateUrl(url);
		$('#clients-content').load( url );
	},
	
	findLink: function(){
		var hash = window.location.hash.replace('#', '');
		if( hash == 'consumer' ){
			url = window.location.href.replace('#'+hash, '');
			
		}else{
			url = window.location.href.replace('#'+hash, '');
			url = url + hash + '-clients';
		}
		$('.index-link[href='+url+']').click();
	},
	
	init: function(){
		$('.index-link').click( C.handleClick );
		C.findLink();
	}
	
	
}



$(document).ready( C.init );
