

// init
$(document).ready(
	function()
	{
		//$("form,form input,form textarea, form .component, form .subentry, form .parent > div, form .child > div, div.linking").corner();

		//$("form, div.linking").css("background-color", "#B0C0A0");
		//$("form input,form textarea").css("background-color", "#EAFFE0");
		
		$("div.enlarge").click(
			function ()
			{
				var textarea = $(this).siblings("textarea:first");	
				var rows =  $(this).siblings("textarea:first").attr("rows")+3;
				textarea.attr("rows", rows);
			}
		);	
		$("div.shrink").click(
			function ()
			{
				var textarea = $(this).siblings("textarea:first");
				var rows = textarea.attr("rows")-3;
				if(rows < 1) { rows = 1; }
				textarea.attr("rows", rows);
			}
		);
		
		
		// create multilevel template selector		
		$("select[name='type']").each(
			function()
			{
				var value = $(this).val();
				$(this).next("span.templateMenu").find("select").hide()
				$(this).next("span.templateMenu").find("select#"+value).show();
				
				$(this).change(
					function()
					{
						var value = $(this).val();
						$(this).parent("div").attr("class", value);
						$(this).next("span.templateMenu").find("select").hide()
						$(this).next("span.templateMenu").find("select#"+value).show();
					}
				)
			}
		)
		
		// activate remove link button 
		$("span.unlink").corner();
		$("span.unlink").toggle(
			function()
			{
				$(this).parent("div").parent("div").removeClass("link").addClass("unlink");
				$(this).addClass("activated");
				$(this).text("cancel");
			},
			function()
			{
				$(this).parent("div").parent("div").addClass("link").removeClass("unlink");
				$(this).removeClass("activated");
				$(this).text("remove");
			}
		).css("cursor", "pointer");
		
		// create linking nav
		$("div.linking ul").hide()
		$("div.linking li.selected").each(
			function()
			{
				$(this).parents("ul").show();
				$(this).children("ul").show();
			}
		);
		$("div.linking li > span").click(
			function()
			{
				$("div.linking ul").hide();
				$(this).parents("ul").show();
				$(this).parent("li").children("ul").show();
			}
		).css("cursor", "pointer");
		$(".linking li span" ).draggable({ revert: true });
		$("#childrenList" ).sortable();
		$("#childrenList" ).droppable({
			hoverClass: 'hilight',
			accept : ".ui-draggable",
			drop: function(event, ui) { 
				var newLink = $("#childLink").clone();
				newLink.find("span.title").text(ui.draggable.text());
				newLink.find("input:hidden").val(ui.draggable.find("input:hidden").val());
				newLink.removeAttr("style");
				newLink.attr("id", "entry_"+ui.draggable.find("input:hidden").val());
				newLink.addClass("child");
				newLink.find(".subentry").corner();
				newLink.find("span.unlink").toggle(
					function()
					{
						$(this).parent("div").removeClass("link").addClass("unlink");
						$(this).addClass("activated");
						$(this).text("cancel");
					},
					function()
					{
						$(this).parent("div").addClass("link").removeClass("unlink");
						$(this).removeClass("activated");
						$(this).text("remove");
					}
				).css("cursor", "pointer");
				newLink.find("select[name='type']").each(
					function()
					{
						var value = $(this).val();
						$(this).next("span.templateMenu").find("select").hide()
						$(this).next("span.templateMenu").find("select#"+value).show();
						
						$(this).change(
							function()
							{
								var value = $(this).val();
								$(this).parent("div").attr("class", value);
								$(this).next("span.templateMenu").find("select").hide()
								$(this).next("span.templateMenu").find("select#"+value).show();
							}
						)
					}
				)
				$(this).append(newLink);
					
			}
		});		
		$("input[name='submit']:button").click( 
			function()
			{
				var data = new String();
				var entry = new Object;
				data = $(this).parents("form").find(".entry:first").attr("id") +"\n";
				$(this).parents("form").find(".entry:first").find("input:hidden, input:checked, select, input:text, textarea").each(
					function()
					{
						data += $(this).attr("name") + "  :  "+ $(this).val()+" \n";
						entry[$(this).attr("name")] = $(this).val();
					}
				);
				var links = new Object();	
				entry["parent[]"] = new Array();		
				entry["child[]"] = new Array();		
				entry["type[]"] = new Array();		
				entry["template[]"] = new Array();	
				entry["removeParent[]"] = new Array();	
				entry["removeChild[]"] 	= new Array();	
				$(this).parents("form").find(".link").each(
					function()
					{
						data +=  $(this).find("span.title").text() +  "  " +$(this).attr("id")+" : "+ $(this).attr("class")+ "  \n";
						
						if($(this).hasClass("parent"))
						{
							entry["parent[]"].push( $(this).find("input:hidden[name='id']").val() );
							//entry["child"].push( entry['id'] );
						}
						else
						{
							//entry["parent"].push( entry['id'] );
							entry["child[]"].push( $(this).find("input:hidden[name='id']").val() );
						}
						
						entry["type[]"].push($(this).find("select:visible[name='type']").val() );
						entry["template[]"].push( $(this).find("select:visible[name='template']").val() );
						
					}					
				);
				$(this).parents("form").find(".unlink").each(
					function()
					{
						if($(this).hasClass("parent"))
						{
							entry["removeParent[]"].push( $(this).find("input:hidden[name='id']").val() );
						}
						if($(this).hasClass("child"))
						{
							entry["removeChild[]"].push( $(this).find("input:hidden[name='id']").val() );
						}
					}
				);
				entry['commit'] = 'change';
				//var params = new Obkect(entry, links);
				
				var ajaxlocation = new String( window.location );
				var thislocation =new String( window.location );			
				ajaxlocation = ajaxlocation.replace(/\/[^\/]{1,}\.php/, '/ajax.php');					
				$.ajax({
					type : 'POST',
					data : $.param(entry, true),
					url : ajaxlocation,
					async : true,
					success:
					function(data)
					{
						thislocation = thislocation.replace(/(action=[^&]*$)|(action=[^&]*)/, 'action=view');
						window.location = thislocation;	
					}
				});
				
			}
		);
		
	}
);

