function screen(CityID) {
	$("#Form_ShopID option[value='"+CityID+"']").attr("selected", "selected");
	return false;	
}

$(document).ready(function(){
	
	if ($.autogrow) $('#CommentForm textarea, .SimpleRequestForm textarea').livequery(function(){
		$(this).autogrow();
	});
	
	$('a.RemoveFile').live('click', function(){
		var UploadIDs = $("#Form_UploadIDs").val();
		if(UploadIDs){
			UploadIDs = UploadIDs.split(",");
			var id = $(this).attr('rel');
			for(var i = 0; i < UploadIDs.length; i++){
				if(UploadIDs[i] == id){
					UploadIDs.splice(i, 1);
					break;
				}
			}
			
			$("#Form_UploadIDs").val( UploadIDs.join(",") );
			$(this).closest('ol.UploadDataList').fadeOut('fast');
		}
		return false;
	});
	
	if ($.fn.popup) $('a.RequestStatus').popup({
		afterSuccess: function(json, sender){
			if (sender.Targets) gdn.processTargets(sender.Targets);
		}
	});
	
	if($("#Form_CityID").length > 0){
		
		$("li.OnlineCityMenu").show('slow');
		$("#Form_CityID").change(function(){
			var CityID = $(this).val();
			var Map = $('li.OnlineCityMoscow').get(0);
			CityID == 754350 ? $(Map).show() : $(Map).hide();
			var url = gdn.combinePaths(gdn.definition('WebRoot', '/'), 'online/cityshops/'+CityID);
			$.getJSON(url, function(json){
				//console.log(json);
				var ShopSelect = $("#Form_ShopID").html('<option value=""/>').get(0);
				$.each(json, function(){
					$(ShopSelect).append('<option value="%1">%2</option>'.replace("%1", this.ShopID).replace("%2", this.Address));
				});
				var SelectedShopID = gdn.definition("SelectedShopID", false);
				if(SelectedShopID > 0) $("#Form_ShopID").val(SelectedShopID);
			});
		});
		$("#Form_CityID").change();
	}
	
	if($("#Form_KitchenModel").length > 0){
		$("#Form_KitchenModel").change(function(){
			var kitchenid = $(this).val();
			if(!kitchenid) return;
			var url = gdn.combinePaths(gdn.definition('WebRoot', '/'), 'online/kitchencolors/'+kitchenid);
			$.getJSON(url, function(json){
				if(!json.FormSaved) return;
				$("#Form_KitchenColorText").replaceWith(json.DropDown);
				var model = $('<strong><a target="_blank" href="'+'/catalog/'+json.Model.ID+'/">'+json.Model.Name+'</a></strong>');
				$("#KitchenImage").fadeOut("fast", function(){
					$(this).html(json.KitchenImage).append(model)
					.fadeIn("fast");
				});
			});
		});
		$("#Form_KitchenModel").change();
		

		$("#KitchenImage a[href^='#']").livequery(function(){
			$(this).fancyZoom({
				scaleImg: true,
				closeOnClick: true
			});
		});

	}
	
	$('a.CloseRequest, a.ResendNotify').click(function(){
		var self = this;
		$.ajax({
			type: "POST",
			url: $(self).attr('href'),
			data: 'DeliveryType=BOOL&DeliveryMethod=JSON',
			dataType: 'json',
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$.popup({}, textStatus);
			},
			success: function(json){
				if(json.ErrorMessage) return gdn.inform(json.ErrorMessage, false);
				//$(self).text(json.LinkText);
				if(json.StatusMessage) gdn.inform(json.StatusMessage);
			}
			
		});
		return false;
	});
	
	$("#JumpToRequestModule form").submit(function(){
		var Num = $(this).find('#Form_JumpRequestID').val();
		var url = gdn.combinePaths(gdn.definition('WebRoot', '/'), 'online/request/' + Num);
		document.location = url;
		return false;
	});
	
	$("#Form_Status").livequery(function(){
		$(this).change(function(){
			var totaloptions = $(this).find('option').length - 1;
			var libox = $(this).parents('li').first();
			var form = $(this).parents('form').first();
			var status = $(this).val();
			var box = $('li.'+status+'Box', form);
			var boxes = $('li[class$=Box]', form).not(box).hide();
			box.fadeIn('fast');
			if (totaloptions != boxes.length) libox.addClass('StatusWithInfo')
			else libox.removeClass('StatusWithInfo');
		});
		$(this).change();
	});
	
});

