/* ------------------------------------------------------------------------
	Author: SlopeTracker (http://www.pluginexpert.com)
	Version: 1.0
------------------------------------------------------------------------- */
var stcore = {
    loadPage: function(action, data_post, message, callback_function) {
        jQuery('#message').fadeOut('fast');
        var post_data = "ajax=1&action="+action;
        if(data_post != '' || data_post != null){
            for( var key in data_post){
                post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            //url: "admin.php?page=home",
            data: post_data,
            cache: false,
            beforeSend: function(){
                jQuery("#loading").fadeIn("fast");
            }, //show loading just when link is clicked
            complete: function(){
                jQuery("#loading").fadeOut("fast");
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#inner_content").html(data);
                jQuery("#inner_content").append('<input type="hidden" value="'+action+'" id="current_action"');
                if(message){
                    jQuery('#message').html('<p>'+message+'</p>');
                    jQuery('#message').fadeIn('slow');
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
            }
        });
    },

    load_tooltip: function(){
        jQuery(document).ready(function(){
            jQuery('.desc_true').focus(function() {
                jQuery(this).parent().addClass('focus');
                jQuery(this).parent().next().css('visibility','visible');

            });
            jQuery('.desc_true').blur(function() {
                jQuery(this).parent().removeClass('focus');
                jQuery(this).parent().next().css('visibility','hidden');
            });

        });
    },

    on_submitform: function(form, callback_function){
        jQuery(document).ready(function(){
                jQuery(form).validate({
                    errorPlacement: function(error, element) {
                        //error.appendTo( element.parent());
                    },
                    submitHandler: function() {
                        jQuery("#loading").show('slow');
                        jQuery.post(jQuery(form).attr('action')+'&ajax=1', jQuery(form).serialize(),function(data, textStatus){
                            if(callback_function){
                                callback_function.call(this, data);
                            }
                        });
                    }

                });
        });
    },

    load_main_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#nav li a').click(function(){
                var action = jQuery(this).attr('action');
                stcore.loadPage(action);
                jQuery('#nav ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });

                if(jQuery(this).hasClass('have_child')){
                    jQuery('#side-nav').fadeIn();
                    jQuery('#side-nav ul').each(function(){
                        if(jQuery(this).attr('id') == action){
                            jQuery(this).fadeIn();
                        }else{
                            jQuery(this).css('display','none');
                        }
                    });
                    jQuery('#side-nav ul li').each(function(){
                        if(jQuery(this).children('a').attr('action') == action){
                            jQuery(this).addClass('current');
                        }else{
                            jQuery(this).removeClass('current');
                        }
                    });

                    jQuery('#inner_content').css('width','83%');
                    jQuery('#message').css('width','70%');
                }else{
                    jQuery('#side-nav').fadeOut();
                    jQuery('#inner_content').css('width','98.3%');
                    jQuery('#message').css('width','90%');
                }
            });
            //alert(jQuery(window).width());
        });
    },

    load_side_menu: function(){
        jQuery(document).ready(function(){
            jQuery('#side-nav li a').click(function(){
                var action = jQuery(this).attr('action');
                stcore.loadPage(action);
                jQuery('#side-nav ul li').each(function(){
                    var current_action = jQuery(this).children().attr('action');

                    if(current_action == action ){

                        jQuery(this).addClass('current');
                    }else{
                        jQuery(this).removeClass('current');
                    }
                });
            });
            jQuery('#side-nav li a.have_child').click(function(){
                jQuery('#side-nav').fadeIn();
            });
        });
    },

    sortColumn: function(){
        jQuery(document).ready(function(){
            jQuery('th.manage-column').css('cursor', 'pointer');
            jQuery('th.manage-column').click(function(){
                var current_action = jQuery('#current_action').val();
                var sort_type = jQuery(this).attr('sort_type');
                var sort_id = jQuery(this).attr('id');
                if(sort_type == null){
                    sort_type = 'asc';
                }
                var data_post = new Array();
                data_post['sort'] = sort_id;
                data_post['sort_type'] = sort_type;
                jQuery('#st_pagination ul li').each(function(){
                    if(jQuery(this).hasClass('current')){
                        data_post['cur_page'] = jQuery(this).children().html();
                    }
                });
                stcore.loadPage(current_action, data_post, '', function(data_post){
                    if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                        jQuery('#'+data_post['sort']).attr('sort_type','asc');
                        jQuery('#'+data_post['sort']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;</span>');
                    }else{
                        jQuery('#'+data_post['sort']).attr('sort_type','desc');
                        jQuery('#'+data_post['sort']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;</span>');
                    }
                });
            });
        });
    },

    shortAction: function(){
        jQuery(document).ready(function(){
            jQuery('div.short_action span').css('cursor', 'pointer');
            jQuery('div.short_action span').click(function(){
                var current_action = jQuery('#current_action').val();
                var action = jQuery(this).attr('action');
                var short_action = jQuery(this).attr('short_action');
                var id = jQuery(this).attr('id');
                var confirm_message = jQuery(this).attr('confirm');
                var data_post = new Array();
                data_post['id'] = id;
                data_post['short_action'] = short_action;

                if(action){
                    current_action = action;
                }

                if(confirm_message){
                    if(confirm(confirm_message)){
                        stcore.loadPage(current_action, data_post);
                    }
                }else{
                    stcore.loadPage(current_action, data_post);
                }
            });
        });
    },

    sendMessage: function(message){
        jQuery(document).ready(function(){
            jQuery('#message').html('<p>'+message+'</p>');
            jQuery('#message').fadeIn('slow');
        });
    },

    paging: function(page_num, action){
        var data_post = new Array();
        data_post['cur_page'] = page_num;

        jQuery('th.manage-column').each(function(){
            if(jQuery(this).attr('sort_type')){
                //alert(jQuery(this).attr('sort_type'));
                data_post['sort'] = jQuery(this).attr('id');
                if(jQuery(this).attr('sort_type') == 'desc')
                    data_post['sort_type'] = 'asc';
                else
                    data_post['sort_type'] = 'desc';
            }
        });

        stcore.pagingCallbackFunction(data_post);

        stcore.loadPage(action, data_post, '', function(data_post){
                if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                    jQuery('#'+data_post['sort']).attr('sort_type','asc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;</span>');
                }else{
                    jQuery('#'+data_post['sort']).attr('sort_type','desc');
                    jQuery('#'+data_post['sort']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;</span>');
                }
        });
    },
    pagingCallbackFunction: function(data){},

    loadUploadButton: function(thumbnail_width, action){
        if(!action){
            action = 'ajaxPostAction';
        }
        if(!thumbnail_width){
            thumbnail_width = '';
        }
        jQuery(document).ready(function(){
            jQuery('.image_upload_button').each(function(){
                var clickedObject = jQuery(this);
                var clickedID = jQuery(this).attr('id');
                new AjaxUpload(clickedID, {
                    action: location.href,
                    name: clickedID, // File upload name
                    data: { // Additional data to send
                        action: action,
                        ajax: 1,
                        type: 'upload',
                        data: clickedID,
                        thumbnail_width: thumbnail_width
                    },
                    autoSubmit: true, // Submit file after selection
                    responseType: false,
                    onChange: function(file, extension){},
                    onSubmit: function(file, extension){
                        clickedObject.text('Uploading'); // change button text, when user selects file
                        this.disable(); // If you want to allow uploading only 1 file at time, you can disable upload button
                        interval = window.setInterval(function(){
                            var text = clickedObject.text();
                            if (text.length < 13){clickedObject.text(text + '.');}
                            else {clickedObject.text('Uploading');}
                        }, 200);
                    },
                    onComplete: function(file, response) {
                        response = response.split('|*|');
                        var file_url = response[0];
                        var file = response[1];
                        window.clearInterval(interval);
                        clickedObject.text('Upload Image');
                        this.enable(); // enable upload button

                        // If there was an error
                        if(file_url.search('Upload Error') > -1){
                            var buildReturn = '<span class="upload-error">' + file_url + '</span>';
                            jQuery(".upload-error").remove();
                            clickedObject.parent().after(buildReturn);

                        }
                        else{
                            var buildReturn = '<img class="hide preview" id="image_'+clickedID+'" src="'+file_url+'" alt="" />';

                            jQuery(".upload-error").remove();
                            jQuery("#image_" + clickedID).remove();
                            clickedObject.parent().after(buildReturn);
                            jQuery('img#image_'+clickedID).fadeIn();
                            clickedObject.next('span').fadeIn();
                            clickedObject.parent().prev('input').val(file);
                        }
                    }
                });

            });
        });
    },

    loadRemoveButton: function(action){
        if(!action){
            action = 'ajaxPostAction';
        }
        jQuery(document).ready(function(){
            jQuery('.image_reset_button').click(function(){
                var clickedObject = jQuery(this);
                var theID = jQuery(this).attr('title');

                var ajax_url = location.href;

                var data = {
                    action: action,
                    ajax: 1,
                    type: 'image_remove',
                    data: theID,
                    file: jQuery('#id_'+theID).val()
                };

                jQuery.post(ajax_url, data, function(response) {
                    var image_to_remove = jQuery('#image_' + theID);
                    var button_to_hide = jQuery('#reset_' + theID);
                    image_to_remove.fadeOut(500,function(){jQuery(this).remove();});
                    button_to_hide.fadeOut();
                    clickedObject.parent().prev('input').val('');
                });
                return false;

            });
        });
    },

    sendMessageNew: function(data){
        jQuery(document).ready(function(){
            try{
                data = eval('('+data+')');
            }catch (e){}

            if(typeof(data)=='string'){
                var message = data;
                var status = 'message';
            }else{
                message = data['message'];
                status = data['status'];
            }
            if(status == 'success'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).addClass('notification_success');
                jQuery('.notification .text strong').html('Påmelding registrert!!');
            }else if(status == 'message'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_message');
                jQuery('.notification .text strong').html('Melding!');
            }else{
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_error');
                jQuery('.notification .text strong').html('Feil!');
            }
            jQuery('.notification .text span').html(message);
            jQuery('.notification').fadeIn('slow');

            jQuery('.notification').click(function() {
                jQuery(this).fadeOut(800);
            });
            window.setTimeout(function(){
               jQuery('.notification').fadeOut();
            }, 2500);
        });

    }
};
