$(function(){

	var $topleft = $("#left .top");
	var $subleft = $("#left .sub");
	var $focus = $("#focus");
	var $topright = $("#right .top");
	var $subright = $("#right .sub");
	
	$("input.focus").focus();

	$("a.back").bind('click',function(){
		history.go(-1);
		return false;
	});
	
	//INIT LEFT//
	$.ajax({
		url: '/t/left',
		type: 'GET',
		beforeSend: function(){
			$topleft.addClass('loading');
		},
		success: function(response){
			$topleft.html(response);
			$topleft.removeClass('loading');
			console.log('cached');
			ajaxify();
			sortMenus();
			//INIT RIGHT//
			$.ajax({
				url: '/t/right',
				type: 'GET',
				beforeSend: function(){
					$topright.addClass('loading');
				},
				success: function(response){
					$topright.html(response);
					$topright.removeClass('loading');
					console.log('cached');
					ajaxify();
					sortMenus();
					//INIT BODY//
					hashChange();
				}
			});
		}
	});
	
/*
	$focusFolders.each(function(){
		var $this = $(this);
		var $item = $this.closest('.item');
		$.ajax({
			url: $this.data('href'),
			type: 'GET',
			beforeSend: function(){
				$item.addClass('loading');
			},
			success: function(){
				$item.removeClass('loading');
				console.log('cached');
			}
		});
	});
*/
	
	$(".folder").live('click',function(){
		var $this = $(this);
		window.location.hash = '!' + $this.data('href');
	});
	
	$("a.manage").bind('click',function(){
		var $this = $(this);
		$this.hide().siblings("div.hidden").show().find("input").focus();
		return false;
	});
	
	$(window).bind('hashchange',function(){
		hashChange();
	})
	
	var hashChange = function(){
		if(window.location.hash && window.location.hash.indexOf('#!') != -1){
			var href = window.location.hash.substr(2);
		} else {
			var href = '/index';
		}
		var bHref = "#"+href;
		$.ajax({
			url: '/d'+href,
			type: 'GET',
			beforeSend: function(){
				$focus.addClass('loading');
			},
			success: function(response){
				if(response.indexOf('item') != -1) {
					$focus.html(response);
					$(".item").removeClass('active').each(function(){
						var $this = $(this);
						if((bHref.indexOf(decodeURI($this.data('href'))) != -1) || (bHref.indexOf($this.data('href')) != -1)) {
							$this.addClass('active');
						}
					});
					$(".root_items > .active.item").each(function(){
						var $this = $(this);
						var $subitems = $(".sub_items",$this);
						var html = '';
						if($(".item",$subitems).length > 0) html = $subitems.clone();
						$this.closest(".top").siblings(".sub").html(html);
					});
					ajaxify();
				}
				$focus.removeClass('loading');
			}
		});
	}
	
	var ajaxify = function(){
		$("form").ajaxForm({
			beforeSend: function(){
				$focus.addClass('loading');
			},
			success: function(response){
				$focus.html(response);
				$focus.removeClass('loading');
				ajaxify();
			}
		});
	}
	
	var sortMenus = function(){
		$("ul[data-order=desc]").each(function(){
			var $this = $(this);
			$("li",$this).tsort({order:$this.data('order'),attr:'data-href'});
		});
	}
	
});
