$(document).ready(function() {
			$(".changeSelect").each(function() {
				var control = $(this).closest('.verifiable');
				var dialogContent = control.find(".object");
				var title = control.find(".description").html();
				dialogContent.attr("title", title);
				dialogContent.dialog( {
					modal : true,
					minWidth : 400,
					autoOpen : false
				});

				$(this).click(function() {
					dialogContent.dialog("open");
				});
				
				dialogContent.find(".item").click(function() {
					dialogContent.dialog("close");
					changeSelect(control, $(this).find(".key").text(), $(this).find(".value").text());
					return false;
				});
			});
			
			$(".changeManySelect").each(function() {
				var control = $(this).closest('.verifiable');
				loadSelectMany(control);
				var dialogContent = control.find(".object");
				var title = control.find(".description").html();
				var name = control.find(".tableName").text();
				dialogContent.attr("title", title);
				dialogContent.dialog( {
					modal : true,
					minWidth : 400,
					autoOpen : false,
					buttons: {
						Ok: function() {
						$(this).dialog("close");
						var target = control.find(".target");
						target.empty();
						dialogContent.find(".item").each(function(){
							if($(this).find("input:first").attr("checked")){
								target.append("<input type='hidden' name='" + name + "[]' value='" + $(this).find(".key").text() + "' />");
								target.append("<p>" + $(this).find(".value").text() + " </p>");
							}
						});
					}
				}

				});

				$(this).click(function() {
					dialogContent.dialog("open");
				});
			});
			
			var jSelectControls = $(".selectRow");
			if(jSelectControls.length > 0){
				$("#multiselectButton").show().submit(function(){
					var jForm = $(this);
					jSelectControls.each(function(){
						if($(this).attr("checked")){
							jForm.append("<input type='hidden' name='id[]' value='" + $(this).val() + "' />");
						}
					});
				});
			}
			
			$(".selectAll").change(function(){
				var checked = $(this).attr("checked");
				$(this).closest("table").find(".selectRow").each(function(){
					$(this).attr("checked", checked);
					$(this).change();
				});
			});
			
			$(".selectRow").change(function(){
				if($(this).attr("checked"))
				{
					$(this).closest("tr").addClass("selectedRow");
				}
				else
				{
					$(this).closest("tr").removeClass("selectedRow");
				}
			});
		});

function changeSelect(targetElement, key, value) {
	var input = targetElement.find("input.value");
	var p = targetElement.find("p.target");
	p.text(value);
	input.attr("value", key);
	input.change();
}

function makeShadow(jEl) {
	var shadow = $('<div class="shadow"><img src="admin/images/shadow-loader.gif" /></div>');
	shadow.css("position", "absolute");
	shadow.offset(jEl.offset());
	shadow.height(jEl.height());
	shadow.width(jEl.width());
	var img = shadow.find("img");
	jEl.after(shadow);
	var margin = (jEl.height() - img.height()) / 2;
	img.css("margin-top", margin);
	jEl.resize(function() {
		shadow.height(jEl.height());
		shadow.width(jEl.width());
	});
}

function removeShadow() {
	$(".shadow").remove();
}

function loadSelectMany(jControl) {
	var selected = [];
	jControl.find(".target input[type='hidden']").each(function(){
		selected.push($(this).val());
	});
	
	jControl.find(".object .item").each(function(){
		var key = $(this).find(".key").text();
		if($.inArray(key, selected) > -1){
			$(this).find("input[type='checkbox']").attr("checked","checked");
		}
	});
}
