/*
 * $Id: popup.js,v 1.2 2008/05/30 18:43:28 mripplin Exp $
 *
 * +------+   Copyright 2008, ICx Transportation Group
 * | ICx  |                   All Rights Reserved
 * +------+----------------------------------------------------------
 * This file is property of ICx Transportation Group.  Any unauthorized use or
 * duplication of this file, or removal of the header, constitutes
 * theft of intellectual property.
 *
 * This file contains functions to open the interactive map window, and
 * to open links from within popup windows.  It also contains utility
 * functions used in various places on the traffic site.
 *
 * TODO: Move non-popup-related functions to other files.
 */

/**
 * Open the interactive map.
 */
function openMap() {
	return openMapWindow("");
}

/**
 * Open the map with the driving times tool active.
 */
function openDrivingTimes() {
	return openMapWindow("drivingtimes=1");
}

/**
 * Open the map in a new browser window.
 * @param querystring	String to be appended to the query portion of the map URL.
 */
 
function openMapWindow(querystring) {
	var params = "left=0,top=0,screenX=0,screenY=0,toolbars=0,resizable=1,scrollbars=1";
	if (screen.availWidth >= 800) {
		params += ",width=790,height=550";
	}
	var mapwin = window.open("traffic_map.asp?" + querystring, "mapwin", params);
	mapwin.focus();
	return false;
}

/**
 * Find all links with target="_blank" or target="parent" and make them open in
 * the parent window instead of a new window.  Links with target="_blank" should
 * also close the current window.
 */
function makeParentLinks() {
	for (var i=0; i < document.links.length; i++) {
		link = document.links[i];
		if (link.target == "_blank")
			link.onclick = parentLink;
		else if (link.target == "parent")
			link.onclick = parentLink2;
	}
}

/**
 * If possible, open this link in the window that opened this frameset.
 * CLOSE this window afterwards.
 */
function parentLink() {
	if (parent.opener && !parent.opener.closed) {
		parent.opener.location = this.href;
		parent.opener.focus();
		parent.close();
		return false;
	}
	else return true;
}

/**
 * If possible, open this link in the window that opened this frameset.
 * DON'T close this window afterwards.
 */
function parentLink2() {
	if (parent.opener && !parent.opener.closed) {
		parent.opener.location = this.href;
		parent.opener.focus();
		return false;
	}
	else return true;
}

/**
 * Return integer "num" as a 2-digit string (for dates and times).
 */
function pad(num) {
	return (num < 10) ? '0' + num : num;
}

/**
 * Return date "d" formatted as "M/D/YY h:mm am/pm"
 * @param d	A javascript "Date" object.
 */
function timestring(d) {
	var h = d.getHours();
	var hour = h % 12;
	if (hour == 0) hour = 12;
	var year = d.getYear()
	if (year < 1900)
		year = 1900 + year;

	var theString = d.getMonth()+1 + '/' + d.getDate() +	'/' + pad(year%100);

	theString += ' ' + hour + ':' + pad(d.getMinutes());
	theString += (h < 12) ? '&nbsp;a.m.' : '&nbsp;p.m.';

	return theString.replace(" ", "&nbsp;");
}
