jQuery(document).ready(function() {
  jQuery('a.target_blank').attr('target', '_blank');

  // Determine which items get fancied
  jQuery('a.fancy').fancybox({
    'overlayOpacity'     : 0.7,
    'overlayColor'       : '#fff',
    'autoDimensions'     : true,
    'hideOnContentClick' : false
  });
});

/**
 * This will cycle the images in the header
 * @param int - This is the amount of time in ms between images
 * @param int - This number is supplied by PHP, it's the limit of images before it cycles
 * @param string - subfolder where the desired images are located
 */
function cycle_images(duration, limit, folder) {

  // Get the current ID
  var current_num = parseInt(jQuery('#cycle img').attr('alt'));
  next_num = ((current_num + 1) > limit) ? 1 : current_num + 1;

  // Fade the current image out
  jQuery('#cycle img').fadeOut('slow', function() {
    // Fade in the current image
    jQuery('#cycle img').attr('src', '/img/' + folder + '_cycle/image_' + next_num + '.jpg');
    jQuery('#cycle img').attr('alt', next_num);

    // Fade the current image in
    jQuery('#cycle img').fadeIn('slow', function() {
      setTimeout('cycle_images(' + duration + ',' + limit + ',"' + folder + '")', duration);
    });
  });

  // Preload the next image
  preload_num = ((next_num + 1) > limit) ? 1 : next_num + 1;
  jQuery('.preload').html('<img src="/img/' + folder + '_cycle/image_' + preload_num + '.jpg" alt="preload" height="1px" width="1px" />');

  return false;
}

/**
 * This will perform a poor man's pagination
 * @param int - This is the page that you want to go to
 * @param string - This is the section that you want to update
 */
function show_page(page_id, section) {
  // Hide all page/groups
  jQuery('ul.' + section).hide();

  // Show the desired page/group
  jQuery('ul.' + section + '.group_' + page_id).show();
}

/**
 * This function updates the total cost of the ad space.
 */
function update_total() {
  var total = 0;

  // Cycle through the checked items to grab a value
  jQuery('input:checked').each(function() {
    total += parseInt(jQuery(this).val());
  });

  jQuery('#CompanyTotal').val(total);

  // Update the total amount of ads the person wants
  jQuery('#CompanyInserts').val(jQuery('input:checked').length);
}

/**
 * This function updates the price based on the selected
 * level and number of additional tickets
 */
function update_sponsor() {

}

/**
 * This function updates the price based on the selected
 * level and number of additional tickets
 */
function update_employer() {
  // Get the value of the level
  var exhibitor_level_id = jQuery('#CompanyExhibitorLevelId').val();

  // Get the value (if any) of the amount of extra tickets they want
  var additional_tickets = parseInt(jQuery('#CompanyAdditionalTickets').val());
  // Calculate the cost of the additional tickets
  var ticket_price = (additional_tickets > 0) ? (additional_tickets * 2) : 0;

  // Get the amount of checked locations
  var location_cost = 1000;
  var total_locations = jQuery('.locations_join input').length / 2;
  var checked_locations = jQuery('.locations_join input:checked').length;
  var location_price = 0;

  switch(checked_locations) {
  case 1:
    location_price = location_cost * checked_locations;
    break;
  case 2:
    location_price = (location_cost * checked_locations) * 0.8;
    break;
  case 3:
    location_price = (location_cost * checked_locations) * 0.7;
    break;
  default:
    break;
  }

  // Calculate the total price of everything
  var total = ticket_price + location_price;

  total = (!isNaN(total)) ? total.toFixed(2) : 0;

  // Update the total input box
  jQuery('#CompanyTotal').val(total);
}

/**
 * This function updates the price based on the selected
 * level and number of additional tickets
 */
function update_exhibitor() {
  // Get the value of the level
  var exhibitor_level_id = jQuery('#CompanyExhibitorLevelId').val();

  // Get the value (if any) of the amount of extra tickets they want
  var additional_tickets = parseInt(jQuery('#CompanyAdditionalTickets').val());
  // Calculate the cost of the additional tickets
  var ticket_price = (additional_tickets > 0) ? (additional_tickets * 2) : 0;

  // Get the amount of checked locations
  var location_cost = 500;
  var total_locations = jQuery('.locations_join input').length / 2;
  var checked_locations = jQuery('.locations_join input:checked').length;
  var location_price = location_cost * checked_locations;

  // Put the pre-discounted value in the form
  jQuery('#CompanyGrossTotal').val(location_cost * checked_locations);
  
  // Calculate the total price of everything
  var total = ticket_price + location_price;

  total = (!isNaN(total)) ? total.toFixed(2) : 0;

  // Update the total input box
  jQuery('#CompanyTotal').val(total);
}

/**
 * This function will update the total cost of the advertiser in real time
 * @param string - which section ex: a-la-carte or all-in-one
 */
function update_advertiser(section) {

  // Check to see if they have checked an item in the "all in one" section
  if(jQuery('.all-in-one input:checked').length > 0 && section == 'all-in-one') {
    // Remove all the checks in the a la carte section.
    jQuery('.a-la-carte input:checked').removeAttr('checked');

    // Split apart the values
    var location_price = parseInt(jQuery('.all-in-one input:checked').val().split(':', 1));
    var total_inserts = 1;
  }

  // Check to see if the person selected a location a la carte
  if(jQuery('.a-la-carte input:checked').length > 0 && section == 'a-la-carte') {
    // Uncheck the all in one checkbox if checked
    jQuery('.all-in-one input:checked').removeAttr('checked');

    var location_price = 0;
    jQuery('.a-la-carte input:checked').each(function() {
      location_price = location_price + parseInt(jQuery(this).val().split(':', 1));
    });

    var total_inserts = jQuery('.a-la-carte input:checked').length;
  }

  // Calculate the total price of everything
  var total = location_price;
  
  // Put the pre-discounted total in the hidden gross total box
  jQuery('#CompanyGrossTotal').val(total.toFixed(2));
  
  total = (!isNaN(total)) ? total.toFixed(2) : 0;

  // Update the total input box
  jQuery('#CompanyTotal').val(total);

  // Update the total inserts
  jQuery('#CompanyInserts').val(total_inserts);
}

/**
 * This function will update the list of events on the registration form for demonstrators
 */
function update_events() {
  // Get the value of the selected location
  var location_id = jQuery('#CompanyLocationId').val();

  // Hide all lists
  jQuery('tr.events').hide();

  // Now display the corresponding list element
  jQuery('tr.location_' + location_id).show();
}

/*
 * This function will show the form
 */
function show_form(id) {
  jQuery('#' + id).show();
  return false;
}

/**
 * On the demonstration registration page, this will toggle the location tables and extra form data
 */
function toggle_table() {
  // Hide all of the event boxes
  jQuery('.location_data').hide();

  // Cycle through all the potentially checked locations and show each of the location data sections
  jQuery('.location_checkbox:checked').each(function() {
    var class_name = jQuery(this).attr('class').replace('location_checkbox ', '');
    jQuery('div.' + class_name).show();
  });
}

/**
 * This will turn on and off the select box below the exhibitor checkbox
 */
function toggle_select() {
  // Cycle through the element to show/hide the booth select
  jQuery('.locations_join div').each(function() {


  var class_name = jQuery(this).attr('class');

    var is_checked = (jQuery('.locations_join .' + class_name + ' input:checked').length) ? 1 : 0;

    if(is_checked) {
      jQuery('.locations_join .' + class_name + ' p.select').show();
    } else {
      jQuery('.locations_join .' + class_name + ' p.select').hide();
    }
  });
}

/**
 * This will check if the form has accepted value has been checked or not
 */
function check_accepted() {
  var is_checked = (jQuery('input.accept:checked').length) ? 1 : 0;

  var return_value = false;

  if(is_checked) {
    return_value = true;
  } else {
    alert('You have to agree to the terms agreement.')
    return_value = false;
  }

  return return_value;
}

/**
 * This will validate the promo code 1 field to make sure that
 * it's only alpha characters
 */
function validate_promo_code_1() {
  // Get the value of the promo code
  var current_value = jQuery('#CompanyPromoCode1').val();
  var last_character = current_value.charAt(current_value.length - 1);
  if(!isNaN(last_character)) {
    jQuery('#CompanyPromoCode1').val(current_value.substring(0, current_value.length - 1));
  }
}

/**
 * This will validate the promo code 2 field to make sure that
 * it's only numeric characters
 */
function validate_promo_code_2() {
  // Get the value of the promo code
  var current_value = jQuery('#CompanyPromoCode2').val();
  var last_character = current_value.charAt(current_value.length - 1);
  if(isNaN(last_character)) {
    jQuery('#CompanyPromoCode2').val(current_value.substring(0, current_value.length - 1));
  }
}

/**
 * This function toggles a specific div id
 * @param string - id which should be toggled
 */
function toggle_id(id) {
  is_displayed = (jQuery('#' + id).css('display') == 'block') ? 1 : 0;
  
  if(is_displayed) {
    jQuery('#' + id).hide();
  } else {
    jQuery('#' + id).show();
  }
}

/**
 * This function will update the demonstrator
 */
function update_demonstrator() {

}

