/* Arrays */
var postion = [];
postion['auto'] = '-153px 0px';
postion['home'] = '-105px 0px';
postion['health'] = '-55px 0px';
postion['life'] = '-11px 0px';

var featured = [];
featured[0] = 'one';
featured[1] = 'two';
featured[2] = 'three';


$(document).ready(function() {
/* This creates the type text on mouse over and reverts back on mouse out*/	
$('div#buttons .image').mouseover(function() {
	var buttonsID = $(this).attr('id');
	
	 $('div#buttons .text').html(buttonsID);
	 
});
	
$('div#buttons .image').mouseout(function() {
	
	if($('div#buttons .image').hasClass('selected')) {
		 $('div#buttons .text').html($('div#buttons .selected').attr('id'));
	 }else{
		 $('div#buttons .text').html('');
	 }
	 
});

/* When form changes, I need to change all my classes again */
$("select#insurance-type").change(function() {

	$('div#buttons .image').removeClass('selected');
	MoveObjects(this.value);	
	$('div#buttons #'+this.value).addClass('selected');
		
});

/* Removes and adds classes to all moving functions once button is clicked */
$('div#buttons .image').click(function() {
	var buttonsID = $(this).attr('id');

	$('div#buttons .image').removeClass('selected');
	MoveObjects(buttonsID);	
	$(this).addClass('selected');
	
	/* Changes select once button is clicked */
	 $('#insurance-type').get(0).value = buttonsID; 		
	
});

/* Create on clicks for featured articles */
$('div.featured-nav div').click(function() {
	var buttonsID = $(this).attr('id');
	
	$('div#featured .info').hide();
	$('div#featured .image').hide();
			
	$('div#featured .'+buttonsID).show();
	
	$('div.featured-hovers div').removeClass('hover');
	
	$('div.featured-hovers div.'+buttonsID).addClass('hover'); 

});

/* Removes or adds default value to text inputs */
$("input.labelinside, textarea.labelinside").each(function(i){
	if ( $(this).val().length == 0 ) {                    
		$(this).val($(this).attr('title'));
		$(this).addClass("default");
	}
});
$("input.labelinside, textarea.labelinside").focus(function(e){
	if ( $(this).val() == $(this).attr('title') ) {
		$(this).val('');
		$(this).removeClass("default");
	}
});
$("input.labelinside, textarea.labelinside").blur(function(e){
	if ( $(this).val() == '' ) {
		$(this).val($(this).attr('title'));
		$(this).addClass("default");
	}
 });

/* Removes or adds default value to text inputs with passwords */
$("input.labelinside-password").each(function(i){
	if ( $(this).val().length == 0 ) {                    
		$(this).val($(this).attr('title'));
		$("input.password-login").hide();
	}
});
$("input.labelinside-password").focus(function(e){
	if ( $(this).val() == $(this).attr('title') ) {
		$(this).val('');
		$(this).hide();
		$("input.password-login").show();
		$("input.password-login").get(0).focus();
	}
});

$("input.password-login").blur(function(e){
	if ( $(this).val() == '' ) {
		$(this).hide();
		$("input.labelinside-password").show();
		$("input.labelinside-password").val($("input.labelinside-password").attr('title'));

	}
 });	 

 
 /* This Triggers the form action aka: submits form */
 $('div.form div.button').click(function() {
	$('#header-form').submit(FormCheck(this, 'header-zipcode', 'header-select'));
 });
 
 
});

/* Super function to move all my objects */
function MoveObjects(type) {
	$('div#text div').removeAttr('class');
	$('div#image div').removeAttr('class');
	$('div#cta div').removeAttr('class');
	
	$('div#text div').addClass(type);
	
	$('div#image div').addClass(type); 
	
	$('div#cta div').addClass(type);  
	
	
	$("#slider").animate({
		backgroundPosition: postion[type]
	}, 500);
	
	$('div#buttons div.text').html(type);
	
}

function is_valid_zipcode(zipcode) {  
    var reg = /^([0-9]{5})$/;
    return reg.test(zipcode); 
}


function FormCheck(form, zipcode, select) {
   
    var siteURL ="";
	var insurance_type = $(select).val();
	
		siteURL ="/" + insurance_type + "/";
		siteURL = siteURL + "?zip=" + $(zipcode).val();
		
		$(zipcode).removeClass('error');
	
		if ( !is_valid_zipcode($(zipcode).val()) ) {
			$(zipcode).addClass('error');
			alert("Please enter a valid zip code.");
			return false;
			$(zipcode).focus()
		}else{
			
			if(insurance_type != '') {
	
				window.open(siteURL,null,"fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes"); 
				$(form).submit();
				return true;
			
			}
		}
	
}      


/* This makes our navigation dropdowns appear/disappear */
var activeInfoDiv = null;

function changeInfoDiv(div) {
if(!arguments[0] && activeInfoDiv) {
	document.getElementById(activeInfoDiv).style.display = 'none';
	activeInfoDiv = null;
}
else if(div != activeInfoDiv) {
	if(activeInfoDiv) {
		document.getElementById(activeInfoDiv).style.display = 'none';
	}
	document.getElementById(div).style.display = 'block';
	activeInfoDiv = div;
}

} 

