/*
 * APPLICATION SPECIFIC JAVASCRIPT
 */


function toggleCheckBoxBySelector(box, class_name )
{
  $$( class_name ).each(function(e){
    e.checked = box.checked;
  }); 
  
}

/*
 * APPLICATION SPECIFIC JAVASCRIPT
 */

function copyOrderShipAddress(box)
{
  if (box.checked)
  {
    fields = $w('to address_1 address_2 city state zip country');
    fields.each(function(field){
      from = $('order_ship_' + field);
      to =  $('order_bill_' + field);
      to.value = from.getValue();
    })
  }
}

HideFlashMessage = function()
{
  setTimeout("$('flash_message').fade()", 3000)
}
Event.observe(window, 'load', HideFlashMessage );


var TVControl =
{
  channelsHeight: 438,
  channelsMorphing: false,
  channelsMorphSpeed: 0.8,
  channelsMaxHeight: -890,

  play: function(vid)
  {
    new Ajax.Updater('left', '_channel.php?video=' + vid, {evalScripts: true});
  },

  nextChannel: function()
  {
    if (TVControl.channelsMorphing) return;
    TVControl.channelsMorphing = true;
    top = parseInt($('tv_channels').getStyle('top'));
    top -= TVControl.channelsHeight;

    if (top < TVControl.channelsMaxHeight) top = 0;
    new Effect.Morph('tv_channels', {
       style: 'top: ' + top + 'px',
       duration: TVControl.channelsMorphSpeed,
       afterFinish: function() {TVControl.channelsMorphing = false; }
    });


  },

  prevChannel: function()
  {
    if (TVControl.channelsMorphing) return;
    TVControl.channelsMorphing = true;
    top = parseInt($('tv_channels').getStyle('top'));
    top += TVControl.channelsHeight;

    if (top > 0 ) top = TVControl.channelsMaxHeight;
    new Effect.Morph('tv_channels', {
       style: 'top: ' + top + 'px',
       duration: TVControl.channelsMorphSpeed,
       afterFinish: function() {TVControl.channelsMorphing = false; }
    });


  }
}

/*
 * The goal of this function is to create the google maps code *hopefully* by
 * using elements currently in the document to build the required code.
 */
var StoreMap = {

  initialize: function(map_div)
  {
    this.default_point = new GLatLng(39.935660, -91.414884);
    this.map_div = map_div


    this.map = new GMap2(this.map_div);
    this.addControls();

    this.map.setCenter(this.default_point, 10);

    this.bounds = new GLatLngBounds(this.default_point, this.default_point);
    this.bounds = new GLatLngBounds();

    this.addMarkers();

    zoom = this.map.getBoundsZoomLevel(this.bounds);
    this.map.setZoom(zoom);
    this.map.setCenter(this.bounds.getCenter());

  },


  addControls: function()
  {
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GMapTypeControl());
  },

  addMarkers: function()
  {
    $$('#stores .store').each(function(e) {
      this.addMarkerFromElement(e)
    }.bindAsEventListener(this));
  },

  addMarkerFromElement: function(e)
  {

    lat = e.down('.latitude').innerHTML;
    lng = e.down('.longitude').innerHTML;
    point = new GLatLng(lat, lng);
    marker = new GMarker(point);
    this.map.addOverlay(marker);

    marker.bindInfoWindow(e.down('.info_window'));

    this.bounds.extend(marker.getLatLng());

  },

  load: function()
  {
    if (GBrowserIsCompatible())
    {
      $$('#storemap').each(function(t){
        if ( ! t.hasClassName('loaded'))
        {
          StoreMap.initialize(t);

        }
        t.addClassName('loaded');
      });
    }
  }

}


//  Event.observe(window, 'load', StoreMap.load);


function validate(form_id,email) {
 
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {
      alert('Invalid Recipient Email Address');
      return false;
   }
   return true;
}
