// main js

function validate_email(field) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos < 1 || dotpos - apos < 2) {
			return false;
		} else {
			return true;
		}
	}
}
function validate_text(field) {
	with (field) {
		if (value == null || value == "") {
			return false;
		} else {
			return true
		}
	}
}
function validate_pass(field, field_val) {
	if (field.value != field_val.value) {
		return false;
	} else {
		return true
	}
}


var timer = null;
var timer2 = null;

expandableBehavior = Behavior.create({
  initialize: function() {
    this.sub_menu = this.element.up('li').down('ul');
  },
  onmouseover: function(event) {
    clearTimeout(timer);
    hideAllNav();
    if(this.sub_menu) {
      this.sub_menu.show();
    }
  },
  onmouseout: function(event) {
    if(this.sub_menu) {
      timer = setTimeout(function(){
        this.hide();
      }.bind(this.sub_menu), 300);
    }
  }
});

function hideAllNav() {
  $$("a.expandable").each(function(_lnk) {
    _lnk.next('ul').hide();
  });
}

subNavBehavior = Behavior.create({
  onmouseover: function(event) {
    clearTimeout(timer);
  },
  onmouseout: function(event) {
    timer = setTimeout(function(){
      this.hide();
    }.bind(this.element.up('ul')), 300);
  }
});

expandable2Behavior = Behavior.create({
  initialize: function() {
    this.sub_menu = this.element.up('li').down('ul');
  },
  onmouseover: function(event) {
    clearTimeout(timer2);
    hideAllNav2();
    if(this.sub_menu) {
      this.sub_menu.show();
    }
  },
  onmouseout: function(event) {
    if(this.sub_menu) {
      timer2 = setTimeout(function(){
        this.hide();
      }.bind(this.sub_menu), 300);
    }
  }
});

function hideAllNav2() {
  $$("a.expandable2").each(function(_lnk) {
    _lnk.next('ul').hide();
  });
}

sub2NavBehavior = Behavior.create({
  onmouseover: function(event) {
    clearTimeout(timer2);
    clearTimeout(timer);
  },
  onmouseout: function(event) {
    timer2 = setTimeout(function(){
      this.hide();
    }.bind(this.element.up('ul')), 300);

    timer = setTimeout(function(){
      this.hide();
    }.bind(this.element.up('ul', 1)), 300);
  }
});

Event.addBehavior({
  'a.expandable': expandableBehavior,
  '#menu>li>ul>li>a' : subNavBehavior,
  'a.expandable2': expandable2Behavior,
  '#menu>li>ul>li>ul>li>a' : sub2NavBehavior
});
