if (document.location.hash && document.location.hash.substr(1,1)=='/') {
	document.location=document.location.hash.substr(1);
} else {
	var lsth = '';
	function hashCheck() {
		var thsh = document.location.hash.substr(1);
		if (lsth !== thsh) {lsth = thsh;hashBrowse(lsth,0);}
	}
	setInterval(hashCheck, 100);
}

var base = '/';

function addChild(d,t,u,e) {
	var n = 1;
	var c = $('#'+d).children(t);
	if (c.length) {
		l = c[c.length-1].id;
		n = parseInt(l.replace(d+'_',''))+1;
	}
	$.get(u+'&i='+n, function(data) {
		$(data).appendTo('#'+d);
		$('#'+d+'_'+n).slideDown('fast', e);
	});
}
function deleteChild(d) {
	$('#'+d).slideUp('fast',function() {$(this).remove();});
}

function hashBrowse(q) {
	h = document.domain;
	if(q.indexOf(h)!=-1)q=q.substr(q.indexOf(h)+h.length);
	$('body').load(q+(q.indexOf('?')!=-1?'&':'?')+'ajax');
	location.hash = q;
	lsth = location.hash.substr(1);
	pageTracker._trackPageview(q);
}
function hashRefresh() {
	hashBrowse((document.location.hash&&document.location.hash.substr(1,1)=='/')?location.hash.substr(1):window.location.pathname);	
}

function initPage() {
	initVotes();
	initMenu();
}

function initMenu() {
	$(".item a.add").click(
		function () {
			if ($('#order-checkout').size()) {
				$('#order-checkout').fadeIn();
				var p = $(this).parents("form").find("input[value]").val();
				addChild('order-items','li',base+'scripts/order/?a=add&'+$(this).parents("form").serialize(),function() {
					$("#p"+p).find(".warning").slideDown('fast').delay(2000).slideUp('fast');
				});
			} else {
				$.getScript(base+'scripts/order/?a=add&'+$(this).parents("form").serialize());
			}
			return false;
		}
	);
	$("input[maxlength]").keyup(function(){
		if ($(this).val().length == parseInt($(this).attr("maxlength"))) {
			$(this).next('input').focus();
		}
	});
}

function initVotes() {
	$(".stars_pink a").hover(
		function () {
			$(this).parent().find("a:lt("+$(this).index()+") span").add($(this).find("span")).addClass("on");
			$(this).parent().find("a:gt("+$(this).index()+") span").removeClass("on");
		}, 
		function () {
			var i = parseInt($(this).parent().find("input").val());
			if (i > 0) {
				$(this).parent().find("a:lt("+$(this).parent().find("input").val()+") span").addClass("on");
				$(this).parent().find("a:gt("+(parseInt($(this).parent().find("input").val())-1)+") span").removeClass("on");
			} else {
				$(this).parent().find("a span").removeClass("on");
			}
		}
	);
	$(".stars_pink a").click(
		function () {
			$(".stars_pink a").unbind();
			var p = $(this).parents("form").find("input[name='p']").val();
			var s = parseInt($(this).index())+1;
			$('#p'+p+'_v').fadeTo('fast', 0.01, function() {
				$('#p'+p+'_v').load(base+'scripts/vote/?p='+p+'&s='+s, function() {
					$('#p'+p+'_v').fadeTo('fast', 1);
					initVotes();
				});
			});
			return false;
		}
	);
}

function locations(i) {
	if (i) {
		$('#location').fadeTo('fast', 0.01, function() {
			$('#location').load(base+'scripts/locations/?a=info&id='+i, function() {
				$('#location').fadeTo('fast', 1);
			});
		});
	}
}

function order(a,b) {
	switch (b) {
		case 0:
			$('#order-'+a).fadeTo('fast', 0.01, function() {
				$('#order-'+a).load(base+'scripts/order/?a='+a, function() {
					$('#order-'+a).fadeTo('fast', 1);
				});
			});
			break;
		case 1:
			$('#order-'+a).fadeTo('fast', 0.01);
			$.post(base+'scripts/order/', $('#frm_'+a).serializeArray(), function(){}, 'script');
			break;
	}
}

function user(a,i) {
	switch (a) {
		case 'account':
			$.post(base+'scripts/account/', $('#frm_account').serializeArray(), function(){}, 'script');
			break;
		case 'address-modify':
			$('#addresses_'+i).fadeTo('fast', 0.01, function() {
				$('#addresses_'+i).load(base+'scripts/addresses/?a=modify&i='+i, function() {
					$('#addresses_'+i).fadeTo('fast', 1);
				});
			});
			break;
		case 'address-new':
			$('#addresses-new').fadeOut('fast', function() { addChild('addresses','div',base+'scripts/addresses/?a=add',null); });
			break;
		case 'address-save':
			$.post(base+'scripts/addresses/', $('#frm_address_'+i).serializeArray(), function(){}, 'script');
			break;
		case 'address-use':
			$('#addresses input[type=checkbox]:not(#address-'+i+')').attr('checked','');
			$.getScript(base+'scripts/addresses/?a=use&i='+i);
			break;
		case 'login':
			$.post(base+'scripts/login/', $('#frm_login').serializeArray(), function(){}, 'script');
			break;
		case 'logout':
			$.getScript(base+'scripts/logout/');
			break;
		case 'password':
			$('a.password').fadeOut('fast');
			$('#user-password').slideDown('fast');
			break;
		case 'password-change':
			if(i){$('#frm_account_pws').slideDown('fast')}else{$('#frm_account_pws').slideUp('fast')};
			break;
		case 'password-send':
			$.post(base+'scripts/password/', $('#frm_password').serializeArray(), function(){}, 'script');
			break;
	}
}

function toggleHeader() {
	$('#menu .masquer a').fadeOut();
	if ($('#header').hasClass('small')) {
		$("h1").fadeOut(250, function() {
			$("#header .holder").hide();
			$('#header').animate({ backgroundPositionY : '0', height : 337 }, 500, function() {
				$("#header").removeClass('small'); $("#header .holder").show(); $("h1,h2").fadeIn(); $('#menu .masquer a').html(translate.HEADER_HIDE); $('#menu .masquer a').fadeIn();
			});
		});
		var b = 1;
	} else {
		$("h1,h2").fadeOut(250, function() {
			$("#header .holder").hide();
			$('#header').animate({ backgroundPositionY : '-97px', height : 50 }, 500, function() {
				$("#header").addClass('small'); $("#header .holder").show(); $("h1").fadeIn(); $('#menu .masquer a').html(translate.HEADER_SHOW); $('#menu .masquer a').fadeIn();
			});
		});
		var b = 0;
	}
	var e = new Date();
	e.setDate(e.getDate()+365);
	document.cookie = "h="+b+";expires="+e.toUTCString()+";path=/;domain=sushitaxi.net";
}
