// 01/24/2012 - RCF Updated to use jQuery instead of Prototype
var dpalm;
var apalm;

YAHOO.util.Event.onDOMReady( function () {
  YAHOO.bart.init();
});

YAHOO.bart = function() {
  var overlayLink = '';var dpalm;
var apalm;

YAHOO.util.Event.onDOMReady( function () {
  YAHOO.bart.init();
});

YAHOO.bart = function() {
  var overlayLink = '';
  return {
    init : function() {
      this.ie6FormSelectFix();
      this.accessible();
      this.calendar.init();
      this.activateSlider();
      this.activateOverlays();  
      if (YAHOO.util.Dom.get('js-rotation-list')) {
        this.rotationListContainer = YAHOO.util.Dom.get('js-rotation-list');
        this.rotator = setInterval(this.activateRotation,3000);
      }
    },

    //    activate rotating thumbnail images on station pages
    activateRotation : function() {
      var selectedImageMatte = $(YAHOO.bart.rotationListContainer).getElementsByClassName('matte-selected')[0];

      if (YAHOO.util.Dom.getNextSibling(selectedImageMatte.parentNode)) {
        var nextImageLI = YAHOO.util.Dom.getNextSibling(selectedImageMatte.parentNode);
      } else {
        selectedImageMatte = $(YAHOO.bart.rotationListContainer).getElementsByClassName('matte')[0];
        YAHOO.util.Dom.replaceClass(selectedImageMatte,'matte','matte-selected');
        nextImageLI = selectedImageMatte.parentNode;
      }
      nextMatte = YAHOO.util.Dom.getFirstChild(nextImageLI);
      nextImage = YAHOO.util.Dom.getLastChild(nextImageLI);
      YAHOO.util.Dom.replaceClass(selectedImageMatte,'matte-selected','matte');
      YAHOO.util.Dom.replaceClass(nextMatte,'matte','matte-selected');

      if (YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0])
        imageScreen = YAHOO.util.Dom.get(YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0]).getElementsByTagName('img')[0];
      else
        imageScreen = YAHOO.util.Dom.get(YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0]).getElementsByTagName('img')[0];

      nextImageUrl = nextImage.src.replace('-thumb','');
      imageScreen.src = nextImageUrl;
    },

    accessible : function() {
//    hide any js required fields
      $('.js-hidden').show();
    },

    ie6FormSelectFix : function() {
      $('.subnav').next().show();
      $('.subnav').show();
    },

    activateOverlays : function () {
      var overlayLinks = YAHOO.util.Dom.getElementsByClassName('js-open-details');
      if (overlayLinks) {
        for(var i=0,len=overlayLinks.length; i < len;i++)
        {
          YAHOO.util.Event.addListener(overlayLinks[i], "click", YAHOO.bart.openOverlay, overlayLinks[i]);
        }
      }
    },

    openOverlay : function (e, obj) {
      if (e) YAHOO.util.Event.stopEvent(e);
      if (obj.id.indexOf('map-link') != -1) 
        var popupId = 'overlay-' + obj.id.substring(9,obj.id.length);
      else {
        var popupId = 'overlay-' + obj.id;
        if (YAHOO.bart.openOverlayDivs) {
          $(YAHOO.bart.openOverlayDivs).hide();
        }
      }

      if ($('home-date-cover') && (obj.id == 'arriving-station' || obj.id == 'departing-station'))
        $('#home-date-cover').show();
      YAHOO.bart.openOverlayDivs = $(popupId);
      $('#'+popupId).show();
      if ($(popupId).attr("class") != 'module-square-ds') {
        YAHOO.bart.showMapPin(popupId);
      }
      YAHOO.bart.overlayLink = obj;
      var closeLink = YAHOO.util.Dom.getElementsByClassName('close-dialog','a',popupId);
      YAHOO.util.Event.addListener(closeLink[0], "click", YAHOO.bart.closeDialogLink, popupId);
    },

    closeDialogLink : function (e,popupId) {
      YAHOO.util.Event.preventDefault(e);
      $('#'+popupId).hide();
/* 06/26/08 - RCF Added code to unhide data selector in IE6 */
      if ($('#home-date-cover') && (popupId == 'overlay-arriving-station' || popupId == 'overlay-departing-station'))
          $('#home-date-cover').hide();

    },

    showMapPin : function (stationId) {
      var mapPinDiv = YAHOO.util.Dom.getElementsByClassName('station-indicator')[0];
      if (mapPinDiv) {
        var currentStationName = mapPinDiv.className.split('station-indicator ')[1];
        YAHOO.util.Dom.replaceClass(mapPinDiv,currentStationName,stationId.split('overlay-')[1]);
        mapPinDiv.innerHTML = "<a href=\"/stations/" + stationId.split('overlay-s')[1] + "/index.aspx\"><img src=\"/images/icons/station-pointer.png\" alt=\"Selected Station\" title=\"Selected Station\" width=\"36\" height=\"37\"></a>";
      }
    },

    viewTabPanel : function (link) {
      var table = YAHOO.util.Dom.getAncestorByClassName(link,'panel-tabs');
      var selectedTabsFirst = YAHOO.util.Dom.getElementsByClassName('panel-label-selected','td',table);
      var selectedTabsSecond = YAHOO.util.Dom.getElementsByClassName('separator-selected','td',table);
      YAHOO.util.Dom.replaceClass(selectedTabsFirst[0],'panel-label-selected','panel-label');
      YAHOO.util.Dom.replaceClass(selectedTabsSecond[0],'separator-selected','separator');
      var tabfirst = YAHOO.util.Dom.getAncestorByTagName(link,'td');
      var tabsecond = YAHOO.util.Dom.getNextSibling(tabfirst);
      if (tabfirst.className.indexOf('selected') == -1) {
        YAHOO.util.Dom.replaceClass(tabfirst,'panel-label','panel-label-selected');
        YAHOO.util.Dom.replaceClass(tabsecond,'separator','separator-selected');
      }
      this.showTabPanel(link.className, YAHOO.util.Dom.getFirstChild(selectedTabsFirst[0]).className);
    },

    showTabPanel : function (panelToShow,panelToHide) {
      if (YAHOO.bart.openOverlayDivs) {
        $(YAHOO.bart.openOverlayDivs).hide();
        YAHOO.bart.openOverlayDivs = null;
      }
      $('#'+panelToHide).hide();
      $('#'+panelToShow).show();
    },

    activateSlider : function() {
      if ($('#itinerary-slider-hover')) {
        $('#itinerary-slider-hover').hide();
        var itineraryLinks = YAHOO.util.Dom.getElementsByClassName('js-itinerary-link','a','itinerary-nav');
        for(var i=0,len=itineraryLinks.length; i < len;i++)
        {
          YAHOO.util.Event.addListener(itineraryLinks[i], "mouseover", YAHOO.bart.activateSliderHover, itineraryLinks[i]);
          YAHOO.util.Event.addListener(itineraryLinks[i], "mouseout", YAHOO.bart.deactivateSliderHover, itineraryLinks[i]);
        }
      }
    },

    activateSliderHover : function(e, obj) {
      var tdPosition = 'itinerary-' + YAHOO.util.Dom.getAncestorByTagName(obj,'td').className;
      var sliderPosition = $('#itinerary-slider-hover').attr("class");
      YAHOO.util.Dom.removeClass('itinerary-slider-hover',sliderPosition);
      YAHOO.util.Dom.addClass('itinerary-slider-hover',tdPosition);
      $('#itinerary-slider-hover').show();
    } ,

    deactivateSliderHover : function(e, obj) {
      $('#itinerary-slider-hover').hide();
    }
  };
}();

YAHOO.bart.calendar = function() {
  return {
    init : function () {
      this.activateCalendar();
    },

    activateCalendar : function () {
      var calendarInputs = YAHOO.util.Dom.getElementsByClassName('js-date','input');
      var calendarIcons = YAHOO.util.Dom.getElementsByClassName('calendar-icon','a');
      for(var i=0,len=calendarInputs.length; i < len;i++)
      {
        YAHOO.util.Event.addListener(calendarInputs[i], "click", this.openCalendar, calendarInputs[i]);
        YAHOO.util.Event.addListener(calendarIcons[i], "click", this.openCalendar, calendarInputs[i]);
      }
      var calendarInstances = YAHOO.util.Dom.getElementsByClassName('js-calendar','div');
      for(var i=0,len=calendarInstances.length; i < len;i++)
      {
        calId = calendarInputs[i].id.substr(0,calendarInputs[i].id.indexOf('-calendar-widget-input'));
        calendarInstances[i].id = 'calendar-container-'+calId;
        this.renderCalendar('cal-'+calId,'calendar-container-'+calId)
      }
    },

    renderCalendar : function (id,container) {
      var today = new Date();
// Lost and Found goes from today's day backward one year. Other calendars go forward one year
      if ( id.indexOf('lostfound') >= 0) {
        var minDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear()-1;
        var maxDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear();
      } else {
// Allow people to go back to yesterday if between 11pm and 3am
        if ( today.getHours() == 23 || today.getHours() < 3 ) {
          var minDate = today.getMonth() + 1 + '/' + (today.getDate() - 1) + '/' + today.getFullYear();
        } else {
          var minDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear();
        }
        nextYear = today.getFullYear()+1
        var maxDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + nextYear;
      }
      YAHOO.bart.calendar.id = new YAHOO.widget.Calendar(id,container,
                                                          {mindate: minDate,
                                                          maxdate: maxDate}
                                                          );
        YAHOO.bart.calendar.id.render();
        YAHOO.bart.calendar.id.selectEvent.subscribe(this.handleSelect, YAHOO.bart.calendar.id, true);
    },

    openCalendar : function (e, obj) {
      YAHOO.util.Event.preventDefault(e);
      var calendarWidgetId = obj.id.split('-input')[0];
      var xCoord = YAHOO.util.Dom.getX(obj);
      var yCoord = YAHOO.util.Dom.getY(obj) + 20;
      $('#'+calendarWidgetId).show();
      YAHOO.util.Dom.setXY(calendarWidgetId,[xCoord,yCoord]);
      var closeLink = YAHOO.util.Dom.getElementsByClassName('close-dialog','a',calendarWidgetId);
      YAHOO.util.Event.addListener(closeLink[0], "click", function () {
        $('#'+calendarWidgetId).hide();
        YAHOO.util.Event.preventDefault(e);
      });
    },

    handleSelect : function (type,args,obj) {
      var dates = args[0];
      var date = dates[0];
      var year = date[0], month = date[1], day = date[2];

      var txtDateId = obj.id.split('cal-')[1] + '-calendar-widget-input';

      var txtDate1 = document.getElementById(txtDateId);
      txtDate1.value = month + "/" + day + "/" + year;
      $('#'+obj.id.split('cal-')[1] + '-calendar-widget').hide();
     }
  };
}();

// Get/Set/Check JavaScript cookies
function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) {
      return unescape(y);
    }
  }
}
 
function setCookie(c_name,value,exdays) {
  if ( exdays != 0 ) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  } else {
    var c_value=escape(value);
  }
  document.cookie=c_name + "=" + c_value;
}
 
function checkCookie(c_name) {
  var username=getCookie(c_name);
  if (username!=null && username!="") {
    return true;
  }
  return false;
}
  return {
    init : function() {
      this.ie6FormSelectFix();
      this.accessible();
      this.calendar.init();
      this.activateSlider();
      this.activateOverlays();  
      if (YAHOO.util.Dom.get('js-rotation-list')) {
        this.rotationListContainer = YAHOO.util.Dom.get('js-rotation-list');
        this.rotator = setInterval(this.activateRotation,3000);
      }
    },

    //    activate rotating thumbnail images on station pages
    activateRotation : function() {
      var selectedImageMatte = $(YAHOO.bart.rotationListContainer).getElementsByClassName('matte-selected')[0];

      if (YAHOO.util.Dom.getNextSibling(selectedImageMatte.parentNode)) {
        var nextImageLI = YAHOO.util.Dom.getNextSibling(selectedImageMatte.parentNode);
      } else {
        selectedImageMatte = $(YAHOO.bart.rotationListContainer).getElementsByClassName('matte')[0];
        YAHOO.util.Dom.replaceClass(selectedImageMatte,'matte','matte-selected');
        nextImageLI = selectedImageMatte.parentNode;
      }
      nextMatte = YAHOO.util.Dom.getFirstChild(nextImageLI);
      nextImage = YAHOO.util.Dom.getLastChild(nextImageLI);
      YAHOO.util.Dom.replaceClass(selectedImageMatte,'matte-selected','matte');
      YAHOO.util.Dom.replaceClass(nextMatte,'matte','matte-selected');

      if (YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0])
        imageScreen = YAHOO.util.Dom.get(YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0]).getElementsByTagName('img')[0];
      else
        imageScreen = YAHOO.util.Dom.get(YAHOO.util.Dom.getElementsByClassName('img-container', 'div')[0]).getElementsByTagName('img')[0];

      nextImageUrl = nextImage.src.replace('-thumb','');
      imageScreen.src = nextImageUrl;
    },

    accessible : function() {
//    hide any js required fields
      $('.js-hidden').show();
    },

    ie6FormSelectFix : function() {
      $('.subnav').next().show();
      $('.subnav').show();
    },

    activateOverlays : function () {
      var overlayLinks = YAHOO.util.Dom.getElementsByClassName('js-open-details');
      if (overlayLinks) {
        for(var i=0,len=overlayLinks.length; i < len;i++)
        {
          YAHOO.util.Event.addListener(overlayLinks[i], "click", YAHOO.bart.openOverlay, overlayLinks[i]);
        }
      }
    },

    openOverlay : function (e, obj) {
      if (e) YAHOO.util.Event.stopEvent(e);
      if (obj.id.indexOf('map-link') != -1) 
        var popupId = 'overlay-' + obj.id.substring(9,obj.id.length);
      else {
        var popupId = 'overlay-' + obj.id;
        if (YAHOO.bart.openOverlayDivs) {
          $(YAHOO.bart.openOverlayDivs).hide();
        }
      }

      if ($('home-date-cover') && (obj.id == 'arriving-station' || obj.id == 'departing-station'))
        $('#home-date-cover').show();
      YAHOO.bart.openOverlayDivs = $(popupId);
      $('#'+popupId).show();
      if ($(popupId).className != 'module-square-ds') {
        YAHOO.bart.showMapPin(popupId);
      }
      YAHOO.bart.overlayLink = obj;
      var closeLink = YAHOO.util.Dom.getElementsByClassName('close-dialog','a',popupId);
      YAHOO.util.Event.addListener(closeLink[0], "click", YAHOO.bart.closeDialogLink, popupId);
    },

    closeDialogLink : function (e,popupId) {
      YAHOO.util.Event.preventDefault(e);
      $('#'+popupId).hide();
/* 06/26/08 - RCF Added code to unhide data selector in IE6 */
      if ($('home-date-cover') && (popupId == 'overlay-arriving-station' || popupId == 'overlay-departing-station'))
          $('home-date-cover').hide();
    },

    showMapPin : function (stationId) {
      var mapPinDiv = YAHOO.util.Dom.getElementsByClassName('station-indicator')[0];
      if (mapPinDiv) {
        var currentStationName = mapPinDiv.className.split('station-indicator ')[1];
        YAHOO.util.Dom.replaceClass(mapPinDiv,currentStationName,stationId.split('overlay-')[1]);
        mapPinDiv.innerHTML = "<a href=\"/stations/" + stationId.split('overlay-s')[1] + "/index.aspx\"><img src=\"/images/icons/station-pointer.png\" alt=\"Selected Station\" title=\"Selected Station\" width=\"36\" height=\"37\"></a>";
      }
    },

    selectStation : function (e,obj) {
      YAHOO.util.Event.preventDefault(e);
/* 04/18/08 - RCF Changed to pass PALM abbrev */
      var tmp = obj.id;
      if (YAHOO.bart.overlayLink.id.substring(0,1) == "d" ) {
        dpalm = tmp.substring(1); 
      } else {
        apalm = tmp.substring(1); 
      }
      YAHOO.bart.overlayLink.value = obj.innerHTML;
      $('overlay-'+YAHOO.bart.overlayLink.id).hide();
/* 06/26/08 - RCF Added code to unhide data selector in IE6 */
      if ($('home-date-cover') && (YAHOO.bart.overlayLink.id == 'arriving-station' || YAHOO.bart.overlayLink.id == 'departing-station'))
        $('home-date-cover').hide();
    },

    viewTabPanel : function (link) {
      var table = YAHOO.util.Dom.getAncestorByClassName(link,'panel-tabs');
      var selectedTabsFirst = YAHOO.util.Dom.getElementsByClassName('panel-label-selected','td',table);
      var selectedTabsSecond = YAHOO.util.Dom.getElementsByClassName('separator-selected','td',table);
      YAHOO.util.Dom.replaceClass(selectedTabsFirst[0],'panel-label-selected','panel-label');
      YAHOO.util.Dom.replaceClass(selectedTabsSecond[0],'separator-selected','separator');
      var tabfirst = YAHOO.util.Dom.getAncestorByTagName(link,'td');
      var tabsecond = YAHOO.util.Dom.getNextSibling(tabfirst);
      if (tabfirst.className.indexOf('selected') == -1) {
        YAHOO.util.Dom.replaceClass(tabfirst,'panel-label','panel-label-selected');
        YAHOO.util.Dom.replaceClass(tabsecond,'separator','separator-selected');
      }
      this.showTabPanel(link.className, YAHOO.util.Dom.getFirstChild(selectedTabsFirst[0]).className);
    },

    showTabPanel : function (panelToShow,panelToHide) {
      if (YAHOO.bart.openOverlayDivs) {
        $(YAHOO.bart.openOverlayDivs).hide();
        YAHOO.bart.openOverlayDivs = null;
      }
      $('#'+panelToHide).hide();
      $('#'+panelToShow).show();
    },

    activateSlider : function() {
      if ($('#itinerary-slider-hover')) {
        $('#itinerary-slider-hover').hide();
        var itineraryLinks = YAHOO.util.Dom.getElementsByClassName('js-itinerary-link','a','itinerary-nav');
        for(var i=0,len=itineraryLinks.length; i < len;i++)
        {
          YAHOO.util.Event.addListener(itineraryLinks[i], "mouseover", YAHOO.bart.activateSliderHover, itineraryLinks[i]);
          YAHOO.util.Event.addListener(itineraryLinks[i], "mouseout", YAHOO.bart.deactivateSliderHover, itineraryLinks[i]);
        }
      }
    },
    

    activateSliderHover : function(e, obj) {
      var tdPosition = 'itinerary-' + YAHOO.util.Dom.getAncestorByTagName(obj,'td').className;
      var sliderPosition = $('#itinerary-slider-hover').attr("class");
      YAHOO.util.Dom.removeClass('itinerary-slider-hover',sliderPosition);
      YAHOO.util.Dom.addClass('itinerary-slider-hover',tdPosition);
      $('#itinerary-slider-hover').show();
    } ,

    deactivateSliderHover : function(e, obj) {
      $('#itinerary-slider-hover').hide();
    }
  };
}();


YAHOO.bart.calendar = function() {
  return {
    init : function () {
      this.activateCalendar();
    },

    activateCalendar : function () {
      var calendarInputs = YAHOO.util.Dom.getElementsByClassName('js-date','input');
      var calendarIcons = YAHOO.util.Dom.getElementsByClassName('calendar-icon','a');
      for(var i=0,len=calendarInputs.length; i < len;i++)
      {
        YAHOO.util.Event.addListener(calendarInputs[i], "click", this.openCalendar, calendarInputs[i]);
        YAHOO.util.Event.addListener(calendarIcons[i], "click", this.openCalendar, calendarInputs[i]);
      }
      var calendarInstances = YAHOO.util.Dom.getElementsByClassName('js-calendar','div');
      for(var i=0,len=calendarInstances.length; i < len;i++)
      {
        calId = calendarInputs[i].id.substr(0,calendarInputs[i].id.indexOf('-calendar-widget-input'));
        calendarInstances[i].id = 'calendar-container-'+calId;
        this.renderCalendar('cal-'+calId,'calendar-container-'+calId)
      }
    },

    renderCalendar : function (id,container) {
      var today = new Date();
// Lost and Found goes from today's day backward one year. Other calendars go forward one year
      if ( id.indexOf('lostfound') >= 0) {
        var minDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear()-1;
        var maxDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear();
      } else {
// Allow people to go back to yesterday if between 11pm and 3am
        if ( today.getHours() == 23 || today.getHours() < 3 ) {
          var minDate = today.getMonth() + 1 + '/' + (today.getDate() - 1) + '/' + today.getFullYear();
        } else {
          var minDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + today.getFullYear();
        }
        nextYear = today.getFullYear()+1
        var maxDate = today.getMonth() + 1 + '/' + today.getDate() + '/' + nextYear;
      }
      YAHOO.bart.calendar.id = new YAHOO.widget.Calendar(id,container,
                                                          {mindate: minDate,
                                                          maxdate: maxDate}
                                                          );
        YAHOO.bart.calendar.id.render();
        YAHOO.bart.calendar.id.selectEvent.subscribe(this.handleSelect, YAHOO.bart.calendar.id, true);
    },

    openCalendar : function (e, obj) {
      YAHOO.util.Event.preventDefault(e);
      var calendarWidgetId = obj.id.split('-input')[0];
      var xCoord = YAHOO.util.Dom.getX(obj);
      var yCoord = YAHOO.util.Dom.getY(obj) + 20;
      $('#'+calendarWidgetId).show();
      YAHOO.util.Dom.setXY(calendarWidgetId,[xCoord,yCoord]);
      var closeLink = YAHOO.util.Dom.getElementsByClassName('close-dialog','a',calendarWidgetId);
      YAHOO.util.Event.addListener(closeLink[0], "click", function () {
        $('#'+calendarWidgetId).hide();
        YAHOO.util.Event.preventDefault(e);
      });
    },

    handleSelect : function (type,args,obj) {
      var dates = args[0];
      var date = dates[0];
      var year = date[0], month = date[1], day = date[2];

      var txtDateId = obj.id.split('cal-')[1] + '-calendar-widget-input';

      var txtDate1 = document.getElementById(txtDateId);
      txtDate1.value = month + "/" + day + "/" + year;
      $('#'+obj.id.split('cal-')[1] + '-calendar-widget').hide();
     }
  };
}();

// Get/Set/Check JavaScript cookies
function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) {
      return unescape(y);
    }
  }
}
 
function setCookie(c_name,value,exdays) {
  if ( exdays != 0 ) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  } else {
    var c_value=escape(value);
  }
  document.cookie=c_name + "=" + c_value;
}
 
function checkCookie(c_name) {
  var username=getCookie(c_name);
  if (username!=null && username!="") {
    return true;
  }
  return false;
}
