// Based on bernard's code. CALL him for advices ($10 per adivce)
var FAQ = {
  openAnswer: function(answer, opener) {
    var answerId = this.getAnswerId(answer);
    var answerElem = $(answerId);
    var visible = Element.visible(answerId);
    if (visible) {
      Effect.toggle(answerId)
      this.visibleQuestion = this.opener = null;
      this.switchClasses(opener, 0);
    } else {
      if (null != this.visibleQuestion)
        Effect.toggle(this.visibleQuestion);
      if (null != this.opener)
        this.switchClasses(this.opener, 0);
      this.opener = opener;
      if (null != this.opener)
        this.switchClasses(this.opener, 1);
      this.visibleQuestion = answerId;
      //alert(answerElem.offsetWidth);
      //alert(answerElem.offsetWidth);
      Effect.toggle(answerId);
    }
  },
  getAnswerId: function(answer) {
    return 'faq_answer_' + answer;
  },
  switchClasses: function(elem, index) {
    var classNames = ['collapsed', 'expanded'];
    Element.removeClassName(elem, classNames[1 - index]);
    Element.addClassName(elem, classNames[index]);
  }
};

function getClassName(className, withName, withoutName) {
  if (null == className) className = "";
  var styles = className.split(" ");
  var found = false;
  for (var i = styles.length; --i >= 0; ) {
    if (styles[i] == withoutName) {
      styles[i] = withName;
      found = true;
    } else if (styles[i] == withName) {
      found = true;
    }
  }
  if (!found) styles.push(withName);
  return styles.join(' ');
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


var rowRollover = {
	tipElements : ['tr', 'td', 'div'], // @Array: Allowable elements that can have the rollover effect
    onClass: 'ron',
    offClass: 'roff',
    init : function(options) {
    options = options || {};
    if (null != options['onClass']) this.onClass = options['onClass'];
    if (null != options['offClass']) this.onClass = options['offClass'];
    var parent = options['parent'] || document;
    var   i;
    for ( i=0; i <this.tipElements.length; i++ ) {
      var current = parent.getElementsByTagName(this.tipElements[i]);
      var curLen = current.length;
      for ( j=0; j<curLen; j++ ) {
        if (current[j].className.indexOf('roff') >= 0 && current[j].id != null) {
          addEvent(current[j],'mouseover',this.ron);
          addEvent(current[j],'mouseout',this.roff);
        }
      }
    }
  },
  ron: function(e) { rowRollover.doRowRollover(this, 1); rowRollover.callIfExists("rowTurnedOn", this); },
  roff: function(e){ rowRollover.doRowRollover(this, 0); rowRollover.callIfExists("rowTurnedOff", this); },
  doRowRollover: function(row, isInRow) {
    var className = row.className;
    var styles=[rowRollover.onClass, rowRollover.offClass];
    row.className = rowRollover.switchClassName(className, styles[1-isInRow], styles[isInRow]);
  },
  switchClassName: function(className, withName, withoutName) {
    if (null == className) className = "";
    var styles = className.split(" ");
    var found = false;
    for (var i = styles.length; --i >= 0; ) {
      if (styles[i] == withoutName) {
        styles[i] = withName;
        found = true;
      } else if (styles[i] == withName) {
        found = true;
      }
    }
    if (!found) styles.push(withName);
    return styles.join(' ');
  },
  callIfExists: function(funcName, parm1) {
    var f = rowRollover[funcName];
    if (null != f) {
      f(parm1);
    }
  }
}

function hideFlash() {
  var div = $('notice_div');
  if (div && Element.visible(div)) {
    Element.hide(div);
  }
}

var Utils = {
	redirectTo: function(url) {
      window.location.href = url;
  },
  now: function() {
      return new Date();
  },
	centerInWindow: function(element) {
		this.alignInWindow(element, 0.5, 0.33);
	},
	alignInWindow: function(element, horizontalRatio, verticalRatio) {
		var windowDimensions = Utils.windowDimensions();
		var elementDimensions = Element.getDimensions(element);

		var new_left = (windowDimensions.width - elementDimensions.width)*horizontalRatio + "px";
		var new_top = (windowDimensions.height - elementDimensions.height)*verticalRatio + "px";
		element.style.left = new_left;
		element.style.top = new_top;
	},
	windowDimensions: function() {
		return {
			height: this.window().innerHeight || document.body.clientHeight,
			width: this.window().innerWidth || document.body.clientWidth
		}
	},
	useMockWindow: function (w) {
		this.mockedWindow = w;
	},
	window: function() {
		return this.mockedWindow || window;
	},
  redirectIfConfirm: function(msg, url) {
    if (confirm(msg)) this.redirectTo(url);
  },
  deSnap: function() {
    var children = document.body.getElementsByTagName('a');
    var i;
    for (i = children.length; --i >= 0; ) {
      var a = children[i];
      if (!Element.hasClassName(a, 'snap_preview'))
        Element.addClassName(a, 'snap_nopreview');
    }
  },
  setSubmit: function() {
    $('submit').disabled = !$F('credit_card_terms_accepted');
  }
};

function loadPage () {
}
