/*
 * admin.js
 *
 * @name        admin
 * @package     www.foto-de-sign.de
 * @category    script
 * @author      Marcel Jänicke Software <mail@mj-software.net>
 * @copyright   © Marcel Jänicke Software, 2011 - Alle Rechte vorbehalten.
 * @since       30.09.2011
 * @modified    11.12.2011
 */
// scrollTop value
var scroll;
// internal interval
var interval;

$(window).ready(function() {
    $(".draggable.text.center").draggable({ disabled: true });
    bindButtons();
});

function bindButtons() {
 // category
    $(".save").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    save : true,
	    id : $(this).next().next().val(),
	    name: $(this).prev().val(),
	    oldName: $(this).next().next().next().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".delete").click(function() {
	var check = confirm("Soll diese Kategorie wirklich gelöscht werden? Alle Bilder in dieser Kategorie werden gelöscht.");
	if (check) {
	    $.post("?page=admin", {
		ajax : true,
		delete : true,
		id : $(this).next().val(),
		category: $(this).prev().prev().val(),
	    }, function(data) {
		manageDialog(data);
	    });
	}
    });
    $(".plus").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    plus : true,
	    position : $(this).prev().val(),
	    id : $(this).prev().prev().prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".minus").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    minus : true,
	    position : $(this).prev().prev().val(),
	    id : $(this).prev().prev().prev().prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".add").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    add : true,
	    category : $(this).prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    
    // images
    $(".saveImage").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    saveImage : true,
	    id : $(this).next().next().val(),
	    start: $(this).prev().prev().prev().is(":checked"),
	    line: $(this).prev().prev().is(":checked"),
	    text: $(this).prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".deleteImage").click(function() {
	var check = confirm("Soll dieses Bild wirklich gelöscht werden?");
	if (check) {
	    $.post("?page=admin", {
		ajax : true,
		deleteImage : true,
		id : $(this).next().val()
	    }, function(data) {
		manageDialog(data);
	    });
	}
    });
    $(".plusImage").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    plusImage : true,
	    position : $(this).prev().val(),
	    id : $(this).prev().prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".minusImage").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    minusImage : true,
	    position : $(this).prev().prev().val(),
	    id : $(this).prev().prev().prev().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
    $(".saveText").click(function() {
	$.post("?page=admin", {
	    ajax : true,
	    saveText : true,
	    text : $(this).prev().val(),
	    id : $(this).next().val()
	}, function(data) {
	    manageDialog(data);
	});
    });
}

function manageDialog(data) {
    scroll = $(".draggable").scrollTop();
    $(".draggable").remove();
    $("#iframe").before(data);
    $(".draggable").scrollTop(scroll);
    bindButtons();
}

function checkIframe() {
    $(".draggable").css("cursor", "wait");
    return true;
}
