$(document).ready(function() {

	var country_input_text = 'Start typing a country...';

	$("#overlay").height($(document).height());

	var isOpera = navigator.userAgent.indexOf("Opera") > -1;
	var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
	var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;
	 
	function textboxSelect(oTextbox, iStart, iEnd) {
		switch(arguments.length) {
			case 1:
				oTextbox.select();
				break;
			case 2:
				iEnd = oTextbox.value.length;
				/* falls through */
			case 3:
				if(isIE) {
					var oRange = oTextbox.createTextRange();
					oRange.moveStart("character", iStart);
					oRange.moveEnd("character", -oTextbox.value.length + iEnd);
					oRange.select();
				} else if(isMoz) {
					oTextbox.setSelectionRange(iStart, iEnd);
				}
		}
		oTextbox.focus();
	}

	var country = $("#expert_country_select").val();
	if(country != "0") {
		$("#expert_area_select").load("/scripts/get-fields-by-country.asp", { country: country });
	};

	$("#expert_country_select").change(function() {
		var country = $(this).val();
		if(country != "0") {
			$("#expert_area_select").load("/scripts/get-fields-by-country.asp", { country: country });
			$("#all_experts").load("/scripts/experts-list.asp", { country: country });
		} else {
			$("#expert_area_select").load("/scripts/get-fields-by-country.asp");
			$("#all_experts").load("/scripts/experts-list.asp");
		}
	});

	$("#expert_area_select").change(function() {
		var field = $(this).val();
		var country = $("#expert_country_select").val();
		if(field != "0") {
			if(country != "0") {
				$("#all_experts").load("/scripts/experts-list.asp", { field: field, country: country });
			} else {
				$("#all_experts").load("/scripts/experts-list.asp", { field: field });
			}
		}
	});

	$("#countries-input").val(country_input_text).css("color","#BBB");

	$("#countries-input").focus(function() {
		if($(this).val() == country_input_text) {
			$(this).val("").css("color", "#6F6F6F");
		}
	});

	$("#countries-input").blur(function() {
		if($(this).val() == "") {
			$(this).val(country_input_text).css("color","#CCC");
		}
	});

	$("#countries-input").keyup(function(e) {
		var inputbox = $(this);
		var country = inputbox.val();
		var cursorPos = country.length;
		var keycode = e.keyCode;
		if(cursorPos > 1)
		{
			if(keycode == 32 || (keycode >= 65 && keycode <= 90)) { // only make AJAX request if input is a letter or a space
				var result;
				$.get("/autocomplete.asp", { 'country': country }, function(data) {
					if(data.length > 0) {
						inputbox.val(data);
						textboxSelect(document.getElementById("countries-input"), cursorPos, data.length);
					}
				});
			}
		}
	});

	var hash = window.location.hash;

	if(hash == "#countries" || hash == "countries") {
		$("#countries-link").addClass("here")
			                .siblings("li")
							.removeClass("here");
		$("#content").scrollTo("#countries");
	}

	function redirectForm() {
		var field1_value = $("#what-do-input").val();
		var field2_value = $("#countries-input").val();
		if(field1_value.length > 0 && field2_value.length > 0 && field2_value != country_input_text) {
			$("#searchform").submit();
		}
	}

	$("#login-link").click(function() {
		if($(this).hasClass("active")) {
			$(this).removeClass("active");
			$("#login-form").slideUp("fast");
		} else {
			$(this).addClass("active");
			$("#login-form").slideDown("fast");
			$("#username").focus();
		}
		return false;
	});

	$(".dropdown-button").click(function() {
		var popup = $(this).parents(".textbox")
			               .siblings(".popup");
		var textbox = $(this).parents(".textbox");

		if(popup.is(":visible")) {
			popup.slideUp(150);
			textbox.css("background-image", "url(/images/ui/search-form.png)");
		} else {
			popup.slideDown(250);
			textbox.css("background-image", "url(/images/ui/search-form-open.png)");
		}
		return false;
	});

	$("#home-link").click(function() {
		$(this).siblings("li").removeClass("here");
		$(this).addClass("here");
		$("#content").scrollTo("#startpage", 400);
		window.location.hash = "";
		return false;
	});

	$("#countries-link").click(function() {
		$(this).siblings("li")
			   .removeClass("here");
		$(this).addClass("here");
		$("#content").scrollTo("#countries", 400);
		return false;
	});

	$("#resources-link").click(function() {
		$(this).siblings("li")
			   .removeClass("here");
		$(this).addClass("here");
		$("#content").scrollTo("#resources", 400);
		return false;
	});

	$("#searchform input").change(function() {
		redirectForm();
	});

	$(".popup a").click(function() {
		$(this).parents(".search-container")
			   .children(".textbox")
			   .css("background-image", "url(/images/ui/search-form.png)")
			   .children(".input")
			   .val($(this).text())
			   .css("color", "#6F6F6F");
		$(this).parents(".popup")
			   .slideUp("fast");
		redirectForm();
		return false;
	});

	$("#countries-slider div").slider({
		orientation: "vertical",
		min: 0,
		max: 100,
		value: 100,
		change: handleSliderChange,
		slide: handleSliderSlide
	});

	function handleSliderChange(e, ui) {
		$("#countries-list-small").scrollTo((100 - ui.value) + "%");
	}

	function handleSliderSlide(e, ui) {
		$("#countries-list-small").scrollTo((100 - ui.value) + "%");
	}

	$("#subnav .navigation > li:not(li.nomenu)").hover(
		function() {
			$("#subnav .navigation ul").hide();
			$(this).addClass("open");
			$(this).children("ul")
				   .slideDown("fast");
		},
		function() {
			$("#subnav .navigation ul").stop(true, true);
			$("#subnav .navigation > li.open").removeClass("open");
			$("#subnav .navigation ul").hide();
		}
	);

	$("#ask_question_nologin").click(function() {
		$("#login_needed, #overlay").show();
		return false;
	});

	$("#ask_question_loggedin").click(function() {
		$("#expert_form").slideDown("fast");
		return false;
	});

	$("#question_cancel").click(function() {
		$("#expert_form").slideUp("fast");
		return false;
	});

	$("#close").click(function() {
		$("#login_needed, #overlay").hide();
		$("#login-link").click();
		return false;
	});

	$("a.country_flag").click(function() {
		var country = $(this).attr("rel");
		var popup = $("#country_popup");

		var scrollHeight	= window.pageYOffset;
		var documentHeight	= window.innerHeight;
		var documentWidth	= window.innerWidth;

		var topPosition		= scrollHeight + (documentHeight / 2);
		var leftPosition	= documentWidth / 2;

		$("#overlay").show();
		popup.load("/scripts/country-popup.asp", { country: country }, function() {
			popup.css("top", topPosition);
			popup.css("left", leftPosition);
		}).show();

		return false;
	});

	$("#close_popup").livequery("click", function() {
		$("#overlay, #country_popup").hide();
		return false;
	});

});