$(document).ready(function(){

    $table = "";
    $field = "";
    $key   = "";
    $primary_key = "";
    $type  = "";
    $divID = "";

    //
    // Function to display an editable textarea to update a text
    //
    $(".edit").click(function () {      
       
        explodeData($(this).parent().attr('id'));
     
        if ($type == "blob") makeSTEditor($divID, true);
        if ($type == "string") makeSTEditor($divID, false)
        
        //hiding the edit-button and the static text
        $(this).hide(); 
        $(this).parent().children(".save").slideDown();
        $(this).parent().children(".close").slideDown();
        $(this).parent().children(".saveandclose").slideDown();

    });


    $(".close").click(function () {

        explodeData($(this).parent().attr('id'));
        
        var postdata = {};
        postdata.action = "select";
        postdata.table = $table;
        postdata.field = $field;
        postdata.where = $primary_key + " LIKE '" + $key + "'";

        $div_text = $('#' + $divID);
      
        $.post("admin/ajax/ajax.lhmr.edit.php", postdata,
            function(data) {
                if(data != ""){
                    $div_text.html(data);
                }
            });
       
        $div_text.show();
        $("#" + $divID + "editor-ste").remove();
        $(this).hide();
        $(this).parent().children(".save").hide();
        $(this).parent().children(".saveandclose").hide();
        $(this).parent().children(".edit").show();

        return false;
    });

    $(".save").click(function () {
       
        explodeData($(this).parent().attr('id'));

        $text = $("#" + $divID + "editor-frame").contents().find('#cms').html();

        if($text == null)   $text = $("#" + $divID + "editor-ste").val();
          
        var postdata = {};
        postdata.action = "update";
        postdata.table = $table;
        postdata.field = $field;
        postdata.value = $text;
        postdata.primary_key = $primary_key;
        postdata.key = $key;

        $.post("admin/ajax/ajax.lhmr.edit.php", postdata,
            function(data) {
                if(data != ""){
                    alert(data);
                }
            });
        return false;
    });


     $("#dropdownedit").change(function () {

        $container = $(this).parent();

        $data  = $container.attr('id').split('&');
        $table = $data[0];
        $field = $data[1];
        $keyfield = $data[2];
        $keyvalue   = $data[3];
        
        var postdata = {};
        postdata.action = "update";
        postdata.table = $table;
        postdata.field =  $field;
        postdata.value = $(this).val();
        postdata.primary_key = $keyfield;
        postdata.key = $keyvalue;

        $.post("admin/ajax/ajax.lhmr.edit.php", postdata,
            function(data) {
                if(data != ""){
                    alert(data);
                }
            });
        return false;
    });


      $(".saveandclose").click(function () {

        $(this).parent().children(".save").click();
        $(this).parent().children(".close").click();
        return false;
    });


        $(".delete").click(function () {

    $container = $(this).parents('.contcontainer');

        $data  = $container.attr('id').split('&');
        $table = $data[0];
        $keyfield = $data[1];
        $keyvalue   = $data[2];
        $name   = $data[3];




         var x=window.confirm("Er du sikker på at du vil slette aktiviteten '"+$name +"'");
         if (!x) return false;

    

     
        postdata = {};
        
        postdata.action = 'delete';
        postdata.table = $table;
        postdata.keyfield = $keyfield;
        postdata.keyvalue = $keyvalue;


        $.post("admin/ajax/ajax.lhmr.edit.php", postdata,
            function(data) {
                if(data != ""){
                    alert(data);
                }
            });
            
        $container.remove();
        return false;
    });

     $("#hideadminlinks").click(function () {
         if($(this).hasClass('hideadminlinks')){
             $(".adminlinks").hide();
             $(this).removeClass('hideadminlinks');
             $(this).html("Vis redigeringsknapper")
         } else {
             $(".adminlinks").show();
             $(this).html("Gjem redigeringsknapper")
             $(this).addClass('hideadminlinks');
         }


        $(this).parent().children(".save").click();
        $(this).parent().children(".close").click();
        return false;
    });

    function makeStringEditor($divID){
        $div_text = $('#'+$divID);
        $div_cont = $div_text.parent();

        $textfield = $(document.createElement("input"))
        .css( "width",  40)
        .attr("id", $divID + "editor-ste")
        .val($div_text.text());

        $div_text.hide();
        $("#"+$divID).before($textfield);
 

    }

    function makeSTEditor($divID, $showOptions) {
        $div_text = $('#'+$divID);
        $div_cont = $div_text.parent();

        $STEditor = $divID + "editor";

        $textarea = $(document.createElement("textarea"))
        .css( "width",  $div_text.css("width"))
        .css( "height", $div_text.css("height"))
        .attr('id', $STEditor)
        .attr('name', $STEditor)
        .text($div_text.html());

        $div_text.hide();
        $div_text.after($textarea);

   
        var expr =  $STEditor + " = new SimpleTextEditor('" + $STEditor + "', '" + $STEditor + "', " + $showOptions + ");";
       eval(expr);

        eval($STEditor  + ".init()");

        if(!$showOptions){
$("#" + $divID + "editor-frame").css("margin-top","-10px");

        $editbody = $("#" + $divID + "editor-frame").contents().find('#cms');
        $editbody.css('font-size',$div_text.css('font-size'));
        $editbody.css('font-weight',$div_text.css('font-weight'));
        $editbody.css('font-style',$div_text.css('font-style'));
        $editbody.css('color',$div_text.css('color'));
        $editbody.css('margin-top','0px');
        }
    
     

    }

    function explodeData($compound_string){
        $data  = $compound_string.split('&');
        $table = $data[0];
        $field = $data[1];
        $key   = $data[2];
        $primary_key = $data[3];
        $type  = $data[4];
        $divID = $data[5];
    }
});



