$(document).ready(function(){
	$('.jq-show').show();
	$('.jq-hide').hide();
	$('.jq-remove').remove();

	$('a[rel=external]').click(function(){
		window.open(this.href);
		return false;
	});

	$('.close-message').click(function(){
		$('#messages div').fadeOut('fast');
	});

	jQuery.fn.setPos=function(e){return this.css("top",(e.pageY+0)+"px").css("left",(e.pageX-150)+"px");};
	$(".tooltip").hover(function(e){
		nid = 'date-info' + $(this).parent().attr('id').substr(0, 8);
		body = $('#'+nid).html();
		$("body").append('<p id="tooltip">' + body + '</p>');
		$("#tooltip").setPos(e);
		$("#tooltip").show();
	}, function(){$("#tooltip").remove();});

	$(".tooltip").mousemove(function(e){ $("#tooltip").setPos(e); });

	var hidden = false;
	var shidden = false;

	function maximize(){
		sidebar_hide();
		$('#header').animate({ marginTop: '-147px' }, 200);
		$('#maximizer img').attr('src', 'static/img/icon/arrow_in.png');
		$('#maximizer img').attr('alt', 'Double-click to restore the working area');
		$('#maximizer img').attr('title', 'Double-click to restore the working area');
		$('#wrapper').css('background-position', 'left -150px');
		hidden = true;
	}
	function restore(){
		sidebar_show();
		$('#header').animate({ marginTop: '0px' }, 200);
		$('#maximizer img').attr('src', 'static/img/icon/arrow_out.png');
		$('#maximizer img').attr('alt', 'Double-click to maximize the working area');
		$('#maximizer img').attr('title', 'Double-click to maximize the working area');
		$('#wrapper').css('background-position','left top');
		hidden = false;
	}

	function sidebar_hide(){
		$('#sidebar').animate({ marginLeft: '-202px' }, 200);
		$('#content').animate({ marginLeft: '2px' }, 200);
		$('#sidebar').css('border-right', '4px solid #A7C024');
		shidden = true;
	}
	function sidebar_show(){
		$('#sidebar').animate({ marginLeft: '0px' }, 200);
		$('#content').animate({ marginLeft: '200px' }, 200);
		$('#sidebar').css('border-right', 'none');
		shidden = false;
	}

	function changer( cs ){
		if( cs === null ) return;
		if( cs === false ) {
			maximize();
		} else {
			restore();
		}
	}

	$("a.del").click(function(){
		return confirm('Are you sure?');
	});

	$('#maximizer').dblclick(function(){
		changer(hidden);
	});

	$('#sidebar').dblclick(function(){
		if( shidden === false ){
			sidebar_hide();
		} else {
			sidebar_show();
		}
	});

	$('span.editable').dblclick(function(){
		if( !$(this).hasClass('editing') ){
			unid = Math.floor(Math.random()*1e+10);
			val = $(this).children('span').text();
			$(this).addClass('editing');
			$(this).attr('id', 'ee_'+unid);
			$(this).children('span').html('<input type="text" value="' + val + '" /> <button>Save</button> <a href="#" class="close">Close</a>');
		}
	});

	$('span.editable.editing button').live('click', function(){
		target = $(this).parent().parent().children('var.target').text();
		field = $(this).parent().parent().children('var.field').text();
		id = $(this).parent().parent().children('var.id').text();
		val = $(this).parent().children('input[type=text]').val();
		parent_id = $(this).parent().parent().attr('id');

		$.post('script/admin/editables',
			{ target: target, field: field, value: val, id: id  },
			function( msg ){
				if( msg.status === 1 ){
					$('#'+parent_id+' span').parent().removeClass('editing');
					$('#'+parent_id+' span').html( msg.value );
					$('#'+parent_id).removeAttr('id');
				} else {
					alert('Update failed!');
					location.reload(true);
				}
			}
			, 'json'
		);
	});

	$('span.editable.editing a.close').live('click', function(){
		parent = $(this).parent().parent().children('span');
		val = $(this).parent().children('input[type=text]').val();
		parent.html( val );
		parent.parent().removeClass('editing');
		parent.parent().removeAttr('id');
		return false;
	});
});
