$(function()
{
	$('.tooltip').tooltip({
		showURL: false,
		bodyHandler: function()
		{
			var tip = $(this).attr('tip');
			
			if (!tip)
			{
				return $(".tip",this).clone().css('display','block');
			}
			else
			{
				return $("#" + tip).clone().css('display','block');
			}
		}
	});
	
	$(".lightbox").lightBox();
	
	// search form
	var showDistrictSelect = function()
	{
		$("#search_dist_1,#search_dist_2").hide().attr('disabled','disabled');
		
		switch ($("#search_location")[0].selectedIndex)
		{
			case 1:
			$("#search_dist_1").show().removeAttr('disabled','disabled');
			$("#search_dist").show();
			break;
			case 2:
			$("#search_dist_2").show().removeAttr('disabled','disabled');
			$("#search_dist").show();
			break;
			default:
				$("#search_dist").hide();
			break;
		}
	};
	
	$("#search_location").bind("change",showDistrictSelect);
	showDistrictSelect();
	
	var showDistrictSelect2 = function()
	{
		$("#submit_dist_1,#submit_dist_2,#submit_dist_0").hide().attr('disabled','disabled');
		
		switch ($("#submit_location")[0].selectedIndex)
		{
			case 1:
			$("#submit_dist_1").show().removeAttr('disabled','disabled');
			break;
			case 2:
			$("#submit_dist_2").show().removeAttr('disabled','disabled');
			break;
			default:
			$("#submit_dist_0").show().removeAttr('disabled','disabled');
			break;
		}
	};
	
	
	if ($("#submit_location").length)
	{
		$("#submit_location").bind("change",showDistrictSelect2);
		showDistrictSelect2();
	}
	
	// submit form
	$("#pt_1,#pt_2").hide();
	
	$.validator.addMethod('checkCatalog', function(v,el)
	{
		var n;
		
		if (v == 1)
		{
			n = "#pt_1";
		}
		else if (v == 2)
		{
			n = "#pt_2";
		}
		
		if (n)
		{
			var map = {
				3: 1,
				4: 2,
				5: 3,
				6: 4,
				7: 5,
				8: 6,
				9: 7,
				10: 8,
				11: 9
			};
		
			if ($(n).val() != '')
			{
				$("#ptype").val(map[$(n).val()]);
				
				return true;
			}
		}
		
		return false;
	}, "???");
	
	$("#submitForm").validate({
		errorLabelContainer: $("#submitFormError"),
		rules: {
			"input[Title]": "required",
			"input[Introduction]": "required",
			"input[Attributes][author]": "required",
			"input[Attributes][author_phone]": "required",
			"__catalog": "checkCatalog"
		},
		messages: {
			"input[Title]": "Bạn phải nhập vào tiêu đề.",
			"input[Introduction]": "Bạn phải nhập vào nội dung mô tả.",
			"input[Attributes][author]": "Bạn phải nhập vào tên bạn",
			"input[Attributes][author_phone]": "Bạn phải nhập vào số điện thoại.",
			"__catalog": "Bạn phải chọn loại tin đăng"
		}
	});
});

var productSubmitForm = {
	changeType: function(v)
	{
		$("#pt_1,#pt_2").attr('disabled','disabled').hide();
		
		var n;
		
		if (v == 1)
		{
			n = "#pt_1";
		}
		else if (v == 2)
		{
			n = "#pt_2";
		}
		
		if (n)
		{
			$(n).removeAttr('disabled').show();
		}
	}
};