var current_date = new Date();
var this_year = current_date.getFullYear();

$(document).ready(function(){

	function init(){

		if (isset(curr_continent)){

			update_region(curr_continent);
		}

		if (isset(curr_region)){

			update_city(curr_region);
		}

	}
	
	$('select#continent').change(function(){ update_region($(this).val()); });

	$('select#region').change(function(){ update_city($(this).val()); });

	$('input#name').focus(function(){
		
		$('p#name-notice').fadeIn('fast');
	});

	$('input#name').blur(function(){
		
		$('p#name-notice').fadeOut('slow');
	});

	$('input#name').keyup(function(){
		
		checkCaps($(this));
	});

	$('td.categories-cont input').focus(function(){
		
		var other_images				= $('td.categories-cont img');

		other_images.css('opacity', '0.5');
		other_images.css('-moz-opacity', '0.5');
		other_images.css('filter', 'alpha(opacity=50)');

		var this_image					= $(this).parent().find('img');

		this_image.css('opacity', '1');
		this_image.css('-moz-opacity', '1');
		this_image.css('filter', 'alpha(opacity=100)');

		$(this).blur();
	});

	$('#cal-cont-start').datepicker({ 
		minDate: new Date(this_year, 1 - 1, 1),  
		maxDate: new Date(this_year + 2, 12 - 1, 31),  
		defaultDate: date_from_drops('start'),    
		onSelect: updateStart 
	});

	$('#cal-cont-end').datepicker({ 
		minDate: new Date(this_year, 1 - 1, 1),  
		maxDate: new Date(this_year + 2, 12 - 1, 31),  
		defaultDate: date_from_drops('end'),    
		onSelect: updateEnd 
	});
	
	$('a#enable-city').click(function(){
		
		$("select#city-id").removeAttr('disabled');
		$("select#region").removeAttr('disabled');
		
		return false;
	});

	function update_city(ref){

		$.getJSON('/cities/find_all_for_region/' + ref + '/' + $('select#continent').val() + '/', null, function(j){
		
			var options = '<option>Choose The Nearest City</option>';

			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].id + '">' + j[i].name + '</option>';
			}

			$("select#city-id").html(options);
			$('select#city-id option:first').attr('selected', 'selected');
			$("select#city-id").removeAttr('disabled');
			
			if (isset(curr_city) && curr_city != ''){

				$('select#city-id option[@value="' + curr_city + '"]').attr('selected', 'selected');
				curr_city = '';
			}
		})
	}

	function update_region(ref){
		
		$.getJSON('/cities/find_parents_for_continent/' + ref + '/', null, function(j){
		
			var options = '<option>Choose A Region</option>';

			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i] + '">' + j[i] + '</option>';
			}

			$("select#region").html(options);
			$('select#region option:first').attr('selected', 'selected');
			$("select#region").removeAttr('disabled');
			// $("select#city-id").attr('disabled', true);

			if (isset(curr_region) && curr_region != ''){

				$('select#region option[@value="' + curr_region + '"]').attr('selected', 'selected');
				curr_region = '';
			}
		})
	}
	 
	// Update three select controls to match a date picker selection 

	function updateLinked(date, prefix) { 
		$('#' + prefix + '-month').val(date.substring(0, 2)); 
		$('#' + prefix + '-day').val(date.substring(3, 5)); 
		$('#' + prefix + '-year').val(date.substring(6, 10)); 
	}

	function updateStart(date){
		
		updateLinked(date, 'start');

		match_start_with_end(date);
	}

	function updateEnd(date){
		
		updateLinked(date, 'end');
	}

	function match_start_with_end(date){
		
		end_date						= $('#cal-cont-end').datepicker('getDate');
		start_date						= $('#cal-cont-start').datepicker('getDate');

		if (start_date > end_date){
			
			$('#cal-cont-end').datepicker('setDate', start_date);
			updateEnd(date);
		}
	}
	 
	$('#start-month, #start-year').change(function(){
		
		checkLinkedDays('start');
	});

	$('#end-month, #end-year').change(function(){
		
		checkLinkedDays('end');
	}); 
	 
	// Prevent selection of invalid dates through the select controls 
	function checkLinkedDays(prefix) { 
		var daysInMonth = 32 - new Date($('#' + prefix + '-year').val(), 
			
		$('#' + prefix + '-month').val() - 1, 32).getDate(); 
			$('#' + prefix + '-day option').attr('disabled', ''); 
			$('#' + prefix + '-day option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
			
			if ($('#' + prefix + '-day').val() > daysInMonth) { 
				$('#' + prefix + '-day').val(daysInMonth); 
			} 
	} 

	$('#start-day, #start-month, #start-year').change(function(){
		
		update_calendar('start');
		update_drops('end');
		update_calendar('end');

	});

	$('#end-day, #end-month, #end-year').change(function(){
		
		update_calendar('end');

	});

	$('#start-time').change(function(){
		
		var this_val					= $(this).val();
		var end_val						= $('#end-time').val();

		if (end_val == '-1'){
			
			$('#end-time').val(this_val);
			$('#end-time option[selected]').next().next().next().next().attr('selected', true); // set to 2 hours ahead of start time.
		}
	});

	function update_calendar(ref){
		
		$('#cal-cont-' + ref).datepicker('setDate', date_from_drops(ref));
	}

	function update_all(){
		
		update_calendar('start');
		update_calendar('end');
	}

	function update_drops(ref){
		
		var alt_ref = ref == 'start' ? 'end' : 'start';

		$('#' + ref + '-year').val($('#' + alt_ref + '-year').val());
		$('#' + ref + '-month').val($('#' + alt_ref + '-month').val());
		$('#' + ref + '-day').val($('#' + alt_ref + '-day').val());
	}

	function date_from_drops(ref){

		var year		= $('#' + ref + '-year').val();
		var month		= $('#' + ref + '-month').val();
		var day			= $('#' + ref + '-day').val();
		
		return new Date(year, month - 1, day)
	}

	$('select#saved_venues').change(function(){
		
		$.getJSON('/venues/get_info_in_json/' + $(this).val(), null, function(j){
			
			$('input#location').val(j.location);
			$('input#address').val(j.address);
			$('input#city').val(j.city);
			$('input#state').val(j.state);
			$('input#zip').val(j.zip);
		});
	});

	$('a.city-for-user').click(function(){
		
		city_id		= $(this).attr('rel');
		curr_city	= city_id;

		$.getJSON('/cities/get_info/' + city_id, false, function(data){
			
			curr_continent	= data.parent.code;
			curr_region		= data.parent.name;

			$('#continent option[@value="' + data.parent.code + '"]').attr('selected', 'selected');
			update_region(data.parent.code);
			update_city(data.parent.name);
		});

		return false;
	});

	init();
});

capsWarn = false;

function checkCaps(strInput) {

	var isCaps = false;
	var count = 0;
	var limit = strInput.val().length;

	if(limit > 1 && capsWarn == false){
		for(var i=0; i<limit; i++){
			var char = strInput.val().charAt(i);
			if(char >= "A" && char <= "Z"){
				if(isCaps && count == 3){
					if(i == limit-1){
						capsWarn = true;

						$('p#name-notice, input#name').css('background-color', '#FFFF55');

						return;
					}
				}
				
				isCaps = true;
				count = count + 1;
			} else{
				isCaps = false;
				count = 0;
			}
		}
	}
}