$(function() {

	$('#praise').cycle({ 
    fx:      'fade', 
		pause:   1,
		timeout: 5500 
	});
	$('#news').cycle({
	    fx:      'scrollUp',
			pause:   1,
			timeout: 4500 
	}); 
	$('div#showcasePreview').filter(function() {
		$(this).cycle({
		    fx:     'scrollLeft',
				pause:   1,
				timeout: 2500 
		}); 
	});
	$('div#featuredShowcasePreview').filter(function() {
		$(this).cycle({
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0, 
		    next:   '#scrollRight', 
		    prev:   '#scrollLeft' 
		}); 
	});
	if($('#country').val() == '') {
		$("#item-stateprov").hide();
		$("#item-city").hide();
		$("#item-postalcode").hide();
	}
	else if($('#country').val() == 'US') {
		$("#item-stateprov").hide();
		$("#item-city").hide();
		$("#item-postalcode").show();
	}
	
	$('#country').change(function() {
		$('#zip-message').remove();
		$('#state').val('');
		$('#city').val('');

		if($(this).val() == '') {
			$("#item-stateprov").css("display","none");
			$("#item-city").css("display","none");
			$("#item-postalcode").css("display","none");
			return;
		}
		else if($(this).val() == 'US') {
			$("#item-stateprov").css("display","none");
			$("#item-city").css("display","none");
			$("#item-postalcode").css("display","block");
			$('#postal_code').addClass("micro").attr('maxlength','5').val('');
			return;
		}
		
		$("#item-stateprov").css("display","block");
		$("#item-city").css("display","block");
		$("#item-postalcode").css("display","block");
		$('#postal_code').removeClass("micro").attr('maxlength','15').val('');
	});
	
	$('#postal_code').keyup(function() {
		var postal_code = $(this).val();
		if(($('#country').val() != 'US') || (postal_code.length != 5)) return;
		$.get('/services/locations/' + postal_code, function(data) {
			if($('locations location', data).size() == 0) {
				var message = $('<p id="zip-message" class="form-item error">Please enter a valid ZIP Code</p>');
				$('#zip-message').remove();
				$('#state').val('');
				$('#city').val('');
				$('#postal_code').after(message);
				return;
			}
			
			$('#zip-message').remove();
			var message = '<p id="zip-message" class="form-item success">';
			$('locations location', data).each(function() {
				var location = $(this);
				var state = location.find('state').text();
				var city = location.find('city').text();
				message += city + ', ' + state;
				$('#state').val(state);
				$('#city').val(city);
			});
			message += '</p>';
			$('#postal_code').after(message);
		});
	});

	if($('#industry_segment').val() == '') {
		$("#item-function").hide();
		$("#item-details").hide();
	}
	$('#industry_segment').change(function() {
		if($(this).val() == '') {
			$('#job_function').val('').attr('disabled','disabled');
			$("#item-function").hide();
			$("#item-details").hide();
			return;
		}
		
		$.get('/services/jobfunctions/' + $(this).val(), function(data) {
			html = '<select id="job_function" name="job_function">';
			$('jobfunctions jobfunction', data).each(function() {
				var jobfunction = $(this);
				var id = jobfunction.find('id').text();
				var display = jobfunction.find('display').text();
				html += '<option value="' + id  + '">' + display + '</option>';
			});
			html += '</select>';
			$("#item-function").show();
			$("#item-details").show();
			$('#job_function').after(html).remove();
			return;
		});
	});
	
	$(".imageContainer").filter(function() {
		$(this).hover(function(){
			$(this).addClass("imageContainerHover");
		}, function (){
			$(this).removeClass("imageContainerHover");
		});
	});
});
