jQuery(document).ready(function() {
	showtimeClient = new jQuery.ShowtimeClient('extra');
				
	function showtimeGetResults(evt) {
		evt.preventDefault();
		jQuery('.showtimeClient .find').hide();
				
		var zipcode = jQuery('.showtimeClient .find form').find('input[type="text"]').val();
		showtimeClient.lookup(zipcode, function(data) {
			if(data.length == 0) {
				jQuery('.showtimeClient .results .times .station').html('Sorry, no showtimes found.');
				jQuery('.showtimeClient .results .times .showtimes').empty();
			} else {
				jQuery.cookie('showtimeZipcode', zipcode.toString());
				
				// only get first station
				station = data[0].station;
				//jQuery('.showtimeClient .results .station').html(station.affiliate + ' (' + station.callsign + ')');
				jQuery('.showtimeClient .results .station').html( "Channel " + station.callsign );
				
				showtimes = jQuery.map(station.showtimes, function(showtime) {
					return(showtime.day + ' ' + showtime.time);
				});
				jQuery('.showtimeClient .results .showtimes').html(showtimes.join(', '));
			}
				
			jQuery('.showtimeClient .results').show();
		});
	}
				
	jQuery('.showtimeClient .find form').submit(showtimeGetResults);
	jQuery('.showtimeClient .find form button').click(showtimeGetResults);
				
	jQuery('.showtimeClient .results a').click(function(evt) {
		evt.preventDefault();
		jQuery('.showtimeClient .results').hide();
		jQuery('.showtimeClient input[type="text"]').val('Enter Zipcode');
		jQuery('.showtimeClient .find').show();
		jQuery.cookie("showtimeZipcode", null);
		jQuery('.showtimeClient .find input').focus();
	});
				
	if(jQuery.cookie('showtimeZipcode')) {
		jQuery('.showtimeClient input[type="text"]').val(jQuery.cookie('showtimeZipcode'));
		jQuery('.showtimeClient .find form').submit();
	}
});
