

$(function() {
  // Page setup
  $('input.btn').attr('src', '/images/clear.gif');
  $('form table tr:odd td').addClass('table-td-alt');
  
  // jQuery UI tab setup
  $('#tabs').tabs();
  
  // Sponsor banner rotation
  $('.sponsors').after('<div class="sponsor-nav">').cycle({
    pager: '.sponsor-nav',
    pause: 1
  });
  
  // Raffle banner rotation
  $(".raffles").after('<div class="raffle-nav">').cycle({
    pager: '.raffle-nav',
    pause: 1
  });
  
  // Update the page total; also handles updating subtotals
  $("form#paypal select#individual_ticket, form#paypal select#table_ticket," +
    "form#paypal select#raffle_ticket_single,form#paypal select#raffle_ticket_triple," +
    "form#paypal select#raffle_ticket_book").change(function() {
    cew.update_grand_total();
  });
  
  // Update the total/subtotal display fields - required when we come back to
  // this page with errors
  cew.update_grand_total();
  
  // Toggle Search...
  $('input#s').each(function() {
    var default_value = 'Search...';
    this.value = default_value;
    $(this).focus(function() {
      if(this.value == default_value) {
        this.value = '';
      }
    });
    $(this).blur(function() {
      if(this.value == '') {
        this.value = default_value;
      }
    });
  });
  
  $('.profile-item').hide();
  
  $('.profile-btn').click(function(){
    $(this).toggleClass('selected');
    $(this).next('.profile-item').toggle();
    return false;
  });
    
  var emailReplaceChar = '%fsdfdsml%';
  $('.replace-email').each(function(){
    var encodedEmail = $(this).text();
    var atcualEmail = encodedEmail.replace(emailReplaceChar, '@');
    $(this).html('<a href="mailto:'+atcualEmail+'">'+atcualEmail+'</a>');
  });
});


var cew = {
  calculate_ticket_subtotal: function() {
    var subtotal = 0;
    if ($("select#individual_ticket").length > 0 && $("select#table_ticket").length > 0 )
    {
      subtotal += ($("select#individual_ticket").val()) * ($("input#price_ticket_single").val());
      subtotal += ($("select#table_ticket").val()) * ($("input#price_ticket_table").val());
    }
    return subtotal;
  },
  
  calculate_raffle_ticket_subtotal: function() {
    var subtotal = 0;
    
    // Raffle ticket fields are on the page conditionally
    if ($("select#raffle_ticket_single").size()) {
      subtotal += ($("select#raffle_ticket_single").val()) * ($("input#price_raffle_ticket_single").val());
      subtotal += ($("select#raffle_ticket_triple").val()) * ($("input#price_raffle_ticket_triple").val());
      subtotal += ($("select#raffle_ticket_book").val())   * ($("input#price_raffle_ticket_book").val());
    }
    return subtotal;
  },
  
  calculate_grand_total: function() {
    var total = 0;
	if ($("span#ticket-subtotal").length > 0)
	{
		total += parseFloat($("span#ticket-subtotal").text().replace(",", ""));
	}
    
    // Raffle ticket fields are conditional
    if ($("span#raffle-ticket-subtotal").size()) {
      total += parseFloat($("span#raffle-ticket-subtotal").text().replace(",", ""));
    }
    
    return total;
  },
  
  update_ticket_subtotal: function() {
    s = this.calculate_ticket_subtotal();
    $("span#ticket-subtotal").text(this.format_money(s));
    //this.update_grand_total();
    return s;
  },
  
  update_raffle_ticket_subtotal: function() {
    s = this.calculate_raffle_ticket_subtotal();
    $("span#raffle-ticket-subtotal").text(this.format_money(s));
    //this.update_grand_total();
    return s;
  },
  
  update_grand_total: function() {
    this.update_ticket_subtotal();
    this.update_raffle_ticket_subtotal();
    
    t = this.calculate_grand_total();
    $("span#grand-total").text(this.format_money(t));
  },
  
  format_money: function(s) {
    // 1.Parse to float; 2.Format to 2 dec pls; 3. Add thousand seperator
    return parseFloat(s).toFixed(2).replace(/(\d{1,})(\d{3})/i, "\$1,\$2");
  }
};

