/* common functions */
$(document).ready(function() {
    // prevent autocompleting form values
    $(':input').attr('autocomplete', 'off');

    // enable jump on dropdown jump menus
    $(".jump").change(function() {
      window.location = $(this).val();
    });

    $('.editable').live('click', function(e) {
      e.preventDefault();
    });
});


// show output in dialog box
function dialogMessage(response, boxtitle)
{
    var titleclass = '';
    boxtitle = boxtitle || false; // default to hide title
    if (boxtitle == false) titleclass = 'hide-title';
    
    $('#dialog-message').html(response); // fill the div with the message html

    $("#dialog-message").dialog({
      modal: true,
      width: 450,
      dialogClass: titleclass,
      title: boxtitle,
      buttons: {
        Ok: function() {
          $(this).dialog('close');
        }
      }
    });
}

// used for to-do timer with create / cancel option
function dialogConfirmation(response, boxtitle, url)
{
    var titleclass = '';
    boxtitle = boxtitle || false; // default to hide title
    if (boxtitle == false) titleclass = 'hide-title';

    $('#dialog-message').html(response); // fill the div with the message html

    $("#dialog-message").dialog({
      modal: true,
      width: 450,
      dialogClass: titleclass,
      title: boxtitle,
      buttons: {      
        Cancel: function() {
					$(this).dialog('close');
				},

        'Create': function() {
          var complete = $('#mark-as-complete').attr('checked');
          if (complete == true) url += '&complete=true';
          exitWarning(false);
          window.location = url;
        }
      }
    });
}


// used to redirect user when ok is pressed
function dialogRedirect(response, url)
{
    var titleclass = '';
    var boxtitle = false; // default to hide title
    if (boxtitle == false) titleclass = 'hide-title';

    $('#dialog-message').html(response); // fill the div with the message html

    $("#dialog-message").dialog({
      modal: true,
      width: 450,
      dialogClass: titleclass,
      title: boxtitle,
      buttons: {
        'Ok': function() {
          window.location = url;
        }
      }
    });
}


// show dialog if navigating away from page
function exitWarning(warning, message)
{
    // show prompt if navigating away
    if (warning) {
      window.onbeforeunload = function() {
        return message;
      } 
    } else {
      window.onbeforeunload = null;
    }
}


// show standard warning if ajax failed
/* Possible Values for @error
 * timeout - when your specified timeout is exceeded
 * error - http error, like 404
 * notmodified - when requested resource was not modified since last request
 * parsererror - when an xml/json response is bad
*/
function ajaxError(request, error){
    if (error == "timeout") {
      dialogMessage("The request timed out, please resubmit");
    } else {
      dialogMessage("ERROR: " + error);
    }
}


// determine if any filters are set and show the warning if necessary
function filtersSet(page)
{
    jQuery.ajax({
    type: 'GET',
    url: '/ajax.common?action=filtersSet',
    data: 'page='+ page,
    cache: false,
    success: function(response){
        $('#filters-set').html(response);
    }});

    return false;
}


// animate the background of an element with a green fade out
function animateBG(selector)
{
    $(selector).css('background', '#99FF99');
    $(selector).animate({backgroundColor: '#fff'}, 'slow');
}


// prevents crazy javascript rounding errors
function Round(Number, DecimalPlaces)
{
   return Math.round(parseFloat(Number) * Math.pow(10, DecimalPlaces)) / Math.pow(10, DecimalPlaces);
}


// uses Round with fixed decimal places
function RoundFixed(Number, DecimalPlaces)
{
   return Round(Number, DecimalPlaces).toFixed(DecimalPlaces);
}


// highlight plan change column onclick
function highlight(plan_id)
{
    $("table.plans > tbody > tr > td").removeClass("highlight gray-darker"); // remove all references of highlight
    $("table.plans > tbody > tr > td.plan-"+ plan_id).addClass("highlight");
    $("table.plans > tbody > tr > td.plan-"+ plan_id +".plan").addClass("gray-darker");
    $("#radio-"+ plan_id).attr("checked", true);
}


// dropdown menu 
$(function(){

    $("ul.dropdown li").hover(function(){

        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');

    }, function(){

        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');

    });

    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

});

/*
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];

  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}*/


// image pre-loader for ajax spinners
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

jQuery.preLoadImages("/css/images/spinner.gif", "/css/images/in-progress.gif");
