// Page manipulation
$(document).ready(
	function() {
	    	    
		// Picking "one-time" or "repeating"
		$('#add-ride-form #type-select').change(
			function() {
				if ($(this).val() == 'repeating') {
					$('#add-ride-form #depart-date').hide();
					$('#add-ride-form #return-date').hide();
					$('#add-ride-form #depart-days').fadeIn();
					$('#add-ride-form #return-days').fadeIn();
				} else {
					$('#add-ride-form #depart-days').hide();
					$('#add-ride-form #return-days').hide();
					$('#add-ride-form #depart-date').fadeIn();
					$('#add-ride-form #return-date').fadeIn();
				}
			}
		);
        	
        $('#wall_text').focus( function() {
            clear_generic(this,'Comment on this ride...');
            $('#wall_post_submit_button').css({display:'block'});
        });
        
        $('#wall_text').blur( function() {
            unclear_generic(this, 'Comment on this ride...');
        });
        
        $('#wall_post_submit_button').click(function() {
                if ($('#wall_text').val() == '' || $('#wall_text').val() == 'Comment on this ride...') {
                    return false;
                }
        });
        
        $('#networks').click(
			function() {
                    if ($('#networks').val() != '0') {
                        window.location.href = $('#networks').val();
                    }
				}
		);

        /** Search Bar Functions */
        $('#search_s').locationinput('s', userLocations, {
            autocompleteOptions: { max: 3, width: $('#search_s').width() + parseInt($('#search_s').css("padding-right"))  + parseInt($('#search_s').css("padding-left")) },
            textBlack: true
		});

        $('#search_e').locationinput('e', userLocations, {
			autocompleteOptions: { max: 3, width: $('#search_s').width() + parseInt($('#search_s').css("padding-right"))  + parseInt($('#search_s').css("padding-left")) },
			textBlack: true
		});
        
        $('.clickaway').live('focus', function(){
            if ($(this).val() == $(this).attr('placeholder')) {
                $(this).val('');
            }
            $(this).css('color','#111111');

			// simulate a keydown event
			var e = jQuery.Event('keydown');
			e.keyCode = 40;
			$(this).trigger(e);
        });

        $('.clickaway').live('blur', function(){
            if (0 == $(this).val().length) {
                $(this).val($(this).attr('placeholder')).css('color','#999999');
            }
        });

        $('.clickaway_confirm').live('click', function(){
            $(this).parents('form:first').find('.clickaway').each(function(){
                if ($(this).val() == $(this).attr('placeholder')) {
                    $(this).val('');
                }
            });
        });
        
        /* Adds datepicker to all fields with class 'datepicker' */
        $('.datepicker').datepicker({
           showOn: 'both',
           buttonImage: '/assets/icon_calendar.png',
           buttonImageOnly: true
        });
        
        /* User homepage actions */
		$('.feedback .input_text').focus(function(){
			if ($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
			}
			$(this).css('color','#111111');
		});

		$('.feedback .input_text').blur(function(){
			if (0 == $(this).val().length) {
				$(this).val($(this).attr('placeholder')).css('color','#999999');
			}
		});

		$('.feedback button').click(function(){
			$('.feedback .input_text').each(function(){
				if ($(this).val() == $(this).attr('placeholder')) {
					$(this).val('');
				}
			});
		});
	}
);

$(window).load(
	function() {
		var options = {
			keysClose: ['c', 27]
		};
	}
);

