$(document).ready(function() {
	
	// Make the subnav's height match the content's height
	var $subNav = $("#sub_nav");
	if ($subNav.length > 0) {
		$subNav.matchHeight("div#content");
	}
	
	// Homepage banner cycling
	$navLinks = $("div#bb_nav li a");
	$learnMoreLink = $("#learn_more_btn");
	lastBannerID = "#bb_focus";
	timer = null;
	navIndex = 1;
	if ($navLinks) {
		$navLinks.click(bbNavClick);
		timer = setTimeout("autoChangeBillboard()", 9000);
	}
	
	// Get Started Popup
	$("a.get-started").click(showGetStarted);
	
	// Homepage Twitter feed
	getTwitters('tweets', { 
		id: 'nuezra',
		count: 3,
		ignoreReplies: false,
		newwindow: true,
		//template: '"%text%" <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
		template: '<p>%text%</p><span>%time% from %source%</span>'
	});
});	

function bbNavClick(e) {
	e.preventDefault();
	
	$link = $(this);
	navIndex = $link.text();
	if (navIndex > 2) {
		navIndex = 0;
	}
	
	// Reset the timer so you have a chance to read the billboard
	clearTimeout(timer);
	timer = setTimeout("autoChangeBillboard()", 9000);
	
	changeBillboard($link);
}

function changeBillboard($link) {
	var bannerID = $link.attr("href");
	$learnMoreLink.attr("href", "services/billboard"+$link.text()+"/");
	
	$navLinks.removeClass("on");
	$link.addClass("on");
	
	$(lastBannerID).fadeOut("def");
	$(bannerID).fadeIn("def");
	
	if (bannerID == "#bb_welcome") {
		$learnMoreLink.fadeOut("def");
	} else {
		$learnMoreLink.fadeIn("def");
	}
	
	lastBannerID = bannerID;
}

function autoChangeBillboard() {
	if (timer) {
		changeBillboard($($navLinks[navIndex]))
		if (navIndex == 3) {
			navIndex = 0;
		} else {
			navIndex++;
		}
	}
	timer = setTimeout("autoChangeBillboard()", 9000);
}

function showGetStarted(e) {
	e.preventDefault();
	
	var contact_url = $(this).attr("href");
	var overlay = $("div#overlay");
	
	overlay.click(hideGetStarted);
	
	$("#contact_form").load(contact_url+" #contact_form div", function() {
		$(this).fadeIn("def");
		overlay.fadeIn("def");
		$(this).find("a#cancel, a#close").click(hideGetStarted);
    first_name = new LiveValidation('first_name', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    first_name.add(Validate.Presence, {failureMessage: "Please enter a first name" } );
    last_name = new LiveValidation('last_name', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    last_name.add(Validate.Presence, {failureMessage: "Please enter a last name" } );
    company = new LiveValidation('company', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    company.add(Validate.Presence, {failureMessage: "Please enter a company" } );
    email = new LiveValidation('email', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    email.add(Validate.Presence, {failureMessage: "Please enter an email" } );
    email.add(Validate.Email, {failureMessage: "Not a valid email" } );
    email2 = new LiveValidation('email2', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    email2.add(Validate.Presence, {failureMessage: "Please enter your email again" } );
    email2.add( Validate.Confirmation, { match: 'email', failureMessage: "Email must match" } );    
    phone = new LiveValidation('phone', {validMessage: '', wait: 500, onlyOnSubmit: true } );
    phone.add(Validate.Presence, {failureMessage: "Please enter a phone number" } );
	});
	
	function hideGetStarted(e) {
		e.preventDefault();
		$("#contact_form").fadeOut("fast");
		overlay.fadeOut("def")
	}
}

// Code to auto tab contact us form fields

function autotab(current,to){
    if (current.getAttribute && 
      current.value.length==current.getAttribute("maxlength")) {
        to.focus() 
        }
}

