// from: http://snipplr.com/view/8423/left-pad-string/
function leadingZeros(num, totalChars, padWith) {
	num = num + "";
	padWith = (padWith) ? padWith : "0";
	if (num.length < totalChars) {
		while (num.length < totalChars) {
			num = padWith + num;
		}
	} else {}

	if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded
		num = num.substring((num.length - totalChars), totalChars);
	} else {}

	return num;
};

$(function() {
  // Set up 'Scan Loyalty Card' Dialog
  $("#scanner_dialog").dialog({
      bgiframe: true,
      autoOpen: false,
      closeOnEscape: true,
      modal: true,
      resizable: false,
      draggable: false,
      buttons: {
        "Next": function() {
          // Submit the first form in the dialog
          $('#scanner_dialog form:first').submit();
        },
        "Cancel": function() { 
          // Blank the Card Number field
          $("#scanner_dialog form:first input[name='card_number']").val('');
          $(this).dialog("close");
        }
      }
    });

  // Set up 'Scan Loyalty Card' Dialog
  $("#search_dialog").dialog({
      autoOpen: false,
      closeOnEscape: true,
      modal: true,
      resizable: false,
      buttons: {
        "Search": function() {
          // Submit the first form in the dialog
          $('#search_dialog form:first').submit();
        },
        "Cancel": function() { 
          // Blank the Card Number field
          $("#search_dialog form:first input[name='search_term']").val('');
          $(this).dialog("close");
        }
      }
    });

  // 'Scan Loyalty Card' Dialog - Form
  $('#scanner_dialog form:first').submit(function() {
    var card_number = $.trim($(this).find("input[name='card_number']").val());

    if (!(/^\d+$/.test(card_number))) { 
      // TODO: Close this dialog, and show an error dialog
      return false;
    }

    // Construct a URL and submit the form to it
    $(this).attr(
      'action',
      $(this).attr('action') + '/' + card_number
    ).find("input[name='card_number']").val('').attr('disabled', 'disabled');
  });

  // 'Search Loyalty Card' Dialog - Form
  $('#search_dialog form:first').submit(function() {
    var search_type = $.trim($(this).find("select[name='search_for']").val());

    // Construct a URL and submit the form to it
    $(this).attr(
      'action',
      $(this).attr('action') + '/' + search_type
    ).find("input[name='search-term']").val('').attr('disabled', 'disabled');
  });

  // 'Scan Card' link
  $('a.scan-card').click(function() {
    $('#scanner_dialog form:first').attr('action', $(this).attr('href')).attr('disabled', '');
    $('#scanner_dialog').dialog("open");
    $("#scanner_dialog form:first input[name='card_number']").focus();
    return false;
  });

  // 'Scan Card' link
  $('a.search').click(function() {
    $('#search_dialog form:first').attr('action', $(this).attr('href')).attr('disabled', '');
    $('#search_dialog').dialog("open");
    return false;
  });



  // TODO: process the .card_number class, padding 0s to it if its less than 6 digits
  $('.card_number').each(function() {
   $(this).text(leadingZeros($(this).text(), 6, "0"));
  });

  $('.rounded').each(function(){
    $(this).corner();
  });
  
  $('#accordion').accordion({collapsible: true, autoHeight: false});

  // Redemption
  $("button#redemption_button").attr('disabled', 'disabled');

  $("select#redemption_value").change(function() {
    var redemption_value = parseInt($(this).find('option:selected').val());

    if (redemption_value > 0) {
      $('span#redemption_status').text(redemption_value);
      $('span#dialog_redemption_status').text("AED " + redemption_value);
      $('button#redemption_button').attr('disabled', '');
    } else {
      $('span#redemption_status').text("0");
      $('span#dialog_redemption_status').text("");
      $('button#redemption_button').attr('disabled', 'disabled');
    }
  });

  $("#confirmation_dialog").dialog({
      bgiframe: true,
      autoOpen: false,
      closeOnEscape: false,
      modal: true,
      resizable: false,
      draggable: false,
      buttons: {
        "Yes": function() { 
          $(this).dialog("close");
          $('form#redemption').attr('action', "/redeem/" + $('input#card_number').val() + "/" + $('select#redemption_value option:selected').val()).attr('disabled', '');
          $('form#redemption').submit();
        },
        "No": function() { 
          $(this).dialog("close");
        }
      }
  });

  $('button#redemption_button').click(function() {
    $('div#confirmation_dialog').dialog("open");
    return false
  });

  $('a.ajax_action').each(function() {
      $(this).click(function() {
        $(this).attr('id', 'currentRow');
        var action_url = $(this).attr("href");
        $.ajax({type: "GET", url: action_url, dataType: "script"});
        return false;
      });
  });

  $('span.comments').each(function() {
    $(this).simpletip({content: "<pre>" + $(this).find("img").attr("title") + "</pre>", fixed: false});
  });
});
