$(document).ready(function() {
	first=1;
	noload=0;
	$.history.init(function(hash) {
		if (hash == "") {
			// initialize app
			if (first==0)
			{
				jQuery.history.load("/");
				first=0;
			}
			if (first==1){first=0};

		} else {
			if (noload==1){
				window.location.hash=hash;
				noload=0;
			}else{
			$('#body').fadeTo(0, 0.2);
			$.post(hash, {
				a : "1"
			}, function(data) {
				receive(data);
			}, "json");
			noload=0;
			}
		}
	}, {
		unescape : ",/"
	});
	make();
});

function make(nomsg) {
	$(".ajax").unbind('click');
	$(".ajax").click(
			function() {
				run=true;
				if($(this).hasClass('del')){
					    run=confirm("آیا تمایل به ادامه ی عملیات حذف دارید؟ این عملیات قابل بازگشت نیست");
				}
				if (run){
					if ($(this).attr('href').indexOf('/') === 0
							| $(this).attr('href').indexOf('/') > 6) {
						title = $(this).attr('href');
					} else {
						title = '';
						$.each($(this).attr('href').split('/'), function(i, item) {
							if (i == 3) {
								title = title + item
							}
						});
						title = title + '/';
					}
					window.location.hash=title;
				}
				return false;
			});
	$(".logout").unbind('click');
	$(".logout").click(
	function() {
			$('#body').fadeTo(0, 0.2);			
			$.post('/index/user/logout/a/1/', {
				a : "1"
			}, function(data) {
				receive(data);
				if(window.location.hash!="#/index/" & window.location.hash!="")
				{
					noload=1;
					window.location.hash='/index/';
				}
			}, "json");
			return false;
	});

	
	$(".blind").unbind('click');
	$(".blind").click(
	function() {
			run=true;
			if($(this).hasClass('del')){
				    run=confirm("آیا تمایل به ادامه ی عملیات دارید؟ این عملیات قابل بازگشت نیست");
			};
			if($(this).hasClass('warn')){
				    run=confirm("آیا تمایل به ادامه ی عملیات دارید؟ این عملیات قابل بازگشت نیست");
			};
			if (run==true){
				$('#body').fadeTo(0, 0.2);
			if ($(this).attr('href').indexOf('/') === 0
					| $(this).attr('href').indexOf('/') > 6) {
				title = $(this).attr('href');
			} else {
				title = '';
				$.each($(this).attr('href').split('/'), function(i, item) {
					if (i == 3) {
						title = title + item
					}
				});
				title = title + '/';
			}
			$.post(title, {
				a : "1"
			}, function(data) {
				receive(data);
			}, "json");
			}
			return false;
	});
		if (!nomsg)
		{
			if ($('.thanks').length){
			$('.thanks').get(0).style.position='fixed';
			$('.thanks').get(0).style.width='100%';
			$('.thanks').get(0).style.left='0px';
			$('.thanks').get(0).style.top='0px';
			$('.thanks').get(0).style.textAlign='center';
			$('.thanks').get(0).style.zIndex='1';
			$('.thanks').get(0).style.padding='10px 0px';
			$('.thanks').fadeTo(6000, 0);
			window.setTimeout(normal_state, 6000, true);
			}

			if ($('.error').length){
			$('.error').get(0).style.position='fixed';
			$('.error').get(0).style.width='100%';
			$('.error').get(0).style.left='0px';
			$('.error').get(0).style.top='0px';
			$('.error').get(0).style.textAlign='center';
			$('.error').get(0).style.zIndex='1';
			$('.error').get(0).style.padding='10px 0px';
			$('.error').fadeTo(6000, 0);
			window.setTimeout(normal_state, 6000, true);
			}
		}
}

function receive(data) {
	document.title = data.title;
	$.each(data, function(i, item) {
		if (i != 'js') {
			$("#" + i).html(item)
		}
		if (i == 'js') {
			eval(item);
		}
		if (i == 'css') {
			$('#css').html(item);
		}
	});
	make();
	$('#body').fadeTo(0, 1);
}
function normal_state(){
	if ($('.thanks').length){
		$('.thanks').get(0).style.position='inherit';
		$('.thanks').get(0).style.width='';
		$('.thanks').fadeTo(1000, 1);
		}
	if ($('.error').length){
		$('.error').get(0).style.position='inherit';
		$('.error').get(0).style.width='';
		$('.error').fadeTo(1000, 1);
		}
}

