/* Jquery Extrnded functions
 * 
 * Here you can find custom extended functions for jquery.
 * 
 * @author Igor Josevski
 */

$(document).ready(function($) {
    
/**
 * function show_more
 * 
 * Used for displaying more results throught ajax with easy load.
 *  
 * @author Igor Joshevski <igor.josevski@invideous.com>
 * @todo 
 * @return 
 * @example
 * 
 * var function_data = 'filter_by='+filter_by+'&search_by='+search_by+'&from='+from+'&to='+to+"&limit="+limit+"&offset="+offset;
   var parameters = '[{\n\
                        "element_for_ajax":".transactions_table", \n\
                        "element_for_content" : "#transactions_content_table", \n\
                        "action_button" : "#customer_transactions_show_more", \n\
                        "action" : "append", \n\
                        "function_to_call" : "consumer/transactions/get_transactions", \n\
                        "function_data" : "'+function_data+'",\n\
                        "if_no_result" : "#no_data", \n\
                        "ajax_type" : "html",\n\
                        "ajax_method" : "POST" \n\
                     }]';
 */
$.fn.Show_more = function show_more(parameters){
    
   var parameters_array = JSON.parse(parameters);
   
   var element_for_ajax = parameters_array[0].element_for_ajax;
   var element_for_content = parameters_array[0].element_for_content;
   var action = parameters_array[0].action;
   //var action_button = parameters_array[0].action_button;
   var function_to_call = parameters_array[0].function_to_call;
   var function_data = parameters_array[0].function_data; //Function data can be empty
   var no_data = parameters_array[0].if_no_result;
   var ajax_type = parameters_array[0].ajax_type;
   var ajax_method = parameters_array[0].ajax_method;
   
   if(element_for_ajax != '' && element_for_ajax != undefined){
       var positions = $(element_for_ajax).position();
       var width = $(element_for_ajax).width();
       var height = $(element_for_ajax).height();
       var left = positions.left;
       var top = positions.top;
       
       $(element_for_ajax).append('<div class="loader"></div>');
       $(".loader").css({position: "absolute", top: top, left: left, width: width, height: height});
   }
   else{
       $(".loader_fullscreen").show();
   }
   
   if(element_for_content != '' && element_for_content != undefined){
       if(action == '' || action == undefined){
           action = 'replace'; //Set default action, if no action set up into the parameters
       }
       
       if(ajax_type == '' || ajax_type == undefined){
           ajax_type = "json";
       }
       
       if(ajax_method == '' || ajax_method == undefined){
           ajax_method = 'POST';
       }
       
       if(function_to_call != '' && function_to_call != undefined){
           
           $.ajax({
                      url: url_base+""+function_to_call,
                      type: ajax_method,
                      data: function_data,
                      dataType: ajax_type,
                      complete: function(data){
                          $(".loader_fullscreen").hide();
                          $(".loader").hide();
                          $(".loader").remove();
                      },
                      success: function(data){

                          if(data != ''){
                              if(no_data != "" && no_data != undefined){
                                  if(action == "replace"){
                                      $(element_for_content).html(data);
                                  }else if(action == "append"){
                                      $(element_for_content).append(data);
                                  }else{
                                      $(element_for_content).html(data);
                                  }
                              }else{
                                  if(action == "replace"){
                                      $(element_for_content).html(data);
                                  }else if(action == "append"){
                                      $(element_for_content).append(data);
                                  }else{
                                      $(element_for_content).html(data);
                                  }
                              }

                          }else{
                              if(no_data != "" || no_data != undefined){
                                $(no_data)
                                $(no_data).show(); //If no data to display
                              }
                          }
                      }
           });
       }
       else{
           return false; // If no function to call is set
       }
   }else{
       return false; // IF no element for content is set
   }
   
   
}

/**
 * function popup_show
 * 
 * Used for displaying popup by id, css_class, element attr, and
 * also giving new positions and arguments into array format.
 * 
 * @author Igor Joshevski <igor.josevski@invideous.com>
 * @todo 
 * @return mixed
 */
$.fn.Popup_show = function popup_show(id, css_class, position, args){
    var element = '';
    var positionX = 0;
    var positionY = 0;
    var argument = '';
        if(id != undefined || id != ""){
            element = $("#"+id);
        }
        else if(css_class != undefined || css_class != ""){
            element = $("."+css_class);
        }
        else{
            return false;
        }
    
        if(position == undefined || position.length == 0){
            position = new Array(0,0); //Reset to zeros if position is empty
        }else{
            positionX = position[0];
            positionY = position[1];
        }
     
    element.css({top: positionY, left: positionX});
    
        if(args != undefined || args.length != 0){
            
            for(var i = 0; i < args.length; i++){
                argument = args[i][0];
                element.css({argument: args[i][1]}); //Auto set css values from the args array
            }
        }
        
    element.show(); //Show element
}

/**
 * function popup_close
 * 
 * Used for hiding popup by id, css_class or element attribute.
 * 
 * @author Igor Joshevski <igor.josevski@invideous.com>
 * @todo 
 * @return 
 */
$.fn.Popup_close = function popup_close(id, css_class){
    var element = '';
    
    if(id != undefined || id != ""){
        element = $("#"+id);
    }
    else if(css_class != undefined || css_class != ""){
        element = $("."+css_class);
    }
    else{
        return false;
    }
    
    element.hide();
}

$.fn.Calculate_my_sql = function calculate_Sql_Limit(page, limit){
    var offset = limit*page; // 15 * 1 = 15 - second page
    
    return " LIMIT "+limit+" OFFSET "+offset;
}

});

$.fn.Calculate_days_in_month = function calculate_days_in_month(month, year){
    return new Date(year,month,0).getDate();
}
//End of the document
