﻿//////////////////////////////////////////////////////////////////////////////////////////////
// Author: Kris Kasias
// Date: January 26, 2007
// Purpose: Classes that define a standard search results panel for use with the google maps
//          api.
//////////////////////////////////////////////////////////////////////////////////////////////

Realosophy.Web.Client.SearchWell = function(boundElement, map) 
{
  this._items = [];
  this._map = map;
  this.boundElement = boundElement;  
}

Realosophy.Web.Client.SearchWell.prototype = 
{
  createSection: function(sectionID, headerText) 
  {
    var rv = new Realosophy.Web.Client.SearchWellSection(this.boundElement.id + '_' + sectionID, headerText);
    rv.set_map(this._map);
    return rv;
  },
  
  addSection: function(searchWellSection) 
  {
  	this.boundElement.appendChild(searchWellSection.get_Item());
  	this._items.push(searchWellSection);
  },
  
  getElementByID: function(sectionID) 
  {
    return $get(this.boundElement.id + '_' + sectionID);
  },
  
  getSectionByID: function(sectionID) 
  {
  	for(var i=0, length=this._items.length; i < length; i++)
  	{
  	  if( sectionID == this._items[i].ID )
  	    return this._items[i];
  	}
  	return null;
  },
  
  clear: function() 
  {
    this.dispose();
    this._items = [];
    this.boundElement.innerHTML = "";
  },
    
  removeSection: function(sectionID) 
  {
    var elemName = this.boundElement.id + '_' + sectionID;
    var e = $get(elemName);
    if( e != null ) {
      this.boundElement.removeChild(e);
    }
    
  	for(var i=0, length=this._items.length; i < length; i++)
  	{
  	  if( elemName == this._items[i].ID ) {
  	    this._items[i].dispose();
  	    this._items.splice(i, 1);
  	    break;
  	  }
  	}    
  },
  
  dispose: function() 
  {
    for(var i = 0, length = this._items.length; i < length; i++) {
      this._items[i].dispose();
      delete this._items[i];
    }
    delete this._items;
  } 
}
Realosophy.Web.Client.SearchWell.registerClass('Realosophy.Web.Client.SearchWell', null, Sys.IDisposable);

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/***************************************SearchWellSection************************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Realosophy.Web.Client.SearchWellSection = function(sectionID, headerText) 
{
  this._element = document.createElement('div');
  this._element.id = sectionID;
  
  this._map = null;
  this._itemRefID = 0;
  this._items = [];
  this.ID = this._element.id;  
    
  if(headerText != null) 
  {
    var rvHeader = document.createElement('div');
    rvHeader.id = sectionID + "_header";
    rvHeader.className = "sr-section-header";
    rvHeader.innerHTML = headerText;
    this._element.appendChild(rvHeader);		
  }
  return this;
}

Realosophy.Web.Client.SearchWellSection.prototype = 
{
  getNextItemID: function() 
  {
    return this._element.id + '_item' + this._itemRefID++;
  },
  
  get_map: function(){
    return this._map;
  },

  set_map: function(value){
    this._map = value;
  },
      
  add_SectionItem: function(item) {
    if( Realosophy.Web.Client.ISearchWellItem.isImplementedBy(item) ) 
    {
      item.map = this.get_map();
      var e = item.get_ListItem()
      this._element.appendChild(e);
      this._items.push(item);
    }
  },
  
  getItemByID: function(itemID)
  {
    for(var i = 0, length = this._items.length; i < length; i++) 
    {
      if( this._items[i].ID == itemID ) 
      {
        return this._items[i];
      }      
    }
    return null;
  },
  
  get_Item: function() 
  {
    return this._element;
  },
  
  dispose: function() 
  {
    for(var i = 0, length = this._items.length; i < length; i++) {
      this._items[i].dispose();
      delete this._items[i];
    }    
    delete _items;
  }
}
Realosophy.Web.Client.SearchWellSection.registerClass('Realosophy.Web.Client.SearchWellSection', null, Sys.IDisposable);

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/***************************************SearchWellItem Interface*****************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Realosophy.Web.Client.ISearchWellItem = function() 
{
    this.set_MarkerHTML = Function.abstractMethod;
    this.get_MarkerHTML = Function.abstractMethod;    
    this.get_LatLong = Function.abstractMethod;    
    this.get_HotMarker = Function.abstractMethod;    
    this.get_ColdMarker = Function.abstractMethod;
    this.setParent = Function.abstractMethod;
}
Realosophy.Web.Client.ISearchWellItem.registerInterface('Realosophy.Web.Client.ISearchWellItem');

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/***************************************SearchWellItem Class*********************************************/
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Realosophy.Web.Client.SearchWellItem = function(section, result, coldIcon, hotIcon) {
  Realosophy.Web.Client.SearchWellItem.initializeBase(this);
  
  this.map = null;
  this.isHot = false;
  this.hotMarker = new GMarker(new GLatLng(result.lat, result.lng), hotIcon);
  this.coldMarker = new GMarker(new GLatLng(result.lat, result.lng), coldIcon);  
  this.result = result;
  this.coldMarkerClickDelegate = null;
  this.itemClickDelegate = null;
  //this.parentElement = section.get_Item();

  if( Realosophy.Web.Client.SearchWellSection.isInstanceOfType(section) ) 
  {
    this._e = document.createElement('div');
    this._e.id = section.getNextItemID();  
  } else {
    RealSystem.trace('Parameter [section] is not of type Realosophy.Web.Client.SearchWell');
    return null;
  }
  
  this.ID = this._e.id;
}

Realosophy.Web.Client.SearchWellItem.prototype = 
{
  initialize: function() {
    Realosophy.Web.Client.SearchWellItem.callBaseMethod(this, 'initialize');
  },
  
  className: function(CSSClass)
  {
    this._e.className = CSSClass;
  },
  
  appendChild: function(element) 
  {
    this._e.appendChild(element);
  },  

  hiliteMarker: function()
  {
    if( this.isHot ) {
      this.map.removeOverlay(this.hotMarker);
      this.map.addOverlay(this.coldMarker);
    } else {
      this.map.removeOverlay(this.coldMarker);
      this.map.addOverlay(this.hotMarker);
    }
    this.isHot = !this.isHot;
  },

  showMarker: function()
  {
    if( this.coldMarker )
    {      
      this.map.addOverlay(this.coldMarker);
    }
  },

  hideMarker: function()
  { 
    if( this.coldMarker )
    {      
      this.map.removeOverlay(this.coldMarker);
    }     
  },
  
  showInfoWindow: function()
  {
    this.coldMarker.openInfoWindow(this.markerHTML);
  },
  
  add_MarkerClick: function(markerHTML){
    this.markerHTML = markerHTML;
    this.coldMarkerClickDelegate = GEvent.addListener(this.coldMarker, 'click', 
      function(){
        this.openInfoWindow(markerHTML);
      }
    );  
  },

  remove_MarkerClick: function(handler){
    GEvent.removeListener(this.coldMarkerClickDelegate);  
  },
  
  get_ListItem: function() 
  {
    return this._e;
  },
  
  add_disposeItem: function(handler){
    this.get_events().addHandler('disposeItem', handler);
  },
  
  remove_disposeItem: function(handler){
    this.get_events().removeHandler('disposeItem', handler);
  },

  dispose: function() 
  {
    this.hideMarker();
    delete this.coldMarker;
    delete this.hotMarker;
    this.coldMarker = null;
    this.hotMarker = null
    this.get_ListItem().parentNode.removeChild(this._e);
    var handler = this.get_events().getHandler("disposeItem");
    if (handler) {
        handler(this, Sys.EventArgs.Empty);
    }
  }
}
Realosophy.Web.Client.SearchWellItem.registerClass('Realosophy.Web.Client.SearchWellItem', Sys.Component, Realosophy.Web.Client.ISearchWellItem);