$(document).ready(function(){
    $(document).on('click', '#generalUpdate', generalUpdate);
    $(document).on('click', '#socialUpdate', socialUpdate);
    $(document).on('click', '#apiKeyUpdate', apiKeyUpdate);
    $(document).on('click', '#mailUpdate', mailUpdate);
    $(document).on('click', '#sitemapUpdate', sitemapUpdate);
    $(document).on('click', '#commentUpdate', commentUpdate);
    $(document).on('click', '#paginationUpdate', paginationUpdate);
    $(document).on('click', '#layoutUpdate', layoutUpdate);
    $(document).on('click', '#cacheUpdate', cacheUpdate);
    $(document).on('click', '#adsUpdate', adsUpdate);
    $(document).on('click', '#defaultStyleChange', defaultStyleChange);
    $(document).on('click', '#clearAllCache', clearAllCache);

    // Comment Facebook
    $('#comment_facebook').on('switchChange.bootstrapSwitch', function(event, state) {
        if ($('#comment_facebook').bootstrapSwitch('state')){
            $('#comment_facebook_key').show(500);
        } else {
            $('#comment_facebook_key').hide(500);
        }
    });

    // Comment Disqus
    $('#comment_disqus').on('switchChange.bootstrapSwitch', function(event, state) {
        if ($('#comment_disqus').bootstrapSwitch('state')){
            $('#comment_disqus_username').show(500);
        } else {
            $('#comment_disqus_username').hide(500);
        }
    });

    // Api Key (Pagepeeker)
    $('.pagepeeker_api_status').change(function() {
        if (this.value == 1) {
            $('#pagepeeker_api').show(500);
        } else {
            $('#pagepeeker_api').hide(500);
        }
    });
});

var generalUpdate = function(){

    var url = $(this).parents('form').attr('action');

    /* CkEditor Update */
    for (var add in CKEDITOR.instances){
        CKEDITOR.instances[add].updateElement();
    }

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var socialUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var apiKeyUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var mailUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var sitemapUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var commentUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var paginationUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var layoutUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var cacheUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};

var adsUpdate = function(){

    var url = $(this).parents('form').attr('action');

    $(this).ajaxHandler(url, {
        data : $(this).parents('form').serialize(),
        success : function(response){
            if(response.status){
                swal("Updated!", response.message, "success");
            }else{
                swal("Warning!", response.message.join("\r\n"), "error");
            }
        }
    })
    return false;
};


var defaultStyleChange = function(){

    swal({
        title: "Warning",
        text: "Are you sure?",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "Cancel",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes",
        closeOnConfirm: false }, function(){

        var style = [{
            header_bgc : '#006ac1',
            header_link_c : '#ffffff',
            header_link_hc : '#006ac1',
            header_link_hbc : '#ffffff',
            button_bgc : '#007ee5',
            button_fc : '#ffffff',
            button_bc : '#007ee5',
            button_hbgc : '#0c7ad3',
            button_hfc : '#ffffff',
            button_hbc : '#00559b',
            search_button_bgc : '#409945',
            search_button_fc : '#ffffff',
            search_button_bc : '#004074',
            search_button_hbgc : '#26882c',
            search_button_hfc : '#ffffff',
            search_button_hbc : '#004074',
            module_title_bgc : '#f3f3f5',
            module_title_fc : '#007ee5',
            footer_bgc : '#47505d',
            footer_tbgc : '#3d464d',
            footer_fc : '#ffffff',
            fixed_menu_bgc : '#006AC1',
            fixed_menu_lc : '#ffffff',
            fixed_menu_lhc : '#ffffff',
            fixed_menu_lhbc : '#3e90d3',
            fixed_menu_bbc : '#ffffff',
            module_bc : '#c9ced1',
            module_bhc : '#c9ced1',
            module_shc : '#f3f3f5',
            module_subtitle_bgc : '#f3f3f5',
            module_subtitle_fc : '#4a5166'
        }];

        $.each(style[0], function(key, value){
            $('#' + key).val(value);
            $('#' + key + '_bg .input-group-addon i').css('background-color', value);
        });

        $('#layoutUpdate').trigger( "click" );

    });

    return false;
};

var clearAllCache = function(){

    var $btn = $(this);

    swal({
        title: "Warning",
        text: "Are you sure?",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "Cancel",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, clear all cache!",
        closeOnConfirm: false }, function(){

        var url = $btn.attr('href');

        $btn.ajaxHandler(url, {
            success : function(response){
                if(response.status){
                    swal("OK!", response.message, "success");
                }
            }
        })
    });

    return false;
};