YAHOO.namespace("AMAN.map");  // create the AMAN map namespace

// create the object with initialisation, animation & properties
YAHOO.AMAN.map = 
{

  // duration for the animation
  anim_speed:2.0,
  anim_counter:0,
  map_shown:false,
  $D:"",  // variable to hold YAHOO.util.Dom reference
  $M:"",  // variable to hold YAHOO.AMAN.map reference
  $Ev:"",  // variable to hold YAHOO.util.Event reference
  $El:"",  // variable to hold YAHOO.util.Element reference
  dots:"",
  panels:"",
  links:"",
  dotlinks:"",
  navlinks:"",
  origpanel:"",
  origheight:"",
  xoffset:"",
  mainlinkcol:"",
  mainlinkbgcol:"",
  dropdowns:"",
  checkavail:"",
  mapnavlinks:"",
  mapsubnavs:"",
  
  animate: function(e) {
    $Ev.preventDefault(e); // prevent navigating away from this page

    if ($M.map_shown)
    {
      // window.status = "closing map";
      // hide the map panel
      $D.setStyle(['mappanel'],'height','0px');
      // hide the crosshairs
      $D.setStyle(['xline','yline'],'display','none');
      
      // show the original panel
      $D.setStyle(origpanel,'display','block');
      
      // reset the map link colours
      $D.setStyle('maplink','background-color',$M.mainlinkbgcol);
      $D.setStyle('maplink','color',$M.mainlinkcol);

      $D.setStyle($M.dropdowns,'height',$M.origheight);
      $D.setStyle($M.checkavail,'height',parseInt($M.origheight)+18+'px');
      $D.setStyle('checkavailbox_inner','top',(parseInt($M.origheight)-30)+"px");


      // blank the detailed map
      var exmap = $D.get('extramap');
      exmap.innerHTML = "";
      $M.map_shown = false;

    }
    else
    {
      // window.status = "opening map";
      // show the map panel
      $D.setStyle(['mappanel'],'height','439px');
      
      // hide the original panel
      $D.setStyle(origpanel,'display','none');
      $M.map_shown = true;
      
      // highlight the map link
      $D.setStyle('maplink','background-color','#DDD9D1');
      $D.setStyle('maplink','color','#333333');
      
      $D.setStyle($M.dropdowns,'height','439px');
      $D.setStyle($M.checkavail,'height','457px');
      // set the position of the close box on the check avail panel
      $D.setStyle('checkavailbox_inner','top','412px');
      
    }
    
  },
  
  close_detail_map: function(e)
  {
      $Ev.preventDefault(e); // prevent navigating away from this page
      $D.setStyle('extramap','display','none');
      var exmap = $D.get('extramap');
      exmap.innerHTML = "";  
      $D.setStyle('mapclose2','display','none');
      $D.setStyle('mapclose','display','block');
  },

  adjust_heights: function(e)
  {
    // function to adjust the height of the drop downs
  
  },
  
  crosshairs_hide: function(e) {  
    $D.setStyle(['xline','yline'],'display','none');
  },
  
  crosshairs: function(e) {
    var x = parseInt($Ev.getPageX(e));
    var y = parseInt($Ev.getPageY(e));
    var posx;
    var posy;

    if (e.pageX || e.pageY)   
    {
      posx = e.pageX;  posy = e.pageY;
    }
    else if (e.clientX || e.clientY)  
    {
      posx = e.clientX + document.body.scrollLeft  + document.documentElement.scrollLeft;
      posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
    }

    var target;
    target = $Ev.getTarget(e);
    // window.status = posx + ":" + posy + "[" +$M.xoffset + ":" + target.id +"]" ;
  
    $D.setStyle(['xline','yline'],'display','block');
    $D.setStyle(['xline','yline'],'z-index','4');
    $D.setStyle(['yline'],'height','1px');
    
    $D.setY('yline', y-1); // set the vertical position of the 800px wide horizontal line
    
    var tmpDisplay = $D.getStyle(origpanel,'display');
    if (tmpDisplay != 'none')
    {
          $D.setStyle(origpanel,'display','none');
    }
    
    // switch off the crosshairs if worldnav is the parent of the target element
    if ($D.isAncestor('worldnav',target))
    { 
      $M.crosshairs_hide(e);
    }
    else
    {
      $D.setX('xline', posx-1); // set the horizontal position of the 439px tall vertical line
      $D.setY('xline', 0); // the vertical line has to start at the top of the panel
    }
  },

  showdot: function(e) {
    $Ev.preventDefault(e); // prevent navigating away from this page
    var info_panel = this.id;
    info_panel= info_panel.replace('link','info');
    $M.hidepanels();
    $D.setStyle(info_panel,'display','block');
    var xpos = parseInt($D.getX(info_panel));
    if (xpos < 120) { $D.replaceClass(info_panel,'mapinfo','mapinfo2');}
  },

  showdot2: function(e) {
    $Ev.preventDefault(e); // prevent navigating away from this page
    var info_panel = this.id;
    info_panel= info_panel.replace('link2','info');
    $M.hidepanels();
    $D.setStyle(info_panel,'display','block');
    var xpos = parseInt($D.getX(info_panel));
    if (xpos < 120) { $D.replaceClass(info_panel,'mapinfo','mapinfo2');}
  },

  hidepanels: function() {
    $D.setStyle(panels,'display','none');
  },

  topNavTopLevel: function(el) {
     var tmpel = el.parentNode;
     // window.status = el.nodeName + ":" + tmpel.id;
     if (tmpel.id == 'topnav')
     { return false; }
     else
     { return true;}
  },

  addDots: function() {
    dots = $D.getElementsByClassName('mapdot', 'a', 'mappanel'); // look for all a tags in mappanel div with class mapdot
    $Ev.addListener(dots,'mouseover', $M.showdot);
    $Ev.addListener(dots,'click', $M.noclick);
    
    panels = $D.getElementsByClassName('mapinfo','div','mappanel');
    links = $D.getElementsByClassName('maplink2','a','mappanel');
    $Ev.addListener(links,'mouseover',$M.showdot2);
  },

  noclick: function(e) {
    $Ev.preventDefault(e); // prevent navigating away from this page
  },
  
  mapnavclick: function(e) {
    $Ev.preventDefault(e); // prevent navigating away from this page
    var tmpel = $Ev.getTarget(e);
    var tmppar = tmpel.parentNode;
    
    // alert(tmppar.nodeName);
    
    $D.removeClass($M.mapsubnavs,'subnavvisible');
    // tmpel = $D.getElementsByClassName('mapsubnav','ul',tmppar);
    tmpel = tmpel.nextSibling;
    tmpel = tmpel.nextSibling;
    // alert(tmpel.nodeName);
    
    $D.addClass(tmpel,'subnavvisible');
    
  },

  init: function() {
    $M = YAHOO.AMAN.map;
    $D = YAHOO.util.Dom;
    $Ev = YAHOO.util.Event;
    
    // $D.setStyle('mappanel','position','absolute');
    // $M.xoffset = parseInt($D.getStyle('mappanel','left'));
    // $D.setStyle('mappanel','position','relative');
    
    var tmpel = $D.get('topnav_2_1');
    tmpel = tmpel.parentNode;
    $M.origheight = $D.getStyle(tmpel,'height');
    
    $M.mainlinkcol = $D.getStyle('maplink','color');
    $M.mainlinkbgcol = $D.getStyle('maplink','background-color');
    $M.dropdowns = $D.getElementsBy($M.topNavTopLevel,'ul','topnav');
    $M.checkavail = $D.get('avail_page');
    
    $Ev.addListener('maplink','click',$M.animate,false); // add the click handler for the link
    $Ev.addListener('mapcloselink','click',$M.animate,false); // add the click handler for the link
    $Ev.addListener('mapcloselink2','click',$M.close_detail_map,false); // add the click handler for the link
    $Ev.addListener('worldnav','mouseover',$M.crosshairs_hide,false); // add the mousemove handler for the crosshairs for the map panel
    $Ev.addListener('mappanel','mouseout',$M.crosshairs_hide,false); // add the mousemove handler for the crosshairs for the map panel
    $Ev.addListener('mappanel','mousemove',$M.crosshairs,false); // add the mousemove handler for the crosshairs for the map panel

    $M.addDots();

    // now wire the links for the information under the dots and navigation links on the right
    // get all the links
    $M.dotlinks = $D.getElementsByClassName('maplink3','a','mapdots');
    $M.navlinks = $D.getElementsByClassName('maplink2','a','worldnav');
    $M.mapnavlinks = $D.getElementsByClassName('mapnavlink','a','worldnav');
    $M.mapsubnavs = $D.getElementsByClassName('mapsubnav','ul','worldnav');
    
    // add the listeners for the click event
    $Ev.addListener($M.dotlinks,'click',doit);
    $Ev.addListener($M.navlinks,'click',doit);

    $Ev.addListener($M.mapnavlinks,'click',$M.mapnavclick);

    // look for the panel we're going to replace - the id needs to be the main panel or album etc
    origpanel = $D.get('mainimgcol');
    if (origpanel)
    {
       $D.setStyle(origpanel,'overflow','hidden');
     return false
    }

  origpanel = $D.get('album');
    if (origpanel)
    {
       $D.setStyle(origpanel,'overflow','hidden');
       return false
    }
      origpanel = $D.get('home-slide');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
    return false
      }
      origpanel = $D.get('album');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
      return false
    }
      origpanel = $D.get('content_green');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
      return false
    }
      origpanel = $D.get('content_white');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
      return false
    }    
      origpanel = $D.get('content_green_2');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
      return false
    }
    origpanel = $D.get('imgSShow');
      if (origpanel)
      {
         $D.setStyle(origpanel,'overflow','hidden');
      return false
    }
  else
    {
       // do nothing
    }

  }


};

YAHOO.util.Event.onAvailable('maplink',YAHOO.AMAN.map.init); // wait for the div map to appear before wiring it up

var AjaxObject = {

  handleSuccess:function(o){
    // This member handles the success response
    // and passes the response object o to AjaxObject's
    // processResult member.
    // this.callAlert(o);
    this.processResult(o);
  },

  handleFailure:function(o){
    // Failure handler
    //alert("whoops - xhr problem");
  },

  processResult:function(o){
    // This member is called by handleSuccess
    // alert(o.responseText);
    
    var exmap = YAHOO.util.Dom.get('extramap');
    exmap.innerHTML = o.responseText;
    
    YAHOO.util.Dom.setStyle('extramap','display','block');
  },

  startRequest:function(code) {
    //  YAHOO.util.Connect.asyncRequest('GET', '/singlemap.aspx?id='+code, callback, null);
     YAHOO.util.Connect.asyncRequest('GET', '/resortmap.aspx?'+code, callback, null);
  }

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback =
{
  success:AjaxObject.handleSuccess,
  failure:AjaxObject.handleFailure,
  timeout: 5000, // 5 second timeout
  scope: AjaxObject
};


function doit(e)
{
  YAHOO.util.Event.preventDefault(e); // prevent navigating away from this page

  YAHOO.util.Dom.setStyle('mapclose2','display','block');
  YAHOO.util.Dom.setStyle('mapclose','display','none');
  
  var tmp;  
  var re = new RegExp('(.*?)mapid');
  tmp = this.getAttribute('href');
  
  tmp = tmp.replace(re,'mapid');
  
  // window.status = "retrieving mapid " + tmp;

  // make the call
  AjaxObject.startRequest(tmp);
  return false;
}

function blockError(){return true;}
window.onerror = blockError;