function setPositions()
{
    $("ul#positions li").live('click', function()
    {
        $('ul#positions li').removeClass('active');
        $(this).addClass('active');
        shownMenuDetail($(this).attr('id'));
        return false;
    });


    try {
        sortUp();
    } catch(e) {};
    try {
        sortDown();
    } catch(e) {};
    try {
        addPosition();
    } catch(e) {};
    try {
        deletePosition();
    } catch(e) {};
    try {
        deletePositions();
    } catch(e) {};
    try {
        setSortablePositions();
    } catch(e) {};
}

function showPosition()
{
    //	$('#subnavi li:first.ui-selected').live('click', function()
    // {
    var id_string = $('#subnavi li:first.ui-selected').attr('id');
    shownMenuDetail(id_string);
//	});
}

function setSortablePositions()
{
    $('#positions').sortable(
    {
        'stop':savePositions,
        'connectWith':['#positions'],
        'tolerance':'pointer',
        'placeholder':'placeholder',
        'cursor':'pointer',
        //			'cancel':'.folder',
        'items':'li'
    }).disableSelection();
}

function savePosition(id_string)
{
    var param = 'id_string=' + id_string;
    var html = getAjax('save_position', 'text', param);
}

function savePositions()
{
    var object = {};
    $('#positions li').each(function(i)
    {
        var name = strip_tags_with_content($(this).html());
        object[i] = {
            "id_string": $(this).attr('id'),
            "name": name
        };
    });
    var param = {
        'arr_positions': object
    };
    var html = getAjax('save_positions', 'json', param);
}

function deletePosition()
{
    $("img.delete_position").live('click', function()
    {
        var id_string = $(this).parent().attr('id');
        $(this).parent().remove();

        var param = 'id_string=' + id_string;
        var html = getAjax('delete_position', 'text', param);

        return false;
    });

    $('#btn_delete_position').unbind('click').click(function()
    {
        var id_string = $('#positions li.active').attr('id');
        $('#positions li.active').remove();

        var param = 'id_string=' + id_string;
        var html = getAjax('delete_position', 'text', param);

        return false;
    });
}

function deletePositions()
{
    $('.submit_delete').unbind('click').click(function()
    {
        var param = '';
        var html = getAjax('delete_positions', 'text', param);
        $('#positions').html(html);
        return false;
    });
}

function loadPositions()
{
    var html = "Wollen Sie die gespeicherten Elemente laden ?";
    $("#dialog").html(html);

    $("#dialog").dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            'Ja': function()
            {
                $.ajax(
                {
                    type: "POST",
                    url: getAjaxUrl('load_positions'),
                    success: function(data)
                    {
                        $('#positions').html(data);
                        $("#dialog").dialog('close');
                        setPositions();
                    }
                });

            },
            "Nein": function() {
                $(this).dialog('close');
            }
        }
    }).dialog('open');
}


/**
 * dropable und sortable aktivieren

function setDraggable()
{

debug('setDraggable()');

	//$('#subnavi ul li, #search_results ul li').draggable("destroy");	// destroy alle draggable elemente
//	setPositions();	// positions galama

	$('#subnavi ul li.ui-selected, #search_results ul li.ui-selected').draggable(
	{
		connectToSortable: '#positions',
		helper: 'clone',
		revert: 'invalid',
		start: function(){},
		drag: function(){},
		stop: function(event, ui)
		{
			$('#positions li.default').remove();	// entfernt den default text
			var id_string = $(ui.helper).attr('id');

			// andere selektierte elemente hinzufuegen.
			$('ul li.ui-selected').each(function(i)
			{
				var id_string_more = $(this).attr('id');
				if (id_string_more != id_string)
				{
					var pos = $('#' + id_string_more).clone();

//					html = $(pos).html();
//					$(pos).html(html + getDeleteImg());

					pos.insertAfter('#positions li:last.ui-selected');
				}
			});

			$('#positions li').each(function(i)
			{
				// img anhaengen beim drag element
				if ($(this).find('img.delete_position').length == 0)
				{
					$(this).html(strip_tags($(this).html()));
					$(this).append(getDeleteImg());
				}
			});

			deselectSelectable();
			savePositions();
		}
	});

}
*/

function setDraggable()
{
    // nicht mehr live verwenden da der IE7 nicht mit klar kommt
    $('ul.bausteine').delegate('li', 'click', function()
    {
        shownMenuDetail($(this).attr('id'));
        $(this).addClass('ui-selected');
        return false;
    });
    // nicht mehr live verwenden da der IE7 nicht mit klar kommt
    $('ul.symbolgruppen ul.systeme').delegate('li', 'click', function()
    {
        shownMenuDetail($(this).attr('id'));
        $(this).addClass('ui-selected');
        return false;
    });
    
    // creates the selected variable
    // we are going to be storing the selected objects in here
    var selected = $([]), offset = {
        top:0,
        left:0
    };
    // console.log will outout
    // Object length=0 prevObject=Object jquery=1.2.6
    /*	console.log("This is the value of selected when it is created " +selected + ""); */

    // initiate the selectable id to be recognized by UI
    $('ul.bausteine, ul.symbolgruppen ul.systeme').selectable({
        tolerance: 'fit'
    });

    // declare draggable UI and what we are going to be doing on start
    $('ul.bausteine li, ul.symbolgruppen ul.systeme li').draggable(
    {
        connectToSortable: '#positions',
        helper: 'clone',
        revert: 'invalid',
        stack:	'true',
        start: function(ev, ui)
        {
            $(this).is(".ui-selected") || $(".ui-selected").removeClass("ui-selected");
            /*console.log("The value of 'this' currently is: "+this); */

            selected = $(".ui-selected").each(function()
            {
                var el = $(this);
                /* console.log("The value of el is: "+el); */
                el.data("offset", el.offset());

            //					$(this).append(getDeleteImg());

            });
            /*	console.log("The new value of selected is now "+selected); */
            offset = $(this).offset();
        /*	console.log("The value of top value is "+offset.top+" and the left value is "+offset.left); */
        },
        drag: function(ev, ui)
        {
            // create two new variables
            // dt and dl, subract the ui.position and the offset position
            var dt = ui.position.top - offset.top, dl = ui.position.left - offset.left;
        /* console.log("The value of dt is "+dt+" and is equal to "+ui.position.top+" - "+offset.top); */
        /*  console.log("The value of dl is "+dl+" and is equal to "+ui.position.left+" - "+offset.left); */
        /*  console.log("The value of ui.position.top is "+ui.position.top); */
        /*  console.log("The value of ui.position.left is "+ui.position.left); */
        /*  console.log("The value of offset.top is "+offset.top); */
        /*  console.log("The value of offset.left is "+offset.left); */

        // take all the elements that are selected expect $("this"), which is the element being dragged and loop through each.
        //debug(selected);
        //selected.not(this).each(function()
        //{
        // create the variable for we don't need to keep calling $("this")
        // el = current element we are on
        // off = what position was this element at when it was selected, before drag
        //	var el = $(this), off = el.data("offset");
        //	el.css({top: off.top + dt, left: off.left + dl});

        //$(this).clone().insertAfter('#positions li#' + id_string);
        //});

        },
        stop: function(ev, ui)
        {
            var id_string = $(ui.helper).attr('id');
            selected.not('li#' + id_string).each(function()
            {
                var pos = $(this).clone();
                pos.clone().insertAfter('#positions li#' + id_string);
            });

            //default text loeschen
            if ($('#positions li').length > 1)
            {
                $('#positions li.default').remove();	// entfernt den default text
            }

            selected.each(function()
            {
                $(this).removeClass('ui-selected');
            });

            // img anhaengen
            $('#positions li').each(function(i)
            {
                if ($(this).find('img.delete_position').length == 0)
                {
                    $(this).html(strip_tags($(this).html()));
                    $(this).append(getDeleteImg());
                }
            });

            deselectSelectable();
            savePositions();
        }
    });
}

function sortUp()
{
    $("#btn_sort_up_position").unbind('click').click(function()
    {
        var current = $('#positions li.active');
        var prev = current.prev();

        prev.insertAfter(current);
        savePositions();
        return false;
    });
}

function sortDown()
{
    $("#btn_sort_down_position").unbind('click').click(function()
    {
        var current = $('#positions li.active');
        var next = current.next();

        next.insertBefore(current);
        savePositions();
        return false;
    });
}

function addPosition()
{
    $("#btn_add_position").live('click', function()
    {
        $('ul li.ui-selected').each(function(i)
        {
            var id_string = $(this).attr('id');
            var current = $(this).clone();
            var html = $(current).html();
            $(current).html(html + getDeleteImg());

            if ($('#positions li.active').length != 0)
            {
                current.insertAfter('#positions li.active');
            }
            else
            {
                current.appendTo('#positions');
            }

            savePosition(id_string);
        });

        //default text loeschen
        if ($('#positions li').length > 1)
        {
            $('#positions li.default').remove();	// entfernt den default text
        }
		
        deselectSelectable();
    });
}
