$(function() {

	var $ = jQuery;
	
	var	c3 = {

		nav : function(){
			$('#header ul li').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});		
		},

		links : function(){
		
			$("#print").click(function(){
				
				window.print();
				
				return false;
				
			});

			$("#share").click(function(){

				var url = $(this).attr("href");
				$.ajax({
					type: "POST",
					url: url,
					data: "",
					success: function(data){
						var winW = $(window).width();
						$("body").append(data);
						var w = $("#contact_overlay").width();
						$("#contact_overlay").css('left', winW/2-w/2).fadeIn("slow");
						$("#overlay").fadeIn("slow", function(){
							$.scrollTo(0, 400);
						});
					}
				});
				return false;
						
			});
		
		},

		gallery : function(){
		
			$("a[rel='gallery']").colorbox();
		
		},
	
		init : function(){

			c3.links();
			
			c3.nav();
			
			c3.gallery();

		}
	
	};
	
	c3.init();


	
	//pagination js
	$("#pagination a em").hide();
	
	$("#pagination a").hover(
		function () {
			$(this).children("em").show()
		}, 
		function () {
			$(this).children("em").hide();
		}
	);	

	$(".close").live("click",function(){
		$("#overlay").fadeOut("slow", function(){
			$(this).remove();
		});
		$("#contact_overlay").fadeOut("slow", function(){
			$(this).remove();
		});
		return false;
	});
		
	$("#overlay").live("click",function(){
		$("#overlay").fadeOut("slow", function(){
			$(this).remove();
		});
		$("#contact_overlay").fadeOut("slow", function(){
			$(this).remove();
		});
		return false;
	});
	$.validity.setup({ outputMode:"summary" });
    $("#registration_form").validity(function() {
    
        $("#first_name")
            .require();
         $('#last_name')
            .require();
         $('#email')
            .require()
            .match('email');
            
    });

	$('#registration_form input[name="tickets"]').keyup(function(e) {
	   // total attendees 
		var attendees = $(this).val();
		// make sure a number is entered
		if (attendees !="") {
		   // enforce only positive numbers
		   if (attendees == '0' || !attendees.match(/^\d+$/)) {
		      // reset to 1 if character isn't allowed
		      $(this).val('1');
		      attendees = "1";
		   }
	      // convert string to an integer
		   attendees = parseInt(attendees);
   		// are there any existing fields?
   		var currentFields = $('#attendees div').length;
   		// what is the change needed for the fields? we subtract 1 to account for the user
         var fieldChange = attendees - currentFields - 1;
         // extra safe check
   		if(attendees > 0 && $('div#attendees').length > 0) {
   		   // do we need to add fields?
   		   if (fieldChange > 0) {
   		      $('p#attendee-label').show();
   		      var input = '';
               $('#attendees div').show();
   		      for(i = currentFields; i < (fieldChange+currentFields); i++) {

      				input = ' \
      				<div> \
      				   <label for="attendee-'+ i +'">Guest '+ (i+1) +'</label> \
      					<input type="text" id="attendee-'+ i +'" class="required" name="attendees[' + i + '][first_name]" value="" title=" Guest '+(i+1)+' First Name" /> \
      					<input type="text" class="required" name="attendees[' + i + '][last_name]" value="" title=" Guest '+(i+1)+' Last Name" /> \
      					<input type="text" name="attendees[' + i + '][email]" value="" /> \
      				</div> \
      				';

      				$('div#attendees').append(input);
      			}

      		    $("#registration_form").validity(function() {

      		        $(".required")
      		            .require();
      		    });
      		// do we need to subtract fields?
   		   } else if (fieldChange < 0) {
   		      // number of fields to subtract
               var removeDivs = Math.abs(fieldChange);
               for(i=0; i < removeDivs; i++) {
                  $('#attendees div:last').remove();
               }
            }
            // if we removed all fields, hide the label
            if ($('#attendees div').length == 0) {
               $('p#attendee-label').hide();
            }
   		}
		}
	});

});
