
var HomeMarksSite = Class.create(HomeMarksBase,{ 
  
  initialize: function() {
    this.ajaxFrom = $('ajaxforms_wrapper');
    this.ajaxFromLinks = $$('a.ajaxform_link');
    this.supportForm = $('support_form');
    this.loginArea = $('login_area'); this.loginForm = $('login_form'); 
    this.forgotPwArea = $('forgotpw_area'); this.forgotPwForm = $('forgotpw_form'); 
    this.forgotPwButton = $('forgotpw_button'); this.forgotPwCancel = $('forgotpw_cancel'); 
    this.signupForm = $('signup_form');
    this.editForm = $('edit_form');
    this.flashes = $$('div.flash_message');
    this.initEvents();
  },
  
  clearFlashes: function() {
    this.flashes.invoke('hide');
    this.flashes.invoke('update','');
  },
  
  flash: function(mood,html) {
    this.clearFlashes();
    var moodFlash = this.flashes.find(function(e){ if (e.id == 'flash_'+mood) {return true}; });
    moodFlash.update(html);
    moodFlash.show();
    $('site_wrapper').scrollTo();
  },
  
  flashModal: function(mood,html) {
    var moodColor = this.flashMoodColors.get(mood);
    HomeMarksModal.show(html,{color:moodColor});
  },
  
  toggleAjaxFormBlind: function(event) {
    if (event) { event.stop(); event.element().blur(); };
    Effect.toggle(this.ajaxFrom, 'blind', {duration:0.4});
  },
  
  startAjaxForm: function(event) {
    event.stop();
    var form = event.findElement('form');
    var options = Object.extend({imgSrc:'loading_invert.gif'}, arguments[1] || {});
    var loadArea = form.down('#form_loading');
    var imgTag = IMG({src:('/images/'+options.imgSrc)});
    $(loadArea).update(imgTag);
    new Ajax.Request(form.action,{
      onComplete: function(request){ this.delegateCompleteAjaxForm(form,request) }.bind(this),
      parameters: form.serialize(true),
    });
    form.disable();
  },
  
  delegateCompleteAjaxForm: function(form,request) {
    var mood = this.getRequestMood(request);
    this.completeAjaxForm(form,{mood:mood});
    this.clearFlashes();
    if (mood == 'good') { 
      switch (form) { 
        case this.supportForm   : this.completeSupportForm(request); break;
        case this.loginForm     : this.completeLoginForm(request); break;
        case this.forgotPwForm  : this.completeForgotPwForm(request); break;
        case this.signupForm    : this.completeSignupForm(request); break;
        case this.editForm      : this.completeEditForm(request); break;
      }
    }
    else { 
      form.enable();
      if (!request.responseText.blank()) {
        var flashHTML = DIV([H3('Errors On Form!'),this.messagesToList(request)]);
        this.flashModal('bad',flashHTML);
      };
    };
  },
  
  completeAjaxForm: function(form) {
    var options = Object.extend({mood:'good'}, arguments[1] || {});
    var loadArea = form.down('#form_loading');
    var completeId = 'complete_ajax_form_' + form.id;
    var imgSrc = '/images/'+options.mood+'.png';
    var moodHtml = SPAN({id:completeId,className:'m0 p0'}, [IMG({src:imgSrc})]);
    $(loadArea).update(moodHtml);
    setTimeout(function() { $(completeId).fade(); },3000);
  },
  
  submitSupportForm: function(event) { 
    this.startAjaxForm(event,{imgSrc:'loading.gif'});
  },
  
  completeSupportForm: function(request) {
    setTimeout(function(){ this.supportForm.reset(); this.supportForm.enable(); }.bind(this),2000);
    this.toggleAjaxFormBlind();
    this.flash('good','Thanks for submitting a support request!');
  },
  
  completeLoginForm: function(request) {
    this.redirectToUserHome();
  },
  
  completeForgotPwForm: function(request) {
    var message = "Please check your email for a link that will allow you to automatically login and " +
      "and reset your password. If you still have problems, use our support form on the help page.";
    var flashHTML = DIV([H3('Forgot Password Email Sent:'),P(message)]);
    this.flash('good',flashHTML);
  },
  
  completeSignupForm: function(request) {
    var message = "Thank your for signing up for your own HomeMarks page. An email has been sent to " +
      "your address along with a link to activate your account. If you have not done so already, please " +
      "take a moment to read the HomeMarks documentation and help section.";
    var flashHTML = DIV([H3('Signup Complete:'),P(message)]);
    this.flash('good',flashHTML);
  },
  
  completeEditForm: function(request) {
    var message = "You are being redirected to your HomeMarks page...";
    var flashHTML = DIV([H3('Account Updated:'),P(message)]);
    this.flash('good',flashHTML);
    this.redirectToUserHome.delay(2);
  },
  
  toggleLoginForgotPwForms: function(event) {
    if (this.loginArea.visible()) { this.loginArea.hide(); this.forgotPwArea.show(); }
    else { this.loginArea.show(); this.forgotPwArea.hide(); };
  },
  
  initEvents: function() {
    if (this.supportForm) { this.supportForm.observe('submit', this.submitSupportForm.bindAsEventListener(this)); };
    if (this.signupForm) { this.signupForm.observe('submit', this.startAjaxForm.bindAsEventListener(this)); };
    if (this.editForm) { this.editForm.observe('submit', this.startAjaxForm.bindAsEventListener(this)); };
    if (this.loginArea) {
      this.loginForm.observe('submit', this.startAjaxForm.bindAsEventListener(this));
      this.forgotPwForm.observe('submit', this.startAjaxForm.bindAsEventListener(this));
      this.forgotPwButton.observe('click',this.toggleLoginForgotPwForms.bindAsEventListener(this));
      this.forgotPwCancel.observe('click',this.toggleLoginForgotPwForms.bindAsEventListener(this));
    };
    this.ajaxFromLinks.each(function(element){ 
      element.observe('click', this.toggleAjaxFormBlind.bindAsEventListener(this)); 
    }.bind(this));
  }

});





/*test*/

var Quickie = Class.create({
  
  initialize: function(path, options){
    var time = Try.these(
      function() { return Date.now() },
      function() { return +new Date }
    );
    var defaultOptions = {
      id: null,
      height: 1,
      width: 1,
      container: null,
      attributes: {}
    }
    this.path = path;
    this.instance = 'Quickie_' + time;
    this.options = Object.extend(defaultOptions, arguments[1] || {});
    this.id = this.options.id || this.instance;
    this.container = $(this.options.container);
    this.attributes = this.options.attributes;
    this.attributes.src = this.path;
    this.height = (this.attributes.controller == 'true') ? this.options.height + 16 : this.options.height; 
    this.width = this.options.width;
    this._assignElement();
  },
  
  toElement: function() {
    return this.element;
  },
  
  _assignElement: function() {
    var build;
    if (Prototype.Browser.IE) {
      build = this._buildObjectTag()
    } else if (Prototype.Browser.WebKit) {
      build = this._buildVideoTag();
    } else {
      build = this._buildEmbedTag();
    };
    this.element = ((this.container) ? this.container.update('') : new Element('div')).update(build).down();
  },
  
  _buildObjectTag: function() {
    var build = "";
    build = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"';
    build += ' id="' + this.id + '"';
    build += ' width="' + this.width + '"';
    build += ' height="' + this.height + '"';
    build += '>';
    for (var attribute in this.attributes) {
      if (this.attributes[attribute]) {
        build += '<param name="' + attribute + '" value="' + this.attributes[attribute] + '" />';
      }
    }
    build += '</object>';
    return build;
  },
  
  _buildVideoTag: function() {
    var build = "";
    build = '<video';
    if (this.attributes.autoplay == 'true') { build += ' autoplay '; };
    if (this.attributes.loop == 'true') { build += ' loop '; };
    build += ' src="' + this.attributes.src + '"';
    build += ' width="' + this.width + '"';
    build += ' height="' + this.height + '"';
    build += '>';
    build += '</video>';
    return build;
  },
  
  _buildEmbedTag: function() {
    var build = "";
    build = '<embed ';
    build += ' id="' + this.id + '"';
    build += ' width="' + this.width + '"';
    build += ' height="' + this.height + '"';
    for (attribute in this.attributes) {
      if (this.attributes[attribute]) {
        build += ' ' + attribute + '="' + this.attributes[attribute] + '"';
      }
    }
    build += ' pluginspage="http://www.apple.com/quicktime/download/"></embed>';
    return build;
  }

});




var MyQuickie = {};

MyQuickie.defaultAttrs = {
  controller: 'false',
  autoplay: 'true',
  loop: 'true',
  enablejavascript: 'false'
};

MyQuickie.getContainerAttributes = function(container) {
  var attributes = Object.clone(MyQuickie.defaultAttrs);
  container.readAttribute('rel').split('|').each(function(keyvalue) {
    pair = keyvalue.split('=');
    attributes[pair[0]] = pair[1];
  });
  return attributes;
};

MyQuickie.getSome = function(container) {
  var attributes = MyQuickie.getContainerAttributes(container);
  var qt = new Quickie(attributes['src'], {
    id: container.id.sub('container','movie'), 
    width: container.getWidth(), 
    height: container.getHeight(), 
    container: container, 
    attributes: attributes
  });
  container.update(qt);
};


Event.observe(window,'load',function() {
    
  $$('div.quicktime_video').each(function(container){
    if (container.hasClassName('quickie')) {
      MyQuickie.getSome(container); // Load up the QuickTime now.
    } else {                        // Means we have a Lazie, essentially a poster image.
      var img = container.down('img');
      img.observe('click',MyQuickie.getSome.curry(container));
    };
  });
  
});

