﻿$().ready(function () {

    //for language 
    $('#languagenav > select').change(  function(){   
            var newlang=$(this).val(); 
            $.ajax({ type: "POST", url: blogpub.action_language, data: "lang="+newlang,
            success: function(){ $('.inlinemessage').show().html("Switching Language"); window.location.reload(); }
        })  });   
    
    //setup default form behaviors via classes 
    $('.edit').click( function() { admin.showedit(this); } );
    $('.delete').click( function() { admin.showdelete(this); } );    
    $('.add').click( function() { admin.showadd(); } );    

    //$('.add').click(function() { $('.addplaceholder').load( $('.addlink').html() ); });
});


admin.showadd = function(usersettings)
{
    defaultsettings = { action : $('.addlink:first').html(),
                        targetcontainer : $('.addplaceholder') }
    
    var settings = $.extend(defaultsettings, usersettings);      

    
    $('.editplaceholder').hide().remove();
    
    //load add form into add container
    settings.targetcontainer.load( settings.action );    

}

//show edit form underneath current row
admin.showedit = function(caller,usersettings)
{
    //default settings
    defaultsettings = { action : $('.editlink:first').html() + '/' + $(caller).parent().parent().attr('title') ,
                        targetcontainer : $(caller).parent().parent() }

    //merge default settings with user passed settings      
    var settings = $.extend(defaultsettings, usersettings);      
    
    //console.log(settings.action); console.log(settings.targetcontainer);
    
    $('.editplaceholder').remove();
    $('.addplaceholder').empty();
    
    settings.targetcontainer.after("<div class='editplaceholder'></div>");
    $('.editplaceholder').load(settings.action);
}

//confirm delete then delete row and fade out column
admin.showdelete = function(caller,usersettings)
{
    //default settings
    defaultsettings = { action : $(caller).parents('.editlist').find('.deletelink:first').html(),
                        targetcontainer : $(caller).parent().parent(),
                        idtype : $(caller).parents('.editlist').find('.deletelink:first').attr('title')
                       }

    //merge default settings with user passed settings      
    var settings = $.extend(defaultsettings, usersettings);      
    
    // console.log(settings.action);  console.log(settings.targetcontainer);
    
    if (confirm("Are you sure you want to delete " + $(caller).parent().parent().find('.col1').html() + "?"))
    { 
        $('.editplaceholder').remove();
        settings.targetcontainer.next('br').remove().next('br').remove();// settings.targetcontainer.next('br').remove();
        settings.targetcontainer.fadeOut("1000", function(){ $(this).remove()});

        var args = "'" + settings.idtype + "':'" + $(caller).parent().parent().attr('title') + "'";
        var pdata = eval (  "({" + args + "})" );
        
            $.ajax({
            type: "POST",
            data: pdata  , 
            url: settings.action
        });
    }
}

