
// levels are for ssws station popup
var gmap = null;
var level_graphics = [
	'grey_box.png', 'green_box.png', 'yellow_box.png', 'orange_box.png', 'red_box.png', 'purple_box.png'];
var level_name = ['Blowout', 'Normal', 'Near Flood', 'Minor Flood', 'Moderate Flood', 'Major Flood'];
var level_colors = ['#999999', '#00FF5F', '#FFFF00', '#FF9933', '#FF0044', '#CC33FF'];
var banner_thread = null;
var show_cms_leftCtl = null;
var cms_logoCtl= null;
var banner_first_time = 1;
var maintitleCtl = null;
var bannerCtl = null;
var clientCtl = null;
var client_current_pos = 0;
var total_banner_move = 0;
var is_banner_shown = true;
var is_mouse_over_client = 0;
var browser = 'IE';
var does_banner_hide = true;

function $(element) {
	return document.getElementById(element);
}

function set_banner_hide_switch (does_hide) {
	does_banner_hide = does_hide;
}
function set_mouse_over_client(is_over) {
	is_mouse_over_client = is_over ? 1 : 0;
}
	
function set_banner_client_control(client) {
	clientCtl = client;
}

function kill_banner_thread() {
	if (banner_thread) {
		clearInterval(banner_thread);
	}
	banner_thread = null;
}
function clear_banner_first_time() {
	banner_first_time = 0;
}

function hide_banner(interval, immediate) {
	if (!does_banner_hide) {return;}
	if (! is_banner_shown) { return;}
	kill_banner_thread();
	banner_thread = setInterval('hide_banner_process()', interval);
	hide_all_popups();
	
}
function hide_banner_process() {
	if (!does_banner_hide) {return;}
	if (client_current_pos <=maintitleCtl.offsetHeight + 5) {
		is_banner_shown = false;
		cms_logoCtl.style.visibility = 'hidden';
		kill_banner_thread();
		var w_h = get_width_and_height();
		if (w_h[0] > 1150) {
			show_cms_leftCtl.style.visibility = 'visible';
			cms_logoCtl.style.visibility = 'hidden';
		}
		else {
			show_cms_leftCtl.style.visibility = 'visible';
			cms_logoCtl.style.visibility = 'hidden';
		}
		return;
	}
	client_current_pos -= 3;
	clientCtl.style.top = client_current_pos + 'px';
}

function show_banner_process() {
	if (!does_banner_hide) {return;}
	if (client_current_pos >= maintitleCtl.offsetHeight + bannerCtl.offsetHeight - 5) {
		is_banner_shown = true;
		cms_logoCtl.style.visibility = 'visible';
		kill_banner_thread();
		return;
	}
	client_current_pos += 3;
	clientCtl.style.top = client_current_pos + 'px';
}


function show_banner(interval) {
	if (!does_banner_hide) {return;}
	kill_banner_thread();
	banner_thread = setInterval('show_banner_process()',interval);
	cms_logoCtl.style.visibility = 'visible';
	show_cms_leftCtl.style.visibility = 'hidden';
}

function load_ssws_list() {
	var s;
	var as_of_date = '';
	if (typeof(presentSources) != 'undefined') {
		s = presentSources;
		as_of_date = '<BR>as of: <u>' + print_start_date + '</u>';
	}
	var stations = $("ssws_popup_insert");
	var str =  
		'<table width="420px" class="front_page_ssws" ' +
//		' style="line-height:1.5" cellspacing="1";' +
		' cellpadding="0" >  <tbody> <tr> ' +
		' <td colspan="2" class="bannerbuttontext_over" height="80px" ' +
		' style="padding:4px;background-color:#000065" ' + 
		//' style="padding:4px;letter-spacing:2px;background-color:#000065" ' + 
		'	onclick="window.location.href=\'http://hudson.dl.stevens-tech.edu/SSWS/\'" >' +
		'Click for Storm Surge Warning System (SSWS)' + as_of_date + 
		'<BR><br>Stations below have current or forecast flood levels.' +
		'<br>Box color indicates current with forecast in parens</td></tr> '  ; 
		//'<br>Box color shows current level.Forecast level shown in parens</td></tr> '  ; 
		//'<BR><br><br>Stations below have<br>current or forecast flood levels</td></tr> '  ; 

	if (typeof(s) == 'undefined') {
		str += '<tr><td></td><td class="bannerbuttontext">Storm Surge Forecast Not Currently Available</td></tr>';
	}
	else {
		var alerted_stations = false;
	for (var i= 0; i < s.length; i++) {
		if (s[i].alert_level != 1 || s[i].model_alert){
			alerted_stations = true;
		str += '<tr> ' + 
//			' <td height="15" align="center" valign="center" width="30" style="cursor:pointer"  ' +
			' <td height="15" align="right"  style="cursor:pointer"  ' +
			' onclick="window.location.href=\'http://hudson.dl.stevens-tech.edu/SSWS/d/index.shtml?station='+s[i].did + '\'" >' +
			' <img src="/banner/images/' + level_graphics[s[i].alert_level] + '" height="15" ' +
			' width="15"> </td> <td height="15"  style="cursor:pointer" class="ssws_stations_text"' +
			' onmouseover="this.className = \'ssws_stations_text_over\'" ' +
			' onmouseout="this.className = \'ssws_stations_text\'" ' +
			' onclick="window.location.href=\'http://hudson.dl.stevens-tech.edu/SSWS/d/index.shtml?station='+s[i].did + '\'" >' +
			 s[i].title + ' (' + level_name[s[i].flag_model_alert] + ')' +  '</a>' + ' </td></tr>';
			 //s[i].title + '</a>' + ' </td></tr>';
		}
	}
	if (!alerted_stations) {
		str += '<tr> ' + 
			' <td height="15" align="center" valign="center" style="cursor:pointer">' +
			'No flooding alerts were found';
	}
	}
	str += '</table>';;

	stations.innerHTML = str;

}

var fade_thread = null;
var reload_thread = null;
function change_button_text(button_name, text) {
	top_buttons[button_name].self.innerHTML = text;
}

var top_buttons = {  // buttons on the top banner
present: { self:null,   popup_ref: null, is_popup_open: 0, 
				is_button_over: 0 , thr: null, text: 'Present<BR>Conditions', over_text: 'Present<BR>Conditions'},
forecast: { self:null,   popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'NYHOPS<BR>Forecast', over_text: 'NYHOPS<BR>Forecast'},
cmn: {self:null,    popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'NJ Coast<BR>(CMN)', over_text: 'NJ Coast<BR>(CMN)'},
ssws: {self:null,    popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'Storm<BR>Surge', over_text: 'Storm<BR>Surge'},
mobile: {self:null,    popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'Mobile<BR>Stations', over_text: 'Mobile<BR>Stations'},
hudson: {self:null,    popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'CMS<BR>Partners', over_text: 'CMS<BR>Partners'},
data: {self:null,    popup_ref: null, is_popup_open: 0, 
	is_button_over: 0 , thr: null, text: 'Data &amp;<BR>Time Series', over_text: 'Plot &amp;<br>Download'}
};

function goto_present(region, param) {
	var reg = region == 'ny' ? 'nynjreg' : 'njreg';
	var cookie = 'rtregion=' + reg + '; path=/';
	document.cookie = cookie;
	var cookie = 'rtparam=' + param + '; path=/';
	document.cookie = cookie;
	window.location.href='http://hudson.dl.stevens-tech.edu/maritimeforecast/PRESENT/index.shtml';

}

function goto_forecast(region) {
	var cookie = 'reqregion' + '=' + region + '; path=/';
	document.cookie = cookie;
	//cookie = 'reqparam' + '=' + 'salttop' + '; path=/'; // ?? why bother
	document.cookie = cookie;
	window.location.href='http://hudson.dl.stevens-tech.edu/maritimeforecast/maincontrol.shtml';

}

 
function set_opacity(div, dim_or_full) {
	 var opacity = dim_or_full == 'dim' ? 70 : 100;
	div.style.opacity = opacity * .01;
	div.style.filter = 'alpha(opacity=' + opacity + ')';
}

function reset_banner_button(button_name) {
	top_buttons[button_name].thr = setTimeout('reset_button(\'' + button_name + '\')', 30);
}

function reset_button(button_name) {
	if ( !top_buttons[button_name].is_button_over &&
		! top_buttons[button_name].is_popup_open) {
		
	top_buttons[button_name].self.style.backgroundImage = 'url(/banner/images/bb_' + button_name + '.gif)';
	top_buttons[button_name].self.className = 'bb';
	top_buttons[button_name].self.innerHTML = top_buttons[button_name].text
	top_buttons[button_name].popup_ref.style.visibility = 'hidden';
	}
}

function hide_all_popups() {
	for (elem in top_buttons) {
		top_buttons[elem].popup_ref.style.visibility = 'hidden';
	}
}
		
function over_button(button_name) {
	top_buttons[button_name].self.style.backgroundImage = 'url(/banner/images/bannerc_' + button_name + '_over.jpg)';
	top_buttons[button_name].self.className = 'bb_over';
	top_buttons[button_name].self.innerHTML = top_buttons[button_name].over_text
	top_buttons[button_name].popup_ref.style.visibility = 'visible';
	top_buttons[button_name].is_button_over = 1;
}
function normal_button(button_name) {
	top_buttons[button_name].is_button_over = 0;
	reset_banner_button(button_name);
}
function popup_out(button_name) {
	top_buttons[button_name].is_popup_open = 0;
	reset_banner_button(button_name);
}
function popup_over(button_name) {
	top_buttons[button_name].is_popup_open = 1;
}

function banner_load() {

	browser = BrowserDetect.browser;
	show_cms_leftCtl = $('show_cms_left');
	cms_logoCtl = $('cms_logo');
	maintitleCtl = $('maintitle');
	bannerCtl = $('banner');

	var elem;
	for (elem in top_buttons) {
		top_buttons[elem].self = $(elem );
		top_buttons[elem].popup_ref = $(elem + '_popup');
		// preload mouse over graphics
		var img = new Image(10,10);
		img.src = 'http://hudson.dl.stevens-tech.edu/banner/images/bannerc_' + elem + '_over.jpg';
	}
	img.src = 'http://hudson.dl.stevens-tech.edu/banner/images/silverbannerbutton_over_200x40.jpg';
	img.src = 'http://hudson.dl.stevens-tech.edu/banner/images/silverbannerbutton_over_250x40.jpg';

	load_ssws_list();

	if (clientCtl) {
		client_current_pos = maintitleCtl.offsetHeight + bannerCtl.offsetHeight + 3;
	}
	total_banner_move = bannerCtl.offsetHeight - maintitleCtl.offsetHeight;
}	


function get_width_and_height() {
	if (browser == 'Netscape') {
		var winW = window.innerWidth;
		var winH = window.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientHeight) { //IE 6 strict mode
		var winW = document.documentElement.clientWidth;
		var winH = document.documentElement.clientHeight;
	}
	else if( document.body) { // other IE
		var winW = document.body.clientWidth;
		var winH = document.body.clientHeight;
	}
	return [winW, winH];
}

function show_backgrounds() {
	var elem;
	for (elem in top_buttons) {
	top_buttons[elem].self.style.backgroundImage = 'url(/banner/images/bannerc_' + elem + '_over.jpg)';
	}
}

function hide_backgrounds() {
	var elem;
	for (elem in top_buttons) {
	reset_button(elem);
	}
}
