/* UI Stuff */
UI = {
	
	showStep : function(id){
		UI.hideStep();

		if(id != undefined && id != "#undefined") {
			
			if(id == parseInt(id)){
				Hash.set("step", id);
				$("#step"+id).show();
				
				id = parseInt(id);
				switch(id){
					case(1):
						Building.getAll();
					break;
					
					case(2):
						if(Building.Current == undefined)
							UI.showStep(1);
					break;
					
					case(3):
						$("#lockerID").html(Locker.Current.Batch + Locker.Current.Number);
						$(".note").hide();
						$("#Name").focus();
					break;
					
					case(4):
						Registrant.display();
					break;
					
					case(5):
						Transaction.create();
					break;
					
					case(6):
						Session.clear();
					break;
					
				}
			} 
			
		} else {
			Hash.clear()
			UI.showStep(1);
		}
			
		w = id * 20;
		if(id<=5)
			$("#bar").css({width: w+'%'})
	},
	
	hideStep : function(id){
		if(id == undefined)
			$(".step").hide();
		else
			$("#step"+id).hide();
	},
	
	showBuilding : function(obj){
		UI.hideBuilding();
		
		if(obj.Id == undefined)
			$(".building").show();
		else
			$("#building"+obj.Id).show();
	},
	
	hideBuilding : function(id){
		if(id == undefined)
			$(".building").hide();
		else
			$("#building"+id).hide();
	}
}

Hash = {

	read : function() {
		var hash = window.location.hash;
		var hashObj = new Object();
		hash = hash.split('/');
		
		for (i=1; i<hash.length; i+=2){
			
			if(hash[i+1] == undefined) {
				Hash.clear();
			} else {
				hashObj[hash[i]] = hash[i+1];
			}
				
		}

		Hash.hash = hashObj;
	},

	write: function() {
		var hash = "#!";
		for (key in Hash.hash)
			hash += "/" + key + "/" + Hash.hash[key];
			
		document.location.hash = hash;
		Hash.current = document.location.hash;
	},

	get: function(name) {
		if(name==undefined) {
			return "";
		}
		return Hash.hash[name];
	},

	set: function(name, value) {
		Hash.hash[name] = value;
		Hash.write();
	},
	
	clear : function() {
		Hash.hash = new Object;
		Hash.write();
	},
	
	check : function(){
		
		if(Hash.current != document.location.hash) {
			Hash.read();
			
			if(parseInt(Hash.hash.step) == Hash.hash.step)
				UI.showStep(Hash.hash.step);
			else
				Tab.show("#"+Hash.hash.step);
				
			Hash.current = document.location.hash;
		}
	}
}

/* Loading Icons */
Loading = {
	Interval : 0,
	pos : 0,
	
	loading : function(){
		Loading.pos = Loading.pos - 12
		
		if(Loading.pos < -48)
			Loading.pos = 0;
			
		bgPos = "0 " + Loading.pos + "px";
		$("#loading").css({backgroundPosition: bgPos});
	},
	
	start : function(){
		if(Loading.Interval == 0) {
			Loading.Interval = setInterval('Loading.loading()', 100);
			$("#loading").show();
		}
	},
	
	stop : function(){
		clearInterval(Loading.Interval);
		Loading.pos = 0;
		Loading.Interval = 0;
		$("#loading").css({backgroundPosition: '0 0'});
	}
}

/* Timer */
Timer = {
	
	//get time remaining and count down
	start : function(){
		$("#timer").show();
		Timer.setTime();
		Timer.timeout = setTimeout('Timer.stop()', Timer.remaining);
		Timer.interval = setInterval('Timer.update()', 1000)
	},
	
	stop : function(){
		clearInterval(Timer.interval);
		clearTimeout(Timer.timeout);
		$("#timer").hide();
	},
	
	update : function(){
		Timer.remaining = Timer.remaining - 1000;
		Timer.setTime();
	},
	
	setTime : function(){
		
		m = Math.floor(Timer.remaining / 60000);
		s = (Math.floor(Timer.remaining % 60000) / 1000);			
		tl = m + " minutes " + s + " seconds";
		$("#timer span").html(tl);
	},
	
	reset : function(){
		Timer.remaining = 600000;
		Timer.stop();
		Timer.start();
	}
	
}

Tab = {
	show : function(id){
		UI.showStep(id);
		Tab.hideAll();
		
		id = id.substr(1);
		
		if(id!=undefined && id !="undefined") {
			Hash.set("p", id);
			//If the div exists
			if($("#"+id).html() != null) {
				$("#"+id).show();
			//Assume its a search method
			} else {
				$("#tab_search_id").show();
				$("#Method").val(id);
			}
				
			$(".tab_nav li a.active").removeClass("active");
			$("#tab_"+id).addClass("active");
		} else {
			Hash.clear();
		}


	},
	
	hideAll : function(){
		$(".tab_contents").hide();
	}
}

Cookie = {
	create : function(name, value, days){
		
		if(!days)
			days = 0.5;
		
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	read : function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	destroy : function(name){
		Cookie.create(name,"",-1);
	}
}

Format = {
	
	phone : function(p){
		p1 = p.substr(0,3);
		p2 = p.substr(3,3);
		p3 = p.substr(6,4);
		return "(" + p1 + ")" + " " + p2 + "-" + p3;
	},
	
	combo : function(c){
		p1 = c.substr(0,2);
		p2 = c.substr(2,2);
		p3 = c.substr(4,2);
		return p1 + "-" + p2 + "-" +p3;
	},
	
	padZero : function(n){
		n = n + "";
		
		if(n.length==1)
			n = "0"+n
			
		return(n);
	}
	
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
