function formHide(f) {
	var fw = $(f).innerWidth();
	var fh = $(f).innerHeight();
	var fid = $(f).attr('id');
	$(f).parent('.fwrap').remove();
	$(f).wrap(function() {
		return '<div class="fwrap '+ fid +'" style="width:'+ fw +'px;height:'+ fh +'px" />';
	});
	$(f).fadeOut(400,function() {
		if ( ! $('div.fwrap.finished').length) $('div.fwrap.'+ fid).css('background','transparent url(/cr/images/spinner.gif) no-repeat center center');
	});
}
function formShow(f,m,r,h) {
	$(f).parent('.fwrap').prepend(m).addClass('finished').css('background','none');
	r = typeof(r) !== 'undefined' ? r : false;
	if (r) $(f).show();
	h = typeof(h) !== 'undefined' ? h : false;
	if (h && !r) $(f).parent('.fwrap').animate({height:h+'px'},200);
}

$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname !== location.hostname);
};

$(function() {
	
	// External Links in New Windows
	$('a:external,a[href$=pdf]').attr('target','_blank').addClass('external');


	/*if ($('ul.feature li').length) {
			if ($('ul.feature li').length > 1) {
				$('ul.feature').cycle({
					timeout:6000,
					speed: '0',
					pager:'#feature-ctrl',
					pagerAnchorBuilder:function(i,s) {
						return '<li><a href="#"></a></li>';
					}
				});
			} else {
				$('ul.feature li').show();
			}
		}
	*/
	
	
	// Contact Form
	// Validate & Handle Contact Form
	if ($('form#contact').length) {
		
		// Define Validation
		var contactVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:$(f).attr('action'),
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						lname:$('input#contact-lname',f).val(),
						email:$('input#contact-email',f).val(),
						phone:$('input#contact-phone',f).val(),
						addr:$('input#contact-addr',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						comments:$('textarea#contact-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							//_gaq.push(['_trackEvent','Forms','Form Submitted','contact']);
						} else {
							m = '<p>'+json.message+'</p>';
							//_gaq.push(['_trackEvent','Forms','Form Error','contact, json error']);
						}
						formShow(f,m);
					},
					error:function(x,t,e) {
						console.log([x,t,e]);
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						//_gaq.push(['_trackEvent','Forms','Form Error','contact']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#contact').validate(contactVal);
		
		// Mask Phone & Zip Input
		$('form#contact input#contact-phone').mask("999 999-9999",{placeholder:" "});
		$('form#contact input#contact-zip').mask("99999",{placeholder:" "});
		
	}
	
	
	$("#twitter-feed li:lt(2)").css('display','block');
	$("a.fancybox-fixed").fancybox({
		'padding':0,
		'width':820,
		'height':500,
		'autoDimensions':false
	});
	$("a.fancybox").fancybox({
		'padding':0,
		'width':820,
		'height':500,
		'titlePosition':'over'
	});
	$("a.team-fancybox").fancybox({
		'padding':0,
		'width':780,
		'height':420,
		'titlePosition':'over'
	});
	$("a.pg-fancybox").fancybox({
		'padding':0,
		'titlePosition':'over'
	});
	$("input#text-sub").change(function() {
		if ($(this).is(':checked')){
			$('label[for=text-sub]').hide(200);
			$('label.hide').show(200);
		} else {
			$('label.hide').hide(200);
		}
	});
	// CYCLE PLUGIN FEATURE
	if ($('ul.feature li').length) {
		if ($('ul.feature li').length > 1) {
			$('ul.feature').cycle({
				timeout:6000,
				next:'#feature-right',
				prev:'#feature-left',
				pager:'#feature-ctrl',
				pagerAnchorBuilder:function(i,s) {
					return '<li><a href="#"></a></li>';
				}
			});
		} else {
			$('ul.feature.li').show();
		}
	} else {}
	
	// PAUSE CYCLE PLUGIN
	// on hover/blur change the image out (user feedback)
	//$('a#pause').hover(function(){
	//    ($('a.paused').length) ? : $(this).addClass('hover');
	//},function(){
	//    ($('a.paused').length) ? : $(this).removeClass('hover');
	//})
	
	// if the feature is "playing"
	$('a#pause').click(function() {
		if ($('a.playing').length) {
			// if it's playing, pause it
			$('ul.feature').cycle('pause');
			// add the class of "paused"
			$('a#pause').removeClass('playing').addClass('paused');
			// add the new click function
			$('a#pause').click(function() {
			    // resume the feature rotator
			    $('ul.feature').cycle('resume');
			    // add the class of "playing"
			    $('a#pause').removeClass('paused').addClass('playing');
				return false;
			})
			return false;
		// otherwise do this
		} else {
			// if it's paused, play it
			$('ul.feature').cycle('resume');
			// add the class of "playing"
			$('a#pause').removeClass('paused').addClass('playing');
			// add the new click function
			$('a#pause').click(function() {
				// pause the feature rotator
				$('ul.feature').cycle('pause');
				// add the class of "paused"
				$('a#pause').removeClass('playing').addClass('paused');
				return false;
			})
			return false;
		}
	})
	
	
	
	// ROLLOVER INTERACTIVE FEATURE
	if($('dl.interactive').length) {
		$('dl.interactive dt').each(function(i) { $(this).attr('id','f'+i); });
		$('dl.interactive dd').each(function(i) { $(this).addClass('f'+i); });
		
		$('dl.interactive dt').click(function() {
			var id = $(this).attr('id');
			if ($('dl.interactive dd.'+id+':visible').length) {
				//$('dl.interactive dt#'+id+' span').html('+');
				//$('dl.interactive dd.'+id).slideUp(100);
			} else {
				$('dl.interactive dt#'+id+' span').html('&ndash;');
				$('dl.interactive dd:not(.'+id+')').slideUp(100,function() {
					$('dl.interactive dd.'+id).slideDown(100);
					$('dl.interactive dt#'+id).css('cursor','default');
					$('dl.interactive dt:not(#'+id+') span').html('+');
					$('dl.interactive dt:not(#'+id+')').css('cursor','pointer');
				});
			}
		});
		$('dl.interactive dt#f0').click();
	}
})

