/* =====================
   Core framework script
   ---------------------
*/

// flags to indicate changes not saved
var fw_popup_changed = false;
var fw_popup2_changed = false;

// body onload event - sets the ajax error handler and calls the initial server body_onload event
function fw_body_onload() {
   // set the ajax error handler
   $("#error").ajaxError(function(event, request, settings, error){
      fw_working_off();
      $(this).html("AJAX error: <br />" + settings.url + " - <br />" + request.responseText + ' - <br />' + error);
      fw_error_show();
   });

   fw_json('body_onload', {});
}

/* =========================================
   function fw_json(fw_event, fw_parameters)
   -----------------------------------------
   Used to handle events in the browser which require server interaction
   Overview:
      Passes event data to a handler on the server, initially to the JSON class, which then passes it to the appropriate handler.  
      Reads the response from the handler, which is in json format, and takes the actions specified by the response.
   Parameters:
      fw_event =  An application-defined name for the event which matches a server process to handle it
                  Eg, if fw_event = body_onload then the request will be passed to the body_onload->render in body_onload.class.php
      fw_parameters =
                  An object, containing additional parameters to pass to the server
                  These will appear in the $_REQUEST variable on the server
   Usage example:
      An item in the view layer may contain html such as:
         <body onload="fw_json('body_onload', {x:1, y:2})">
      Note: fw_event can also contain additional parameters, for instance the following is also valid
         <body onload="fw_json('body_onload/1/2')">
         In this case the additional parameters are passed as positional parameters, rather than named parameters.  The server will receive them in the $arg[] array.
   Actions:
      Passes the parameters to server, eg
         serverurl?q=json/body_unload&x=1&y=2
      The response is an object which is, effectively, a list of commands to perform at the view layer.  
      Each command contains an action element which then determines the further actions
      Possible actions:
         action = html - updates the view layer with specific html
            selector = JQuery selector which determines which elements are to be updated
            html = the HTML to be placed in the selected element
         action = js - some JavaScript to be directly executed
            js = the Javascript to be executed
   Return value:
      none
   
*/
function fw_json(fw_event, fw_parameters) {
   fw_parameters = (fw_parameters == null) ? {} : fw_parameters;
   fw_parameters['q'] = fw_event;
   fw_working_on();
   $.getJSON('', fw_parameters, function (data) {
      $.each(data, function (k, v) {
         fw_working_off();
         switch (v['action']) {
            case 'html':
               selector = v['selector'];
               html = v['html'];
               method = v['method'];
               switch (method) {
                  case 'append': $(selector).append(html); break;
                  case 'prepend': $(selector).prepend(html); break;
                  case 'replace':
                  default: $(selector).html(html); break;
               }
               break;
            case 'js':
               js = v['js'];
               eval(js);
               break;
            default:
               alert(v['action']);
         }
      });
   });
}

// display the working sign
function fw_working_on() {
   $("#working").show();
   $("#working").animate( {width: "300px"}, 3000, function() {
      $("#working").css( {width: ""});
      fw_working_on();
   });
}
function fw_working_off() {
   $("#working").stop();
   $("#working").css( {width: ""});
   $("#working").hide();
}


// shows the error clear button
function fw_error_show() {
   $("#error_clear").show();
}

// clears the error box
function fw_error_clear() {
   $("#error").text("");
   $("#error_clear").hide();
}

// show and hide the popup
function fw_show_popup() {
   $("#main select").hide(); // nasty ie6 fix
   $("#popupouter").fadeIn();
   $("#popup").fadeIn();
}
function fw_hide_popup() {
   if (fw_popup_changed) {
      fw_popup_changed = false;
      fw_popup_status("Some changes have not been saved - either save your changes or click again to close this window WITHOUT saving");
   } else {
      $("#main select").show(); // nasty ie6 fix
      $("#popupouter").fadeOut();
      $("#popup").fadeOut();
   }
}
function fw_show_popup2() {
   $("#popup select").hide(); // nasty ie6 fix
   $("#popup2outer").fadeIn();
   $("#popup2").fadeIn();
}
function fw_hide_popup2() {
   if (fw_popup2_changed) {
      fw_popup2_changed = false;
      fw_popup2_status("Some changes have not been saved - either save your changes or click again to close this window WITHOUT saving");
   } else {
      $("#popup select").show(); // nasty ie6 fix
      $("#popup2outer").fadeOut();
      $("#popup2").fadeOut();
   }
}

// show popup status message
function fw_popup_status(message) {
   fw_popup_status_fade("#popupstatus", message);
}
function fw_popup2_status(message) {
   fw_popup_status_fade("#popup2status", message);
}
// fade method used on all popup status windows
function fw_popup_status_fade(popup, message) {
   $(popup).stop(true, true);
   $(popup).html(message);
   $(popup).fadeIn("fast", function() {
      $(popup).fadeOut(10000, function () {
         $(popup).html("");
      });
   });
}

// show/hide the print area
function fw_print_toggle() {
   $("#print").toggle("slow");
}
// show/hide the print area
function fw_print_show() {
   $("#print").show("slow");
}
