﻿var goingUp = "none";
var uptimer;
$(document).ready(function() {
	////////////////////////////////////////
	// top navigation mousovers
	$(".menuHeader").hover(function() {
		$(this).addClass("over");
		if (goingUp != $(this).attr("id")) {
			$(this).find(".menuDropdown").stop(true, true);
			$(this).find(".menuDropdown").slideDown("slow");
		} else {
			clearTimeout(uptimer);
			goingUp = "none";
		}
	}, function() {
		goingUp = $(this).attr("id");
		$(this).find(".menuDropdown").stop(true, true);
		uptimer = setTimeout("$('#" + $(this).attr("id") + "').removeClass('over');$('#" + $(this).attr("id") + "').find('.menuDropdown').slideUp('slow', function() { goingUp = 'none'; });", 500);
	});
	// add bg iframe
	$(".menuHeader").bgiframe();
	//////end top navigation mouseovers/////

	////////////////////////////////////////
	//////button mousovers//////////////////
	if ($(".hover").length) {
		$(".hover").each(function() {
			$(this).hover(function() {
				$(this).addClass("over");
			}, function() {
				$(this).removeClass("over");
			});
		});
	}
	//////end button mousovers//////////////

	//////setup search//////////////////////
	$("#txt_search").focus(function() {
		if (this.value == 'search') {
			this.value = '';
		}
		this.style.color = '#000';
	});
	$("#btn_search").click(function() {
		if (this.value == 'search') { return false; }
		window.location = "/search.html?q=" + escape($("#txt_search").val());
	});
	$("#search_form").submit(function() {
		if (this.value == 'search') { return false; }
		window.location = "/search.html?q=" + escape($("#txt_search").val());
		return false;
	});

	/////setup email overlays
	$("#btn_email").attr("href", "/email.aspx?p=" + escape(window.document.title) + "&u=" + escape(window.document.URL));
	$("#btn_email").click(function() {
		return GB_showCenter('', this.href, 540, 435);
	});
});

var map;
var panel;
var latBoston = 42.358897;
var lonBoston = -71.057795;
var latNJ = 40.480047;
var lonNJ = -74.409542;
var latChina = 30;
var lonChina = 74;
var curLoc = "b";
function initialize() {
	if (GBrowserIsCompatible()) {
		panel = document.getElementById("panel");
		map = new GMap2(document.getElementById("map_canvas"));

		var fullURL = parent.document.URL;
		var queryString = fullURL.substring(fullURL.indexOf('?') + 1, fullURL.length);
		if (queryString == "l=n") {
			curLoc = "n";
		} else if (queryString == "l=c") {
			curLoc = "c";
		}
		
		map.setUIToDefault();

		// load the lat lon for the current position
		var point;
		if (curLoc == "b") {
			$("#boston").show();
			point = new GLatLng(latBoston, lonBoston);
		} else if (curLoc == "n") {
			$("#new_jersey").show();
			point = new GLatLng(latNJ, lonNJ);
		} else if (curLoc == "c") {
			$("#china").show();
			$("#directions_form").hide();
			point = new GLatLng(latChina, lonChina);
		}
		map.setCenter(point, 13);
		map.addOverlay(new GMarker(point));

	}
}

function getDirections() {
	if (!$("#txt_address") || !$("#txt_city") || !$("#ddl_state")) {
		return;
	}
	var error = false;
	if ($("#txt_address").val().length < 1) {
		error = true;
		$("#error_txtAddress").fadeIn("fast");
	} else {
		$("#error_txtAddress").fadeOut("fast");
	}
	if ($("#txt_city").val().length < 1) {
		error = true;
		$("#error_txtCity").fadeIn("fast");
	} else {
		$("#error_txtCity").fadeOut("fast");
	}
	if ($("#ddl_state").val() == "none") {
		error = true;
		$("#error_ddlState").fadeIn("fast");
	} else {
		$("#error_ddlState").fadeOut("fast");
	}

	if (error) {
		return;
	}
	
	$("#error_txtAddress").hide("fast");
	$("#error_txtCity").hide("fast");
	$("#error_ddlState").hide("fast");

	var loc = $("#txt_address").val() + ", " + $("#txt_city").val() + ", " + $("#ddl_state").val();
	var dir = new GDirections(map, panel);

	GEvent.addListener(dir, "load", function() {
		$("#directions_error").fadeOut("fast");
		$("#directions_form").fadeOut("fast", function() {
			$("#panel").fadeIn("fast");
			$("#panel").attr("style", "");
		});
	});
	GEvent.addListener(dir, "error", function() {
		$("#directions_error").fadeIn("fast");
	});

	if (curLoc == "b") {
		dir.load(loc + " to " + latBoston + ", " + lonBoston);
	} else if (curLoc == "n") {
		dir.load(loc + " to " + latNJ + ", " + lonNJ);
	}
}
function areasOfInterestValidate(sender, args) {
	var gotOne = false;
	if ($(".ddlInterest input").length) {
		$(".ddlInterest input").each(function() {
			if ($(this).is(":checked")) {
				gotOne = true;
			}
		});
	}
	args.IsValid = gotOne;
	return gotOne;
}
