var rmenu = 0;
var toggle = true;

window.addEvent('domready', function() {
	setMorphs();
});




function setMorphs() 
{
	var morph = new Fx.Morph('menu_right');
	var morph2 = new Fx.Morph('main');
	var morph3 = new Fx.Morph('menu1');
	var morph4 = new Fx.Morph('menu2');
	
	var morphing = false;

	$('morphEffect').addEvent('click', function(e) {
		e.stop();
		
		if (!morphing) {
		
			if (toggle) {
			
				// copy over menu
				document.getElementById('menu2_div').innerHTML = document.getElementById('productmenu').innerHTML;
			
			
				morph.start({
					right: '-200px'
				}).chain(function(){ 
					document.getElementById('productmenu').innerHTML = '';
					document.getElementById('menu_right').style.display = 'none';
					setSubMenu2();
					document.getElementById('morphEffect').innerHTML = '<img src="/public/images/menu.png" width="9" height="9" align="absmiddle"> &nbsp;Dual Menu View';
					createCookie('view','single',1000);
				});
				
				menu_right_width	= document.getElementById('menu_right').offsetWidth;
				main_width			= document.getElementById('main').offsetWidth;
				
				main_full_width = menu_right_width + main_width;
				
				morph2.start({
					width: main_full_width + 'px'
				});	
				
				
				morph3.start({
					marginLeft: '-200px'
				});	
				morph4.start({
					left: '0px'
				});	
				
				toggle = false;
				
			} else {

				// copy over menu
				document.getElementById('menu_right').style.display = 'block';
				document.getElementById('productmenu').innerHTML = document.getElementById('menu2_div').innerHTML;
				
			
				morph.start({
					right: '0px'
				});
				
				main_width			= document.getElementById('main').offsetWidth;
				
				main_full_width = main_width - 200;
				
				morph2.start({
					width: main_full_width + 'px'
				});	
				
				morph3.start({
					marginLeft: '0px'
				});	
				morph4.start({
					left: '200px'
				}).chain(function(){ 
					document.getElementById('menu2_div').innerHTML = '';
					document.getElementById('morphEffect').innerHTML = '<img src="/public/images/menu1.png" width="9" height="9" align="absmiddle"> &nbsp;Single Menu View';
					setSubMenu();
					createCookie('view','dual',1000);
				});
					
				toggle = true;
			}
			morphing = true;
		
		}
		
	});
		
	// set flag at end of animation
	morph.addEvent('complete', function() {
		morphing = false;
	});


	//create our Accordion instance
	var accordion = new Accordion($('menu'), 'a.atStart', 'ul.atStart', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#fff');
			toggler.setStyle('background-image', 'url(/public/images/arrow_down.png)');
			element.setStyle('display','block');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#b6d1e3');
			toggler.setStyle('background-image', 'url(/public/images/arrow_right.png)');
		}
	});	
	
	
	
	
	
	
}

function changeSections(inForm)
{
	product2 = inForm.sections.value;
	if (product2) {
		
		if (product2 == 'agweather') { 
			window.open('http://agweather.mesonet.org')
		} else if (product2 == 'sip') {
			window.open('http://sip.mesonet.org')
		} else if (product2 == 'okfirst') {
			window.open('http://okfirst.mesonet.org')
		} else if (product2 == 'okfire') {
			window.open('http://okfire.mesonet.org')		
		} else {
			url = '/public/menus/' + product2 + '_menu.php';
			div = 'menu2_div';
			getHTML(url,div);
		}
	}
}


function getHTML(inURL, inDiv) 
{

	var req = new Request({url: inURL, 
		onSuccess: function(txt) {
			$(inDiv).set('html', txt);
			setSubMenu();
		},
		onFailure: function() {
			$(inDiv).set('text', 'The request failed.');
		}
	}).send();
}


function setSubMenu() 
{
	var accordion2 = new Accordion($('productmenu'), 'a.atStart2', 'ul.atStart2', {
		opacity: false,
		alwaysHide: true,
		display: 1,
		onActive: function(toggler, element){
			toggler.setStyle('background-image', 'url(/public/images/minus.gif)');
			element.setStyle('display','block');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('background-image', 'url(/public/images/plus.gif)');
		}
	});	
	
	if (document.singlesite) {
		changeyear(document.singlesite);
	}	
}


function setSubMenu2() 
{
	var accordion3 = new Accordion($('menu2_div'), 'a.atStart2', 'ul.atStart2', {
		opacity: false,
		alwaysHide: true,
		display: 1,
		onActive: function(toggler, element){
			toggler.setStyle('background-image', 'url(/public/images/minus.gif)');
			element.setStyle('display','block');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('background-image', 'url(/public/images/plus.gif)');
		}
	});	
	
	if (document.singlesite) {
		changeyear(document.singlesite);
	}	
}



function setPage() 
{
	
	// setPage calculates absolute values for page element widths and heights
	// different calculations are needed for different elements bases on which page is active

	var window_height		= 0;
	var window_width		= 0;
	var menu_left_height	= 0;
	var menu_right_height	= 0;
	var main_height		= 0;
	var main_width		= 0;	
	var menu_right_xpos	= 0;
	
	// get the current width and height of the browser window
	
	if ( navigator.appName.indexOf("Microsoft") != -1 ) {
		window_height = document.documentElement.clientHeight;
		window_width  = document.documentElement.clientWidth;
	} else {
		window_height = window.innerHeight;
		window_width  = window.innerWidth;
	}

	//get height and width of resized area
	header_height	   	= document.getElementById('header').offsetHeight;
	footer_height      	= document.getElementById('footer').offsetHeight;
	menu_left_width		= document.getElementById('menu_left').offsetWidth;
	
	
	menu_right_display	= document.getElementById('menu_right').style.display;
	
		menu_right_width	= (menu_right_display == 'none') ? 0: document.getElementById('menu_right').offsetWidth;
		menu_right_xpos		= (menu_right_display == 'none') ? '0px': document.getElementById('menu_right').style.right;
		menu_right_xpos 	= menu_right_xpos.replace('px','');
			
	//calculations (set height and width of resized area)
	menu_left_height	= window_height - header_height - footer_height;
	menu_right_height	= window_height - header_height - footer_height;
	main_height			= window_height - header_height - footer_height;
	main_width			= window_width - menu_left_width - menu_right_width - menu_right_xpos - 1;
	
	
	
	// custom stuff
	menu2_select_height = document.getElementById('select_div').offsetHeight + 10;  // 10 is the padding for the select_div
	
	
	
	// set new height and width	
	document.getElementById('menu_left').style.height		= menu_left_height + 'px';
	
	document.getElementById('menu1_content').style.height	= menu_left_height + 'px';
	document.getElementById('menu2_div').style.height		= (menu_left_height - menu2_select_height) + 4 + 'px';  // +4 for padding
	
	document.getElementById('menu_right').style.height		= menu_right_height + 'px';
	document.getElementById('main').style.height			= main_height + 'px';
	document.getElementById('main').style.width				= main_width + 'px';
	
	// for the my products iframe
	//if (document.getElementById('customMaps')) { document.getElementById('customMaps').style.height = menu_items_height + 'px'; }
	
}

function moveMenu()
{
	

}



function changeyear(inForm) 
{
	onedate = new Date();
	xyear = onedate.getYear();
	if (xyear >= "100" && xyear < "2000") {xyear = xyear + 1900}
	xmonth = onedate.getMonth();
	xday = onedate.getDate();
	xmonth2 = xmonth - 1;
	if (xmonth2 < 0) {xmonth2 = xmonth2 + 12}
	if (xday < 6) {
		xmonth2 = xmonth2 - 1;
	}

	// Set Array for months
	var themonth = new Array(11);
	themonth[0] = "January";
	themonth[1] = "February";
	themonth[2] = "March";
	themonth[3] = "April";
	themonth[4] = "May";
	themonth[5] = "June";
	themonth[6] = "July";
	themonth[7] = "August";
	themonth[8] = "September";
	themonth[9] = "October";
	themonth[10] = "November";
	themonth[11] = "December";

	theyear = inForm.year.value;
	
	if (theyear < xyear) {
		monthlength = 11;
	} else {
		monthlength = xmonth;
	}
	inForm.month.length = monthlength;
	ii = 0;
	for (i=monthlength; i > -1; i--) {
		inForm.month.options[ii] = new Option(themonth[i],(i+1));
		ii++;
	}
	inForm.month.selectedIndex = 0;
}



function goMCD(inForm) 
{
	stid = inForm.stid.value;
	year = inForm.year.value;
	month = inForm.month.value;
	format = inForm.format.value;	
	// call cgi
	//top.product.location.href='/cgi-bin/public/mcdshow.cgi?stid=' + stid + '&year=' + year + '&month=' + month + '&FORMAT=' + format
	top.product.location.href='/common/db/library/functions/mcd/mcd.php?stid=' + stid + '&year=' + year + '&month=' + month + '&format=' + format;;
}




function goOWRB(inForm) {
	winname = new Date();
	yy = winname.getTime();
	x = inForm.stid.value;
	prod = inForm.product.value;
	
	u = inForm.timelen.value;
	year = inForm.year.value;
	month = inForm.month.value;
	day = inForm.day.value;
	type = inForm.type.value;
	
	
	if (type == 'Graph') {
		if (prod == 'GH20' || prod == 'GH20-TH20') { prod = prod  + '-RAIN'; }
		xx = '/common/wxcode/graphs.php?stid=' + x + '&hours=' + u + '&year=' + year + '&month=' + month + '&day=' + day + '&product=' + prod;
	}
	if (type == 'mts') {
		xx = '/public/owrb_text.php?type=mts&stid=' + x + '&timelen=' + u + '&year=' + year + '&month=' + month + '&day=' + day + '&product=' + prod;
	}
	if (type == 'csv') {
		xx = '/public/owrb_text.php?type=csv&stid=' + x + '&timelen=' + u + '&year=' + year + '&month=' + month + '&day=' + day + '&product=' + prod;
	}
	top.product.location.href = xx;
}



function ud(mon, day, oyear) {
  var i = mon.selectedIndex;

  if(mon.options[i].value == "02") {
    day.options[30] = null;
    day.options[29] = null;

    var j = oyear.selectedIndex;
    var year = eval(oyear.options[j].value);
    if ( ((year%400)==0) || (((year%100)!=0) && ((year%4)==0)) ) {
      if (day.options[28] == null) {
        day.options[28] = new Option("29");
        day.options[28].value = "29";
      }
    } else {
      day.options[28] = null;
    }

  }

  if(mon.options[i].value == "01" ||
     mon.options[i].value == "03" ||
     mon.options[i].value == "05" ||
     mon.options[i].value == "07" ||
     mon.options[i].value == "08" ||
     mon.options[i].value == "10" ||
     mon.options[i].value == "12")
  {
    if (day.options[28] == null) {
      day.options[28] = new Option("29");
      day.options[28].value = "29";
    }
    if (day.options[29] == null) {
      day.options[29] = new Option("30");
      day.options[29].value = "30";
    }
    if (day.options[30] == null) {
      day.options[30] = new Option("31");
      day.options[30].value = "31";
    }
  }

  if(mon.options[i].value == "04" ||
     mon.options[i].value == "06" ||
     mon.options[i].value == "09" ||
     mon.options[i].value == "11")
  {
    if (day.options[28] == null) {
      day.options[28] = new Option("29");
      day.options[28].value = "29";
    }
    if (day.options[29] == null) {
      day.options[29] = new Option("30");
      day.options[29].value = "30";
    }
    day.options[30] = null;
  }

  if (day.selectedIndex == -1)
    day.selectedIndex = 0;
}


function go(y) {
	x = document.datamenu.stid.options[document.datamenu.stid.selectedIndex].value;
	xx = x + '.met';
	xx = '/common/wxcode/image.php?image=/data/public/mesonet/meteograms/' + xx + '.gif';
	if (x != '') {top.product.location.href = xx}
}




function go2(y) {
	x = document.datamenu2.stid.options[document.datamenu2.stid.selectedIndex].value;
	xx = 'stid=' + x + '&hours=24' + '&product=standard';
	xx = '/common/wxcode/graphs.php?' + xx;
	if (x != '') {top.product.location.href = xx}
}

function getRadar(x) 
{
	radar = document.nids.radar.value;
	if (x > 1) {
		top.product.location.href='/common/wxcode/radar.maps.php?radar=' + radar + '&product=BREF1&numframes=6';
	} else {
		top.product.location.href='/common/wxcode/radar.maps.php?radar=' + radar + '&product=BREF1';
	}
}

function getRadar2()
{
	readRadar = readCookie('radar');
	radar = (readRadar) ? readRadar : 'KTLX';
	getHTML('/public/menus/radar_menu.php','productmenu');
	product.location.href='/common/wxcode/radar.maps.php?radar=' + radar + '&product=BREF1'
}


function setRadar(x) 
{
	if (x == 'KTLX') { x = 0}
	if (x == 'KINX') { x = 1}
	if (x == 'KVNX') { x = 2}
	if (x == 'KFDR') { x = 3}
	if (x == 'KFWS') { x = 4}
	if (x == 'KAMA') { x = 5}
	if (x == 'KLBB') { x = 6}
	if (x == 'KDYX') { x = 7}
	if (x == 'KICT') { x = 8}
	if (x == 'KDDC') { x = 9}
	if (x == 'KSRX') { x = 10}
	if (x == 'KLZK') { x = 11}
	if (x == 'KSHV') { x = 12}
	if (x == 'KSGF') { x = 13}
	if (x == 'KPUX') { x = 14}
	document.nids.radar.selectedIndex = x;
	getRadar(1);
}

function setMenu(x)
{
	document.section_form.sections.selectedIndex = x;
}


function saveRadar()
{
	radar = document.nids.radar.value;
	createCookie('radar',radar,1000);
	alert(radar + ' is now your default radar.');
}

function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}


function newWindow() 
{
	x = top.product.location.href;
	
	// check for external pages
	var u = "" + x;
	y = "";
	for (i=0; i < u.length; i++) {
		if (u.charAt(i) == '?') {
			while ( i < u.length) {
			i++;
			y = y + u.charAt(i);
			}
		}
	}
	q = y.length;
	z = y.charAt(q-5) + y.charAt(q-4) + y.charAt(q-3) + y.charAt(q-2) + y.charAt(q-1);
	if (z == '.html') {	
	// open product window	
		MM_openBrWindow(y,'','menubar=yes,scrollbars=yes,resizable=yes');
	} else {
	// open external product window
		MM_openBrWindow(x,'','menubar=yes,scrollbars=yes,resizable=yes');
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function resizeFix()
{
	setPage();
}


function setSingleMenu()
{
	document.getElementById('menu2_div').innerHTML = document.getElementById('productmenu').innerHTML;
	menu_right_width	= document.getElementById('menu_right').offsetWidth;
	main_width			= document.getElementById('main').offsetWidth;
	document.getElementById('productmenu').innerHTML = '';
	document.getElementById('menu_right').style.display = 'none';
				
	main_full_width = menu_right_width + main_width;
	document.getElementById('main').style.width = main_full_width + 'px';
	toggle = false;

}

function init()
{
	view = readCookie('view');
	if (view == 'single')
	{
		setSingleMenu();
	}
	setPage();	
}





window.onload = init;
window.onresize = resizeFix;