$(document).ready(function(){
   /**
   * datepicker
   */
  if($('.datepicker').length > 0)
  {
    bindDates();
  }
  
  /**
   * lightbox
   */
  if($('.lightbox').length > 0)
  {
    $('.lightbox').lightbox({
      fitToScreen: true
    });
  }
  
  /**
   * fancybox
   */
  if($('.fancybox').length > 0)
  {
    $('.fancybox').fancybox({
      autoScale: true,
      titlePosition: 'over',
      hideOnOverlayClick : false
    });
  }
  
  if($('.fancyboxLarge').length > 0)
  {
    $('.fancyboxLarge').fancybox({
      autoScale: true,
      width: 800,
      height: 600,
      titlePosition: 'over'
    });
  }
  
  $('.pager').find('.prev').click(function() {
    $('.pager').find('.currentPageValue').val(parseInt($('.pager').find('.currentPageValue').val()) - 1);
  });
  $('.pager').find('.next').click(function() {
    $('.pager').find('.currentPageValue').val(parseInt($('.pager').find('.currentPageValue').val()) + 1);
  });
 
  $('.choiceButton').bind('change',toggleChoiceButton);  

  // hide messages after 2 seconds
  if($('.message').length > 0)
  {
    $('.message').delay(10000).fadeOut();
  }
  
  // set numeric field filters
  $('.numericField').keydown(numericField);
  
  $('.toggleDefaultField').each(function() {
    // retrieve the default color 
    var default_color   = $(this).attr('default_color');
   // set to #CFCFCF if default color is null
    if(!default_color)
      default_color = '#CFCFCF';
  
    // retrieve the value color 
    var value_color   = $(this).attr('value_color');
   // set to empty string if value color is null
    if(!value_color)
      value_color = '';
      
    var default_value = $(this).val();
    
    $(this).blur(function() {
      // run the blur input with the default email text
      var blur_function = $.proxy(blurInput,this);
    
      if(blur_function(default_value))
        $(this).css('color',default_color);
    });
    
    $(this).focus(function() {
      // run the blur input with the default email text
      var focus_function = $.proxy(focusInput,this);
    
      if(focus_function(default_value))
        $(this).css('color',value_color);
    });
    
    // trigger the events
    $(this).trigger('focus').trigger('blur');
  });
  
});

function updatePager(current_page, page_count, display_count)
{
  // set the page to 1 if no page count is 0
  page_count = page_count == 0 ? 1 : page_count;
  
  // set the pager
  $('.pager').find('.currentPage').html(current_page);
  $('.pager').find('.lastPage').html(page_count);
  $('.pager').find('.displayCount').html(display_count);
  
  // update the current page value
  $('.pager').find('.currentPageValue').val(current_page);
  
  // hide the previous and next if at the first or last page
  if(current_page == 1)
    $('.pager').find('.prev').hide();
  else
    $('.pager').find('.prev').show();
    
  if(current_page == page_count)
    $('.pager').find('.next').hide();
  else
    $('.pager').find('.next').show();
}

function closeParentDialog()
{
  parent.$.fancybox.close();
}

function closeDialog() {
  $.fancybox.close();
}

function toggleChoiceButton()
{
  choiceStatus = $(this).siblings('.choiceStatus');
  
  // remove current colors
  choiceStatus.removeClass('red');
  choiceStatus.removeClass('green');
   
  // switched to on
  if($(this).attr('checked'))
  {
    choiceStatus.addClass('green');
    option = $(this).attr('onText');
    choiceStatus.html(option != null ? option : 'ON');
  }
  // switched to off
  else
  {
    choiceStatus.addClass('red');
    option = $(this).attr('offText');
    choiceStatus.html(option != null ? option : 'OFF');
  }
}

/**
 * binding dates
 */
function bindDates()
{
  // bind the datepicker to each of the datepicker objects
  $('.datepicker').each(datepickerBind);
    
  // bind the start date to update the end date
  $('.start_date').change(startDateChange); 
}

/**
 * Bind the datepickers
 */
function datepickerBind()
{
  // bind the datepicker
  $(this).datepicker({
    dateFormat: 'MM d, yy', // default date format
    minDate: $(this).attr('minDate'), // min date
    maxDate: $(this).attr('maxDate')  // max date
  });
}

/**
 * Update the min date on the start date
 */
function startDateChange()
{
  $('.end_date',$(this).parents('.dateWidget')).datepicker( 'option', 'minDate', $(this).val());    
}

/**
 * Empty the field if the default text is found
 */
function focusInput(default_text)
{
  // check if hte default text is placed
  if($(this).val() == default_text)
  {
    $(this).val('');
    return true;
  }
  
  return false;
}

/**
 * Empty the field if the default text is found
 */
function blurInput(default_text)
{
  // check if hte default text is placed
  if($(this).val() == '')
  {
    $(this).val(default_text);
    return true;
  }
  
  return false;
}

function numericField(event)
{
  // Allow only backspace and delete
  if ( event.keyCode == 46 || event.keyCode == 8 ) {
      // let it happen, don't do anything
  }
  else {
      // Ensure that it is a number and stop the keypress
      if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
          event.preventDefault(); 
      }   
  }
}
