var search_box = null;
var more_toggles = null;
var more_content_height = [];
var more_content_fx = [];

window.addEvent('domready', function() {

  if ($('mr_contact_box')) {
    var contact_accordion = new ContactAccordion();
  }

	if (Browser.Engine.trident && Browser.Engine.version <= 4) {
		$$("#nav li").each(function(el) {
			el.addEvent('mouseenter', menuOver.bind(el));
			el.addEvent('mouseleave', menuOut.bind(el));
		});
	}

  var sidebar_images = $$('#sidebar_links img');
  if (sidebar_images.length) {
    sidebar_images.each(function(s_image) {
      s_image.addEvent('mouseenter', imageOver.bind(s_image));
			s_image.addEvent('mouseleave', imageOut.bind(s_image));
    });
  }

	// handle all more/less text boxes
	more_toggles = $$('.more_toggle');
	if (more_toggles) {
		var idx = 0;
		more_toggles.each(function(toggle) {
			toggle.addEvent('click', function(e) {
				e = new Event(e).stop();
				var tar = $(e.target);
				while (!tar.hasClass('more_toggle')) {
					tar = $(tar.parentNode);
				}
				var content = tar.getPrevious('div');
				if (content.hasClass('more_content')) {
					var idx = content.getAttribute('rel');
					if (content.getSize().y) {
						more_content_fx[idx].start('height', 0);
					}
					else {
						more_content_fx[idx].start('height', more_content_height[idx]);
					}
				}
			});
			var content = toggle.getPrevious('div');
			if (content.hasClass('more_content')) {
				content.setAttribute('rel', idx);
				more_content_fx[idx] = new Fx.Tween(content, {onComplete : function() {
					var content = $(this.element);
					var a = content.getNext('a');
					if (a.hasClass('more_toggle')) {
						if (content.getSize().y) {
							a.set('html', 'Show Less');
							a.removeClass('more_toggle_closed');
						}
						else {
							a.set('html', 'Show More');
							a.addClass('more_toggle_closed');
						}
					}
				}});
				more_content_height[idx] = content.getSize().y;
				more_content_fx[idx].set('height', 0);
				toggle.setStyle('display', 'inline');
				toggle.set('html', 'Show More');
				toggle.addClass('more_toggle_closed');
			}
			idx++;
		});
	}

  setTimeout('delayed_load()', 250);
});

function delayed_load()
{


}

function menuOver() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'main_nav') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.addClass("sfhover");
	}
}

function menuOut() {
	tar = this;
	while (tar.tagName != 'LI') {
		if (tar.getAttribute('id') == 'main_nav') {
			break;
		}
		tar = tar.parentNode;
	}
	if (tar.tagName == 'LI') {
		tar.removeClass("sfhover");
	}
}

function imageOver() {
	tar = this;
	while (tar.tagName != 'IMG') {
		tar = tar.parentNode;
	}
	if (tar.tagName == 'IMG') {
    var src = tar.getAttribute('src');
    var idx = src.lastIndexOf('.');
    if (idx) {
      var ext = src.substring(idx+1);
      var url = src.substring(0, idx-2);
      tar.setAttribute('src', url+'_o.'+ext);
    }
	}
}

function imageOut() {
	tar = this;
	while (tar.tagName != 'IMG') {
		tar = tar.parentNode;
	}
	if (tar.tagName == 'IMG') {
    var src = tar.getAttribute('src');
    var idx = src.lastIndexOf('.');
    if (idx) {
      var ext = src.substring(idx+1);
      var url = src.substring(0, idx-2);
      tar.setAttribute('src', url+'_s.'+ext);
    }
	}
}

var ClearTextInput = new Class({

	Implements: [Events, Options],

  options: {
/*  onFocus: $function,
    onBlur: $function  */
  },

  element: null,

  initialize: function(element, options)
  {
    this.setOptions(options);
    this.element = $(element);
    this.element.addEvent('focus', function(e) {
      if (this.element.value == this.element.defaultValue) {
        this.element.value = '';
      }
      this.fireEvent('focus', [this.element]);
    }.bind(this));
    this.element.addEvent('blur', function(e) {
      if (this.element.value == '') {
        this.element.value = this.element.defaultValue;
      }
      this.fireEvent('blur', [this.element]);
    }.bind(this));
  }

});

var ContactAccordion = new Class({

  accordion: null,
  callback_form: null,
  question_form: null,
  phone_number: null,
  name: null,
  question_name: null,
  question_email: null,
  question_textarea: null,
  visible: false,
  img_submit: null,

  initialize: function() {
    this.accordion = new Accordion($$('#accordion_contact_box .contact_box_toggler'), $$('#accordion_contact_box .contact_box_element'),
                                    {display: -1});
    this.callback_form = document.forms['call_me_back'];
    this.phone_number = $(this.callback_form.phone_number);
    this.name = $(this.callback_form.name);
    this.callback_form = $(this.callback_form);
    this.question_form = document.forms['quick_question'];
    this.question_name = $(this.question_form.name);
    this.question_email = $(this.question_form.email);
    this.question_textarea = $(this.question_form.question);

    new ClearTextInput(this.phone_number, {
      onFocus: function(e) {
        this.accordion.display(0);
      }.bind(this)
    });
    new ClearTextInput(this.name);
    new ClearTextInput(this.question_name);
    new ClearTextInput(this.question_email);
    new ClearTextInput(this.question_textarea);

    this.callback_form.addEvent('submit', function(e) {
      // check input
      this.accordion.display(0);
      why = '';
      if (this.phone_number.value == '' || this.phone_number.value == this.phone_number.defaultValue) {
        why += "Please enter your phone number\n";
      }
      if (this.name.value == '' || this.name.value == this.name.defaultValue) {
        why += "Please enter your name\n";
      }
      if (why != '') {
        alert (why);
        return false;
      }
      return true;
    }.bind(this));
  }

});


function checkEmail (strng) {
  var valid = true;
  if (strng == "") {
     valid = false;
  }
  else if (!isRFC822ValidEmail(strng)) {
     valid = false;
  }
  return valid;
}
function isRFC822ValidEmail(sEmail) {

  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string

  var reValidEmail = new RegExp(sValidEmail);

  if (reValidEmail.test(sEmail)) {
    return true;
  }

  return false;
}