$(document).ready(function() {

	/***********************************
		Prevent Double Payment
	***********************************/
    $('#submitPayment input').click(function() {
        $(this).hide().prev('.loading').show();
    });
    
	/***********************************
		Misc Colorboxes
	***********************************/
	// ajax
	$('.ajaxPop').colorbox({width:'550px'});
	$('.ajax').colorbox();
	
	// sidebar
	$('#sidebar .requestInfoBtn').colorbox({width:'550px',href:'#requestInfo',inline:true});
	$('#sidebar .callMeBackBtn').colorbox({width:'550px',href:'#callMeBack',inline:true});
	
	// trip details
	$('.resources .theProject').colorbox({width:'550px',href:'#theProject',inline:true});
	$('a.itinerary').colorbox({width:'550px',href:'#itinerary',inline:true});
	$('a.photoAlbum').colorbox({href:'#photoAlbum',inline:true});
	
	// press
	$('#oregonStateUnivLink').colorbox({width:'550px',href:'#oregonStateUniv',inline:true});
	
	// position documents
	$('#viewResources').colorbox({href:'#resourcesPop',inline:true});

	/***********************************
		Campus Positions
	***********************************/
	// "volunteered before?"
	$('#volunteeredBefore select').change(function() {
	    if( $(this).val() == 1 )
	    {
	        $(this).parent().hide();
	        $(this).parent().next().show();
	    }
	});
	// agreement colorbox
	$('.viewPositionAgreement').colorbox({width:'550px',href:'#agreement',inline:true});

	/***********************************
		Faqs
	***********************************/
	var count = 1;
	$('#body.faq p').filter(function(index) {
        return $('strong', this).length == 1;
    }).each(function(){
        $('<a name="'+count+'"></a>').insertBefore(this);
        count++;
    });
	
    /***********************************
    	Dashboard
    ***********************************/
    // enforce linear steps
    $('h2.noProceed a').click(function(e){
        e.preventDefault();
        alert('Please complete the previous step before continuing.');
    });
	
    /***********************************
    	Apply Discount Code
    ***********************************/

    $('#applyDiscount:not(.final)').click(function(e){
		e.preventDefault();
    	$.ajax({
    		type: "POST",
    		url: "https://www.encountour.com/user/apply_discount/",
    		data: "discount_code=" + $('input.discountCodeInput').val(),
    		success: function(data) {
    			if( data != 'Discount code not valid.' )
				{
					$('#preAuthAmount').text(data);
					$('#preAuthAmount').animate({color:'#0798BE'},400);
				}
				else
				{
					alert(data);
				}
    		}
    	});
    });
    
    $('#applyDiscount.final').click(function(e){
		e.preventDefault();
    	$.ajax({
    		type: "POST",
    		url: "https://www.encountour.com/user/apply_discount/final",
    		data: "discount_code=" + $('input.discountCodeInput').val(),
    		success: function(data) {
    			if( data != 'Discount code not valid.' )
				{
					$('#preAuthAmount').text(data);
					$('#preAuthAmount').animate({color:'#0798BE'},400);
					var amount = data.replace('$','');
					$('input[name="amount"]').val(amount);
				}
				else
				{
					alert(data);
				}
    		}
    	});
    });
	
    /***********************************
    	Confirm Trip Selection
    ***********************************/

    $('#tripSelectionDropdown').change(function(){
    	$.ajax({
    		type: "POST",
    		url: base_path + "user/confirm_trip",
    		data: "trip=" + $(this).val(),
    		success: function(data) {
				$('#tripConfirmPop').html(data);
    		}
    	});
    });
	$('.applyButton').click(function(e){
		var goAhead = true;
		// validate
		$('#applyForm').find('.req').each(function() {
			if( $(this).val() == '' )
			{
				goAhead = false;
			}
		});
		if( $('.reqCheckbox').is(':checked') && goAhead == true )
		{
			e.preventDefault();
			$.fn.colorbox({open:true,width:'550px',href:'#tripConfirmPop',inline:true});
			// confirm
			$('#confirmBtns .confirmBtn').live('click',function(){
				$('#applyForm').submit();
			});
			// close/cancel
			$('#confirmBtns .cancelBtn').live('click',function(){
				$.colorbox.close();
			});
		}
	});
	
	/***********************************
		Form Styling
	***********************************/
	// datepicker for date form fields
	$('input.date').live('focus', function() {
		$(this).datepicker({
		    showOn:'focus',
	    	dateFormat:		'yy-mm-dd',
    		showAnim:		'fadeIn',
    		changeYear:     true,
    		changeMonth:     true,
    		yearRange:      "-100:+20"
		}).focus();
	});
	
	// on blur, hide
	$('input.date').live('blur', function() {
        $('.ui-datepicker').fadeOut(1000);
    });
	
	// striping
	$('form > ul, ul.rows, ul.facts').find('> li:even').addClass('alt');
	
	// selected field styling
	$('form ul li input, form ul li textarea, form ul li select').live('focus',function() {
		$(this).parent().parent('li').addClass('sel');
	}).live('blur',function(){
		$(this).parent().parent('li').removeClass('sel');
	});
	
	/***********************************
		Form Validation
	***********************************/
	// validate form
	$('form').submit(function(e) {
		// disable submit btn (to prevent double clicking)
		//$(this).find(':submit:not(.signUp)').attr('disabled','disabled');
		// validate
		$(this).find('.req').each(function() {
			if( $(this).val() == '' )
			{
				e.preventDefault();
				alert('Please complete all required fields.');
				//alert($(this).attr('name')); // for debugging
				return false;
			}
		});
		// confirm email address
		if( $('input.emailAddress').length > 0 )
		{
		    if( $('input.emailAddress').val() != $('input.emailAddressConfirm').val() )
		    {
		        alert('Your email address and email address confirmation do not match.  Please double check these.');
		        return false;
		    }
	    }
		// confirm password
		if( $('input.password').length > 0 )
		{
		    if( $('input.password').val() != $('input.passwordConfirm').val() )
		    {
		        alert('Your password and password confirmation do not match.  Please double check these.');
		        return false;
		    }
	    }
		// require certain checkboxes to be checked
		$(this).find('.reqCheckbox').each(function() {
			if( $(this).is(':not(:checked)') )
			{
				e.preventDefault();
				$(':submit').removeAttr('disabled');
				alert('You must read and agree to the Terms and Conditions in order to register.');
				return false;
			}
		});
	});

	/***********************************
		Confirm Actions
	***********************************/
	$('a.confirmAction').click(function(e) {
		if(!confirm('Are you sure you want to perform this action?'))
		{
			e.preventDefault();
		}
	});
	
	/***********************************
		Sliders
	***********************************/
	// home slider
	$('#bannerSlider ul.photos').cycle({
		fx:			'scrollHorz',
		easing:		'easeOutQuint',
		next:		'#bannerWrapper #next',
		prev:		'#bannerWrapper #prev',
		speed:		1500,
		timeout:	6500,
		cleartype:true,
	    cleartypeNoBg:true
	});
	$('#bannerSlider ul.text').cycle({
		fx:			'fade',
		next:		'#bannerWrapper #next',
		prev:		'#bannerWrapper #prev',
		speed:		1500,
		timeout:	6500,
		cleartype:true,
	    cleartypeNoBg:true
	});
	// trip details slider
	$('#tripHeader .photos').cycle({
		fx:			'fade',
		speed:		1000
	});
	
	/***********************************
		Admin
	***********************************/
	// attendees colorbox
	$('#trips.admin li').find('a.attendees').colorbox({width:400});
	// discount users colorbox
	$('#discounts.admin li').find('a.discountUsers').colorbox({width:400});
	
	// add trip dates
	$('#newDate').click(function(e) {
	    e.preventDefault();
	    $('#tripDates > li:last').clone().appendTo('#tripDates');
	});
	
	// remove trip dates
	$('#tripDates li a.remove').live('click',function(e) {
	    e.preventDefault();
	    $(this).parent().parent().remove();
	});
	
    /***********************************
    	Admin: order users
    ***********************************/

    $('#orderBy select').change(function(){
    	$.ajax({
    		type: "POST",
    		url: base_path + "admin/order_by/",
    		data: "order_by=" + $(this).val(),
    		success: function(data) {
    			window.location = window.location.pathname;
    		}
    	});
    });
});








