jQuery.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			
			this.hide = function() {
			  label.css({ textIndent: -10000 })
			}
			
			this.show = function() {
			  if (input.val() == '') label.css({ textIndent: 0 })
			}

			// handlers
			input.focus(this.hide);
			input.blur(this.show);
			label.addClass(overClass).click(function(){ input.focus() });
			
			if (input.val() != '') this.hide(); 
		}
	})
}


$(document).ready(function(){

	$('.form_success').hide();
	
	$('#contact_form').bind('submit', function(){
		var form = $(this);
		$.ajax({
			type: 'POST',
			url: '/post-contact-form',
			data: form.serialize(),
			success: function(data){
				if(data == 'failure'){
					$('.messages').fadeTo(0,0).html("<span class='error'>All fields are required. Email address must be valid.</span>").stop().fadeTo(250, 1);
				}
				else
				{
					$('#contact_form input').val('');
					$('#contact_form textarea').val('');

					$('div.form').fadeTo(200, 0, function() { 
						$('.form_success').insertAfter('.form').fadeTo(0,0).html("<h4>Your message has been successfully sent, thanks!<h4>").stop().fadeTo(500, 1, function(){
							
							_gaq.push(['_trackEvent', 'Contact Success', 'Submit Form', 'Footer Contact Form Success']);
							
							$('div.form').remove();
						});
					});

				}
			}
		})
		return false;
	});

	$(function() {

		$( "#tabs" ).tabs({
		   show: function(event, ui) {
			if(ui.index == 0) {
			
			   	$('.contact_us').animate({opacity: 0}, 0).stop().fadeTo(500, 1);
		  
		   } else if(ui.index == 1) {
			
				$('.location_map').animate({opacity: 0}, 0).stop().fadeTo(500, 1);
			   	$("#map").gMap({ controls: false,
		                  scrollwheel: true,
		                  markers: [{ latitude: 53.508758,
		                              longitude: -113.417761,
		                              icon: { image: "/public/img/marker.png",
										      shadow:  "/public/img/shadow.png",
		                                      iconsize: [48, 59],
		                                      shadowsize: [78, 59],
		                                      iconanchor: [24,59],
		                                      infowindowanchor: [12, 0] } }
		                            ],
		                  latitude: 53.508758,
		                  longitude: -113.417761,
		                  zoom: 15 });
			   
			   }
			   
		   }
		});
	
	});
		
		
	// replace form elements
	$('input.submit_replace').wrap('<div class="submit_right"><div class="submit_left"></div></div>');
	$('input.text_field_replace').wrap('<div class="input_text_right"><div class="input_text"></div></div>');
	
	
	$('input.text_field_replace').focus(function() { 
		$(this).parent().parent().addClass('hover');
		}).blur(function() {
		$(this).parent().parent().removeClass('hover');
	});
	
	$('textarea.replaced').focus(function() { 
		$(this).parent().addClass('hover');
		}).blur(function() {
		$(this).parent().removeClass('hover');
	});
	
	// form label hide/show
	$('.footer label').labelOver('over-apply');
	

	// carousel
	$('.carousel_cycle').cycle({ 
		fx: 'blindY',
	    timeout:  7500,
	    speed: 1000,
		prev:    '#prev',
	    next:    '#next',
	    pager:  '.carousel_nav',
	    before: onBefore,
	    after:   onAfter,
		cleartype:  1,
	    cleartypeNoBg:  true,
	    pause: 1
	});
		
	function onBefore() { 
	   $('.background_layer').hide();
	   $('.content_layer').hide();
	} 
	
	function onAfter() { 
		 	$('.background_layer').show();
		   	$('.background_layer').fadeOut(0);
			$('.background_layer').fadeIn(500);
		 
			$('.content_layer').hide();
        if (jQuery.browser.msie) {
			$('.content_layer').show();
		} else {
		   	$('.content_layer').fadeOut(0);
			$('.content_layer').fadeIn(500);
		}
	} 

	// news carousel 

	$.fn.cycle.transitions.scrollVertUp = function($cont, $slides, opts) {
		$cont.css('overflow','hidden');
		opts.before.push(function(curr, next, opts, fwd) {
	$.fn.cycle.commonReset(curr,next,opts);
		opts.cssBefore.top = fwd ? (next.cycleH-1) : (1-next.cycleH);
		opts.animOut.top = fwd ? -curr.cycleH : curr.cycleH;
	});
		opts.cssFirst = { top: 0 };
		opts.cssBefore= { left: 0 };
		opts.animIn = { top: 0 };
		opts.animOut = { left: 0 };
	};


	$('.news_cycle').cycle({ 
        fx:     'scrollVertUp', 
        speed:   500, 
        timeout: 0, 
		prev:    '.prev_news',
	    next:    '.next_news',
	    nowrap: 1
	}); 




	$('input.submit_replace').mouseenter(function() {
		$(this).parent().addClass('hover');
		$(this).parent().parent().addClass('hover');
		
	}).mouseleave(function() {
		$(this).parent().removeClass('hover');
		$(this).parent().parent().removeClass('hover');
	});

	
	
	




	
	
	// carousel navigation arrows hide/show
	$('a#prev, a#next').hide();
	$("div.carousel").mouseenter(function() {
		$(this).find('a#prev, a#next').stop().fadeTo(500, 1);
	}).mouseleave(function() {
		$(this).find('a#prev, a#next').stop().fadeTo(500, 0);
	});
	



	
	// twitter bird
	$('a.follow').hide();
	
	$("div.twitter").mouseenter(function() {
		
		$(this).find('span').stop().fadeTo(500, 0);
		
		$('a.follow').show().animate({opacity: 0, top: '+10'}, 0).animate({ opacity: 100, top: '-=25'}, 250);
	
	}).mouseleave(function() {
	
		$(this).find('span').stop().fadeTo(500, 1);
		
		$('a.follow').animate({ opacity: 0, top: '-=15'}, 250, function() { 
				$('a.follow').hide();
		});
	
	});
	
	
	
	
	/*
	$("a.hoverButton").mouseenter(function() {
	
		$(this).find('span').stop().fadeTo(500, 0);
	
	}).mouseleave(function() {
		
		$(this).find('span').stop().fadeTo(500, 1);
	
	});
	*/
	
	
	
	// generic button hover
		
	$("a.hoverButton").mouseenter(function () {
	
		var found = $(this).find("span.hover");
		if (found.length == 0) {
			$('<span class="hover"></span>').stop().insertBefore($(this).find('b')).css('opacity', 0);
		}
	
		$(this).find('span.hover').stop().fadeTo(500, 1);
	
	}).mouseleave(function() {
		
		$(this).find('span.hover').stop().fadeTo(250, 0, function() { 
			$(this).remove();
		});

	
	});
	
	
	
	
	// Secondary Nav hovers
		$('<span class="hover"></span>').insertBefore($('ul.secondary_navigation li a b'));
		
		$('ul.secondary_navigation li a ').each(function () {
		  var $span = $('span.hover', this).css('opacity', 0);
		  $(this).hover(function () {
		    $span.stop().fadeTo(150, 1);
		  }, function () {
		    $span.stop().fadeTo(500, 0);
		  });
		});
	
	

});
