$(document).ready(function(){
	var typeState = "alphabet";
	var paramState = '';
	var loadingState = false;
	var currentPath = '';
	var prevState = '';
	
	function setPath(path) {
		currentPath = path;
		var location = window.location +'';
		if((location).indexOf('#',0)>0) {	
			window.location = location.split('#')[0] + '#' + path;
		} else {
			window.location += '#' + path;
		}
	}
	
	function setPathOnLoad(path) {
		currentPath = path;
		prevState = path;
		var location = window.location +'';
		if((location).indexOf('#',0)>0) {	
			window.location = location.split('#')[0] + '#' + path;
		} else {
			window.location += '#' + path;
		}
	}
	
	function getPath(path) {
		var location = decodeURIComponent(window.location) +'';
		if((location).indexOf('#',0)>0) {	
			return location.split('#')[1];
		} else {
			return '';
		}
	}
	
	if(getPath()=='') {
		setPathOnLoad("/teachers/alphabet//1/");	
	}
	
	
	setInterval(function(){
		if(!loadingState) {
			var path = getPath();
			if(prevState != path) {
				prevState = path;
				
				var pathArr = path.split('/');
				if(pathArr[2]=='alphabet') {
					var param = pathArr[3];
					var page = pathArr[4];
					var plus = '';
					if(pathArr.length>6) {
						plus = pathArr[5]+'/'+pathArr[6]+'/';
					}
					
					timeAlphabetAction(param,page,plus);
					
				} else {
					var param = pathArr[3];
					var page = pathArr[4];
					var plus = '';
					if(pathArr.length>6) {
						plus = pathArr[5]+'/'+pathArr[6]+'/';
					}
					
					timeRegionAction(param,page,plus);
				}
			}
		}
	}, 300);
	
	
	function reload(type, param, page, plus) {
		
		typeState = type;
		paramState = param;

		var pathToSet = '/teachers/'+type+'/'+(param)+'/'+page+'/'+(plus+'');
		var path = '/teachers/'+type+'/'+page+'/';
		loadingState = true;

		setPathOnLoad(pathToSet);
		$('#loading').show('fast');
		setTimeout(function(){
			$("#data-container").children().load(path, {param: param}, function(){
					loadingState = false;
					$("#loading").hide('fast');
					if(plus!='' && plus!=undefined) {
						var plusArr = plus.split('/');
						var teacher_id = plusArr[1];							
						$(".teacher-preview[teacher!="+teacher_id+"]").hide();
						$(".teacher-more-text[teacher="+teacher_id+"]").show();
					}
			});
		},400);
		
	}
	
	function timeRegionAction(param, page, plus) {
		$("#button-regions").removeClass('button-unactive');
		$("#button-regions").addClass('button-active');
		
		$("#button-alphabet").removeClass('button-active');
		$("#button-alphabet").addClass('button-unactive');
		
		$(".page-link").removeClass('page-active');
		$(".page-link[page="+page+"]").addClass('page-active');
		
		$(".region-item").removeClass('region-active');
		$(".region-item:contains("+param+")").addClass('region-active');
		
		$("#searchbox").attr('value','');
		$(".region-item").show();
		reload('region', param, page, plus);
	}	
	function timeAlphabetAction(param,page, plus) {
		$("#button-alphabet").removeClass('button-unactive');
		$("#button-alphabet").addClass('button-active');
		
		$("#button-regions").removeClass('button-active');
		$("#button-regions").addClass('button-unactive');
		
		$(".region-item").hide();
		
		reload('alphabet', param, page, plus);
	}
	
	function regionAction() {
		$("#button-regions").removeClass('button-unactive');
		$("#button-regions").addClass('button-active');
		
		$("#button-alphabet").removeClass('button-active');
		$("#button-alphabet").addClass('button-unactive');
		
		$("#searchbox").attr('value','');
		
		$(".region-item").show();
		
		reload('region', $(".region-active").attr('region'), 1,'');
	}
	
	function alphabetAction() {
		$("#button-alphabet").removeClass('button-unactive');
		$("#button-alphabet").addClass('button-active');
		
		$("#button-regions").removeClass('button-active');
		$("#button-regions").addClass('button-unactive');
		
		$(".region-item").hide();
		
		reload('alphabet', $("#searchbox").val(), 1,'');
	}
	
	$(".region-item").click(function(){
		if (!loadingState) {
			$(".region-item").removeClass('region-active');
			$(this).addClass('region-active');
			
			$(".page-link").removeClass('page-active');
			$(".page-link[page=1]").addClass('page-active');
			
			reload('region', $(".region-active").attr('region'), 1,'');
		}
		return false;
	});
	
	$("#button-regions").click(function(){
		if (!loadingState) {
			regionAction();
			
			$(".page-link").removeClass('page-active');
			$(".page-link[page=1]").addClass('page-active');
		}
		return false;
	});
	
	$("#button-alphabet").click(function(){
		if (!loadingState) {
			alphabetAction();
			
			$(".page-link").removeClass('page-active');
			$(".page-link[page=1]").addClass('page-active');
		}
		return false;
	});
	
	$(".page-link").live("click",function(){
		if (!loadingState) {
			$(".page-link").removeClass('page-active');
			$(this).addClass('page-active');
			
			reload(typeState, paramState, $(this).attr('page'),'');
		}
		return false;
	});
	
	$("#searchbox").focus(function(){
		
	});
	
	$("#searchbox").keyup(function(){
		
		alphabetAction();
			
		$(".page-link").removeClass('page-active');
		$(".page-link[page=1]").addClass('page-active');
		
	});
	
	
	$(".teacher-link-item").live("click",function(){
		
		var teacher_id = $(this).attr('teacher');
		
		setPathOnLoad(currentPath+'teacher/'+teacher_id+'/');
		
		$(".teacher-preview[teacher!="+teacher_id+"]").hide('normal');
		$(".teacher-more-text[teacher="+teacher_id+"]").show('normal');
		return false;
	});
	
	$('.return-link').live('click',function(){
		
		var pathArr = currentPath.split('/');
		pathArr = pathArr.slice(0, pathArr.length - 3);
		
		setPathOnLoad(pathArr.join('/')+'/');
		$(".teacher-preview").show('normal');
		$(".teacher-more-text").hide('normal');
		return false;
	});
});
