var mapsearch=null;
var mSearch = null;

var hfoverlay;
var bathyLayer;
var n_radar=null;
var n_wwa = null;
var radarInt = null;
var n_goes_vis = null;
var n_goes_ir = null;
var now = new Date;
var n_wbarb=null;
var g_sst = null;
var s_obs = null;

var n_spill24 = null;
var n_spill48 = null;
var n_spill72 = null;
var n_fishclose = null;

var panoLayer = null;
var webcamLayer = null;
var wikiLayer = null;
var youtubeLayer = null;
var side_p_hieght=100;
var n_weather_zoom1=0;
var n_weather_zoom2=0;
var n_weather_zoom3=0;
var n_weather_zoom4=0;



//function for handle advert box.

function advertise_status()
{
	
	 if(earthnc_getUrlVariable('em'))
	 {
		
		show('advert_inner');
	 }
	 else
	 {
		
		 show('advert_inner');
		 
	 }
	
}
function check_advert_status()
{  
   var side_panel_hieght = document.getElementById('side_panel_id').clientHeight;	
   //alert(side_panel_hieght);
   if(document.getElementById('advert').style.display=='none')
   side_panel_hieght = side_panel_hieght-233;
   //alert(side_panel_hieght);
   if(side_panel_hieght>394)
	 show('advert');
    else
     show('advert');	
	
}

//function for manage left panel scrool bar
function check_sidepanel_hieght()
{
   
   var side_panel_h = document.getElementById('side_panel_id').clientHeight;	

  
    if(side_panel_h>665)
   {
      document.getElementById('left_container_id').className="left_container_scrool";
	  document.getElementById('side_panel_id').className="side_panel_scroll";
	   document.getElementById('side_panel_table_id').className="side_panel_scroll_table";
	  document.getElementById('right_section_id').className="right_section_scrool";
	  side_p_hieght = side_panel_h;
   }
	else
	{ 
	   side_p_hieght = side_panel_h;
	   document.getElementById('left_container_id').className="left_container";
	   document.getElementById('right_section_id').className="right_section_new";
	   document.getElementById('side_panel_id').className="side_panel";
	   document.getElementById('side_panel_table_id').className="side_panel_table";
	}
}

//for recenter
function recenter()
{
if (geo) {geo.getCurrentPosition(updatePosition, handleError);}
    else if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {  
    lat = position.coords.latitude;
    lon = position.coords.longitude;
    tlat = lat;
    tlon = lon;
    zoom = 14;
    updatePosition2(tlat,tlon,zoom);
    }); 
    }	
       	else if (google.loader.ClientLocation) {
      zoom = 13;
      lat = google.loader.ClientLocation.latitude;
      lon = google.loader.ClientLocation.longitude;
      tlat = lat;
      tlon = lon;
      map.setCenter(new GLatLng(lat,lon,zoom));
     }
}

//function for load chart from top panel
function earthnc_chartload_1()
{
	 hide('navaids');
	 show('navaids_on');
    /* document.getElementById("showearthnc").checked='true';
	    earthnc_chartload();*/
	 document.getElementById("showmarkers").checked='true'	;
	 enc_showmarkers();
     
}

//function for remove chart from top panel
function earthnc_chartload_2()
{
     show('navaids');
	 hide('navaids_on');
	 /*document.getElementById("showearthnc").checked=''	;
	 earthnc_chartload();*/
	 document.getElementById("showmarkers").checked=''	;
	 enc_showmarkers();
}

//function for hide map
function mydoc_on(gal_type)
{
   if(document.getElementById('map').style.display=='none')
   hide('uc_image');
   else
   hide('map');
   
   hide('chart_gallery_id');
   hide('gallery_id');
   hide('weather_gallery_id');
   
   if(gal_type=='chart')
   show('chart_gallery_id');
   if(gal_type=='place')
   show('gallery_id');
   if(gal_type=='weather')
   show('weather_gallery_id');
  
  //for hide show details window
   hide('more_info');
	
}
//function for display map
function mydoc_off()
{   
   
	check_advert_status();
	check_sidepanel_hieght();
    show('map');
    hide('gallery_id');
	hide('chart_gallery_id')
	hide('weather_gallery_id')
	hide('uc_image');
	
	//for hide show details window
	hide('more_info');
	
}

//function for toppanel toggle
function toggle_up()
{
      hide('action_panel_main_id');
      hide('toggle_up_id');
      show('toggle_down_id');
	   if(earthnc_getUrlVariable('em'))
	  {
	      map_c=document.getElementById('map');
	       map_c.style.height='676px';
 
	  }
	  else
	  {
	    
	  document.getElementById('map').className='full_screen_new';
	  }
	  
}
//function for toppanel toggle  
function toggle_down()
{ 
     show('action_panel_main_id');
      show('toggle_up_id');
      hide('toggle_down_id');
	  if(earthnc_getUrlVariable('em'))
	  {
	      map_c=document.getElementById('map');
	      map_c.style.height='641px';
	  }
	  else
	  {
	       document.getElementById('map').className='full_screen_old';
	  }
	 
    
 }




function check_status()
{
if(document.getElementById("search_dsp").style.display=='none')
if(document.getElementById("chart_dsp").style.display=='none')
if(document.getElementById("place_dsp").style.display=='none')
if(document.getElementById("nearby_dsp").style.display=='none')
{
  show('searchResults');
  //show('search_dsp');
}

}

//function for leftpanel toggle
function toggle_on()
{
    
    document.getElementById('toggle').className='togle_btn';  
	document.getElementById('toggle_up').className='togle_btn_up';
	
	var h = document.getElementById('side_panel_id').clientHeight;
	document.getElementById('panel_td').style.height=h+'px';
	
	hide('side_panel_id');
	hide('tg');
	show('tg_1');
	show('search_top');
	document.getElementById('action_panel_main_id').className='action_panel_main_new';
	document.getElementById('left_container_id').className='left_container_new';
	document.getElementById('right_section_id').className='right_section';
	document.getElementById('right_container_id').className='right_container_new';
	//load('toggle');
	if(getWindowWidth()>650)
	map.checkResize(); // for manage map accroding to the new width and hieght
}

//function for leftpanel toggle
function toggle_off()
{
	
	document.getElementById('toggle').className='togle_btn_new';
	document.getElementById('toggle_up').className='togle_btn_up_new';
	var h1 = document.getElementById('side_panel_id').clientHeight;
	document.getElementById('panel_td').style.height=h1+'px';
	
	if(side_p_hieght<665){
    document.getElementById('left_container_id').className="left_container";
	document.getElementById('right_section_id').className='right_section_new';
	document.getElementById('side_panel_id').className='side_panel';
	}
	else
	{
	  
	  document.getElementById('right_section_id').className='right_section_scrool';
	  document.getElementById('side_panel_id').className="side_panel_scroll";
	  document.getElementById('left_container_id').className="left_container_scrool";
	}
	
	show('side_panel_id');
	show('tg');
	hide('tg_1');
	hide('search_top');
	document.getElementById('action_panel_main_id').className='action_panel_main';
	//document.getElementById('left_container_id').className='left_container';
	//document.getElementById('right_section_id').className='right_section_new';
	document.getElementById('right_container_id').className='right_container';
	
	
}

function reloadExtern(){
    if (n_radar) noaa_radarload();
    if (n_wwa) noaa_wwaload();
    if (n_goes_vis) noaa_goes_vis();
    if (n_goes_ir) noaa_goes_ir();
    if (n_wbarb) noaa_windbarbload();
    if (g_sst) gcoos_sstload();
    if (s_obs) secoora_obsload();
    if (n_spill24) noaa_spill_forcast(1);
    if (n_spill48) noaa_spill_forcast(2);
    if (n_spill72) noaa_spill_forcast(3);
    if (n_fishclose) window["noaa_fish_close"]();
    if (p_marinascom) marinascom_load();
}

function earthnc_hfradarload(id){ 

     
  if (document.getElementById(id).checked)
  { 
   

	if(n_weather_zoom4==0)
	{  
	   if(map.getZoom()>10)
	   {
	      map.setZoom(10);
		  n_weather_zoom4=1;
	   }
	}
      var rtv = new RTVTileLayer();
      hfoverlay = new GTileLayerOverlay(rtv); /** * Whenever tile layer state changes, map needs refreshing. */ 
        GEvent.bind(hfoverlay.getTileLayer(),'statechange',hfoverlay,hfoverlay.refresh); 
        map.addOverlay( hfoverlay );
   }
  else
  { 
  map.removeOverlay(hfoverlay);
  }
}

function noaa_radarload_default(){
	
	document.getElementById("showradar").checked = "true";
    noaa_radarload();	
}
function noaa_radarload_default_1(){  
	//alert(document.getElementById("showradar").checked);
	document.getElementById("showradar").checked = "";
	
	map.removeOverlay(n_radar); n_radar=null; clearInterval(radarInt);
}

function noaa_radarload(){
	
	if (document.getElementById("showradar").checked){  
    if(n_weather_zoom1==0)
	{   
	   if(map.getZoom()>10)
	   {
	     map.setZoom(10);
	     n_weather_zoom1=1;
	   }
	}
earthnc_weatherdsp_top();
      var path = 'http://nowcoast.noaa.gov/wms/com.esri.wms.Esrimap/obs?VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG:4326&WIDTH=512&HEIGHT=512&LAYERS=RAS_RIDGE_NEXRAD&TRANSPARENT=TRUE&FORMAT=image/png';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&BBOX='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (n_radar){map.removeOverlay(n_radar);}      
      n_radar = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(n_radar);
      //add the legend and timestamp
      $.get('php/radarLegend.php',function(data){
        el('radarLegend').innerHTML=data;      
      });
      show('radarLegend');
      //update radar every 5 minutes
      if (!radarInt) radarInt = setInterval("noaa_radarload()",600000);
      }
      else {if (n_radar!=null) hide('radarLegend'); map.removeOverlay(n_radar); n_radar=null; clearInterval(radarInt); earthnc_weatherdsp_top_1();}
	  
}

function noaa_goes_vis() {
  noaa_nowcoastload("showgeos_vis","obs","RAS_GOES","n_goes_vis");
}

function noaa_goes_ir() {
  noaa_nowcoastload("showgeos_ir","obs","RAS_GOES_I4","n_goes_ir");
}

function noaa_wwaload() {
  noaa_nowcoastload("showwwa","wwa","WARN_SHORT_TOR,WARN_SHORT_EWW,WARN_SHORT_SMW,NHC_TRACK_WWLIN","n_wwa");
}

function noaa_spill_forcast(day){
  var url = 'http://events.arcgisonline.com/ArcGIS/services/Gulf_Coast_Oil_Spill_Forecast/MapServer/WMSServer?&STYLES=default,default,default';
  var layers = "";
  if (day==1){layers="9,10,11"; wmsload("showspill24",url,layers,"n_spill24");} 
  if (day==2){layers="5,6,7"; wmsload("showspill48",url,layers,"n_spill48");}
  if (day==3){layers="1,2,3"; wmsload("showspill72",url,layers,"n_spill72");}  
}


function noaa_fish_close(){
  var url = 'http://events.arcgisonline.com/ArcGIS/services/Gulf_Coast_Fishery_Closure/MapServer/WMSServer?STYLES=default';
  var layers = "0";
  wmsload("showfishclose",url,layers,"n_fishclose"); 
}

function noaa_nowcoastload(name,service,layers,moverlay){
if (document.getElementById(name).checked){
	
	if(n_weather_zoom2==0)
	{   
	   if(map.getZoom()>10)
	   {
	     map.setZoom(10);
	     n_weather_zoom2=1;
	   }
	}
      var path = 'http://nowcoast.noaa.gov/wms/com.esri.wms.Esrimap/'+service+'?VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG:4326&WIDTH=512&HEIGHT=512&LAYERS='+layers+'&TRANSPARENT=TRUE&FORMAT=image/png';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&BBOX='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (window[moverlay]){map.removeOverlay(window[moverlay]);}      
      window[moverlay] = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(window[moverlay]);
      }
      else {if (window[moverlay]!=null) map.removeOverlay(window[moverlay]); window[moverlay]=null;}
}

function wmsload(name,url,layers,moverlay){
if (document.getElementById(name).checked){
      var path = url+'&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG:4326&WIDTH=512&HEIGHT=512&LAYERS='+layers+'&TRANSPARENT=TRUE&FORMAT=image/png';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&BBOX='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (window[moverlay]){map.removeOverlay(window[moverlay]);}      
      window[moverlay] = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(window[moverlay]);
      }
      else {if (window[moverlay]!=null) map.removeOverlay(window[moverlay]); window[moverlay]=null;}
}

function gcoos_sstload(){
if (document.getElementById("showgcoossst").checked){
      var path = 'http://megara.tamu.edu:8080/ncWMS/wms?REQUEST=GetMap&VERSION=1.3.0&STYLES=&CRS=CRS:84&WIDTH=256&HEIGHT=256&FORMAT=image/png&TRANSPARENT=TRUE&LAYERS=SeaDAS_SST/seadas_sst';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&BBOX='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (g_sst){map.removeOverlay(g_sst);}      
      g_sst = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(g_sst);
      }
      else {if (g_sst!=null) map.removeOverlay(g_sst); g_sst=null;}
}

function secoora_obsload(){
if (document.getElementById("showsobs").checked){
      var path = 'http://secoora.org/ncogc/mapserv?MAP=%2Fopt%2Fsecoora_ogc%2Fmaps%2Fsecoora_insitu.map&SRS=EPSG%3A4269&FORMAT=image%2Fpng1&LAYERS=wind_obs_hourly_recent,water_level_obs_hourly_recent,surface_currents,wave_obs_hourly_recent,salinity_obs_hourly_recent,bottom_temperature_obs_hourly_recent,air_temperature_obs_hourly_recent,air_pressure_obs_hourly_recent,sst_obs_hourly_recent&TIME_OFFSET_HOURS=2&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&WIDTH=400&HEIGHT=400&TRANSPARENT=TRUE';
      //var path= 'http://129.116.104.172/arcgis/services/TxHIS/TCOON_Sites/MapServer/WMSServer?request=getMap&styles=&version=1.3&width=400&height=400&crs=EPSG:4326&layers=0';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&bbox='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (s_obs){map.removeOverlay(s_obs);}      
      s_obs = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(s_obs);
      }
      else {if (s_obs!=null) map.removeOverlay(s_obs); s_obs=null;}
}




function noaa_windbarbload(){
if (document.getElementById("showwindb").checked){
if(n_weather_zoom3==0)
	{   
	   if(map.getZoom()>10)
	   {
	      map.setZoom(10);
	      n_weather_zoom3=1;
	   }
	}
	
      var path = 'http://nowcoast.noaa.gov:80/wms/com.esri.wms.Esrimap/analyses?VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG:4326&WIDTH=512&HEIGHT=512&LAYERS=RTMA_RAS_WSPD,us_canada_back,us_states_gen,world_countries,RTMA_PT_WIND_01,RTMA_PT_WIND_05,RTMA_PT_WIND_10,RTMA_PT_WIND_15&TRANSPARENT=TRUE&FORMAT=image/png';
      var mapbounds = map.getBounds();
      var sw = mapbounds.getSouthWest(); var ne = mapbounds.getNorthEast();
      var bbox = '&BBOX='+sw.lng()+','+sw.lat()+','+ne.lng()+','+ne.lat();
      if (n_wbarb){map.removeOverlay(n_wbarb);}      
      n_wbarb = new GGroundOverlay(path+bbox,mapbounds);
      map.addOverlay(n_wbarb);
      $.get('php/obswindLegend.php',function(data){
        el('obswindLegend').innerHTML=data;      
      });
      show('obswindLegend');
      }
      else {if (n_wbarb!=null) map.removeOverlay(n_wbarb); n_wbarb=null; hide('obswindLegend');}
}

function panoload(){
if (panoLayer) {
      if (el('showpano').checked==false) {
        panoLayer.hide();
      } else {
        panoLayer.show();
      }
    } else {
      panoLayer = new GLayer("com.panoramio.all");
      map.addOverlay(panoLayer);
    }
}

function webcamload(){
if (webcamLayer) {
      if (el('showwebcam').checked==false) {
        webcamLayer.hide();
      } else {
        webcamLayer.show();
      }
    } else {
      webcamLayer = new GLayer("com.google.webcams");
      map.addOverlay(webcamLayer);
    }
}

function wikiload(){
if (wikiLayer) {
      if (el('showwiki').checked==false) {
        wikiLayer.hide();
      } else {
        wikiLayer.show();
      }
    } else { 
      wikiLayer = new GLayer("org.wikipedia.en");
      map.addOverlay(wikiLayer);
    }
}

function youtubeload(){
if (youtubeLayer) {
      if (el('showyoutube').checked==false) {
        youtubeLayer.hide();
      } else {
        youtubeLayer.show();
      }
    } else {
      youtubeLayer = new GLayer("com.youtube.all");
      map.addOverlay(youtubeLayer);
    }
}

function earthnc_bathyload(){
if (bathyLayer) {
      if (el('showbathy').checked==false) {
        bathyLayer.hide();
      } else {
        bathyLayer.show();
      }
    } else {
    var mapMinZoom = 2;
    var mapMaxZoom = 19;
    var tilelayer = new GTileLayer(GCopyrightCollection(''), mapMinZoom, mapMaxZoom);
			          var mercator = new GMercatorProjection(mapMaxZoom+1);
			          tilelayer.getTileUrl = function(tile,zoom) {
			              if ((zoom < mapMinZoom) || (zoom > mapMaxZoom)) {
			                  return "http://earthnc.info/images/blank.png";
			              } 
			              var ymax = 1 << zoom;
			              var y = ymax - tile.y -1;
	                  return "http://seamlessbathy.s3.amazonaws.com/"+zoom+"/"+tile.x+"/"+y+".png";
			          }
			          // IE 7-: support for PNG alpha channel
			          // Unfortunately, the opacity for whole overlay is then not changeable, either or...
			          tilelayer.isPng = function() { return true;};
			          tilelayer.getOpacity = function() { return .6; }

			          bathyLayer = new GTileLayerOverlay( tilelayer );
			          map.addOverlay(bathyLayer);
		}
}

// try this (add http:// if needed)
function getHost() {
	
	var window_width= getWindowWidth();
    if(earthnc_getUrlVariable('em'))
	{
	  // if(window_width>585)
        show('full_screen');
	}
    else
       hide('full_screen');
}

function bookmark_new()
{
	var bookmarkUrl = bookmark();
	window.open(bookmarkUrl);
}

function Display_actionBar() 
{ 
  show('action_panel')	;
}
function Hide_actionBar()
{ 
  hide('action_panel')	;
}



//function for top panel chart
function earthnc_chartdsp_top()
{
	hide('chart_btn_id');
    show('chart_btn_on_id');
    loadraster();
	
}
function earthnc_chartdsp_top_1()
{
  show('chart_btn_id');
  hide('chart_btn_on_id');
  removeLayer();
	
}
//end

//function for left panel chart
function earthnc_chartdsp(){  
  //close_menus();
  
  show("chart_dsp");
  hide("link_char_open");
  show("link_char_close");
  

}
function earthnc_chartdsp_close(){  
  //close_menus();
  hide("chart_dsp");
  show("link_char_open");
  hide("link_char_close");
 
  

}
//end

//newly created
//function for left panel search
function earthnc_search(){   
 // close_menus();
 
  show("search_dsp");
 
  hide("link_search_open");
  show("link_search_close");
 
}
function earthnc_search_close(){   
  //close_menus();
  hide("search_dsp");
  show("link_search_open");
  hide("link_search_close");
}

//function for top panel  place
function earthnc_placedsp_top(){ 
	hide('place_btn_id');
    show('place_btn_on_id');
    document.getElementById("showmarinalifemarinas").checked=true;
	earthnc_marinalifeload();
  
}
function earthnc_placedsp_top_1(){
	show('place_btn_id');
    hide('place_btn_on_id');
    document.getElementById("showmarinalifemarinas").checked=false;
	earthnc_marinalifeload();
  
}

//function for left panel place
function earthnc_placedsp(){
  //close_menus();
  show("place_dsp");
  hide("link_place_open");
  show("link_place_close");
}
function earthnc_placedsp_close(){
  //close_menus();
  hide("place_dsp");
  show("link_place_open");
  hide("link_place_close");
}

//function for top panel weather
function earthnc_weatherdsp_top(){
  show('weather_btn_on_id');
	hide('weather_btn_id');
}
function earthnc_weatherdsp_top_1(){
  hide('weather_btn_on_id');
  show('weather_btn_id');
}

//function for left panel weather
function earthnc_weatherdsp(){
  //close_menus();
  show("nearby_dsp");
  hide("link_weather_open");
  show("link_weather_close");
   
}

function earthnc_weatherdsp_close(){
  //close_menus();
 hide("nearby_dsp");
  show("link_weather_open");
  hide("link_weather_close");
}

//function for left panel share
function earthnc_sharedsp(){
 // updateLinks();
   show('share_btn_on_id');
  hide('share_btn_id');
  show("share_dsp");
  hide("link_share_open");
  show("link_share_close");
 }
function earthnc_sharedsp_close(){
   // updateLinks();
  hide('share_btn_on_id');
  show('share_btn_id');
  hide("share_dsp");
  show("link_share_open");
  hide("link_share_close");

}


function earthnc_transdsp(){
  close_menus();
  show("trans_dsp");
}
function earthnc_infodsp(){ 
  close_menus();
  show("info_dsp");
}


function parent_glry_popup()
{
	 show("more_info");
}
function earthnc_helpdsp(){
  //close_menus();
  hide('help_btn_id');
  show('help_btn_on_id');
 show("help_dsp");
}
function earthnc_helpdsp_close(){
  //close_menus();
  show('help_btn_id');
  hide('help_btn_on_id');
   hide("help_dsp");
}


function earthnc_fullscreen(){
	document.getElementById("full_screen").className="";
     document.getElementById("close_screen").className="action_icon2";
	hide('header');hide('tnb');hide('footer');
	show('close_screen');hide('full_screen');
	//for ifrme
	//show('full_view');hide('normal_view');
	 document.getElementById("fullscreen_popup").className="container_fullscreen";
	//alert(document.getElementById('map').style.height);
	//document.getElementById('map').style.height='600px';
	resize();
	
  
}

function earthnc_closescreen(){  
	document.getElementById("full_screen").className="action_icon2";
	document.getElementById("close_screen").className="";
    show('header');show('tnb');show('footer');
	hide('close_screen');show('full_screen');
	//for ifrme
	//hide('full_view');show('normal_view');
	document.getElementById("fullscreen_popup").className="container";
	//alert(document.getElementById('map').style.height);
	document.getElementById('map').style.height='510px';
    
}

function close_menus(){ 
//mapsearchhide();



hide('chart_dsp');
hide('place_dsp');
hide('nearby_dsp');
hide('routemaker');
hide('share_dsp');
//hide('searchResults');
hide('nearby_menu');
hide('trans_dsp');
hide('info_dsp');
hide('help_dsp');
hide('search_dsp');

  
  document.getElementById("link_share").className="action_icon1";
  document.getElementById("link_route").className="action_icon";
  
  //later changes
  show("link_char_open");
  hide("link_char_close");
  
  show("link_place_open");
  hide("link_place_close");
  
  show("link_weather_open");
  hide("link_weather_close");
  
  show("link_search_open");
  hide("link_search_close");
  
  show("link_route_open");
  hide("link_route_close")
  
  
   
}

function searchChart(q,l,d,m,sc){
  //close_menus();
 
  //edited
  var sta_1 =   document.getElementById('tg').style.display;
  if(sta_1=='none')
  var search_text_var = "searchText_top";
  if(sta_1=='block' || sta_1=='')
  var search_text_var = "searchText";
 
  //end
  
  el('searchResults').innerHTML ='';
  removeMarkers('gsearch');
  if (!q) q=(el(search_text_var).value);
  if (!d) d=5;
  if (!l) l=10;
  if (!sc) sc=5;
  var latlon = map.getCenter().lat()+','+map.getCenter().lng();
  var type='all';
  var ttype = q.split(':');
  if (ttype.length>1){
    type = ttype[0];
    q = ttype[1];
  } else {
  type = 'google';
  }
  if (type!='google'){ 
  q = encodeURIComponent(q);       
  $.getJSON("./php/search.php",{ll: latlon, d: 5, limit:10, sc: sc, q: q,type: type, ajax: 'true'}, function(j){
  var results = '';
  if (!m){
  if (j.count>0){
  if (j.results[0].dist<15) map.setCenter(new GLatLng(j.results[0].latitude,j.results[0].longitude));  
  el('searchResults').innerHTML = '<table width=100% border="0" class="search_result_header"><tr><td><a href="javascript:hide(\'searchResults\');removeMarkers(\'gsearch\');uncheck_weather();"><b>Clear Search Results</b>  <img src=\"images/btn_close.png\" /></a><br /><b>Showing EarthNC Search Results</b><br /></td></tr></table><br />';
  for (var i = 0; i < j.results.length; i++) {
  //create list entry
    var tmpIcon = new GIcon(G_DEFAULT_ICON);
    tmpIcon.image = 'http://maps.google.com/mapfiles/kml/paddle/'+(i+1)+'.png';
    tmpIcon.iconSize = new GSize(24,24);
    var tmp = document.createElement('div');
    tmp.setAttribute('id',j.results[i].type+'-'+j.results[i].id);
    tmp.setAttribute('lat',j.results[i].latitude);
    tmp.setAttribute('lon',j.results[i].longitude);
    tmp.setAttribute('icon',j.results[i].icon);
    tmp.setAttribute('sname',j.results[i].name);
    tmp.setAttribute('class','esResult');
	  tmp.innerHTML = '<div class="esRTitle"><img class="sicon" src="'+tmpIcon.image+'" height="24" /><img class="sicon" src="http://earthnc.com/files/icons/'+j.results[i].icon+'" height="16" />'+j.results[i].name+' '+j.results[i].dist+'nm @ '+j.results[i].heading+'&deg;</div>' + 
                     '<div class="esRMenu"><a href="javascript:searchDetail(\''+j.results[i].type+'-'+j.results[i].id+'\'); setPosition(\''+j.results[i].type+'-'+j.results[i].id+'\',\'result\');searchGoto(\''+j.results[i].type+'-'+j.results[i].id+'\')">More Info</a></div>';
    el('searchResults').appendChild(tmp);
  
    //create marker
    
    var tmpmarkerOptions = { icon:tmpIcon };
    mSearch = new GMarker(new GLatLng(j.results[i].latitude,j.results[i].longitude),tmpmarkerOptions);
    map.addOverlay(mSearch);
    window['gsearch'][j.results[i].type+'-'+j.results[i].id]= mSearch;
    var sname = j.results[i].name;
    var id = j.results[i].type+'-'+j.results[i].id;
    var icon = j.results[i].icon;
    mSearch.tname = '<div class="esDTitle"><img class="sicon" src="http://earthnc.com/files/icons/'+icon+'" />'+sname+'</div><div class="esLatLon">Lat/Lon: '+formatll(j.results[i].latitude,j.results[i].longitude,'dm')+
    '</div><div class="esRMenu"><a href="javascript:searchDetail(\''+id+'\');setPosition(\''+id+'\',\'mark\');">More Info</a></div><a href="javascript:removeMarkers(\'gsearch\');" >Clear Search Results</a>';
  }
  }
 else {
el('searchResults').innerHTML = '<a href="javascript:hide(\'searchResults\')"><img src=\"images/btn_close.png\" /></a> - No Results, please try again from a search.';
}
//$("#searchResults").html(results);
show("searchResults");
} 
});
} else {
//run google local search instead
    searchControl.execute(q);
    tmp ="<table border=0 width='100%'><tr><td><b>Showing Google Search Results<br /><a href=\"javascript:searchChart('all:"+el('searchText').value+"')\">Click for EarthNC Chart Search</a></b><br /></td><td style='padding:5px;' valign='top'><div><a href='javascript:removeMarkers(\"gsearch\");hide(\"searchResults\")'><img src=\"images/btn_close.png\" /></a><br /><br /></div></td></tr></table>";
    el('searchResults').innerHTML=tmp;
    show('searchResults');
}

}   



function searchDetail(did){
  var tdiv = el(did);
  var type = tdiv.getAttribute('id');//type is the id for search div
  var sname = tdiv.getAttribute('sname');
  
  var lat = tdiv.getAttribute('lat');
  var lon = tdiv.getAttribute('lon');
  type = type.split('-');
  if (sname=="") sname=type[0];
  if (sname=="null") sname="Google Search";
  var icon = tdiv.getAttribute('icon');
  name = '<div class="esDTitle">'+sname+'</div><div class="esLatLon">Lat/Lon: '+formatll(lat,lon,'dm')+'</div>';
  var img = '<img class="micon" src="http://earthnc.com/files/icons/'+icon+'" />';
  var adspace = '<div id="windowAd"><a href="http://click.linksynergy.com/fs-bin/stat?id=WdYiP0jzO7M&offerid=146261&type=3&subid=0&tmpid=1826&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus%252Fapp%252Fmarine-charts%252Fid386584429%253Fmt%253D8%2526uo%253D4%2526partnerId%253D30&url=encmap" target="_blank"><img src="http://earthnc.info/cvp/images/earthnc_mobile_ad.png" style="width:234px;height:60px;" /></a>';
  
  if (type[0]!='google'){
  $.get('./php/'+type[0]+'.php?id='+type[1],function(data){
 var html = name+data;
 window["gsearch"][did].openInfoWindow('<div class="popUp">'+name+img+data+adspace+'</div>');
});}  else {
 window["gsearch"][did].openInfoWindow('<div class="popUp">'+name+img+tdiv.google+adspace+'</div>');

 }
}

// made by me
function setPosition(ob,type)
{
	if(type!='mark')
	{
		obj = document.getElementById(ob)
		var topValue= 0,leftValue= 0;
		while(obj){
		leftValue+= obj.offsetLeft;
		topValue+= obj.offsetTop;
		obj= obj.offsetParent;
		}
		finalvalue = leftValue + "," + topValue;
		//alert(finalvalue);
		//alert(topValue);
		//For Full Screen
		if(document.getElementById('full_screen').style.display=='none')
		topValue=topValue-51;
		//For Normal  Screen
		if(document.getElementById('full_screen').style.display=='' || document.getElementById('full_screen').style.display=='block')
		topValue=topValue-166;
	   
		document.getElementById('nearby_menu').style.top=topValue+'px';
		document.getElementById('nearby_menu').style.padding='12px';
	}
	else
	{
		document.getElementById('nearby_menu').style.top='23px';
		document.getElementById('nearby_menu').style.padding='12px';
	}
}
//end

function searchGoto(did){
  var tdiv = el(did); 
  var z=map.getZoom();
  if (z<14) z=14;  
  var lat = parseFloat(tdiv.getAttribute('lat'));
  var lon = parseFloat(tdiv.getAttribute('lon'));                           
  map.setCenter(new GLatLng(lat,lon),z);
}

function mapsearchshow(){
// set up pins, use the metalset
          var pins = new Array();
          pins["kml"] = "metalblue";
          pins["local"] = "metalred";

          var labels = new Array();
          labels["kml"] = "metalblue";
          labels["local"] = "metalred";
    
          var options = {
            //listingTypes : google.elements.localSearch.TYPE_BLENDED_RESULTS,
            client : 'pub-7720029083570370',
            channel : '6820387357',
            searchFormHint : 'Search Google',
            Xpins : pins,
            Xlabels : labels
          }
        var bottomLeft= new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(370, 5));
        mapsearch = new google.elements.LocalSearch(options);
        map.addControl(mapsearch,bottomLeft); 
}

function mapsearchhide(){
  map.removeControl(mapsearch);
}

function tweetThis(){ 
var twtUrl = getMapUrl();
var twtTitle  = "EarthNC Chart Link";
var maxLength = 140 - (twtUrl.length + 1);
if (twtTitle.length > maxLength) {
twtTitle = twtTitle.substr(0, (maxLength - 3))+'...';
}
var twtLink = 'http://twitter.com/home?status='+encodeURIComponent(twtTitle + ' ' + twtUrl);
window.open(twtLink);
}

function faceBook(){
var twtUrl = getMapUrl();
var twtTitle  = "EarthNC Chart Link";
var twtLink = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(twtUrl)+'&t=' + encodeURIComponent(twtTitle);

window.open(twtLink);
}

function getMapUrl(){
 var baseUrl = 'http://earthnc.com/dev/chartviewer/?';
 // var baseUrl = 'http://earthnc.rave-staging.com/chartviewer_new/?';
  var lat = map.getCenter().lat();
  var lon = map.getCenter().lng();
  var zoom = map.getZoom();
  return baseUrl + 'll='+lat+','+lon+'&z='+zoom;
}

function updateLinks(){
  bookmark();
  getembedcode();
}

function bookmark(){
var url = document.location.href;
url = url.split("?");
var params = '';
var cent = map.getCenter();
params += '?ll=' + cent.lat() +','+cent.lng();
params += '&z=' + map.getZoom();
  if (earthnc_getUrlVariable('url')){
    params += '&url=' + earthnc_getUrlVariable('url'); 
  }
  if (rastermaploaded==1){
  var tmp  = rasterloadednum.split('.');
  params += '&chart='+tmp[0];
  }

document.getElementById('bmark').value=url[0]+params;
//selectall('bmark');
return url[0]+params;
}

function getembedcode(){
var url = document.location.href;
var parent_url;
url = url.split("?");
var params = '';
var cent = map.getCenter();
params += '?em=1&ll=' + cent.lat() +','+cent.lng();
params += '&z=' + map.getZoom();
if (rastermaploaded==1){
  var tmp  = rasterloadednum.split('.');
  params += '&chart='+tmp[0];
  }

//document.getElementById('embedc').value='<iframe src="'+url[0]+params+'" width="100%" height="600px"></iframe>';

/*document.getElementById('embedc').value="<script language='javascript' type='text/javascript'>var parent_url = document.URL;var crop_url_length = (parent_url.indexOf(\"://\"));var referal_woh = parent_url.substr((crop_url_length+3));if(parent_url.substr(0,5) == 'https')	referal = 'S_'+referal_woh;else	referal = 'H_'+referal_woh;document.write(\"<iframe src='"+url[0]+params+"&referal=\"+parent_url+\"' width='100%' height='600px'\")</script>";*/

document.getElementById('embedc').value="<script language='javascript' type='text/javascript'>var parent_url = document.URL;var crop_url_length = (parent_url.indexOf(\"://\"));var referal_woh = parent_url.substr((crop_url_length+3));if(parent_url.substr(0,5) == 'https')	referal = parent_url.substr(6);else	referal = parent_url.substr(7);document.write(\"<iframe src='"+url[0]+params+"&referal=\"+referal+\"' width='100%' height='600px'\")</script>";

//selectall('embedc');
}

//close place content
function close_place()
{
	//hide oil content
	hide('oil_content');hide('Oil2');show('Oil1');
	document.getElementById('showspill24').checked='';noaa_spill_forcast(1);
    document.getElementById('showspill48').checked='';noaa_spill_forcast(2);
    document.getElementById('showfishclose').checked='';noaa_fish_close();   
   if(hfoverlay){ document.getElementById('showhfradar').checked='';earthnc_hfradarload('showhfradar');}
	
	
	//hide google content
	hide('genral_content');hide('genral_content2');show('genral_content1');
	if (panoLayer) {document.getElementById('showpano').checked='';panoload();}
   if (webcamLayer) { document.getElementById('showwebcam').checked='';webcamload();}
   if (youtubeLayer){ document.getElementById('showyoutube').checked='';youtubeload(); }  
   if (wikiLayer){ document.getElementById('showwiki').checked='';wikiload();}  
	
	//hide omarinalife content
	hide('marinalife');show('mar1');hide('mar2');
	 document.getElementById('showmarinalifemarinas').checked='';earthnc_marinalifeload();
	
	//hide cruisersnet content
	hide('cruisersnet');show('cruis1');hide('cruis2');
	document.getElementById('showcnetmarinas').checked='';cnet_marinaload();
	document.getElementById('showcnetanchorages').checked='';cnet_anchorageload();
	document.getElementById('showcnetbridges').checked='';cnet_bridgeload();
	
	//hide waterway content
	hide('waterway');show('waterway1');hide('waterway2');
	document.getElementById('showwwgmarina').checked='';wwg_marinaload();
    document.getElementById('showwwganchorage').checked='';wwg_anchorageload();
    document.getElementById('showwwgbridge').checked='';wwg_bridgeload();
	
	//hide marinascom content
	hide('marinascom');show('marinascom1');hide('marinascom2');
	document.getElementById('showmarinascom').checked='';marinascom_load();
	
}
function close_weather()
{
	 hide('nearby_w');hide('nearby_w2');show('nearby_w1');
	 hide('searchResults');removeMarkers('gsearch');
	 document.getElementById('metar').checked='';
	 document.getElementById('wxbuoys').checked='';
	 document.getElementById('forecast').checked='';
	 document.getElementById('tides').checked='';
			 
	 hide('regional_w');hide('regional_w2');show('regional_w1');
	 document.getElementById('showsobs').checked='';
	 
	 hide('national_w');hide('national_w2');show('national_w1');
	 
	if(n_radar!=null){
	 document.getElementById("showradar").checked = "";hide('radarLegend');
	 map.removeOverlay(n_radar); n_radar=null; clearInterval(radarInt);
	}
	 document.getElementById('showwwa').checked='';noaa_wwaload();
	 document.getElementById('showgeos_vis').checked='';noaa_goes_vis();
	 document.getElementById('showgeos_ir').checked='';noaa_goes_ir();
	 document.getElementById('showwindb').checked='';noaa_windbarbload();
	 if(hfoverlay){document.getElementById('showhfradar1').checked='';earthnc_hfradarload('showhfradar');}
			  
			  
	
}
function uncheck_weather()
{
	document.getElementById('wxbuoys').checked='';
	document.getElementById('metar').checked='';
	document.getElementById('forecast').checked='';
	document.getElementById('tides').checked='';
}
function parent_gallery(type)
{
	toggle_off();
	mydoc_off();
	map.checkResize();//for dynamic set , map width according to the div
	close_place();//for enabling one partner, disables the rest
	close_weather();//for enabling one partner, disables the rest
	switch(type)
	{
	   //Place comtent
	   case 2:earthnc_placedsp();
	          partnerload('marinalife');show('mar2');hide('mar1');
			  document.getElementById('showmarinalifemarinas').checked='true';earthnc_marinalifeload();
	          break;
			  
	   case 1:earthnc_placedsp();
	          partnerload('cruisersnet');show('cruis2');hide('cruis1');
			  document.getElementById('showcnetmarinas').checked='true';cnet_marinaload();
			  document.getElementById('showcnetanchorages').checked='true';cnet_anchorageload();
			  document.getElementById('showcnetbridges').checked='true';cnet_bridgeload();
			  break;
			  
	  case 10:earthnc_placedsp();
	          show('oil_content');show('Oil2');hide('Oil1');
	          document.getElementById('showspill24').checked='true';noaa_spill_forcast(1);
			  document.getElementById('showspill48').checked='true';noaa_spill_forcast(2);
			  document.getElementById('showfishclose').checked='true';noaa_fish_close();   
			  document.getElementById('showhfradar').checked='true';earthnc_hfradarload('showhfradar');   
	          break;
			  
	  case 11:earthnc_placedsp();
	          show('genral_content');show('genral_content2');hide('genral_content1');
	          document.getElementById('showpano').checked='true';panoload();
			  document.getElementById('showwebcam').checked='true';webcamload();
			  document.getElementById('showyoutube').checked='true';youtubeload();   
			  document.getElementById('showwiki').checked='true';wikiload();   
	          break	 ;
			  
	  case 3:earthnc_placedsp();
	        partnerload('marinascom');show('marinascom2');hide('marinascom1');
			 document.getElementById('showmarinascom').checked='true';marinascom_load();
			 break;
			 
		//weather contant;	  
	  case 4:earthnc_weatherdsp();
	         show('nearby_w');show('nearby_w2');hide('nearby_w1');
			  document.getElementById('metar').checked='true';searchChart('metar:');
			 break;
			 
	 case 12:earthnc_weatherdsp();
	         show('nearby_w');show('nearby_w2');hide('nearby_w1');
			  document.getElementById('forecast').checked='true';searchChart('forecast:');
			 break;
			 
	 case 5:earthnc_weatherdsp();
	         show('nearby_w');show('nearby_w2');hide('nearby_w1');
			  document.getElementById('wxbuoys').checked='true';searchChart('wxbuoys:');
			 break;
			 
	 case 14:earthnc_weatherdsp();
	         show('national_w');show('national_w2');hide('national_w1');
			 document.getElementById('showgeos_vis').checked='true';noaa_goes_vis();
			 break;
			 
	 case 13:earthnc_weatherdsp();
	         show('national_w');show('national_w2');hide('national_w1');
			 document.getElementById('showradar').checked='true';noaa_radarload();
			 
			 break;
	
	 case 9:  earthnc_placedsp();
	          partnerload('waterway');show('waterway2');hide('waterway1');
			  document.getElementById('showwwgmarina').checked='true';wwg_marinaload();
			  document.getElementById('showwwganchorage').checked='true';wwg_anchorageload();
			  document.getElementById('showwwgbridge').checked='true';wwg_bridgeload();
			  break;	
	  case 6:  earthnc_chartdsp();
	          document.getElementById('showmarkers').checked='true';enc_showmarkers();
			  //document.getElementById('showcnetanchorages').checked='true';cnet_anchorageload();
			  //document.getElementById('showcnetbridges').checked='true';cnet_bridgeload();
			  break;
	 case 7:  map.setMapType(rastermap);
			  break;
	 case 8:  map.setMapType(MYTOPO_MAP);
			  break;		  
		
		
	}
	
}

