$(document).ready(function(){
	
	var if_ie = ($.browser.msie && $.browser.version < 9) ? true : false,
	message,
	$this,
	projectHeights = [],
	maxHeight;
	
	//subscribe form
	$('<img/>').src = '/wp-content/themes/johnsonsaia/assets/images/ui/mailinglist-submit-wait.png';
	$("form[name='mailinglist-form']").submit(function(){
		$this = $(this);
		$($this).find("input[type='image']").attr('src','/wp-content/themes/johnsonsaia/assets/images/ui/mailinglist-submit-wait.png');
		$($this).find("input[type='image']").attr('disabled','disabled');
		
		$.ajax({
			type: "POST",
			url: "/subscribe",
			data: 'email='+$(this).find("input[type='text']").val(),
			success: function(msg){
				if (msg === 'subscribed')
				{
					message = 'You have successfully been subscribed to our mailinglist, thank you!';
				}
				else
				{
					message = msg;
				}
				$($this).find("input[type='image']").attr('disabled','');
				$($this).find("input[type='image']").attr('src','/wp-content/themes/johnsonsaia/assets/images/ui/mailinglist-submit.png');
				alert(message);
				// $.fancybox(message, 
				// 	{ 
				// 		'autoDimensions' : false,
				// 		'width' : 400,
				// 		'height' : 'auto',
				// 		'padding' : 40, 
				// 		'scrolling' : 'no'
				// 	}
				// );
			}
		});
		return false;
	});
	
	// case study rollovers
	$('<img/>').src = '/wp-content/themes/johnsonsaia/assets/images/grads/case-study-overlay.png';
	$('.work-item').hover(function(){
		if (if_ie)
		{
			$(this).find('.overlay').show();
		}
		else
		{
			$(this).find('.overlay').fadeIn(250);
		}
	}, function(){
		if (if_ie)
		{
			$(this).find('.overlay').hide();
		}
		else
		{
			$(this).find('.overlay').fadeOut(250);
		}
	});
	
	// search
	$('#searchform').submit(function(){
		if ($(this).find("#search").val().length == 0)
		{
			return false;
		} 
	});
	
	// project images.
	if ($('#project-images').length > 0)
	{
		$('#project-images') 
			.before('<div id="nav">') 
			.cycle({ 
			    fx:     'scrollHorz', 
			    speed:  'fast', 
			    timeout: 3000, 
			    pager:  '#nav' 
		});
	}
	
	// project images.
	if ($('.screen').length > 0)
	{
		$('.screen') 
			.before('<div id="nav">') 
			.cycle({ 
			    fx:     'fade', 
			    speed:  'slow', 
			    timeout: 5000, 
			    pager:  '#nav',
				before: switchContent,
				pagerAnchorBuilder: function(idx, slide) {
				 	return '<a href="#">&bull;</a>';
				}
		});	
	}
	
	// adjust height of projects page (for carousel, dynamic height)
	$('#project-images img').each(function(i){
		projectHeights[i] = $(this).height();
	});
	maxHeight = Math.max.apply(Math, projectHeights);
	if (maxHeight && maxHeight > 1) {
		$('#project-images').css('height', maxHeight + 20 + 'px');
	}
});

var switchContent = function(elm, nextElm) {
	$('#carousel-copy').html($(nextElm).find('.bd').html());
}
