$(document).ready(function(){
	con.group('document.ready called from admin.js');
	$("div.editbrand").children("form").submit(admin.brand.submitChanges);
	$("#toggleUniqueContentEditor").click(admin.managePageInfo.toggleDisplay);
	$("#editUniqueContent").submit(admin.managePageInfo.post);
	con.groupEnd();
});

var admin = {
	brand : {
		submitChanges : function () {
			con.group('submitChanges called');

			var newDescription = $(this).children("textarea").val();
			var brandId = parseInt($(this).attr("action").substr(1));
			log('found description: ' + newDescription);
			log('found brand Id: ' + brandId);

			var postData = {
				"newDescription": newDescription,
				"brandId": brandId
			};

			con.groupEnd();
			$.ajax({
				type: "POST",
				url:  "/ajax/updateBrandDescription.php",
				data: postData,
				success: function(html) {
					log('successfully posted brandtext');
					log('server response: ' + html);
					admin.brand.resynchronizeBrandText(newDescription);
				}
			});

			return false;
		},
		resynchronizeBrandText : function (text) {
			log('resynchonizeBrandText called');
			log(text);
			$("#branddescription").html(text);
		}
	},
	managePageInfo : {
		toggleDisplay : function () {
			if ($("#editUniqueContent").css("display") == "none") {
				$("#editUniqueContent").show();
			} else {
				$("#editUniqueContent").hide();
			}
		},
		post : function () {
			var postData = {};
			$("#editUniqueContent input[name^=]").each(function () {
				postData[$(this).attr("name")] = $(this).attr("value");
			});
			$.ajax({
				type:"POST",
				url: "/ajax/setUniquePageContent.php",
				data: postData,
				success: function(html) {
					log(html);
					window.location.href = window.location.pathname;
				}
			});
			log(postData);
			return false;
		}
	}
};
