// Functions required for populating
// the list of available Mercury
// products in each state

$(document).ready(function(){
	
	// if user uses back button, select option should
	// retain the last state selected
	// this is session-based
	if($.cookie("stateInitials") != null && $.cookie("stateName") != null) {
		//alert($.cookie("stateInitials"))
		//build list
		buildList($.cookie("stateInitials"));
		// swap in the last selected state
		$('#activator div').text($.cookie("stateName"));
	}

	// activator higlight on
	$('#activator').mouseover(function() {
  		$(this).addClass('on');
	});
	// activator highlight off
	$('#activator').mouseout(function() {
  		$(this).removeClass('on');
	});

	// activator clicked to open the dropdown
	$('#activator').click(function() {
		$('#dropdown').show();
	});
	
	
	// list higlight on
	$('#dropdown dt').mouseover(function() {
  		$(this).css('background-color', '#ebebeb');
	});
	// list highlight off
	$('#dropdown dt').mouseout(function() {
  		$(this).css('background-color', 'transparent');
	});
	
	
	$('#dropList dt').click(function() {
		
		// grab the state ID
		var clicked = $(this).attr('id');
		// grab the full state name
		var stateName = $(this).text();
		// grab any class attached to the clicked state
		// we are looking for "current" - it serves as a flag
		var state = $(this).attr('class');
		
		// set cookies in case the user clicks to a new page
		// and then uses the back button - reload last selected State
		$.cookie("stateInitials", clicked);
		$.cookie("stateName", stateName);
		
		// build a product list if the clicked isn't the "Select a State"
		// and isn't a State that has the current flag
		if(state != 'current'){
			// go build the product list
			buildList(clicked);
			//close the dropdown
			$('#dropdown').hide();
			// leave the selected state showing
			$('#activator div').text(stateName);
		} else {
			// it is current, so close the list if the user
			// clicks the state again - no reason to rebuild the product list
			$('#dropdown').hide();
		};
	})
	
	
	// function to close the opened dropdown if user clicks outside of it
	$(document).bind('click', function(e) {
    	var $clickCheck = $(e.target);
    	if (! $clickCheck.parents().hasClass("dropdown-wrapper"))
      	//close the dropdown
		$('#dropdown').hide();
		// leave the selected (with class "current") state showing
		$('#dropdown dt.current').show();
  	});
	
// --- close doc ready
});


function buildList(clicked){
	
	// since we may be replacing a displayed list, 
	// we need to clear current list items
	$('#state-product-list dt').hide();
	
	// match the selected state with its product list
	switch (clicked) {
		case 'AZ':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'mechanical': 'show', 'state-az': 'show'};
			manipulate(productList);
			break;
		case 'CA':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'umbrella': 'show', 'mechanical': 'show', 'commauto': 'show', 'commbusiness': 'show', 'state-ca': 'show'};
			manipulate(productList);
			break;
		case 'FL':
			productList = {'auto': 'show', 'mechanical': 'show', 'commauto': 'show', 'state-fl': 'show'};
			manipulate(productList);
			break;
		case 'GA':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'umbrella': 'show', 'mechanical': 'show', 'state-ga': 'show'};
			manipulate(productList);
			break;
		case 'IL':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'umbrella': 'show', 'mechanical': 'show', 'state-il': 'show'};
			manipulate(productList);
			break;
		case 'MI':
			productList = {'auto': 'show', 'state-mi': 'show'};
			manipulate(productList);
			break;
		case 'NJ':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'mechanical': 'show', 'state-nj': 'show'};
			manipulate(productList);
			break;
		case 'NV':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'mechanical': 'show', 'state-nv': 'show'};
			manipulate(productList);
			break;
		case 'NY':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'mechanical': 'show', 'state-ny': 'show'};
			manipulate(productList);
			break;
		case 'OK':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'umbrella': 'show', 'mechanical': 'show', 'commauto': 'show', 'commbusiness': 'show', 'state-ok': 'show'};
			manipulate(productList);
			break;
		case 'PA':
			productList = {'auto': 'show', 'mechanical': 'show', 'state-pa': 'show'};
			manipulate(productList);
			break;
		case 'TX':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'umbrella': 'show', 'mechanical': 'show', 'commauto': 'show', 'commbusiness': 'show', 'state-tx': 'show'};
			manipulate(productList);
			break;
		case 'VA':
			productList = {'auto': 'show', 'home': 'show', 'condo': 'show', 'renters': 'show', 'mechanical': 'show', 'state-va': 'show'};
			manipulate(productList);
			break;
		default:
			productList = ('default', 'show');
			break;
	}
	
	// loop through the correct state data showing or hiding products
	function manipulate(productList){
		$.each(productList, function(key, value){
			if (value = 'show'){
				// show DT
				$('#state-product-list #' + key).show();
			}
		});
	};
	
	// loop through all the States and clear any "current" flags
	$('#dropdown dt').each(function(){
        $(this).removeClass('current');
      });
	
    // set the latest clicked Sate as "current"
    // this prevents rebuilding the product list
    // when a user clicks on the "current" State
    // to choose another State
	$('#dropdown dt#' + clicked).addClass('current');

};
