////////////////////////////////////////
// Over Label
////////////////////////////////////////
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = $(this); var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = $(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.css("display", "none");
            }
            control.focus(function () {label.css("display", "none");}).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = $(this); var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};

////////////////////////////////////////
// Document Ready
////////////////////////////////////////
$(document).ready(function(){

	////////////////////////////////////////
	// Misc Behavior
	////////////////////////////////////////
	// Remove Title Behavior
	$('#header a, #footer a, h1').removeAttr('title');
	// Open Links in New Window
	$('a[rel=external]').click(function(){
		window.open(this.href);
		return false;   
	});
	// Clear Page 
	$('#page, #primaryContent, #secondaryContent, .content').append('<div class="clear" />');

	////////////////////////////////////////
	// Stick Footer
	////////////////////////////////////////
	function getOffset(){
		var winY = $(window).height();
		var pageY = $('#header').outerHeight() + $('#page').outerHeight();
		var footY = $('#footer').outerHeight(); 
		if ((pageY + 224) < winY) {
			$('#footer').css({
				'position' : 'absolute',
				'top' : winY - footY
			});
		}
	}
	getOffset();
	$(window).resize(getOffset);
    
	////////////////////////////////////////
	// Mobile Facts
	////////////////////////////////////////
/*
	var $trigger = $('#mobileFacts li');
	$($trigger).click(function(){
		var $fact = $(this).find('div'); 
		if ($(this).hasClass('open')) {
			$(this).removeClass('open').find('div:visible').hide();
		} else {
			$(this).removeClass('open').siblings().find('div:visible').hide().end().end()   
			.find('div').show();
		}
	});
	$('#mobileFacts li h3:first, #mobileFacts li .sIFR-flash:first').trigger('click');      */


	////////////////////////////////////////
	// Mobile Terms
	////////////////////////////////////////
	$('.contentBlock.list').prepend('<span class="ne" /><span class="se" />')
	var $mobileTerm = $('.contentBlock.list dt');
	$mobileTerm.hover(function(){
		$(this).addClass('hover');
	}, function (){
		$(this).removeClass('hover');
	});
	// Show Hide Definitions
	$mobileTerm.click(function(){
		if ($(this).hasClass('open')) {
			$(this).removeClass('open').next('dd').hide();
		} else {
			// Find Open Siblings, Remove Open class hide Next DD, Back up to original Selection
			$(this).siblings('dt.open').removeClass('open').next('dd').hide().end().end()
			// Add open class and Show Sibling DD
			.addClass('open').next('dd').show();
			// If scroll is loaded Scroll to the Open DT
			if (jQuery.fn.scrollTo) {
			   $(window).scrollTo(this, 400, { offset: -10 }); 
			}
		}
	});
	
	////////////////////////////////////////
	// Contact Form
	//////////////////////////////////////// 
	$('#contactForm label').overlabel();
	if(jQuery.fn.validate) {
		$('#contactForm').validate({ submitHandler : ajaxSubmit }); 
	}                                
	function ajaxSubmit(form){

			var name = $('#cName').val(); 
			var data = $(form).serialize();

			$('.submit').addClass('working');
			$.ajax({
				type: "POST",
				url: "/components/send.php",
				data: data,
				success: function() {
					$('#contactForm').hide();
					$('.contentBlock.large').html('<h2>Thanks</h2><p class="success">We will be in touch shortly '+name+'.</p>');
				}
			});
			return false;
	}

});
