$(document).ready(function() {
	$('body').addClass('js_enabled');
	$('a.confirm,a.delete,a.uninstall').click(function() {
		var string = $(this).attr('href');
		if(string) {
			$dialog('confirm',$l['dialog_question']+$l['question_mark'],function() {
				var check_protocol = string.indexOf('http://');
				if(check_protocol == -1) {
					string = '/'+string;
				}
				location = string;
			});
			var check_dialog = $('#dialog').css('position');
			if(check_dialog == 'fixed') {
				return false;
			}
		}
	});
	$('li.child').hide();
	$('li.current,li.current li').show();
	$('li.current').siblings().show();
	$('li.toggle ul,fieldset.toggle ul').hide();
	$('li.toggle a,fieldset.toggle legend').click(function() {
		$(this).parent().siblings().children('.toggle ul').hide();
		$(this).siblings('ul').toggle();
	});
	$('textarea').css('overflow','hidden').css('resize','none').keyup(resize_textarea).keyup();
	$('form .meet').click(function() {
		$('form .required').each(function() {
			var tag = $(this).context.tagName;
			if(tag == 'DIV') {
				var value = $.trim($(this).text());
			} else {
				var value = $.trim($(this).val());
			}
			if(value == '') {
				$(this).addClass('error');
			}
		})
		if($('form .error').length > 0) {
			$(this).addClass('disabled').attr('disabled','disabled');
			return false;
		}
	});
	$('form .required').keyup(function() {
		var tag = $(this).context.tagName;
		if(tag == 'DIV') {
			var value = $.trim($(this).text());
		} else {
			var value = $.trim($(this).val());
		}
		if(value) {
			$(this).removeClass('error');
			if(tag == 'DIV') {
				$('textarea.error').removeClass('error');
			} else if(tag == 'TEXTAREA') {
				$('div.error').removeClass('error');
			}
		}
		if($('form .error').length == 0) {
			$('form .meet').removeClass('disabled').removeAttr('disabled');
		}
	});
	var search_terms = $('#search_form input.text').val();
	$('#search_form input.text').focusin(function() {
		if($(this).val() == search_terms) {
			$(this).val('');
		}
	});
	$('#search_form input.text').focusout(function() {
		if($.trim($(this).val()) == '') {
			$(this).val(search_terms);
		}
	});
});

function $dialog(type,note,callback,input)
{
	if($.browser.msie && $.browser.version < 7) {
		return false;
	}
	$('body').append('<div id="overlay"></div><div id="dialog"></div>');
	$('#dialog').append('<div class="title">'+$l[type]+'</div><div class="inner"></div>');
	var output = '<form id="dialog_form" action="" method="post">'+note;
	if(type == 'prompt') {
		output += '<div class="space_top"><input type="text" class="input text" name="prompt" value="'+input+'" />';
	}
	output += '<div class="space_top">';
	output += '<input type="submit" class="ok button" value="'+$l['ok']+'" />';
	if(type == 'confirm' || type == 'prompt') {
		output += '<input type="submit" class="cancel button" value="'+$l['cancel']+'" />';
	}	
	output += '</div></form>';
	$('#dialog .inner').html(output);
	$('#overlay').css('opacity',0).animate({'opacity': 0.5},'fast');
	$('#dialog').css('opacity',0).animate({'opacity': 1},'fast')
	$('#dialog .ok').click(function() {
		if(type == 'confirm') {
			callback.call($(this),true);
		}
		if(type == 'prompt') {
			result = $('#dialog .input').val();
			callback.call($(this),result);
		}
	});
	$('#dialog .ok,#dialog .cancel,#overlay').click(function() {
		$('#overlay,#dialog').remove();
	});
	$('#dialog').click(function() {
		return false;
	});
}

function resize_textarea()
{
	if(this.rows == '' || this.rows < 1) {
		this.rows = 1;
	}
	while(this.clientHeight >= this.scrollHeight && this.rows > 1) {
		this.rows -= 1;
	}
	while(this.clientHeight < this.scrollHeight) {
		this.rows += 1;
	}
}
