var lastX = -1;
var lastY = -1;
var down = false;
var curX = -10;
var curY = -10;
var curImg = 0;
var overlaySizeTimer;
var lastScrollTop = -1;

var uptimer;
var homeImgTimer;
$(document).ready(function() {
	//////////////////////////////////////////////////////////////////////////
	//// gallery stuff
	//setupGallery();
	try {
		setupBlogContent();
		// add click to sample data button
	} catch (ex) { }
	try {
		$("#right_button").click(function() {
			return GB_showCenter('', 'http://dgl.us.neolane.net/res/dgl/survey/SVY453.jsp?deliveryId=7728335', 620, 380);
		});
		$("#request_data_link").click(function() {
			return GB_showCenter('', 'http://dgl.us.neolane.net/res/dgl/survey/SVY453.jsp?deliveryId=7728335', 620, 380);
		});
		// add click to contact us button
	} catch (ex) { }
	try {
		$("#contact_us_link").click(function() {
			return GB_showCenter('', 'http://dgl.us.neolane.net/res/dgl/survey/SVY454.jsp?deliveryId=7728335', 530, 380);
		});
	} catch (ex) { }
	try {
		$("#media_sample_link").click(function() {
			return GB_showCenter('', 'http://dgl.us.neolane.net/res/dgl/survey/SVY452.jsp?deliveryId=7728335', 530, 380);
		});
		$("#sample_sign_up").click(function() {
			return GB_showCenter('', 'http://dgl.us.neolane.net/res/dgl/survey/SVY452.jsp?deliveryId=7728335', 530, 380);
		});
	} catch (ex) { }
	try {
		$("#usage_rules_link").click(function() {
			return GB_showCenter('', '/overlays/editorial-rules.html', 630, 480);
		});
	} catch (ex) { }
	try {
		$(".playVid").click(function() {
			return GB_showCenter('', '/swf/DigitalGlobeVideo.html', 335, 500);
		});
	} catch (ex) { }
	// print
	$("#print_link").click(function() {
		window.print();
		return false;
	});
	// email
	$("#email_link").click(function() {
		return GB_showCenter('', '/overlays/email.aspx?p=' + escape(document.title) + "&u=" + escape(document.URL), 310, 425);
	});

	// top nav stuff
	try {
		$(".navWithDrop").hover(function() {
			if (uptimer != null) {
				clearTimeout(uptimer);
			}
			$(this).find(".navItemLink").addClass("on");
			$(this).find(".navDropdown").stop(true, true);
			$(this).find(".navDropdown").slideDown("fast");
		}, function() {
			$(this).find(".navItemLink").removeClass("on");
			var sonID = $(this).find('.navItemLink').attr('id');
			uptimer = setTimeout("$('#" + sonID +
				"').parent().find('.navDropdown').stop(true, true); $('#" + sonID +
				"').parent().find('.navDropdown').slideUp('fast');", 200);
		});
	} catch (ex) { }

	//////////////////////////////////////////////////////////////////////////
	//// homepage image stuff
	if ($("#home_img").length) {
		$(".homeMainImg div.imgCircle").each(function() {
			$(this).click(function() {

				if (homeImgTimer != null) {
					clearInterval(homeImgTimer);
				}

				$(".curImgCircle").removeClass("curImgCircle");
				$(this).addClass("curImgCircle");
				var ind = $(this).parent().find(".imgCircle").index(this);

				$("#home_img").css("width", $("#home_img img").width() + "px");
				$("#home_img").css("height", $("#home_img img").height() + "px");

				$("#home_img img").css("display", "none");
				$("#home_img img").attr("src", $(".homeImg:eq(" + ind + ") img").attr("src"));
				$("#home_img img").fadeIn("slow", function() {
					$("#home_img").css("width", $("#home_img img").width() + "px");
					$("#home_img").css("height", $("#home_img img").height() + "px");
				});
				$("#home_img_desc").html($(".homeImg:eq(" + ind + ") .desc").html());

				homeImgTimer = setInterval("homeNextImg();", 5500);
			});
		});

		homeImgTimer = setInterval("homeNextImg();", 5000);
	}
	//////////////////////////////////////////////////////////////////////////

	matchUpHeights();
	setInterval("matchUpHeights();", 500);
	lastScrollTop = $(window).scrollTop();

	if ($(".jScrollPane").length) {
		$(".jScrollPane").jScrollPane({ showArrows: true, scrollbarWidth: 12 });
	}

});

function homeNextImg() {
	var ind = $(".homeMainImg div.imgCircle").index($(".homeMainImg div.curImgCircle"));
	ind++;
	//	alert(ind + " >= " + $(".homeMainImg div.imgCircle").length + " -1");
	if (ind >= $(".homeMainImg div.imgCircle").length) { ind = 0; }
	$(".homeMainImg div.imgCircle:eq(" + ind + ")").click();
}

function matchUpHeights() {
	//////////////////////////////////////////////////////////////////////////
	//// equal up the main_content and right_bar heights
	// NOTE: assumes right top and bottom are 10px and main content top
	//       and bottom are 4px (each)

	//alert("main: " + $("#main_content div.contentMiddle").height() + ", right: " + $("#right_bar div.contentMiddle").height());
	var leftHeight;
	if ($("#home_left").length) {
		leftHeight = $("#home_left").height();
	} else {
		leftHeight = $("#main_content").height()
	}

	if (leftHeight > $("#right_bar").height()) {
		if ($("#home_left").length) {
			$("#right_bar div.contentMiddle").css("height", (leftHeight - 53) + "px");
		} else {
			$("#right_bar div.contentMiddle").css("height", ($("#main_content div.contentMiddle").height() - 12) + "px");
		}
	} else {
		$("#main_content div.contentMiddle").css("height", ($("#right_bar div.contentMiddle").height() + 12) + "px");
	}

	// get the left nav height to match up
	$("#left_nav").css("height", $("#main_content .contentMiddle").height() + "px");
	//////////////////////////////////////////////////////////////////////////
}

function setupBlogContent() {
	if ($("#blog_tabs").length) {
		$(".blogTab:eq(" + ($("#blog_tabs .blogTab").length - 1) + ") a").addClass("on");
		$(".blogBody:eq(" + ($("#blog_tabs .blogTab").length - 1) + ")").show();

		// setup click of tabs
		$(".blogTab a").each(function() {
			$(this).click(function() {
				$(".blogBody").hide();
				$(".blogBody:eq(" + $(this).parent().parent().children().index($(this).parent()) + ")").show();
				$(".blogTab a").removeClass("on");
				$(this).addClass("on");
				matchUpHeights();
				return false;
			});
		});
	}
}

var curZoom = 2;
var curMaxW = 2400;
var curMaxH = 1080;
var curMinW = 947;
var curMinH = 426;
var curMidW = 1680;
var curMidH = 756;

function showGallery(gallery, start) {
	curImg = start;

	// fade in image
	// get the image container ready
	$("body").prepend("<div id=\"image_overlay\"></div>");
	$("#image_overlay").css("left", (($(window).width() - 949) / 2) + "px");
	curX = gallery[curImg].startX;
	curY = gallery[curImg].startY;
	$("#image_overlay").load("/overlays/imageOverlay.html", null, function() {
		// overlay is loaded, now get the image loaded and the container centered
		$("#image_overlay_content_image img").load(function() {
			$("#image_overlay").css("left", (($(window).width() - $("#image_overlay").width()) / 2) + "px");
			if (curZoom == 1) {
				$("#image_overlay_content_image img").css("width", (curMaxW * 0.7) + "px");
				$("#image_overlay_content_image img").css("height", (curMaxH * 0.7) + "px");
			}
			$("#image_overlay_content_image img").css("left", curX + "px");
			$("#image_overlay_content_image img").css("top", curY + "px");

			if ($("#image_overlay_content_image img").css("display") == "none") {
				$("#image_overlay_content_image img").fadeIn("fast");
			}
			$("#image_overlay_image_count").html((curImg + 1) + " of " + gallery.length);
		});

		$("#image_overlay_content_image img").attr("src", gallery[curImg].lowRes);
		$("#image_overlay_description").html(gallery[curImg].description);
		$("#image_overlay_satellite").html(gallery[curImg].satellite);
		$("#image_overlay_zoom_overview").addClass("zoomOverlayOverviewOn");

		// setup close button
		$("#image_overlay_close_btn").click(function() {
			$("#image_overlay").hide();
			$(".pageOverlay").fadeOut("fast", function() {
				$("#image_overlay").remove();
				$(".pageOverlay").remove();
			});
			if (overlaySizeTimer != null) {
				clearInterval(overlaySizeTimer);
			}
			lastScrollTop = -1;
		});

		////////////////////////////////////////////////
		// setup scrolling
		$("#image_overlay_content_image img").css("top", curX + "px");
		$("#image_overlay_content_image img").css("left", curY + "px");
		$("#image_overlay_content_image img").select(function() { return false; });
		$("#image_overlay_mask").mousedown(function() {
			down = true;
			return false;
		});
		$("#image_overlay_mask").mousemove(function(e) {
			if (lastX > -1 && lastY > -1 && down) {
				curX = curX - (lastX - e.pageX);
				curY = curY - (lastY - e.pageY);
				var curW = $("#image_overlay_content_image img").width();
				var curH = $("#image_overlay_content_image img").height();
				if ((curX * -1) + 947 > curW) { curX = ((curW * -1) + 947); }
				if ((curY * -1) + 426 > curH) { curY = ((curH * -1) + 426); }
				if (curX > 0) { curX = 0; }
				if (curY > 0) { curY = 0; }
				$("#image_overlay_content_image img").css("left", curX + "px");
				$("#image_overlay_content_image img").css("top", curY + "px");
			}
			lastX = e.pageX;
			lastY = e.pageY;
			return false;
		});
		$("#image_overlay_mask").mouseup(function() {
			down = false;
			return false;
		});
		$("#image_overlay_mask").mouseleave(function() {
			down = false;
			return false;
		});

		////////////////////////////////////////////////
		// setup zoom
		$("#image_overlay_zoom_overview").click(function() {
			curZoom = 2;
			doZoom(gallery[curImg].lowRes);
			$(this).addClass("zoomOverlayOverviewOn");
		});
		$("#image_overlay_zoom_mid").click(function() {
			curZoom = 1;
			doZoom(gallery[curImg].highRes);
			$(this).addClass("zoomOverlayMidOn");
		});
		$("#image_overlay_zoom_detail").click(function() {
			curZoom = 0;
			doZoom(gallery[curImg].highRes);
			$(this).addClass("zoomOverlayDetailOn");
		});

		////////////////////////////////////////////////
		// setup next/last image
		$("#image_overlay_next_img").click(function() {
			if (curImg + 1 < gallery.length) {
				curImg++;
			} else {
				curImg = 0;
			}
			curZoom = 2;
			loadCurImage(gallery);
			doZoom(gallery[curImg].lowRes);
			$("#image_overlay_zoom_overview").addClass("zoomOverlayOverviewOn");
			return false;
		});
		$("#image_overlay_last_img").click(function() {
			if (curImg > 0) {
				curImg--;
			} else {
				curImg = gallery.length - 1;
			}
			curZoom = 2;
			loadCurImage(gallery);
			doZoom(gallery[curImg].lowRes);
			$("#image_overlay_zoom_overview").addClass("zoomOverlayOverviewOn");
			return false;
		});
	});

	// fade out page
	$("body").prepend("<div class=\"pageOverlay\" style=\"display: none;\"></div>");
	lastScrollTop = -47;
	correctPageOverlay();

	setInterval("correctPageOverlay()", 100);

	$('.pageOverlay').fadeTo(1, 0.1, function() {
		$(".pageOverlay").show();
		$(".pageOverlay").fadeTo("normal", 0.7);
	});
}

function correctPageOverlay() {
	if (lastScrollTop != $(window).scrollTop()) {
		$('.pageOverlay').css('top', $(window).scrollTop() + 'px');
		$('#image_overlay').css('top', ($(window).scrollTop() + 50) + 'px');
		$('.pageOverlay').css('height', $(window).height() + 'px');
		$('.pageOverlay').css('width', $(window).width() + 'px');
		lastScrollTop = $(window).scrollTop(); 
	}
}
function loadCurImage(gallery) {
	$(".zoomOverlayMidOn").removeClass("zoomOverlayMidOn");
	$(".zoomOverlayDetailOn").removeClass("zoomOverlayDetailOn");
	$(".zoomOverlayOverviewOn").removeClass("zoomOverlayOverviewOn");
	if (curZoom == 0) {
		$("#image_overlay_zoom_detail").addClass("zoomOverlayDetailOn");
	} else if (curZoom == 1) {
		$("#image_overlay_zoom_mid").addClass("zoomOverlayMidOn");
	} else {
		$("#image_overlay_zoom_overview").addClass("zoomOverlayOverviewOn");
	}

	$("#image_overlay_content_image img").hide();
	$("#image_overlay_content_image img").css("width", "");
	$("#image_overlay_content_image img").css("height", "");
	$("#image_overlay_content_image img").attr("src", gallery[curImg].lowRes);
	$("#image_overlay_description").html(gallery[curImg].description);
	$("#image_overlay_satellite").html(gallery[curImg].satellite);

	// preload large image
	if (document.images) {
		var pic = new Image();
		pic.src = gallery[curImg].highRes;
	}
}
function showComments(id, link) {
	if ($("#" + id).css("display") == "block") {
		$("#" + id).slideUp("fast");
		$("#" + link.id).html("View " + $("#" + link.id).html().substring(5));
	} else {
		$("#" + id).slideDown("fast");
		$("#" + link.id).html("Hide " + $("#" + link.id).html().substring(5));
	}
	return false;
}
function doZoom(newImg) {
	$(".zoomOverlayMidOn").removeClass("zoomOverlayMidOn");
	$(".zoomOverlayDetailOn").removeClass("zoomOverlayDetailOn");
	$(".zoomOverlayOverviewOn").removeClass("zoomOverlayOverviewOn");

	var centerXP = ((curX * -1) + (948 / 2)) / $("#image_overlay_content_image img").width();
	var centerYP = ((curY * -1) + (426 / 2)) / $("#image_overlay_content_image img").height();
	var w = curMaxW;
	var h = curMaxH;
	if (curZoom == 1) {
		w = curMidW;
		h = curMidH;
		curX = (w * centerXP * -1) + (948 / 2);
		curY = (h * centerYP * -1) + (426 / 2);
	} else if (curZoom == 2) {
		w = curMinW;
		h = curMinH;
		curX = 0;
		curY = 0;
	} else {
		curX = (w * centerXP * -1) + (948 / 2);
		curY = (h * centerYP * -1) + (426 / 2);
	}

	if ($("#image_overlay_content_image img").attr("src") != newImg) {
		$("#image_overlay_content_image img").animate({ width: (w + "px"), height: (h + "px"), left: curX + "px", top: curY + "px" }, 250, function() {
			$("#image_overlay_content_image img").attr("src", newImg);
		});
	} else {
		$("#image_overlay_content_image img").animate({ width: (w + "px"), height: (h + "px"), left: curX + "px", top: curY + "px" }, 250);
	}
}

//////////////////////////////////////////////////////////////////////////
//   NEW IMAGE VIEWER POPUP (ALEO)
/////////////////////////////////////////////////////////////////////////
var curMaxW = 2400;
var curMaxH = 1080;
var curMinW = 960;
var curMinH = 432;
var homeImgRestart = false;
var  galleryPath = "/imageviewer/";

function showGallery2(gallery, start) {
	if (homeImgTimer != null) {
			clearInterval(homeImgTimer); //pause homepage slideshow
			homeImgRestart = true;  // set flag to restart it later
	} 
	fadeOutPage() ;
	
	// get the image container ready  // AJAX load:
		$("body").prepend("<div id=\"image_overlay\"></div>");
		$("#image_overlay").css("left", (($(window).width() - 960) / 2) + "px"); //center
		//var dddd = new Date();
		//var somestring = "nocache" + dddd.getMilliseconds();
		//galleryPath = "/imageviewer/" + gallery + "/" + gallery + (++start) + "/imageOverlay.html";
		galleryPath = "/imageviewer/home/home1/imageOverlay.html";
		
		$("#image_overlay").load(galleryPath, null , function() {
			// setup close button
			$("#image_overlay_close_btn").click(function() {
				closePopup();
			});
		}); // end load()

}; // end showGallery2()

function correctPageOverlay() {
	if (lastScrollTop != $(window).scrollTop()) {
		$('.pageOverlay').css('top', $(window).scrollTop() + 'px');
		$('#image_overlay').css('top', ($(window).scrollTop() + 50) + 'px');
		$('.pageOverlay').css('height', $(window).height() + 'px');
		$('.pageOverlay').css('width', $(window).width() + 'px');
		lastScrollTop = $(window).scrollTop(); 
	}
}

function closePopup() {
			$("#image_overlay").hide();
			$(".pageOverlay").fadeOut("fast", function() {
				$("#image_overlay").remove();
				$(".pageOverlay").remove();
			});
			if (overlaySizeTimer != null) {
				clearInterval(overlaySizeTimer);
			}
			if (homeImgRestart) {
				homeImgTimer = setInterval("homeNextImg();", 5500);
			}
			lastScrollTop = -1;
}

function fadeOutPage() {
	// fade out page
	$("body").prepend("<div class=\"pageOverlay\" style=\"display: none;\"></div>");
	lastScrollTop = -47;
	
	correctPageOverlay();
	setInterval("correctPageOverlay()", 100);

	$('.pageOverlay').fadeTo(1, 0.1, function() {
		$(".pageOverlay").show();
		$(".pageOverlay").fadeTo("normal", 0.7);
	});
}
