/*-----------------------------------------------------------------------#
#  IDV Solutions Enterprise Feature Server                               #
#  Copyright (C) 2006 IDV Solutions                                      #
#                                                                        #
#  filename: IdvSidePanelWriter.js                                       #
#  created : 10.25.2006                                                  #
#  modified: 01.03.2007                                                  #
#-----------------------------------------------------------------------*/

// make sure IDV namespace exists
if(typeof idv == "undefined") { var idv = new Object(); }


// Class for interaction with IDV Solutions basic side panel component
idv.SidePanel = IdvCreateClass(idv.FlashMovie, {

// Obtains reference to main map viewer
getMainMap: function()
{
   return idv.MapInstance;
},

// Removes a vector source from the map viewer
removeVectorSource: function(name)
{
   this.getMainMap().removeVectorSource(name);
},

// Adds a new vector source to the main map viewer
addVectorSource: function(options, format, isDynamic, url, layers)
{
   this.getMainMap().addVectorSource(
      new idv.ServerVectorSource(options, format, isDynamic, url, layers)
   );
},

// Sets URL for a vector source of main map viewer
setVectorSourceUrl: function(source, url)
{
   this.getMainMap().getVectorSource(source).setRequest(url);
},

// Sets URL for a vector source of main map viewer
setVectorLayerOrder: function(layers)
{
  this.getMainMap().setVectorLayerOrder(layers);
},

// Removes a raster source from the main map viewer
removeRasterSource: function(name)
{
   this.getMainMap().removeRasterSource(name);
},

// Adds a new raster source to the main map viewer
addRasterSource: function(options, isDynamic, url, isTiled)
{
   var src;


   // create the raster source object
   if(isTiled) {
      src = new idv.TiledRasterSource(options, url);
   } else {
      src = new idv.SingleRasterSource(options, isDynamic, url);
   }

   // add the new source to the map
   this.getMainMap().addRasterSource(src);
},

// Sets the URL for a raster source on the main map viewer
setRasterSourceUrl: function(source, url, bound)
{
   this.getMainMap().getRasterSource(source).setRequest(url, bound);
},

// Sets the alpha value for a raster source in main map viewer
setRasterSourceAlpha: function(source, alpha)
{
   this.getMainMap().getRasterSource(source).setAlpha(alpha);
},

// Removes a find data source from the main map viewer
removeFindSource: function(source)
{
    this.getMainMap().getFindSource(source).removeFindSource(source);
},

// Adds a new find data source to the main map viewer
addFindSource: function(options, url)
{
   this.getMainMap().addFindSource(new idv.FindSource(options, url));
},

// Go directly to a particular find result
gotoFindResult: function(result)
{
    this.getMainMap().gotoFindResult(result);
},

// Sets the find results for a particular find source
setFindResults: function(source, results)
{
   this.getMainMap().getFindSource(source).setResults(source, results);
},

//Sets the find url for a particular find source
setFindUrl: function(source, url)
{
   this.getMainMap().getFindSource(source).setUrl(source, url);
},

// Moves a vector layer above another vector layer
moveVectorLayerAbove: function(layerName, afterName)
{
   this.getMainMap().moveVectorLayerAbove(layerName, afterName);
},

// Moves a raster layer above another raster layer
moveRasterLayerAbove: function(layerName, aboveName)
{
   this.getMainMap().moveRasterLayerAbove(layerName, aboveName);
},

// Instructs the main map viewer to navigate to a point
gotoPoint: function(cx, cy, level, animate)
{
   this.getMainMap().gotoPoint(cx, cy, level, animate);
},

// Instructs the main map viewer to navigate to an extent
gotoExtent: function(north, east, south, west, animate)
{
   this.getMainMap().gotoExtent(north, east, south, west, animate);
},

// Instructs the main map viewer to show an extent
highlightBoundary: function(north, east, south, west)
{
   this.getMainMap().highlightBoundary(north, east, south, west);
},

// Adds a map event listener; evtname is name of event
// to listen for; method is reference of method to call
// and target is reference of object to invoke method on
addMapListener: function(evtname, target, method)
{
   this.getMainMap().addListener(evtname, target, method);
},

// Removes a navigation event listener from the main map viewer
removeListener: function(evtname, target, method)
{
   this.getMainMap().removeListener(evtname, target, method);
},

// Sets the base URL to use for skin elements
setSkinBaseUrl: function(value)
{
   this.mImagePath = value;
},

//Called when the map has been loaded
mapViewerLoaded: function(mapInst)
{
   //Adds a feature listener to the map viewer
   mapInst.addListener("VectorClicked", this, this.onVectorClick);
   mapInst.addListener("NavigateBegin", this, this.onNavigateBegin);
   mapInst.addListener("NavigateEnd",   this, this.onNavigateEnd);

   // call ActionScript to init side panel
   this.invokeASMethod("initialize");
},

//Called by Map
onNavigateBegin: function(vEvent,vParams)
{
   this.invokeASMethod("onNavigateBegin",vParams);
},

//Called by Map
onNavigateEnd: function(vEvent,vParams)
{
   this.invokeASMethod("onNavigateEnd",vParams);
},

//Called by Map
onVectorClick: function(vEvent,vParams)
{
   var pointArray=new Array();
   var pointCounter=0;
   for(var i=0;i<vParams.length;i++)
   {
      if(vParams[i].data!=null && vParams[i].data!=undefined)
      {
         var pLink=vParams[i].data.link;
         var pTitle=vParams[i].data.title;
         var pDescription=vParams[i].data.description;
         if((pLink != "" && pLink != undefined && pLink!=null)
         ||(pTitle != "" && pTitle != undefined && pTitle!=null)
         ||(pDescription != "" && pDescription != undefined && pDescription!=null))
         {
            pointArray[pointCounter++]=vParams[i];
         }
      }
   }
   this.invokeASMethod("onVectorClick",pointArray);
},

//Brings the details tab to the front and sets the src attribute of an iframe
//Called from the Flash layer control
generateFeatureContent: function(attrs)
{
    //Gets the width of the html panel
   var iWidth=document.getElementById(this.mHtmlPanelID).style.width;
   
   //Sets the html panel iframe's src attribute to external link
   document.getElementById(this.mDetailsID).innerHTML	= (
      '<iframe src="'+attrs.data.link+
      '" style="BORDER-STYLE:none; height:100%; width:'+
      iWidth+'; top:0px; scroll:auto"></iframe>'
   );
   
   //Displays the html panel
   this.showHtml();	
},

//Brings the details tab to the front and displays a point's link
//Called from the Flash layer control
generateDefaultContent: function()
{	
   
   ////Sets the html panel contents to diplay a link to an external site
   document.getElementById(this.mDetailsID).innerHTML = this.mDefaultHtml;
   
   //Displays the html panel
   //this.showHtml();	
},

//Brings the details tab to the front and displays title, description and links for multiple points
//Called from the Flash layer control
generateFeatureLinks: function(points)
{	
   var tableStart='<table style="WIDTH:100%; vertical-align:top;">';
   var tableEnd='</table>';
   var tableContent='';

   document.getElementById(this.mDetailsID).innerHTML ="";
   
   for(var i=0; i < points.length; i++) {
   	  var titleTag;
   	  if(points[i].data.link==undefined || points[i].data.link==null)
	  {
	  	titleTag='<a style="text-decoration:none; font-family:arial; font-size:12pt; font-weight:bold; color: #336699;">'+
                 points[i].data.title+'</a>';
	  }
	  else
	  {
	  	titleTag='<a href="'+points[i].data.link+
                 '" target="_blank" style="text-decoration:none; font-family:arial; font-size:12pt; font-weight:bold; color: #336699;">'+
                 points[i].data.title+'</a>';	  	
	  }
      
      tableContent += (
         '<tr style="vertical-align:top;">'+
         '<td style="font-family:arial; font-size:10pt; color: #333333;border-bottom:solid 1px #336699;">'+
         titleTag+
         '</td></tr>'+
         '<tr style="vertical-align:top;">'+
         '<td style="font-family:arial; font-size:10pt; color: #333333;">'+
         points[i].data.description+'</td>'+
         '</tr>'
      );

      if(i < (points.length - 1)) {
         tableContent +='<tr style="vertical-align:top; height:20px;"><td></td></tr>';
      }
   }

   ////Sets the html panel contents to diplay a link to an external site
   document.getElementById(this.mDetailsID).innerHTML = tableStart+tableContent+tableEnd;

   //Displays the html panel
   this.showHtml();	
},

// Writes out the HTML to embed this movie
// into the current position in the document
writeEmbedHtml: function()
{
   // set script access and window mode
   this.setScriptAccess("always");
   this.setWindowMode("opaque");

   // write out the header
   document.write(
      '<div id="'+this.mPanelID+
      '" style="padding-left: 0px; padding-right: 0px; padding-bottom: 0px; padding-top: 0px; margin: 0px; position:relative; left:0; top:0 width:'+
      this.mWidth+'; height:'+this.mHeight+'">'+
      '<div id="'+
      this.mHtmlPanelID+
      '" style="align:left; valign:top; padding-left: 0px; padding-right: 0px; padding-bottom: 0px; padding-top: 0px; margin-top: 0px; DISPLAY: block; Z-INDEX: 5; LEFT: 4px; WIDTH:'+
      (this.mWidth-7)+
      '; POSITION: absolute; TOP: 23px; HEIGHT:'+
      this.mHeight+'; BACKGROUND-COLOR: white"></div>'+
      '<div id="'+
      this.mFeedManagerID+
      '" style="align:left; padding-left: 0px; padding-right: 0px; padding-bottom: 0px; padding-top: 0px; margin: 0px; DISPLAY: block; LEFT: 0px; WIDTH:'+
      this.mWidth+
      '; POSITION: absolute; TOP: 0px; HEIGHT:'+
      this.mHeight+
      '; z-index:4">'
   );

   // write out the flash movie embed tag
   idv.FlashMovie.prototype.writeEmbedHtml.call(this);

   //Write closing tgs
   document.write('</div></div>');		

   //Sets the default html to be shown in the html panel
   this.mDefaultHtml = (
      '<table align="center" style="WIDTH:auto; HEIGHT:100%; valign:middle" cellSpacing="0" cellPadding="0">'+
      '<tr>'+
      '<td><img id="Img1" src="'+this.mImagePath+'instructions.jpg"></td>'+
      '</tr>'+
      '</table>'
   );

   //Writes the contents of the html panel container
   document.getElementById(this.mHtmlPanelID).innerHTML = (
      '<div id="'+
      this.mDetailsID+
      '" style="valign:top; padding:inherit; margin:inherit; overflow:hidden;"></div><div id="bottomImage" style="valign:top; BACKGROUND-COLOR:white; padding:inherit; margin:inherit; "><img src="'+
      this.mImagePath+'bottomborder.jpg"></div>'
   );
   
   //Writes the default html to the html panel
   document.getElementById(this.mDetailsID).innerHTML=this.mDefaultHtml;
   
   this.showFlash();
},

//sets the size of the html panel
//Called from Flash layer control when it is resized
setHtmlSize: function(w,h)
{
   width=w;
   height=(h+3);
    //Sets the size of the side panel div
   document.getElementById(this.mHtmlPanelID).style.height=(height)+'px';
   document.getElementById(this.mHtmlPanelID).style.width=width+'px';
   document.getElementById(this.mDetailsID).style.height=height+'px';
   document.getElementById(this.mDetailsID).style.width=width+'px';
},

//Brings the HTML panel to the front
showFlash: function()
{
   //Hide the html panel
   document.getElementById(this.mDetailsID).style.overflow="hidden";
   document.getElementById(this.mHtmlPanelID).style.visibility="hidden";
},

//Brings the HTML panel to the front
showHtml: function()
{
   document.getElementById(this.mDetailsID).style.overflow="auto";
   document.getElementById(this.mHtmlPanelID).style.visibility="visible";
},

//Brings the HTML panel to the front
handleInitialize: function()
{
   this.getMainMap().addLoadListener(this);	
},

// Constructs a new IDV Side Panel writer object
$construct: function(name, file)
{
   // setup internal variables
   this.mWidth         = "100%";
   this.swfFile	     = file;
   this.mHeight        = "100%";
   this.divID          = name;
   this.mImagePath     = "../images/";
   this.mHtmlPanelID   = this.divID+"ItemDetailsDiv";
   this.mFeedManagerID = this.divID+"FeedManagerDiv";
   this.mSwfID         = this.divID+"Swf";
   this.mPanelID       = this.divID+"PanelContainer";
   this.mDetailsID     = this.divID+"DetailsContent";
   this.mDefaultHtml   = "";

   // call base class constructor
   //++this.$super.$construct.call(this,this.mSwfID, file);
   idv.FlashMovie.prototype.$construct.call(this,this.mSwfID, file);
}

})
