/*!
	\brief Opens a popup window.
	\param url - the url the window should load
	\param width - the width of the window
	\param height - the height of the window
	\param opener - reference to the window that is doing the opening
	\returns Window object
*/
function OpenWindow(url,opener,width,height)
{
	if(width == null)
		width = 400;
	if(height == null)
		height = 300;
		
	// Center this window using the parent window as a guide
	var screenX = window.screenX + (window.outerWidth/2) - (width/2);
	var screenY = window.screenY + (window.outerHeight/2) - (height/2);

	var w = window.open(url,'awindow','height='+height+',width='+width+',menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,screenX='+screenX+',screenY='+screenY+'left='+screenX+',top='+screenY);
	w.opener = opener;
	w.onload = function() { CenterWindow(w); }
	w.focus();
	
	return w;
}

function CenterWindow(w)
{
	/*if(width == null)
		width = 400;
	if(height == null)
		height = 300;*/
		
	// Center this window using the parent window as a guide
	var screenX = window.screenX + (window.outerWidth/2) - (w.outerWidth/2);
	var screenY = window.screenY + (window.outerHeight/2) - (w.outerHeight/2);
	
	w.moveTo(screenX, screenY);
}

/*!
	\brief Opens a help window
	\param topic - id of the topic to load in the window
	\param opener - reference to the window that's doing the opening
	\returns Window object
*/
function OpenHelp(topic,opener)
{
	return OpenWindow("Help.html?topic="+topic, opener, 400, 300);
}

/*!
	\brief Opens a data browser window
	\param dataset_id - id of the dataset the user has selected to browse
	\param opener - reference to the window that's doing the opening
	\returns window object
*/
function OpenBrowseWindow(dataset_id,opener)
{
	return OpenWindow("/BrowseData.html?sceneImageId="+dataset_id, opener, 515, 550);
}

/*
	\brief Opens a window showing the location of a particular scene or granule
	\param scene_id - the id of the scene to locate
	\param opener - reference to the window that's doing the opening
	\returns window object
*/
function OpenLocateGranuleWindow(scene_id, opener)
{
	return OpenWindow("/LocateScene?sceneId=" + scene_id, opener, 380, 200);
}

/*!
	\brief cross-browser(?) element fetcher - fetches an HTML element
	\param id - the ID of the element to fetch
	\returns HTMLElement object
*/
function get_element(id)
{

	if(document.getElementById){
		return document.getElementById(id);
	}else{
		return document.layers[id];
	}
}

// Returns a reference to a form in the page with a particular id
function get_form(id)
{
	for(var i=0; i<document.forms.length; i++)
		if(document.forms[i].id == id)
			return document.forms[i];
	
	return false;
}

function close_div(id)
{
	var element = get_element(id);
	if(document.getElementById)
	{
		element.style.display = 'none';
	}
	else if(document.layers)
	{
		element.display = 'none';
	}
}


function open_div(id)
{
	var element = get_element(id);
	if(document.getElementById)
	{
		element.style.display = 'block';
	}
	else if(document.layers)
	{
		element.display = 'block';
	}
}

function show_div(id, retractor,expanded)
{
	var element = get_element(id);
	var status=null;
	
	if(document.getElementById)
	{
//		element.style.display = (element.style.display == 'none') ? 'block' : 'none';
		element.style.display = 'block';
		status = element.style.display;
	}
	else if(document.layers)
	{
//		element.display = (element.display == 'none') ? 'block' : 'none';
		element.display = 'block';
		status = element.display;
	}
	/*else
	{
		eval("document.all." + id + ".style.display") = (eval("document.all." + id + ".style.display")=='none') ? 'block' : 'none';
		status = eval("document.all." + id + ".style.display");
	}*/
	
	retractor.innerHTML = expanded;

}

/*!
	\brief Expands/collapses an element
	\param id - id of the element to expand/collapse (<XXX id="[id here]" ...>...</XXX>)
	\param retractor (optional) - reference to an html element that is triggering the toggle event - when passed, its inner html is changed to reflect the current expanded/retracted status
	\param contracted (optional) - text that retractor.innerHTML should show when controlled object is in its contracted state
	\param expanded (optional) - text that retractor.innerHTML should show when controlled object is in its expanded state
	\returns nothing
*/
function toggle_div(id, retractor, contracted, expanded)
{
	var element = get_element(id);
	var status=null;
	
	if(document.getElementById)
	{
		element.style.display = (element.style.display == 'none') ? 'block' : 'none';
		status = element.style.display;
	}
	else if(document.layers)
	{
		element.display = (element.display == 'none') ? 'block' : 'none';
		status = element.display;
	}
	/*else
	{
		eval("document.all." + id + ".style.display") = (eval("document.all." + id + ".style.display")=='none') ? 'block' : 'none';
		status = eval("document.all." + id + ".style.display");
	}*/
	
	if(arguments.length > 1)
		retractor.innerHTML = (status == "block") ? expanded : contracted;
}

/*!
	\brief Switches the preview image (id: 'preview_date'), updates a date span (id: 'preview_date'), and marks an html element
	\param src - new location of the image's src property
	\param newdate - the new date (goes into a span named 'preview_date')
	\param mark - id of an element to mark (see Mark function below)
	\param scene_id - id of the scene being previewed
*/
function Switch_Preview(src, newdate, mark, scene_id)
{
	var imag = get_element('preview_img');
	var date = get_element('preview_date');
	
	imag.src = src;
	date.innerHTML = newdate;
	
	Mark(mark);
	
	sceneid = scene_id;
}


/**************** Shopping Cart Functions ****************/
function doCart(span, id)
{
	if(span.innerHTML == "Add to Cart")
		addToCart(span, id);
	else
		removeFromCart(span, id);
}

function addToCart(span, dataset_id)
{
	fillSpan(span, "Adding...", "light");
	
	// Do stuff here to add to cart
	
	// Temp fix to make it look like it's doing something
	setTimeout(fillSpan, 1000, span, "Remove from Cart", "fakeref");
}

function removeFromCart(span, dataset_id)
{
	fillSpan(span, "Removing...", "light");
	
	// Do stuff here to remove from cart
	// Temp fix
	setTimeout(fillSpan, 1000, span, "Add to Cart", "fakeref");
}

function fillSpan(span, text, css_class)
{
	span.innerHTML = text;
	span.className = css_class;
}



/********** Functions for marking / highlighting HTML elements **********/

/*var highlight_bg = "#FFFFCC";	// The color to highlight cells with
var mark_bg = "#EEEEFF";		// The color to mark cells with
var default_bg = "#FFFFFF";		// The color to un-highlight cells with*/
var last_marked = null;		// Reference to an HTML element, the last element that was marked

var highlight_bg = "#D7BE96";	// The color to highlight cells with
var mark_bg = "#BDCADA";		// The color to mark cells with
var default_bg = "#FFFFFF";		// The color to un-highlight cells with

/*!
	\brief Highlights an HTML element by changing its background color (HTMLElement.style.backgroundColor
	\param element - id of the element to highlight
	\returns nothing
	
	This is usually called as part of a mouseover event
*/
function Highlight(element_id)
{
	var element = get_element(element_id);
	
	if(element.marked == null || element.marked == false)
	{
		element.style.backgroundColor = highlight_bg;
	}
}

/*!
	\brief Un-highlights an HTML element by changing its background color (HTMLElement.style.backgroundColor
	\param element - id of the element to un-highlight
	\returns nothing
	
	This is usually called on a mouseout event
*/
function Restore(element_id)
{
	var element = get_element(element_id);

	if(element.marked == null || element.marked == false)
	{
		element.style.backgroundColor = default_bg;
	}
}

/*!
	\brief Marks an HTML element by changing its background color
	\param element_id - the id of the element to mark
	\returns nothing
	
	This function creates 'last_marked' - a reference to the last HTML element that was marked. If this is
	not null, its original background-color is restored before the new element is marked.
*/
function Mark(element_id)
{
	var element = get_element(element_id);
	
	// Un-marked the last element that was marked
	if(last_marked != null)
	{
		last_marked.marked = false;
		last_marked.style.backgroundColor = default_bg;
	}
	
	// Mark the current div
	if(element.marked == null || element.marked == false)
	{
		element.marked = true;
		element.orig_bg = element.style.backgroundColor;
		element.style.backgroundColor = mark_bg;
	}
	else
	{
		element.marked = false;
		element.style.backgroundColor = highlight_bg;
	}
	
	// Make note that this was the last element that was markes
	last_marked = element;
}




/*!
	\brief Image swapping functions, shamelessly ripped from DreamWeaver
	\returns I dunno....
*/
function MM_swapImgRestore()
{ //v3.0
  var i,x,a=document.MM_sr;
  
  for(i=0; a && i < a.length && (x=a[i]) && x.oSrc;i++)
  	x.src=x.oSrc;
}

function MM_preloadImages()
{ //v3.0
  var d=document;
  
  if(d.images)
  {
  	if(!d.MM_p)
  		d.MM_p = new Array();
  		
    var i,j = d.MM_p.length;
    var a = MM_preloadImages.arguments;
    
    for(i=0; i<a.length; i++)
    {
    	if (a[i].indexOf("#")!=0)
    	{
    		d.MM_p[j]=new Image;
    		d.MM_p[j++].src=a[i];
    	}
    }
  }
}

function MM_findObj(n, d)
{ //v4.01
  var p,i,x;
  
  if(!d)
  	d=document;
  	
  if((p=n.indexOf("?")) > 0 && parent.frames.length)
  {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0,p);
  }
  
  if(!(x = d[n]) && d.all)
  	x = d.all[n];
  
  for(i=0; !x && i < d.forms.length; i++)
  	x=d.forms[i][n];
  
  for(i=0; !x && d.layers && i < d.layers.length; i++)
  	x = MM_findObj(n,d.layers[i].document);
  
  if(!x && d.getElementById)
  	x=d.getElementById(n);
  
  return x;
}

function MM_swapImage()
{ //v3.0
  var i,j=0,x;
  var a = MM_swapImage.arguments;
  
  document.MM_sr = new Array;
  
  for(i=0; i < (a.length-2); i+=3)
  {
   if ((x=MM_findObj(a[i]))!=null)
   {
     document.MM_sr[j++]=x;
     
     if(!x.oSrc)
     	x.oSrc=x.src; x.src=a[i+2];
   }
  }
}


function gnl(e,w,n,s)
//getNewLevel
{

	var BBoxEast,BBoxWest,BBoxNorth,BBoxSouth;

	BBoxEast = e;
	BBoxWest = w;
	BBoxNorth = n;
	BBoxSouth = s;

	//adjust zoom level
	var latRatio = (BBoxEast - BBoxWest) * 1.0 / 0.0059008598;
	var lngRatio = (BBoxNorth - BBoxSouth) * 1.0 / 0.0029504299;
	
	var latZoomLevel, lngZoomLevel;
	// for 760*
	if(latRatio<1){
		latZoomLevel = 0;
	}else if(latRatio<2){
		latZoomLevel = 1;
	}else if(latRatio<4){
		latZoomLevel = 2;
	}else if(latRatio<8){
		latZoomLevel = 3;
	}else if(latRatio<16){
		latZoomLevel = 4;
	}else if(latRatio<32){
		latZoomLevel = 5;
	}else if(latRatio<64){
		latZoomLevel = 6;
	}else if(latRatio<128){
		latZoomLevel = 7;
	}else if(latRatio<256){
		latZoomLevel = 8;
	}else if(latRatio<512){
		latZoomLevel = 9;
	}else if(latRatio<1024){
		latZoomLevel = 10;
	}else if(latRatio<2048){
		latZoomLevel = 11;
	}else if(latRatio<4096){
		latZoomLevel = 12;
	}else if(latRatio<8192){
		latZoomLevel = 13;
	}else if(latRatio<16384){
		latZoomLevel = 14;
	}else if(latRatio<32768){
		latZoomLevel = 15;
	}else if(latRatio<65536){
		latZoomLevel = 16;
	}else{
		latZoomLevel = 17;
	}
	
	
	if(lngRatio<1){
		lngZoomLevel = 0;			
	}else if(lngRatio<2){
		lngZoomLevel = 1;			
	}else if(lngRatio<4){
		lngZoomLevel = 2;			
	}else if(lngRatio<8){
		lngZoomLevel = 3;			
	}else if(lngRatio<15.999999){
		lngZoomLevel = 4;			
	}else if(lngRatio<31.999996){
		lngZoomLevel = 5;
	}else if(lngRatio<63.999971){
		lngZoomLevel = 6;			
	}else if(lngRatio<127.99977){
		lngZoomLevel = 7;
	}else if(lngRatio<255.99815){
		lngZoomLevel = 8;			
	}else if(lngRatio<511.98517){
		lngZoomLevel = 9;			
	}else if(lngRatio<1023.8814){
		lngZoomLevel = 10;			
	}else if(lngRatio<2047.0516){
		lngZoomLevel = 11;			
	}else if(lngRatio<4088.4284){
		lngZoomLevel = 12;			
	}else if(lngRatio<8131.9257){
		lngZoomLevel = 13;			
	}else if(lngRatio<15918.630){
		lngZoomLevel = 14;			
	}else if(lngRatio<29455.314){
		lngZoomLevel = 15;			
	}else if(lngRatio<46798.022){
		lngZoomLevel = 16;			
	}else{
		lngZoomLevel = 17;
	}
	
//			alert("lngZoomLevel="+lngZoomLevel+"latZoomLevel="+latZoomLevel);
	
	var newlevel;
	if(lngZoomLevel > latZoomLevel){
		newlevel = lngZoomLevel;
	}else{
		newlevel = latZoomLevel;
	}

	return newlevel;
}