 
//var base_image_dir="/grads_images";
var base_image_dir="http://map.myskeye.com/grads_images";
var modImages = new Array();
var requestNumber=0;
var day_count=7;
var playLastImage=1;

day_count=14;
var week_count=2;
var yyyymmdd="";
var region="";
var previous_region="";
var variable="";
var img_type="png";
var img_type="gif";
var overlay= new Object;
var seconds_per_day=86400;
var curDateObject=new Date();
var newDateObject=new Date();
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var months=["January","February","March","April","May","June","July","August","September","October","November","December"];
var initial_period="daily";
var period=initial_period;
var selected_day=1;
var selected_week=1;
var prev_selected_day=0;
var prev_selected_week=0;
var variable_list = new Array();
variable_list["avg_temp","max_temp","min_temp","ly_avg_temp_anomaly","ly_max_temp_anomaly","ly_min_temp_anomaly","norm_avg_temp_anomaly","norm_max_temp_anomaly","norm_min_temp_anomaly","prcp","uv_index","wspd","snow","max_rh","min_rh"];
var variable_count=variable_list.length;
var weekly_flag = new Object;
var previous_variable="";
for(i=0;i<variable_count;i++){
	cur_var=variable_list[i];
	weekly_flag[cur_var]=0;	
}
weekly_flag['snow']=1;
var previous_type="short_range";
var data_type="short_range";
var previous_data_type="short_range";
//var valid_yui_regexp=/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var valid_yui_regexp=/^(\d{2})\/(\d{2})\/(\d{4})$/;
var yyyymmdd_start="";
var yyyymmdd_end="";
var calendar="julian";
var units="f";
//var img_type="png";

var dynPeriod="daily";
var todayYmd="";
var todayYYYY="";
var todayMM="";
var todayDD="";
var todayMDY="";

var first_image = 1;
var last_image = 7;
var overlay_image_html="";
var overlay_selection_html="";
var debug_padding=0;
var s="&nbsp;";
var max_data_type_Ymd = new Object;
var min_data_type_Ymd = new Object;
var max_data_type_mdY = new Object;
var min_data_type_mdY = new Object;
var mapTitle="";
var requestMapCount=0;
var requestMapKey="";
var prevImageNum=0;
var prevImagePeriod="daily";
var imageCount = new Object;
var currentBaseImageID="";
var prevImageID="";
var imageLoadedCount=0;

var animPlaying=0;
var currentDailyImageNumber=0;
var currentWeeklyImageNumber=0;


$(document).ready(function(){
	//launch();
	todayYmd=$('#animation_date').val();
	todayYYYY=todayYmd.substring(0,4);
	todayMM=todayYmd.substring(4,6);
	todayDD=todayYmd.substring(6,8);
	todayMDY=todayMM+"/"+todayDD+"/"+todayYYYY;
	
	var todayObj=new Date();
	todayObj.setUTCFullYear(todayYYYY,todayMM-1,todayDD);
	
	var todaySeconds=todayObj.getTime();
	
	//Get historical date info
	
	var maxHistYMD=$('#animation_maxHistYMD').val();
	var minHistYMD=$('#animation_minHistYMD').val();
	
	maxHistYYYY=maxHistYMD.substring(0,4);
	maxHistMM=maxHistYMD.substring(4,6);
	maxHistDD=maxHistYMD.substring(6,8);
	maxHistmdY=maxHistMM+"/"+maxHistDD+"/"+maxHistYYYY;
	max_data_type_mdY['historical']=maxHistmdY;
	max_data_type_Ymd['historical']=maxHistYMD;
	
	minHistYYYY=minHistYMD.substring(0,4);
	minHistMM=minHistYMD.substring(4,6);
	minHistDD=minHistYMD.substring(6,8);
	minHistmdY=minHistMM+"/"+minHistDD+"/"+minHistYYYY;
	min_data_type_mdY['historical']=minHistmdY;
	min_data_type_Ymd['historical']=minHistYMD;
	
	//Hard code for now
	var yestObj=new Date(todaySeconds-(seconds_per_day*1000));
	//REMOVEyestObj.setUTCFullYear(todayYYYY,todayMM-1,todayDD);
	yestObj.setUTCFullYear(maxHistYYYY,maxHistMM-1,maxHistDD);
	yestDD=yestObj.getDate();
	if(yestDD < 10){
		yestDD="0"+yestDD;
	}
	
	yestMM=yestObj.getMonth()+1;
	yestMMM=yestObj.getMonth()+0;
	if(yestMM < 10){
		yestMM="0"+yestMM;
	}
	
	yestYYYY=yestObj.getFullYear();
	yestYYYYm1=yestYYYY;
	yestmdY=yestMM+"/"+yestDD+"/"+yestYYYY;

	//REMOVE$('#historical_start_date').val(yestMM+"/"+yestDD+"/"+yestYYYYm1);
	$('#historical_start_date').val(yestMM+"/"+yestDD+"/"+yestYYYY);
	
	var maxFcstYMD=$('#animation_maxFcstYMD').val();
	var minFcstYMD=$('#animation_minFcstYMD').val();
	
	maxFcstYYYY=maxFcstYMD.substring(0,4);
	maxFcstMM=maxFcstYMD.substring(4,6);
	maxFcstDD=maxFcstYMD.substring(6,8);
	maxFcstmdY=maxFcstMM+"/"+maxFcstDD+"/"+maxFcstYYYY;
	max_data_type_mdY['long_range']=maxFcstmdY;
	max_data_type_Ymd['long_range']=maxFcstYMD;
	
	minFcstYYYY=minFcstYMD.substring(0,4);
	minFcstMM=minFcstYMD.substring(4,6);
	minFcstDD=minFcstYMD.substring(6,8);
	minFcstmdY=minFcstMM+"/"+minFcstDD+"/"+minFcstYYYY;
	min_data_type_mdY['long_range']=minFcstmdY;
	min_data_type_Ymd['long_range']=minFcstYMD;
	
	
	hideDescription();
	loadImages();

	//REMOVEinitYCalendar("historical_start_date","Start Date",minHistmdY,maxHistmdY,yestMM+"/"+yestYYYYm1);
	//YUICALinitYCalendar("historical_start_date","Start Date",minHistmdY,maxHistmdY,yestMM+"/"+yestYYYYm1);
	//YUICALinitYCalendar("long_range_start_date","Start Date",minFcstMM+"/"+minFcstDD+"/"+minFcstYYYY,maxFcstMM+"/"+maxFcstDD+"/"+maxFcstYYYY);
	$.getScript("/javascript/jqModal.js",function (){
		$('#jqm_dialog').jqm();
	});	
	
	//loadImages();
	
});

//Pull the images once the page has completely loaded
$(document).ready(function(){
	//loadImages();
});

function getMapDate(){
	
	dateUpdate();
	return 1;
}

function dateUpdate(){
	var cur_yyyymmdd=$('#animation_date').val();
	var cur_yyyy=cur_yyyymmdd.substring(0,4);
	var cur_mm=cur_yyyymmdd.substring(4,6);
	var cur_dd=cur_yyyymmdd.substring(6,8);
	if(cur_mm.substring(0,1) == "0"){
		cur_m=cur_mm.substring(1,2);
	}
	else{
		cur_m=cur_mm;
	}
	
	if(cur_dd.substring(0,1) == "0"){
		cur_d=cur_dd.substring(1,2);
	}
	else{
		cur_d=cur_dd;
	}

	curDateObject.setUTCFullYear(cur_yyyy,cur_m-1,cur_d);
	curDateObject.setUTCHours(12);

	var cur_secs=curDateObject.getTime();
	
	var day_html="<tr id='day_row'><td>Date</td>";
	var week_html="\
	<tr id='week_row'><td>&nbsp;</td>\
	<td colspan=7 id='week_1' class='week_cell'>\
	  <a href='javascript:void(0)' onclick=\"changeImage(1,'weekly');\">Days 1-7</a>\
	</td><td colspan=7 id='week_2' class='week_cell'><a href='javascript:void(0)' onclick=\"changeImage(2,'weekly');\">Days 8-14</a></td>";
	
	//$('#date_controls').html();
	for(i=0;i<14;i++){
		var id_num=i+1;
		var newDateObject=new Date(cur_secs+(seconds_per_day*1000*i));
		var newDay=days[newDateObject.getDay()];
		var newDate=newDateObject.getDate();
		if(newDate < 10){
			newDate="0"+newDate;
		}
		var month=newDateObject.getMonth()+1;
		nice_month=months[month-1];
		nice_mon=nice_month.substring(0,3);
		if(month < 10){
			month="0"+month;
		}
		
		nice_yyyy=newDateObject.getFullYear();
		nice_date=nice_mon+" "+newDate+"<br>"+nice_yyyy;
		nice_date=newDate+" "+nice_mon+"<br>"+newDay.substring(0,3);

		//REMOVEday_html+="<td class='day_cell' id='day_"+id_num+"' onclick=\"changeImage("+id_num+",'daily');\"><a href='javascript:void(0)' onclick=\"changeImage("+id_num+",'daily');\">"+nice_date+"</a></td>";
		day_html+="<td class='day_cell' id='day_"+id_num+"' onclick=\"changeImage("+id_num+",'daily');\"><a href='javascript:void(0)'\">"+nice_date+"</a></td>";
	}

	day_html+="</tr>";
	week_html+="</tr>";
	$('#date_controls').html(day_html+week_html);
	
	//Find the date one week from now
	return;
	
}

function playImage(){
	playDiv='#'+data_type+'_play_control';
	if($(playDiv)){
		//It exists
		control_html="<span id='play_control'>\
                <a href='JavaScript: func()' onClick='stopImage();'>\
                  <img border=0 hspace=0 vspace=0 width=30 height=30 src='/images/stop_no_borders_transparent.gif' alt='Stop'>\
                </a>\
                </span>\
		";
		control_html="<a href='JavaScript: func()' onClick='stopImage();'><img border=0 hspace=0 vspace=0 width=30 height=30 src='/images/stop_no_borders_transparent.gif' alt='Stop'></a>";
		//If the animation is currently stopped we will play it and update the buttons
		if(animPlaying == 0){
			$(playDiv).html(control_html);
			playFwd();
		}	
		
		animPlaying=1;
	}
	else{
		//Doesn't exist
		alert("ID(play_control) does not exist, please debug!");
		return;
	}
	return 1;
}

function stopImage(){
	stopDiv='#'+data_type+'_play_control';
	//If it exists and the animation is currently playing we will stop it
	if($(stopDiv)){
		//It exists
		control_html="<span id='play_control'>\
                <a href='JavaScript: func()' onClick='playImage();'>\
                  <img border=0 hspace=0 vspace=0 width=30 height=30 src='/images/play_no_borders_transparent.gif' alt='Play'>\
                </a>\
                </span>\
		";
		control_html="\<a href='JavaScript: func()' onClick='playImage();'><img border=0 hspace=0 vspace=0 width=30 height=30 src='/images/play_no_borders_transparent.gif' alt='Play'></a>";
		//If the animation is currently playing we will stop it and update the buttons
		if(animPlaying == 1){
			$(stopDiv).html(control_html);
			stop();
		}		
		
		animPlaying=0;
	}
	else{
		//Doesn't exist
		alert("ID(play_control) does not exist, please debug!");
		return;
	}
	return 1;
}

function getRegion(){
	//REMOVEregion=$('#animation_region').val();
	region=$('#animation_region_'+data_type).val();
	return 1;
}

function getVariable(){
	//REMOVEvariable=$('#animation_variable').val();	
	variable=$('#animation_variable_'+data_type).val();
	
	if(variable && variable.length >= 1){
		return 1;
	}
	else{
		//alert("Please choose a valid variable");
	}
	
}





function hideDescription(id){
	
	if(id){
		//ID is set, make sure it exists
		if($("#description_"+id)){
			$("#description_"+id).hide();
		}
		else{
			alert("ID="+id+" doesn't exist");
			return;
		}
	}
	else{
		//No ID is set, close them all
		$("#page_descriptions .description_div").each(function(){
			//alert(this);
			$(this).hide();
		});

	}
	
}

function showDescription(id){
	
	if(id){
		//ID is set, make sure it exists
		if($("#description_"+id)){
			$("#description_"+id).show();
		}
		else{
			alert("ID="+id+" doesn't exist");
			return;
		}
	}
	
}

function changeOverlay(name){
	id='overlay_'+region+'_'+name;
	//

	if(overlay[id] && overlay[id] == 1){
		//That overlay already appears to be on so we should turn it off
		//
		
		//Make sure that it is not really checked
		if($('#check_'+region+'_'+name) && $('#check_'+region+'_'+name).is(':checked')){
			//It is checked, should not be here
			//
			
		}
		else{
			//Phew not checked, we can remove it
			$('#'+id).remove();
			//
			overlay[id]=0;
		}
	}
	else{
		//
		//$('#debug').append("Does not exist<br>");
		//Not set yet, we should make sure the value is actually checked
		if($('#check_'+region+'_'+name) && $('#check_'+region+'_'+name).is(':checked')){
			//It is checked so we should add the overlay
			//debugAppend("changeOverlay("+name+"): turning it on");
			var valid_format=0;
			ov_height=region_overlay_height[region];
			ov_width=region_overlay_width[region];
			ov_real_time_flag=0;
			if($('#real_time_flag_'+region+'_'+name) && $('#real_time_flag_'+region+'_'+name).attr('value') == 1){
				ov_real_time_flag=1;
			}
			else{
			}
			if($('#format_'+region+'_'+name)){
				//Format appears to be set
				if($('#format_'+region+'_'+name).attr('value') === "gif" || $('#format_'+region+'_'+name).attr('value') === "png"){
					//Looks valid
					ov_format=$('#format_'+region+'_'+name).attr('value');
					valid_format=1;
				}
			}
			else{
			}
			//Make sure we found a valid format
			if(valid_format == 1){
				//Yup
				
				//If we are dealing w/a PNG overlay and the user has MSIE6 we need to use the IE6 specific imagery
				var ie6Suffix="";
				if(ov_format === "png" && browserType === "MSIE6"){
					ie6Suffix="-ie6";
					ov_format="gif";
				}
				
				//Do we have a real_time variable?
				if(ov_real_time_flag == 1){
					imgPath="http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_overlay_"+ov_width+"x"+ov_height+"_transparent."+ov_format+"?ts="+real_time_ts;
					imgPath="http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_overlay_"+ov_width+"x"+ov_height+"_transparent"+ie6Suffix+"."+ov_format+"?ts="+real_time_ts;
				}
				else{
					imgPath="http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_overlay_"+ov_width+"x"+ov_height+"_transparent."+ov_format;
					imgPath="http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_overlay_"+ov_width+"x"+ov_height+"_transparent"+ie6Suffix+"."+ov_format;
				}
				
				//REMOVE$('#overlay').append("<div class='overlay' id="+id+"><img src='http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_overlay_"+ov_width+"x"+ov_height+"_transparent."+ov_format+"'></div>");
				$('#overlay').append("<div class='overlay' id="+id+"><img src='"+imgPath+"'></div>");
			}
			else{
				alert("Invalid format (#format_"+region+"_"+name+"="+$('#format_'+region+'_'+name).attr('value')+")");
			}
			overlay[id]=1;
		}
		else{
			//
			//$('#debug').append("not checked<br>");
			//Not checked, that is weird
		}

	}
	//
	//$('#debug').append("<br><br>");
}




function changeImage(number,per){
	
	//Make sure the loop is stoppped
	stopImage();
	
	//REMOVE$('div#debug').append('<br>changeImage('+number+','+per+'):<br>');
	
	//See if we are changing periods
	if(per == period){
		//Not changing periods
	}
	else{
		//Changing periods
		changePeriod(per);
	}
	
	if(data_type == "short_range"){
		currentBaseImageID=period+"_loop_image";
	}
	
	mattchangeImage(number);
	if(per == "daily"){
		currentDailyImageNumber=number;
		
	}
	else if(per == "weekly"){
		currentWeeklyImageNumber=number
	}
	else{
		alert("Invalid period ("+per+") in changeImage");
	}
	return;
	
}

function changePeriod(per){
	
	//Make sure the loop is stoppped
	stop();
	
	//$('div#debug').append('<br>changePeriod('+per+'):<br>');
	period=per;
	
	if(period == "daily"){
		//Load daily
		loadDailyImages();
		first_image=1;
		last_image=imageCount['daily'];
	}
	else if(period == "weekly"){
		loadWeeklyImages();
		first_image=1;
		last_image=imageCount['weekly'];
	}
	else{
		alert("Invalid period ("+period+")");
		return;
	}
	
	
	return;
	
	
}

function loadDailyImages(){
	var index_num=0;
	modImages = new Array();
	for(i=0;i<day_count;i++){
		day_number=i+1;
		modImages[i]=base_image_dir+"/"+yyyymmdd+"/"+region+"_"+day_number+"_"+variable+"_"+yyyymmdd+"."+img_type;

	}
	
	first_image = 1;
	last_image = i;
	theImages[0] = new Image();
  theImages[0].src = modImages[0];
  //$('div#debug').append('<br>loadDailyImages - first image='+modImages[0]+'<br>');
  imageNum[0] = true;
	//REMOVElaunch();
}

function loadWeeklyImages(){
	var index_num=0;
	modImages = new Array();
	for(i=0;i<week_count;i++){
		week_number=i+1;
		modImages[i]=base_image_dir+"/"+yyyymmdd+"/"+region+"_wk"+week_number+""+variable+"_"+yyyymmdd+"."+img_type;
	}
	
	first_image = 1;
	last_image = i;
	theImages[0] = new Image();
  theImages[0].src = modImages[0];
  //$('div#debug').append('<br>loadWeeklyImages - first image='+modImages[0]+'<br>');
  imageNum[0] = true;
	//REMOVElaunch();
}


function load_region_overlay_selection(loc){
	if(!loc){
		loc="";
	}
	//debugAppend("load_region_overlay_selection: called with loc="+loc);
	//region=$('#animation_region').val();
	if(!getRegion()){
		//Region failed
		alert("There was a problem getting the region");
		return;
	}
	
	overlay_selection_html="";
	if(region && region.length >= 1){
		//Valid region
		//See if there are any overlays for this region
		if(region_overlay_count_obj && region_overlay_count_obj[region] && region_overlay_count_obj[region] >= 1){
			//There are overlays
			//$('#overlay_controls').html("Has overlays");
			
			region_index_val=region_index[region];
			newcell_count=0;
			newcell_incr=6;
			
			flt_overlays_per_column=region_overlay_count_obj[region]/3.0;
			flt_overlays_per_column=region_overlay_count_obj[region]/2.0;
			
			//See how many we have
			if(region_overlay_count_obj[region] < 7){
				//Put them all on new lines
				//newcell_incr=6;
			}
			else if(region_overlay_count_obj[region] < 13){
				//Need 2 cells
				//newcell_incr=6;
			}
			else if(region_overlay_count_obj[region] < 19){
				//Need 2 cells
				//newcell_incr=6;
			}
			//overlay_selection_html+="<div id='overlay_div'><table width='100%'><tr><td>";
			overlay_selection_html+="<table width='100%'><tr><td>";
			for(i=0;i<region_overlay_count_obj[region];i++){
				checked_value="";
				if(region_overlay_count_obj[region] > 1){
					checked_value=region_overlay_default_list[region_index_val][i];
				}
				else{
					checked_value=region_overlay_default_list[region_index_val];
				}
				//checked_value=region_overlay_default_list[region_index_val][i];
				ov_format=region_overlay_format_list[region_index_val][i];
				ov_value=region_overlay_value_list[region_index_val][i];
				ov_name=region_overlay_name_list[region_index_val][i];
				ov_bg_flag=region_overlay_bg_flag_list[region_index_val][i];
				ov_real_time_flag=region_overlay_real_time_flag_list[region_index_val][i];
				ov_height=region_overlay_height[region];
				ov_width=region_overlay_width[region];
				checked_html="";

				if(checked_value && checked_value == 1){
					checked_html="checked";
					overlay['overlay_'+region+'_'+ov_value]=1;
				}
				//overlay_selection_html+="<input type='checkbox' class='overlay_checkbox' id='check_"+region+"_"+ov_value+"' value='"+ov_value+"' onclick=\"changeOverlay('"+ov_value+"');\"> "+ov_name+"<br>";
				//overlay_selection_html+="<input type='checkbox' class='overlay_checkbox' id='check_"+region+"_"+ov_value+"' value='"+ov_value+"' onclick=\"changeOverlay('"+ov_value+"');\" "+checked_html+"> "+ov_name+"<br>";
				//WORKINGoverlay_selection_html+="<input type='checkbox' class='overlay_checkbox' id='check_"+region+"_"+ov_value+"' value='"+ov_value+"' onclick=\"changeOverlay('"+ov_value+"');\" "+checked_html+"> "+ov_name+"";
				//WORKINGoverlay_selection_html+="<input type='hidden' id='format_"+region+"_"+ov_value+"' value='"+ov_format+"'>";
				if(ov_bg_flag == 1){
					if(session_id == 52){
						overlay_selection_html+="<input type='checkbox' class='overlay_checkbox' id='check_"+region+"_"+ov_value+"' value='"+ov_value+"' onclick=\"changeBackground('"+ov_value+"');\" "+checked_html+"> "+ov_name+"";
						overlay_selection_html+="<input type='hidden' id='format_"+region+"_"+ov_value+"' value='"+ov_format+"'>";
					}
				}
				else{
					overlay_selection_html+="<input type='checkbox' class='overlay_checkbox' id='check_"+region+"_"+ov_value+"' value='"+ov_value+"' onclick=\"changeOverlay('"+ov_value+"');\" "+checked_html+"> "+ov_name+"";
					overlay_selection_html+="<input type='hidden' id='format_"+region+"_"+ov_value+"' value='"+ov_format+"'>";
					
				}
				overlay_selection_html+="<input type='hidden' id='real_time_flag_"+region+"_"+ov_value+"' value='"+ov_real_time_flag+"'>";

				newcell_count++;

				if(newcell_count >= flt_overlays_per_column && i == region_overlay_count_obj[region] - 1){
					//Last value need, close
					overlay_selection_html+="</td>";
					newcell_count=0;
				}
				else if(newcell_count >= flt_overlays_per_column){
					overlay_selection_html+="</td><td>";
					newcell_count=0;
				}
				else{
					overlay_selection_html+="<br>";
				}
			}
			//overlay_selection_html+="</tr></table></div>";
			overlay_selection_html+="</tr></table>";
			//$('#overlay_controls').html(overlay_selection_html);
		}
		else{
			//$('#overlay_controls').html("No overlays");
		}
	}
	else{
		alert("Bad region");
		return;
	}
	//$('#overlay_controls').html(overlay_selection_html);
	$('#overlay_div').html(overlay_selection_html);
}

function changeBackground(name){
	if(session_id != 52){
		return;
	}
	id='overlay_'+region+'_'+name;
	//
	//$('#debug').append("(vis="+$('#overlay').css('visibility')+" Trying name="+name+" and id="+id+" len="+$(id).length+" child="+$(id).children(':image').length+" t="+$(id).text().length+":<br>");

	if(overlay[id] && overlay[id] == 1){
		//That overlay already appears to be on so we should turn it off
		//
		//$('#debug').append("has an image<br>");
		
		//Make sure that it is not really checked
		if($('#check_'+region+'_'+name) && $('#check_'+region+'_'+name).is(':checked')){
			//It is checked, should not be here
			//
			//$('#debug').append("IS CHECKED<br>");
			
		}
		else{
			//Phew not checked, we can remove it
			$('#'+id).remove();
			//
			//$('#debug').append("removing ID="+id+" ok<br>");
			overlay[id]=0;
		}
	}
	else{
		//
		//$('#debug').append("Does not exist<br>");
		//Not set yet, we should make sure the value is actually checked
		if($('#check_'+region+'_'+name) && $('#check_'+region+'_'+name).is(':checked')){
			//It is checked so we should add the overlay
			var valid_format=0;
			ov_height=region_overlay_height[region];
			ov_width=region_overlay_width[region];
			if($('#format_'+region+'_'+name)){
				//Format appears to be set
				if($('#format_'+region+'_'+name).attr('value') === "gif" || $('#format_'+region+'_'+name).attr('value') === "png"){
					//Looks valid
					ov_format=$('#format_'+region+'_'+name).attr('value');
					valid_format=1;
				}
			}
			else{
			}
			//Make sure we found a valid format
			if(valid_format == 1){
				//Yup
				$('#underlay').append("<div class='underlay' id="+id+"><img src='http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+name+"_background_"+ov_width+"x"+ov_height+"_transparent."+ov_format+"'></div>");
			}
			else{
				alert("Invalid format (#format_"+region+"_"+name+"="+$('#format_'+region+'_'+name).attr('value')+")");
			}
			
			//$('#debug').append("adding overlay for id="+id+"<br>");
			overlay[id]=1;
		}
		else{
			//
			//$('#debug').append("not checked<br>");
			//Not checked, that is weird
		}

	}
	//
	//$('#debug').append("<br><br>");
	return;
}

function changeMapType(type){
	var valid_previous_type=0;
	var valid_type=0;
	if(type){
		//alert("Type is "+type);
		//Make sure we have a valid type
		if(type === "current"){
			stopImage();
			//alert("Show hist");
			$('#cell_2_header').html("Frame Controls");
			valid_type=1;
			data_type="current";
			//$('#date_controls').css("display","");
		}
		else if(type === "historical"){
			stopImage();
			$('#cell_2_header').html("Hist Time Period");
			valid_type=1;
			data_type="historical";
			//$('#date_controls').css("display","none");
		}
		else if(type === "short_range"){
			stopImage();
			$('#cell_2_header').html("Frame Controls");
			valid_type=1;
			data_type="short_range";

			//$('#date_controls').css("display","");
		}
		else if(type === "long_range"){
			stopImage();
			$('#cell_2_header').html("Fcst Time Period");
			valid_type=1;
			data_type="long_range";

			//$('#date_controls').css("display","none");
		}
		else{
			//Invalid
			alert("Invalid type ("+type+") specified");
			return;
		}
		//previous_type=type;
	}
	else{
		alert("No map type set");
		return;
	}
	if(previous_type && previous_type.length >= 1){
		$('#'+previous_type+'_content').hide();
		
		$('#'+previous_type+'_link').removeClass("selected");
		//$('#'+previous_type+'_select').hide();
		$('.'+previous_type+'_select').hide();
	}
	if(valid_type == 1){
		$('#'+type+'_content').show();
		$('#'+type+'_link').addClass("selected");
		//$('#'+type+'_select').show();
		$('.'+type+'_select').show();
		previous_type=type;
	}
	return 1;
}


function dyngetStartDate(){
	//yyyymmdd_start=$('#animation_start_date').val();
	//yyyymmdd_start=$('#'+data_type+'_start_date').val();
	yyyymmdd_start=cvtCalDateToYmd($('#'+data_type+'_start_date').val());
	if(yyyymmdd_start){
		return yyyymmdd_start;
	}
	return;
	
}

function dyngetEndDate(){
	//yyyymmdd_end=$('#animation_end_date').val();
	//yyyymmdd_end=$('#'+data_type+'_end_date').val();
	yyyymmdd_end=cvtCalDateToYmd($('#'+data_type+'_end_date').val());
	if(yyyymmdd_end){
		return yyyymmdd_end;
	}
	return;
}


function dyngetCalendar(){
	
	//if(document.getElementById('retail_calendar') && document.getElementById('retail_calendar').checked){
	if(document.getElementById(data_type+'_retail_calendar') && document.getElementById(data_type+'_retail_calendar').checked){
    calendar="retail";
  }else{
  	calendar="julian";
  }
  return 1;
}

function dyngetPeriod(){
	if(data_type == "short_range"){
		dynPeriod="daily";
		return 1;
	}
	else{
		if(document.getElementById(data_type+'_period_daily') && document.getElementById(data_type+'_period_daily').checked){
			dynPeriod="daily";
			return 1;
		}else if(document.getElementById(data_type+'_period_weekly') && document.getElementById(data_type+'_period_weekly').checked){
			dynPeriod="weekly";
			return 1;
		}
		else if(document.getElementById(data_type+'_period_monthly') && document.getElementById(data_type+'_period_monthly').checked){
			dynPeriod="monthly";
			return 1;
		}
		else{
			alert("Invalid ("+data_type+") period chosen2");
		}
	}
	
	return;
}

function dynloadImages(){
	data_args="";
	var valid_flag=0;
	//debugAppend("dynloadImages: ");
	//debug_padding+=2;
	if(!dyngetCalendar()){
		//Calendar failed
		alert("There was a problem getting the calendar");
		return;
	}
	
	if(!dyngetStartDate()){
		//Start date failed
		//alert("There was a problem getting the start date");
		alert("Please enter a valid date in the MM/DD/YYYY form (e.g. 04/24/2008).");
		return;
	}
	
	if(data_type === "historical" || data_type === "long_range"){
		//Make sure the date is greater than the min date and less than the max date
		if(yyyymmdd_start >= min_data_type_Ymd[data_type] && yyyymmdd_start <= max_data_type_Ymd[data_type]){
			//It is w/in the acceptable range
		}
		else{
			alert("Please enter a date ("+yyyymmdd_start+") that falls between "+min_data_type_mdY[data_type]+" and "+max_data_type_mdY[data_type]);
			return;
		}
	}
	
	if(data_type === "custom"){
		if(!dyngetEndDate()){
			//End date failed
			alert("There was a problem getting the end date");
			return;
		}
		data_args+="sd="+yyyymmdd_start+"&ed="+yyyymmdd_end;
	}

	/*REMOVE
	if(!dyngetEndDate()){
		//End date failed
		alert("There was a problem getting the end date");
		return;
	}
	*/
	
	if(!dyngetPeriod()){
		//End date failed
		alert("There was a problem getting the period");
		return;
	}
	
	if(!getRegion()){
		//Region failed
		alert("There was a problem getting the region");
		return;
	}
	
	/*REMOVE
	if(!dyngetUnits()){
		//Units failed
		alert("There was a problem getting the units");
		return;
	}
	*/
	
	if(!getVariable()){
		//Variable failed
		//alert("There was a problem getting the variable");
		alert("Please choose a valid variable");
		return;
	}
	
	if(data_type == "short_range" && (requestNumber == 0 || requestNumber == 1) && browserType && browserType != "MSIE6"){
		
	}
	else{
		playLastImage=0;
	}
	
	if(data_type == "long_range" && (variable == "prcp" || variable == "ly_prcp_anomaly") && dynPeriod == "daily"){
		alert("We're sorry but forecast precipitation imagery is only available in weekly and monthly periods.");
		return;
	}
	
	//Looks like we are good to go.  We need to loop over all the days and update the image array
	//yyyymmdd_start=20090520;
	//data_type="short_range";
	
	//If we are dealing w/a monthly period, we will need to adjust the date we pass
	if(dynPeriod === "monthly"){
		var newMM=$('#animation_month_'+data_type).val();
		var newYYYY=$('#animation_year_'+data_type).val();
		yyyymmdd_start=newYYYY+newMM+'01';
		
	}

	//data_args="sd="+yyyymmdd_start+"&ed="+yyyymmdd_end+"&v="+variable+"&c="+calendar+"&k="+key+key_num+"&r="+region+"&u="+units;
	data_args="sd="+yyyymmdd_start+"&v="+variable+"&c="+calendar+"&k="+key+key_num+"&r="+region+"&u="+units;
	data_args+="&p="+dynPeriod+"&dt="+data_type+"&opt=request";
	//data_args+="&p="+dynPeriod+"&dt=short_range&opt=request";
	
	//
	//$('#footer').append("ARGS="+data_args+"<br>");
	
	img_url="/images/loading.gif";
	

	
	$.ajax({
		type: "GET",
		url: "/ajax/maps.php",
		data: data_args,
		datatype: "xml",
		success: function(xml) {
			
			var results=$('results',xml);
			//result=$('result',results);
			if(processMapRequestXML(results)){
				//Valid map

				valid_flag=1;
			}
			else{
				//Invalid map
			}


			key_num=key_num+1;
		},
		
		error: function(xml) {
			//REMOVEDEBUG$('#animation_image').html("<p align='center' style='color:black;'>There appears to be a problem loading your image24.</p>");
			$('#jqm_dialog_content').html("We're sorry but there was an error loading your image.  Please send an email to <a href='mailto:info@myskeye.com?subject=Error: 05-01451'>info@myskeye.com</a> with the subject \"Error: 05-01451\".");
			$('#jqm_dialog').jqmShow();
		}
		
	});
	

	return valid_flag;

}


function loadCurrentImages(){
	data_args="";
	var valid_flag=0;
	//debugAppend("dynloadImages: ");
	//debug_padding+=2;
	if(!dyngetCalendar()){
		//Calendar failed
		alert("There was a problem getting the calendar");
		return;
	}
	
	if(!getRegion()){
		//Region failed
		alert("There was a problem getting the region");
		return;
	}
	
	if(!getVariable()){
		//Variable failed
		//alert("There was a problem getting the variable");
		alert("Please choose a valid variable");
		return;
	}
	
	//Looks like we are good to go.  We need to loop over all the days and update the image array
	//yyyymmdd_start=20090520;
	//data_type="short_range";

	//data_args="sd="+yyyymmdd_start+"&ed="+yyyymmdd_end+"&v="+variable+"&c="+calendar+"&k="+key+key_num+"&r="+region+"&u="+units;
	data_args="sd="+yyyymmdd_start+"&v="+variable+"&c="+calendar+"&k="+key+key_num+"&r="+region+"&u="+units;
	data_args+="&p="+dynPeriod+"&dt="+data_type+"&opt=request";
	//data_args+="&p="+dynPeriod+"&dt=short_range&opt=request";
	
	//$('#footer').append("ARGS="+data_args+"<br>");
	
	img_url="/images/loading.gif";
	

	
	$.ajax({
		type: "GET",
		url: "/ajax/maps.php",
		data: data_args,
		datatype: "xml",
		success: function(xml) {
			
			var results=$('results',xml);
			//result=$('result',results);
			if(processMapRequestXML(results)){
				//Valid map

				valid_flag=1;
				
				
				yyyymmdd=$('#animation_date').val();


				//See if weekly should be visible
				if(weekly_flag[variable] && weekly_flag[variable] == 1){
					$('#week_row').css("visibility","visible");
				}
				else{
					$('#week_row').css("visibility","hidden");
				}

				//Looks like we are good to go.  We need to loop over all the days and update the image array
				//stop();
				var index_num=0;
				var day_html="<tr id='day_row'><td>Date</td>";
				currentImg_count=currentImgCount[variable];
				if(currentImg_count > 1){
					var currentImgDate=new Date();
					for(var i=0;i<currentImg_count;i++){
						var id_num=i+1;
						//ts=currentImg_list[i];
						ts=currentImgList[variable][i];
						currentImg_yyyy=ts.substring(0,4);
						currentImg_mm=ts.substring(4,6);
						currentImg_dd=ts.substring(6,8);
						currentImg_hh=ts.substring(8,10);
						currentImg_min=ts.substring(10,12);
						currentImgDate.setUTCFullYear(currentImg_yyyy,currentImg_mm-1,currentImg_dd);

						var month=currentImgDate.getMonth();
						nice_month=months[month];
						nice_mon=nice_month.substring(0,3);

						nice_date=currentImg_dd+" "+nice_mon+"<br>"+currentImg_hh+":"+currentImg_min;
						day_html+="<td class='day_cell' id='day_"+id_num+"' onclick=\"changeImage("+id_num+",'daily');\"><a href='javascript:void(0)' onclick=\"changeImage("+id_num+",'daily');\">"+nice_date+"</a></td>";
						//modImages[i]=base_image_dir+"/current/"+variable+"/"+region+"_"+ts+"_"+variable+"."+img_type;

					}
					day_html+="</tr>";
					week_html="<tr id='week_row'><td>&nbsp;</td>\
	<td colspan=7 id='week_1' class='week_cell'>&nbsp;</td><td colspan=7 id='week_2' class='week_cell'>&nbsp;</td></tr>";
					//$('#date_controls').html(day_html+week_html);
					if(data_type == "current"){
						if(currentImgCount[variable] > 1){
							$('#date_controls').html(day_html+week_html);
							$('#date_controls').css("display","");
						}
						else{
							$('#date_controls').css("display","none");
						}

					}
					else{
						$('#date_controls').html(day_html+week_html);
						$('#date_controls').css("display","");
					}
				}
				else{
					$('#date_controls').css("display","none");
				}
			
			}
			else{
				//Invalid map
			}

			key_num=key_num+1;
		},
		
		error: function(xml) {
			$('#animation_image').html("<p align='center' style='color:black;'>There appears to be a problem loading your image23.</p>");
		}
		
	});
	

	return valid_flag;

}

function chkValidYuiDate(date){
  var return_code=0;
  if(valid_yui_regexp.test(date)){
  	//Date is valid
    match_date=valid_yui_regexp.exec(date);
    //We should have 3 matches (check for 4 though b/c the array length should be one more than the #of elements
    if(match_date && match_date.length == 4){
    	//We have enough elements but do we have valid dates
    	var int_mm=match_date[1];
    	var int_dd=match_date[2];
    	var int_yyyy=match_date[3];
    	//Check 30 day months
    	if(int_mm == 4 || int_mm == 6 || int_mm == 9 || int_mm == 11){
    		//Should have 30 days
    		if(int_dd >= 1 && int_dd <= 30){
    			//Perfect
    		}
    		else{
    			//Invalid number of days
    			return;
    		}
    	}
    	else if(int_mm == 1 || int_mm == 3 || int_mm == 5 || int_mm == 7 || int_mm == 8 || int_mm == 10 || int_mm == 12){
    		//Should have 31 days
    		if(int_dd >= 1 && int_dd <= 31){
    			//Perfect
    		}
    		else{
    			//Invalid number of days
    			return;
    		}
    	}
    	else if(int_mm == 2){
    		//February, but is it a leap year
    		if((int_yyyy % 4 == 0 && int_yyyy % 100 != 0) || int_yyyy % 400 == 0){
    			//Is leap year, need 29 days
    			if(int_dd >= 1 && int_dd <= 29){
    				//Perfect
    			}
    			else{
    				//Invalid number of days
    				return;
    			}
    		}
    		else{
    			//Not leap year, need 28 days
    			if(int_dd >= 1 && int_dd <= 28){
    				//Perfect
    			}
    			else{
    				//Invalid number of days
    				return;
    			}
    		}
    	}
    	else{
    		//Invalid date
    		return;
    	}
    	return 1;
    }
    else{
    	//Not enough elements so it must have failed
    }

  }else{

  }
  return;
}

function cvtCalDateToYmd(date){
	//Make sure we have a valid date format
	
	if(!chkValidYuiDate(date)){
		//alert("chkValidYuiDate indicates an Invalid date");
		//alert("Please enter a valid date in the MM/DD/YYYY form (e.g. 04/24/2008).");
		return;
	}
  
	//Date is valid
	match_date=valid_yui_regexp.exec(date);
	//We should have 3 matches (check for 4 though b/c the array length should be one more than the #of elements
	if(match_date && match_date.length == 4){
		var mm=match_date[1];
		var dd=match_date[2];
		var yyyy=match_date[3];
		var yyyymmdd=yyyy+mm+dd;
		
		return yyyymmdd;
	}
	else{
		//alert("No money "+match_date.length);
	}
	
	return;
	
}

function changeDynPeriod(newPeriod){
	if(data_type){
		//It is set
		//$('div#debug').append("   data_type="+data_type+"<br>");
		if(data_type === "historical"){
			//In historical data_type
			
			//$('div#debug').append("   uncheck LR button via modifyPeriodRadio('long_range',"+dynPeriod+",'uncheck')<br>");
			//Deselect old period button
			modifyPeriodRadio('long_range',dynPeriod,'uncheck');
			//Select the new period button
			//$('div#debug').append("   check LR button via modifyPeriodRadio('long_range',"+newPeriod+",'check')<br>");
			modifyPeriodRadio('long_range',newPeriod,'check');
		}
		else if(data_type === "short_range"){
			//In short_range data_type
		}
		else if(data_type === "long_range"){
			//In long_range data_type
			//Deselect old period button
			modifyPeriodRadio('historical',dynPeriod,'uncheck');
			//Select the new period button
			modifyPeriodRadio('historical',newPeriod,'check');
		}
		else if(data_type === "custom"){
			//In custom data_type
		}
		else{
			//Invalid data type
			alert("Invalid data_type ("+data_type+")");
			return;
		}
		
		//See if we need to show or hide the month and year dropdowns
		if(data_type === "historical" || data_type === "long_range"){
			//Is the new period monthly?
			if(newPeriod === "monthly"){
				//Yes, hide the date input
				$('#'+data_type+'_date_title').hide();
				$('#'+data_type+'_date_input').hide();
				$('#'+data_type+'_month_input').show();
			}
			else if(dynPeriod === "monthly"){
				//No, but the old one is which means we need to hide
				//the dropdowns
				$('#'+data_type+'_month_input').hide();
				$('#'+data_type+'_date_title').show();
				$('#'+data_type+'_date_input').show();				
			}
		}
	}
	else{
		//Not set
		alert("No data_type is set");
	}
	
	dynPeriod=newPeriod;
	//$('div#debug').append("** DONE FUNCTION changeDynPeriod("+newPeriod+"):<br>");
	
	return;
}

function modifyPeriodRadio(type,period,status){
	
	var radioID=type+"_period_"+period;
	//Make sure the element exists
	if($('#'+radioID)){
		//It exists
		//$('div#debug').append("  radioID ("+radioID+") exists<br>");
		
		//Do we need to select or deselect it
		if(status === "check"){
			//Need to select it
			$('#'+radioID).attr('checked','checked');

		}
		else{
			//Need to unselect it
			$('#'+radioID).attr('checked','');
		}
	}
	else{
		//ID does not exist
		alert("No element exists w/that id ("+radioID+")");
	}
	return;
}

function processMapRequestXML(xml){
	var valid=0;
	
	result=$('result',xml);

	if(result && result.text() === "success"){
		//load_region_overlay_selection();
		load_image_overlays('processMapRequestXML');
		
		var imageURL=$('url',xml).text();
		//		$('#animation_image').html("<img src='"+imageURL+"'>");
		
		if($('title',xml)){
			var title_html="";
			//$('#date_controls').html($('title',xml).text());
		}
		requestMapCount=0;
		
		imagePathList = new Object();
		imagePathList['daily'] = new Array();
		imagePathList['weekly'] = new Array();
		
		if($('requestMapCount',xml)){
			requestMapCount=$('requestMapCount',xml).text();
			//alert("FOUND requestMapCount="+requestMapCount);
		}
		else{
			alert("There was an error processing your request.  Please contact feedback@myskeye.com.");
			return;
		}
		
		dailyElementClass="";
		weeklyElementClass="";
		
		//See if we need to load a single image or multiple
		if(requestMapCount > 1){
			//Need more than one
			stop();
			
			var indexNum=0;
			animHTML="";
			//$('#animation_image').html(animHTML);
			var outerDiv=document.getElementById('animation_image');
			animDailyHTML="<div id='daily_loop_images'>";
			animDailyHTML="";
			animWeeklyHTML="<div id='weekly_loop_images'>";
			animWeeklyHTML="";
			imageCount['daily']=0;
			imageCount['weekly']=0;
			if(data_type == "short_range"){
				if(period == "daily"){
					baseImageID="daily_";
				}
				else if(period == "weekly"){
					baseImageID="weekly_";
				}
				else{
					baseImageID="";
				}
				
			}
			else{
				baseImageID="";
			}
			$('image',xml).each(function(){
				//REMOVEmodImages[indexNum]="/mapdata/"+$(this).attr('id')+"/"+$(this).text();
				
				loadJS="";
				
				//For now we need to handle the short_range differently
				if(data_type == "short_range"){

					//imagePathList[indexNum]="/mapdata/"+$(this).attr('id')+"/"+$(this).text();
					if(data_type == "current" && $(this).attr('ts') && $(this).attr('id').length >= 1){
						currentImgList[variable][indexNum]=$(this).attr('ts');
					}
					
					if($(this).attr('period') == "daily"){
						if(browserType != "MSIE6"){
							loadJS="onload=imageStatus('daily_loop_image_"+imageCount['daily']+"','completed',imageLoadedCount++);";
						}
						//loadJS="onload=imageStatus('daily_loop_image_"+imageCount['daily']+"','completed',imageLoadedCount++);";
						imagePathList['daily'][imageCount['daily']]="/mapdata/"+$(this).attr('period')+"/"+$(this).attr('id')+"/"+$(this).text();
						animHTML="<div id='daily_loop_image_"+imageCount['daily']+"' class='loop_image'><img name='animation' src='"+imagePathList['daily'][imageCount['daily']]+"' "+loadJS+" class='hiddenElements' border='0'></div>"+animHTML;
						//
						animDailyHTML="<div id='daily_loop_image_"+imageCount['daily']+"' class='loop_image'><img name='animation' src='"+imagePathList['daily'][imageCount['daily']]+"' "+loadJS+" class='hiddenElements' border='0'></div>"+animDailyHTML;
						

						
						imageCount['daily']++;
					}
					else if($(this).attr('period') == "weekly"){
						if(browserType != "MSIE6"){
							loadJS="onload=imageStatus('weekly_loop_image_"+imageCount['weekly']+"','completed',imageLoadedCount++);";
						}
						
						//loadJS="onload=imageStatus('weekly_loop_image_"+imageCount['weekly']+"','completed',imageLoadedCount++);";
						imagePathList['weekly'][imageCount['weekly']]="/mapdata/"+$(this).attr('period')+"/"+$(this).attr('id')+"/"+$(this).text();
						animWeeklyHTML="<div id='weekly_loop_image_"+imageCount['weekly']+"' class='loop_image'><img name='animation' src='"+imagePathList['weekly'][imageCount['weekly']]+"' "+loadJS+" class='hiddenElements' border='0'></div>"+animWeeklyHTML;
						
						
						imageCount['weekly']++;
					}
					
					

					
					day_number=i+1;
					indexNum++;
					
					dailyElementClass="none";
					weeklyElementClass="none";
					if(data_type == "daily"){
						weeklyElementClass="hiddenElements";
					}
					else if(data_type == "weekly"){
						dailyElementClass="hiddenElements";
					}
				}
				else{
					//loadJS="onload=imageStatus('loop_image_"+indexNum+"','completed');";
					imagePathList[indexNum]="/mapdata/"+$(this).attr('period')+"/"+$(this).attr('id')+"/"+$(this).text();
					if(browserType != "MSIE6"){
						loadJS="onload=imageStatus('loop_image_"+indexNum+"','completed',imageLoadedCount++);";
					}
					
					animHTML="<div id='loop_image_"+indexNum+"' class='loop_image'><img name='animation' src='"+imagePathList[indexNum]+"' "+loadJS+" class='hiddenElements' border='0'></div>"+animHTML;

					if(data_type == "current" && $(this).attr('ts') && $(this).attr('id').length >= 1){
						currentImgList[variable][indexNum]=$(this).attr('ts');
					}
					day_number=i+1;
					imageCount['daily']++;
					imageCount['weekly']++;
					indexNum++;
				}
				
				if(data_type == "long_range"){
					$('#date_controls').css("display","none");
				}
				
				

			});
			
			if(data_type == "short_range"){
				animHTML=animDailyHTML+animWeeklyHTML;
				$('#animation_image').html(animHTML);
			}
			else{
				$('#animation_image').html(animHTML);
			}
			//animHTML=animDailyHTML+animWeeklyHTML;
			//alert(animHTML);
			first_image = 1;
			//REMOVElast_image = indexNum;
			last_image = imageCount[period];
			
			//$('#animation_image').html(animHTML);
			//$('#loop_image_0 img').removeClass("hiddenElements");
			//decrementImage(1);
			//
			mattchangeImage(1);
			//REMOVEhighlightPeriod(1);
			prevImageNum=0;
			currentImgCount[variable]=indexNum;
			
			
		}
		else if(requestMapCount == 1){
			//Only need one
			indexNum=0;
			animHTML="";
			stop();
			if(data_type == "current"){
				//$('#animation_image').html("<img src='"+imageURL+"'>");
				loadJS="";
				$('image',xml).each(function(){
					imagePathList[indexNum]="/mapdata/"+$(this).attr('id')+"/"+$(this).text();
					//REMOVEanimHTML="<div id='loop_image_"+indexNum+"' class='loop_image'><img name='animation' src='"+imagePathList[indexNum]+"' class='hiddenElements' border='0'></div>"+animHTML;
					animHTML="<div id='loop_image_"+indexNum+"' class='loop_image'><img name='animation' src='"+imagePathList[indexNum]+"' "+loadJS+" border='0'></div>";


				});

				currentImgCount[variable]=indexNum;

				$('#animation_image').html(animHTML);

				first_image = 1;
				last_image = indexNum;
			}
			else{
				$('image',xml).each(function(){
					imagePathList[indexNum]="/mapdata/"+$(this).attr('period')+"/"+$(this).attr('id')+"/"+$(this).text();
					//REMOVEanimHTML="<div id='loop_image_"+indexNum+"' class='loop_image'><img name='animation' src='"+imagePathList[indexNum]+"' class='hiddenElements' border='0'></div>"+animHTML;
					animHTML="<div id='loop_image_"+indexNum+"' class='loop_image'><img name='animation' src='"+imagePathList[indexNum]+"' border='0'></div>";
				});

				currentImgCount[variable]=indexNum;

				$('#animation_image').html(animHTML);

				first_image = 1;
				last_image = indexNum;
				
			}
			
			
		}
		else{
			alert("Error w/requestMapCount ("+requestMapCount)
		}
		
		
		valid=1;
		previous_region=region;
		previous_data_type=data_type;
	}
	else{
		var requestError="";
		if($('error',xml)){
			requestError=$('error',xml).text();
		}
		if($('errorMsg',xml)){
			requestErrorMsg=$('errorMsg',xml).text();
		}
		errorHTML="";
		
		if(requestError.length >= 1){
			if(requestError == "00000"){
				$('#jqm_dialog_content').html("We're sorry but you do not appear to be logged in.  Please <a href='/login' class='jqm_mailto_link' ><b><u>click here</u></b></a> to login or send an email to <a class='jqm_mailto_link' href='mailto:info@myskeye.com'><b><u>info@myskeye.com</u></b></a> to find out how to become a member");
				$('#jqm_dialog').jqmShow();
			}
			else if(requestError == "99999"){
				$('#jqm_dialog_content').html(requestErrorMsg);
				$('#jqm_dialog').jqmShow();
			}
			else{
				//REMOVE$('#jqm_dialog_content').html("We're sorry but your account is not authorized to access this image. If you feel this is an error please contact your sales representative or send an email to <a class='jqm_mailto_link' href='mailto:info@myskeye.com'>info@myskeye.com</a>");
				errorHTML=requestErrorMsg+" If you feel this is an error please contact your sales representative or send an email to <a class='jqm_mailto_link' href='mailto:info@myskeye.com'>info@myskeye.com</a>";
				$('#jqm_dialog_content').html(errorHTML);
				$('#jqm_dialog').jqmShow();
			}
		}
		else{
			//Failed
			$('#jqm_dialog_content').html("We're sorry but your account is not authorized to access this image. If you feel this is an error please contact your sales representative or send an email to <a class='jqm_mailto_link' href='mailto:info@myskeye.com'>info@myskeye.com</a>");
			$('#jqm_dialog').jqmShow();
		}
				
		
	}
	return valid;
}

function load_image_overlays(loc){
	if(!loc){
		loc="";
	}
	//debugAppend("load_image_overlays: called with loc="+loc);
	if(previous_region != region && previous_region.length >= 1){
		//remove all the overlays that are set
		$('#overlay').html("");
		overlay= new Object;
		load_region_overlay_selection();
		//REMOVEload_region_overlay_selection("load_image_overlays 1");
	}
	else if(previous_region != region && previous_region.length == 0){
		$('#overlay').html("");
		//debugAppend("load_image_overlays: prev_region("+previous_region+") != region("+region+") and length=0");
		overlay= new Object;
		load_region_overlay_selection();
		//REMOVEload_region_overlay_selection("load_image_overlays 2");
	}
	else{
	}
	overlay_html="";
	if(use_overlay && use_overlay == 1){
		//$("input[@class='overlay_checkbox']").each(function(){
		$("input.overlay_checkbox").each(function(){
			if(this.checked){
				//overlay_html+="<div class='overlay' id='overlay_"+region+"_"+$(this).attr('value')+"'><img src='http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+$(this).attr('value')+"_overlay_"+ov_width+"x"+ov_height+"_transparent.gif'></div>";
				name=$(this).attr('value');
				var valid_format=0;
				ov_height=region_overlay_height[region];
				ov_width=region_overlay_width[region];
				if($('#format_'+region+'_'+name)){
					//Format appears to be set
					if($('#format_'+region+'_'+name).attr('value') === "gif" || $('#format_'+region+'_'+name).attr('value') === "png"){
						//Looks valid
						ov_format=$('#format_'+region+'_'+name).attr('value');
						valid_format=1;
					}
					else{
						//debugAppend("not gif/png ("+$('#format_'+region+'_'+name).attr('value')+")");
					}
				}
				else{
					//debugAppend("#format_"+region+"_name does not exist");
				}
				//Make sure we found a valid format
				if(valid_format == 1){
					//Yup
					
					//If we are dealing w/a PNG overlay and the user has MSIE6 we need to use the IE6 specific imagery
					var ie6Suffix="";
					var imgPath="";
					if(ov_format === "png" && browserType === "MSIE6"){
						ie6Suffix="-ie6";
						ov_format="gif";
					}
					imgPath="http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+$(this).attr('value')+"_overlay_"+ov_width+"x"+ov_height+"_transparent"+ie6Suffix+"."+ov_format;
					//REMOVEoverlay_html+="<div class='overlay' id='overlay_"+region+"_"+$(this).attr('value')+"'><img src='http://map.myskeye.com/grads_images/overlays/"+region+"/"+region+"_"+$(this).attr('value')+"_overlay_"+ov_width+"x"+ov_height+"_transparent."+ov_format+"'></div>";
					overlay_html+="<div class='overlay' id='overlay_"+region+"_"+$(this).attr('value')+"'><img src='"+imgPath+"'></div>";
				}
				else{
					alert("Invalid format (#format_"+region+"_"+name+"="+$('#format_'+region+'_'+name).attr('value')+")");
				}
			}
			else{
			}
		});
	}
	$('#overlay').html(overlay_html);
	$('#overlay').css({'visibility':'visible'});
}

function debugAppend(txt){
	
	indent="";
	for(i=0;i<debug_padding;i++){
		indent+=s;
	}
	$('#debug').append(indent+txt+"<br>");
	return;
}

function imageStatus(id,status,count){
	//	$('div#debug').append("  imageStatus("+id+","+status+","+count+")<br>");
	var lastImageNumber=last_image - 1;
	var curImageNumber=count + 1;
	if(count == 1){
		progBarPercent=0;
		$('#progressBarContainer').css({'visibility':'visible'});
	}
	else{
		progBarPercent=count/lastImageNumber;
	}
	
	progBarPx=progBarPercent*200;
	$('#progressBarInnerDiv').width(progBarPx);
	$('#progressBarText').html("Loading Imagery: "+Math.round(progBarPercent*100)+"%");
	if(count && count == lastImageNumber){
		$('#progressBarContainer').css({'visibility':'hidden'});
		if(playLastImage && playLastImage == 1 && last_image >= 1){
			playImage();
		}
	}
	else{
	}
}