var homehovered = false;
var homeanimate = setTimeout('animateit()', 4000);

jQuery(document).ready(function(){
	//Initialize Contact Form popup
	jQuery(".colorbox").colorbox({inline:true, width:"900px", onComplete:function(){$('#contactMap').html('<iframe width="400" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=640+North+Coit+Road,+Richardson,+TX&amp;aq=2&amp;sll=37.0625,-95.677068&amp;sspn=61.669968,135&amp;vpsrc=6&amp;ie=UTF8&amp;hq=&amp;hnear=640+N+Coit+Rd,+Richardson,+Texas+75080&amp;t=m&amp;ll=32.95877,-96.767921&amp;spn=0.021605,0.034246&amp;z=14&amp;output=embed" style="display:block"></iframe>');} });
	
	//IE Fix for the Videos list on the Media page
	jQuery('ul.vid_list').children('li:nth-child(even)').css({'margin-right': '0'}).addClass('yep');
	
	//Homepage Javascript
	if(jQuery('body').attr('id') == 'home') {
		jQuery('.homearrow').css('opacity', 0);
		jQuery('#home_slidernav').css('opacity', 0).children(':first-child').addClass('current').css({'background-color': '#fff'});
		
		jQuery('#home_content').hover(function(){
			jQuery(this).find('.homearrow').stop().animate({'opacity': 1}, 300);
			jQuery('#home_slidernav').stop().animate({'opacity': 1}, 300);
			clearTimeout(homeanimate);
			homehovered = true;				
		}, function() {
			jQuery(this).find('.homearrow').stop().animate({'opacity': 0}, 300);
			jQuery('#home_slidernav').stop().animate({'opacity': 0}, 300);
			resetTimeout();
			homehovered = false;
		});
	
		jQuery('.homearrow').click(function(){
			slidefrom = jQuery('#home_slides div.current');
			if (jQuery(this).attr('id') == 'rightarrow') {
				slideto = slidefrom.next();
				if (!slideto.length)
					slideto = jQuery('#home_slides').children(':first-child');
			}
			else {
				slideto = slidefrom.prev();
				if (!slideto.length)
					slideto = jQuery('#home_slides').children(':last-child');
			}
			animateit(slideto);
			return false;
		});
		
		jQuery('#home_slides').children(':first-child').addClass('current');
	}
	
	//Add prefilled input field functionality to pre-HTML5 browsers
	var defaultValues = [];
	jQuery('input.prompted, textarea.prompted').each(function( intIndex ){
		defaultValues[ jQuery(this).attr('id') ] = jQuery(this).val();
		jQuery(this).addClass('promptText');
	});
	jQuery('input.prompted, textarea.prompted').focus(function(){
		if ( jQuery(this).hasClass('promptText') )
		{
			jQuery(this).val('');
			jQuery(this).removeClass('promptText');
		}
	});
	jQuery('input.prompted, textarea.prompted').blur(function(){
		if ( jQuery(this).val() == '' )
		{
			jQuery(this).addClass('promptText');
			jQuery(this).val( defaultValues[ jQuery(this).attr('id') ] );
		}  
	});
	jQuery('#home_slidernav a').click(function(){
		if (!jQuery(this).hasClass('current'))
		animateit( jQuery('#slide'+jQuery(this).attr('rel')) );
		return false;
	})

	$(".imggallery").colorbox({rel:'imggallery', width:"900", height:"630px", scalePhotos:false});
	$(".vimeobox").colorbox({iframe:true, innerWidth:640, innerHeight:360});
	$(".vimeoboxbig").colorbox({iframe:true, innerWidth:750, innerHeight:422});

	//Submit the Contact Form
	$('#contactSubmit').click(function(){
		var message = $('#contactMessage').val();
		var name = $('#contactName').val();
		var email = $('#contactEmail').val();
		var mailing = $('#contactMailing').prop("checked");

		//Fix for empty submissions
		if ( (name == 'Name*') && (email == 'Email*') && (message == 'Message') )
			return false;
		var dataString = 'name='+ name + '&email=' + email + '&mailing=' + mailing + '&message=' + message;

		$(this).hide();
		$('#contactAjax').show();
		
		$.ajax({
			type: "POST",
			url: "bin/contact.php",
			data: dataString,
			success: function(){
				$('#contactform').find('input, textarea').prop('disabled', true);
				$('#formSuccess').fadeIn(500);
				$('#contactAjax').hide();				
			}
		});
		return false;
	});
	
	//Submit the Mailing List Form
	$('#mailingSubmit').click(function(){
		var name = $('#mailingName').val();
		var email = $('#mailingEmail').val();
		var address1 = $('#mailingAddress1').val();
		var address2 = $('#mailingAddress2').val();
		var city = $('#mailingCity').val();
		var state = $('#mailingState').val();
		var zip = $('#mailingZip').val();
		var twitter = $('#mailingTwitter').val();
		var facebook = $('#mailingFacebook').val();
		var google = $('#mailingGoogle').val();

		//Fix for empty submissions
		if ( (name == 'Name*') && (email == 'Email*') && (address1 == 'Address 1') )
			return false;

		var dataString = 'name='+ name + '&email=' + email + '&address1=' + address1 + '&address2=' + address2 + '&city=' + city + '&state=' + state + '&zip=' + zip + '&twitter=' + twitter + '&facebook=' + facebook + '&google=' + google;
		
		$(this).hide();
		$('#mailingAjax').css({'display':'block'});

		$.ajax({
			type: "POST",
			url: "bin/mailing.php",
			data: dataString,
			success: function(){
				$('#mailingform').find('input, textarea').prop('disabled', true);
				$('#mailingSuccess').fadeIn(500);
				$('#mailingAjax').css({'display':'none'});				
			}
		});
		return false;
	});	

});

function animateit(slideto) {
	var slidefrom = jQuery('#home_slides div.current');
	if (typeof(slideto) == 'undefined') { //then it's a timed animation
		if (homehovered) //Don't do anything if the mouse is over the main slideshow
			return false;
		var slideto = slidefrom.next();
		if (!slideto.length)
			slideto = jQuery('#home_slides').children(':first-child');	
	}
	slidefrom.css('z-index', '2').removeClass('current');
	slideto.css({'z-index': '3', 'opacity': 0, 'display': 'block'}).addClass('current').animate({'opacity': 1}, 500, function(){slidefrom.removeClass('current').css({'z-index': '1'})});
	jQuery('#slidernav'+slidefrom.attr('rel')).css({'background-color': '#2D1E17'}).removeClass('current');
	jQuery('#slidernav'+slideto.attr('rel')).css({'background-color': '#fff'}).addClass('current');
	
	resetTimeout();
}

function resetTimeout() {
	clearTimeout(homeanimate);
	homeanimate = setTimeout('animateit()', 5000);
}

