var BillingSame = {};
BillingSame = {
  add: function() {
    check_box_p = new Element('p', { id: 'p_billing_same' });
    check_box = new Element('input', { type: 'checkbox', id: 'billing_same' });
    check_box_label = new Element('label').update('My billing information is the same as my shipping information');

    check_box_p.insert(check_box);
    check_box_p.insert(check_box_label);

    $('billing-info-fields').insert({ before: check_box_p });
  },
  attach_event: function() {
    $('billing_same').observe('click', function(event) {
      BillingSame.toggle_business_info();
    });
  },
  override_form_submit: function() {
    $('new-order-form').observe('submit', function(event) {
      // $('billing-info-fields').show();
      BillingSame.clone_info_if_needed();
    });
  },
  fields_are_empty: true,
  select_if_fields_are_empty: function() {
    BillingSame.fields_are_empty = true;
    $w('first_name last_name address city state postal_code').each(function(field) {
      if ($F('order_billing_'+field) != "") {
        BillingSame.fields_are_empty = false;
      }
    });
    if (BillingSame.fields_are_empty) {
      $('billing_same').checked = 'checked';
      BillingSame.toggle_business_info();
    }
  },
  insert_checkbox: function() {
    BillingSame.add();
    BillingSame.attach_event();
    BillingSame.override_form_submit();
    BillingSame.select_if_fields_are_empty();
  },
  toggle_business_info: function() {
    if ($('billing-info-fields').visible()) {
      $('billing-info-fields').hide();
    } else {
      $('billing-info-fields').show();
    }
  },
  clone_info_if_needed: function() {
    if ($F('billing_same')) { BillingSame.clone_info(); }
  },
  clone_info: function() {
    $w('first_name last_name address city state postal_code').each(function(field) {
      $('order_billing_'+field).value = $F('order_ship_to_'+field);
    });
  }
};

var ProductChoice = {};
ProductChoice = {
  setup: function() {
    $$('#content .color-choice').each(function(color, index) {
      ProductChoice.attach_hover(color, index);
    });
    $$('#content .size-choice').each(function(size, index) {
      ProductChoice.attach_hover(size, index);
    });
    // TODO: insert a hidden spinner here
  },
  attach_hover: function(item, index) {
    item.addClassName('active');
    item.down('input').hide();
    if (item.hasClassName('size-choice')) { item.down('label').hide(); } // just for the sizes
    
    block = item.down(); // the first thing should be the span
    block.observe('click', function(event) { ProductChoice.process(this); });
    
    if (index == 0) { ProductChoice.process(block); }
  },
  process: function(choice) {
    is_selected = choice.up().hasClassName('selected');
    choice.up().siblings().each(function(c) { c.removeClassName('selected'); });
    if (!is_selected) { choice.up().addClassName('selected'); }
    
    choice.next('input').checked = true;
    
    // if it's a color, fetch the correct photo and what sizes are available
    if (choice.hasClassName('color-span')) {
      ProductChoice.process_new_color();
    } else {
      ProductChoice.process_new_size();
    } ///if
  },
  process_new_color: function() {
    params = $('add-to-cart-form').serialize(true);
    new Ajax.Request("/colors/"+params.color+"/select", { method: 'get', parameters: params });
  },
  process_new_size: function() {
    params = $('add-to-cart-form').serialize(true);
    new Ajax.Request("/sizes/"+params.size+"/select", { method: 'get', parameters: params });
  }
};

var Popup = {};
Popup = {
  find_all: function() {
    $$('a.popup').each(function(link) {
      link.onclick = function(event) {
        window.open(this.href, 'popup_window', 'menubar=no,toolbar=no,personalbar=no,status=no,resizable=yes,scrollbars=yes,height=660,width=860');
        return false;
      };
    });
  }
};

var Video = {};
Video = {
  insert: function() {
    UFO.getFlashVersion();
    if (UFO.hasFlashVersion(8,0)) {
      anchor = new Element('a', { href:'#' });
      anchor.onclick = function() { Video.play(); return false; };
      image = new Element('img', { src:'/images/flash/movie-preview.gif', width:'320', height:'240' });
      anchor.insert(image);
      $('video').update(anchor);
    }
  },
  play: function() {
    var domain = window.location.hostname;
    // if (window.location.port != '80' && window.location.port != '0') { domain += ':'+window.location.port; }
    video_FO = { 
      movie:'/images/flash/movie.swf', 
      width:'320',
      height:'240',
      majorversion:'8',
      build:'0',
      flashvars:'domain_string='+domain
    };
    UFO.create(video_FO, 'video');
  }
};

var Magazine = {};
Magazine = {
  insert: function() {
    mag_FO = { 
      movie:'pageflip_loader.swf', 
      width:'850',
      height:'550',
      majorversion:'8',
      build:'0'
    };
    UFO.create(mag_FO, 'magazine');
  }
};

document.observe("dom:loaded", function() {
  if ($('billing-info-fields')) { BillingSame.insert_checkbox(); }
  if ($('product-image')) { ProductChoice.setup(); }
  Popup.find_all();
  if ($('video')) { Video.insert(); }
  if ($('magazine')) { Magazine.insert(); }
});