$(function() {
	// load the galleria theme
	Galleria.loadTheme("/assets/home/scripts/galleria.bcbsnc.js");
	
	// init galleria
	$("#carousel").galleria();
	
	// set the default tab for analytics
	blue.tabs.defaultTab = $("div.tabs-container li a:first").text();
	
	var defaultTab = (function() {
		var cookieTab = $.cookie("HOMEPAGETAB"),
			tabIndex = 0;
		
		// if a cookieTab exists, set selected to the index of the tab
		// that matches the hash
		if (cookieTab) {
			$("div.tabs-container li a", tabs).each(function(index, element) {
				var hash = $(this).attr("href").replace("#", "");
				if (hash === cookieTab) {
					// let analytics know the text of the tab
					blue.tabs.defaultTab = $(this).text();
					
					tabIndex = index;
					return false;
				}
			});
			
			return tabIndex;
		} 
	})();
		
	// build the tabs
	var tabs = $("#tabs");
	tabs.tabs({
		selected : defaultTab
	});
	
	// if a cookie has been set for a quote, show the resume application button
	if ($.cookie("HOMEPAGERESUMEAPP")) {
		$("#quote-button-show").hide();
		$("#resume-btn-show").show();
	}
	
	// if we see fullsite = true in the query string, set a cookie
	var fullsiteParam = getParameterByName("fullsite");
	if (fullsiteParam) {
		if (fullsiteParam === "true") {
			$.cookie("mobileRedirect", "false", {
				path : "/"
			});
		}
	}
	
	// build the login model
	var loginDialog = $("#login-dialog").dialog({
		autoOpen : false,
		draggable : false,
		resizable : false,
		width : 224,
		minHeight : 0
	});
	
	loginDialog.bind("submit", function() {
		console.log("the form was submitted");
	});
	
	$("#login-dialog-trigger").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		var offset = $(this).offset(),
			width = $(this).width(),
			height = $(this).height(),
			dialogWidth = loginDialog.dialog("option", "width"),
			left = null,
			top = null;
		
		// calculate the position
		// center it just below the login link
		left = offset.left - dialogWidth + width;
		top = offset.top + height;				
		
		loginDialog.dialog("option", "position", [left, top]);
		loginDialog.dialog("open");
		
		// put the focus on the first input field of the login form in the dialog
		$("input[type='text']:first", loginDialog).focus();
	});
	
	// 
	$(document).click(function(event) {
		if (!$(event.target).closest(".ui-dialog").length > 0) {
			if (loginDialog.dialog("isOpen")) {
				loginDialog.dialog("close");
			}
		}
	});
	
	var loginSelect = $("#login-select");
	loginSelect.bSelect({ 
		width : "187",
		upArrowFileName : "/assets/members/public/images/select-arrow-up.png",
		dnArrowFileName : "/assets/members/public/images/select-arrow-dn.png",
		showSelect : false
	});
				
	loginSelect.change(function() {
		$("div.login-dialog-form:visible").hide();
		$("#login-dialog-" + $(this).val()).show();
	});
	
	// handle any possible error messages
	$(".error-msg").each(function() {
		if ($(this).text().length > 0) {
			$(this).removeClass("hide");
		}
	});
	
	// fade the page in when everything has finished building
	$("#container").fadeIn();
	
	
	// for getting query string parameters
	function getParameterByName(name) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)",
			regex = new RegExp(regexS),
			results = regex.exec(window.location.href);
		
		if(results == null) {
		  return false;
		} else {
		  return decodeURIComponent(results[1].replace(/\+/g, " "));
		}
	}
});
