$(document).ready(function() {
	// these are intially hidden with CSS and shown only in the event JS is on.  could be anything.
	$(".js-required").show();

	var int;
	var curItem = 0;
	var prefix = "";

	// legacy code to save a cookie
	function setCookie(sName, sValue, oExpires) {
		var sCookie = sName + ("=" + encodeURIComponent(sValue));
		if (oExpires) {
			nowTime = (new Date).getTime();
			sCookie += "; expires=" + oExpires.toGMTString() + "; " + nowTime;
		}
		document.cookie = sCookie;
	}
	
	// legacy code to retrieve a cookie
	function getCookie(sName) {
		var sRE = "(?:; )?" + sName + "=([^;]*);?";
		var oRE = new RegExp(sRE);
		if (oRE.test(document.cookie)) {
			return decodeURIComponent(RegExp.$1);
		} else {
			return "";
		}
	}
	
	// legacy code to delete a cookie
	function deleteCookie(sName) {
		setCookie(sName, "", new Date(0));
	}
	
	// carousel.  the item to go to in the carousel.
	function goto(obj) {
		// hide all carousel-img divs. this will include the one we're moving to.
		$(".carousel-img").hide();
		
		// turn the one we're going to on.
		$("#"+$(obj).attr("id")+"-div").show();
		
		// get rid of any 'current' items.  this will include the one we're moving to.
		$(".carousel-controls a").removeClass("current");
		
		// put the 'current' class on our current item.
		$(obj).addClass("current");
	}
	
	// carousel.  used to start the timer on load and when switching audiences.
	function startTimer() {
		clearInterval(int);
		int = setInterval(gotoNext, 5000);
	}
	
	// carousel.  move to the next item
	function gotoNext() {
		// go to it...
		goto($("#"+prefix+curItem));
		
		// ...and then set it to the next item.  if we're at the end, go back to 0.
		curItem = (curItem+1)%$("#carousel-"+prefix+" .carousel-controls a").length;
	}
	
	// used in the phys search.  this will restore the initial, hard-coded HTML values to a given select box.
	function resetValue(id) {
		var prevVal = $("#"+id).val();
		$("#"+id).html(defaults[id]);
		$("#"+id).val(prevVal);
	}
	
	// login.  set up the active tabs and active carousel.
	function setTabs(obj) {
		// set the carousel item to the first.  we'll go there later.
		curItem = 0;
		
		// remove the 'current' class from all login tabs.  add it back to the current one.
		$("#login-tabs a").removeClass("current");
		$(obj).addClass("current");
		
		// hide every carousel group.  we'll show only the one we need next.
		$(".carousel-group").hide();
		
		// could set it to <a href> but just in case, do it statically.
		switch($(obj).attr("id")) {
			case "tab-agent":
				prefix = "agent";
				$("#login").attr("src", "/homepage/logins/agent-login.shtml");
				$("#carousel-agent").show();
				break;
			case "tab-group":
				prefix = "group";
				$("#login").attr("src", "/homepage/logins/group-login.shtml");
				$("#carousel-group").show();
				break;
			case "tab-provider":
				prefix = "provider";
				$("#login").attr("src", "/homepage/logins/provider-login.shtml");
				$("#carousel-provider").show();
				break;
			default:
				prefix = "member";
				$("#login").attr("src", "/homepage/logins/member-login.shtml");
				$("#carousel-member").show();
				break;
		}
		// restart the timer
		startTimer();
		
		// and go to the first item (we set curitem=0 earlier).
		gotoNext();
	}

	// page load / set iframe
	switch(getCookie("loginURL")) {
		case "1":
			setTabs($("#tab-agent"));
			break;
		case "2":
			setTabs($("#tab-group"));
			break;
		case "3":
			setTabs($("#tab-provider"));
			break;
		default:
			setTabs($("#tab-member"));
			break;
	}
		
	// tab clicking.  handled separately because each sends a new number.
	$("#tab-member").click(function() {
		setCookie("loginURL", "0", new Date(Date.parse("Jan 1, 2050")));
		setTabs($(this));
	});
	$("#tab-agent").click(function() {
		setCookie("loginURL", "1", new Date(Date.parse("Jan 1, 2050")));
		setTabs($(this));
	});
	$("#tab-group").click(function() {
		setCookie("loginURL", "2", new Date(Date.parse("Jan 1, 2050")));
		setTabs($(this));
	});
	$("#tab-provider").click(function() {
		setCookie("loginURL", "3", new Date(Date.parse("Jan 1, 2050")));
		setTabs($(this));
	});
			   
	// carousel controls
	$(".carousel-controls a").mouseover(function() {
		// kill the timer so it doesnt change and then go to the rolled over tab
		clearInterval(int);
		goto($(this));
	});
	
	
	
	
	
	
	
	// phys search
	// we use this variable to store the default values of all of our dropdowns.  IE doesn't allow you to hide() option tags, so we have to remove them and re-build the dropdown.
	var defaults = {};
	var curSearch = "doctor";
	$("#search_form")[0].reset();

	// different items get sent as specialty, so instead of using name=specialty we're using class=specialty, and any time a change is made to one of them, set the item with id=specialty (which also has name=specialty) to that value.  that way many items are submitting through one input.
	$(".specialty").change(function() {
		$("#specialty").val($(this).val());
	});
	// same idea as above
	$(".planType").change(function() {
		$("#planType").val($(this).val());
	});
	// if we select vision, we need to send planType as DHP.
	$(".visionPlanType").change(function() {
		$("#planType").val("DHP");
	});
	
	// for every form, add its default contents to the defaults object.  we'll use this like an associative array.  then hide them all.
	$("#search_form select").each(function() {
		defaults[$(this).attr("id")] = $(this).html();
		$(this).hide();
	});
	
	// hide anything that shouldnt be there from the start.  better than using CSS in case they have JS off these things will still display.
	$(".initially-hidden").hide();
	
	// show the main selection dropdown.
	$("#search_type").show();
	
	// we link form elements together by giving them the same class name, remembering that elements can have multiple classes.  we default to doctors mode.
	$(".doctor").show();
	
	// when you change the search_type dropdown (the main one)...
	$("#search_type").change(function() {
		// if they selected a heading or nothing, tell them to make a selection.  stop here.  we don't want to perform any actions with garbage data.
		if(!$(this).val() || $(this).val() == "" || $(this).val() == "none") {
			return;
		}
		// hide all our selects.
		$(".initially-hidden, .doctor, .doctor_sub, .hospital, .hospital_sub, .vision, .hearing, .dentist, .national").hide();
		
		// show dropdowns with a class name equal to that of the selected value.  so value="hospital" shows all selects with class="hospital".
		$("."+$(this).val()).show();
		
		// set the county name depending where we are.  this poor practice is built in to the legacy search and cannot be removed without a rebuild of the backend.
		$("#search-county").attr("name", $(this).val() == "hospital" ? "fcounty" : "provCounty");
		
		curSearch = $(this).val();
		
		// set the action to the appropriate URL.
		switch($(this).val()) {
			case "hospital":
				$("#search_form").attr("action", "http://www.bcbsm.com/FacilityDirectories/FacilitySearch.jsp");
				break;
			case "doctor":
				$("#search_form").attr("action", "http://www.bcbsm.com/provdir/ProviderSearchServlet");  
				break;
			default:
				$("#search_form").attr("action", "http://www.bcbsm.com/vision/ProviderSearchServlet");
				break;





		}
	});
	
	// when you change the search_doctor_type dropdown (the one with all the different plan types)...
	$("#search_doctor_plan,#search_hospital_plan").change(function() {
		var self = $(this);
		// if we select the heading, do nothing.
		if($(this).val() == "") {
			return false;
		}
		// hide any sub items, like the network dropdown.
		$(".dependency").each(function() {
			if($(this).hasClass(curSearch+"_sub") && $(this).hasClass($(self).val())) {
				$(this).show();
			}
			else {
				$(this).hide();
			}
		});
		
		
		// if we find something with the class name equal to that of our value (like Hospital Blue), show it.  this and the above line work together to provide redundancy so we're not concerned about whether or not it's already open.  you don't hurt anything by hiding a hidden item nor by showing a visible item.
		$("."+curSearch).show();

		// since IE doesn't behave, we can't just hide items we want to remove.  Earlier, we saved the contents of every dropdown in an associate array.  Rebuild the current dropdown using those contents.
		resetValue("search_"+curSearch+"_specialty");
		
		// for every option tag, we're going to see if we need to remove it.
		$("#search_"+curSearch+"_specialty option").each(function() {
			if($(this).attr("class") != "" && !$(this).hasClass($(self).val())) {
				// if we're going to remove the one we currently have highlighted, we need to move back to the top.
				if($(this).val() == $(this).parent().val()) {
					$(this).parent().val("");
				}
				$(this).remove();
			}
		});
		
		// special case.  hospital blue needs something hidden if it's the hospital selection.  99% of the time this doesn't do anything.
		if($(this).val() == "MHMP" && $(this).attr("id") == "search_hospital_plan") {
			// hide the facilty type for hospital blue when selecting a hospital
			//$("#search_hospital_specialty").hide();
		}
	});
});
