/**
* WCMC specific functions
* @namespace
*/

/* Shadowbox function - 2 different heights */
function shadowBoxVid() {      

	// Don't setup Shadowbox if the page doesn't use it
	if ($("a.gallery").length > 0) {
		// Setup Shadowbox for popup videos
		Shadowbox.init({
			skipSetup: true,
			players: ["iframe"] //iframe player used for ajax calls, see lib docs for more options
		});

		//give each link a class of "gallery" to activate modal
		Shadowbox.setup("a.gallery", {
			height: 345,
			width: 530
		});	
	}
}

/* Shadowbox function - 2 different heights */
function shadowBoxVid2() {      

	// Don't setup Shadowbox if the page doesn't use it
	if ($("a.gallery2").length > 0) {
		// Setup Shadowbox for popup videos
		Shadowbox.init({
			skipSetup: true,
			players: ["iframe"] //iframe player used for ajax calls, see lib docs for more options
		});

		//give each link a class of "gallery" to activate modal
		Shadowbox.setup("a.gallery2", {
			height: 435,
			width: 530
		});
		
	}
}

/* Shadowbox function - 2 different heights */
function shadowBoxVid3() {      

	// Don't setup Shadowbox if the page doesn't use it
	if ($("a.gallery3").length > 0) {
		// Setup Shadowbox for popup videos
		Shadowbox.init({
			skipSetup: true,
			players: ["iframe"] //iframe player used for ajax calls, see lib docs for more options
		});

		//give each link a class of "gallery" to activate modal
		Shadowbox.setup("a.gallery3", {
			height: 455,
			width: 590
		});
		
	}
}
   


/** 
* JQuery check button / credit button field show/hide 
*/ 
function paymentChoice() {   
	$('.check_option').hide(); // set donation amount field display to hidden by default
	$('.credit_option').hide(); // set donation amount field display to hidden by default
	
	$(".payment_type").click(function(){
    	if ($('input[name=payment]:checked').val() == "check_moneyorder" ) { // if the check/money order radio button is checked
			$('.credit_option').slideUp('fast', function() {  // hide the credit button
				$('.check_option').slideDown('fast');  // reveal the donation amount field & check button
			}).delay(200); // pause between the hide and reveal
		}
		else {
			$('.check_option').slideUp('fast', function() {   // hide the donation amount field field & check button
		    	$('.credit_option').slideDown('fast');  // reveal the credit button
			}).delay(200); // pause between the hide and reveal
	    }
     });
}



/** 
* JQuery "other" field show/hide 
*/
function otherField() {
    $('.other_field').hide(); // set other field display to hidden by default         
	
	$(".allocation_check").change(function(){  
	    var val = $(this).val();
	    if(val != "other_selection_check") {  // if the other is NOT selected
	        $('.other_field').slideUp('fast');  // hide the other field
	    } else {
		    $('.other_field').slideDown('fast');  // reveal the other field
	    }
    });    
}
   
  

/**  
* Uses selected donation amount from radio button to send to PayPal service
*/
function creditDonate() {       
    var url = "http://www.cornellneurosurgery.org/ways_to_give/credit_card.html";  
	$('[name=redirect]').val(url);  // set the redirect to go to the credit thank you page instead    
	  
}
   


/**  
* Uses selected donation amount from radio button to send to PayPal service
*/
function creditDonationRadio() {
	var fee_amt = $("input[name='fee']:checked").val();  // set fee_amt to the checked radio button
	if (fee_amt === undefined) {  // check to make sure they have selected an amount
		$('.error_text').slideDown('swing');  // display error message
		event.preventDefault();  // prevent form from executing
	}
	else {
		$('[name=amount]').val(fee_amt);  // set the paypal amount hidden value to the selected radio button amount
		document.paypal.submit();
	}
}


  
/**
* JQuery FAQ show/hide function 
* (used on grad school site - http://weill.cornell.edu/gradschool/courses/) 
*/
function initMenus() {
$('ul.menu ul').hide();
$('ul.menu li a').click(
	function() {
		var checkElement = $(this).next();
		var parent = this.parentNode.parentNode.id;

		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			if($('#' + parent).hasClass('collapsible')) {
				$('#' + parent + ' ul:visible').slideUp(175);
			}
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			//$('#' + parent + ' ul:visible').slideUp(175);
			$('ul.menu ul').slideUp(175);
			checkElement.slideDown(125);
			return false;
		}
	}
);
$('span.close a').click(
	function() {
		$('ul.menu ul').slideUp(175);
	}		
);
} 
       
 
/**
* JQuery Document Ready function 
* Calls all functions in order to run on JQuery Document Ready
*/
$(document).ready(function(){  
	
	shadowBoxVid();
	shadowBoxVid2();
	shadowBoxVid3();
	otherField(); 
	paymentChoice();
	initMenus();
	 
	jQuery.validator.messages.required = "";                                       
	$("#commentForm").validate({
	    rules: {
			firstname: "required",
			lastname: "required", 
			address: "required", 
			city: "required", 
			state: "required", 
			zip: "required", 
			phone: "required",
			email: {
				required: true,
				email: true        
			},  
			check: {
				required: true,
				email: true,
				equalTo: "#email"
			}
		},  
		messages: {       
			email: {
			    required: "",
			    email: "Please enter a valid email address"
			}, 
			check: {
				required: "",
				email: "Please enter a valid email address", 
				equalTo: "Please enter the same email address"
			}
		},                                              
		invalidHandler: function(form, validator) {
  	    	var errors = validator.numberOfInvalids();         
  	    	if (errors) {       
 				$("div.error_text").html("Please fill out the highlighted fields");
  	        	$("div.error_text").show();
 	      	} else {
  	        	$("div.error_text").hide();
  	      	} 
 		}, 
 		submitHandler: function() {
	        $("div.error_text").hide();
			form.submit();
		},
		onkeyup: false                                                                                 
	 });
	
});                                

