Function.prototype.bindAsEventListener = function(object, element) {
  var __method = this;
  element = $(element);
  return function(event) {
    return __method.call(object, event || window.event, element);
  }
}

Form.InlineHints = function(form, hints) {
    this.form = $(form);
    this.hints  = hints;
    this.onFocusEL  = this.onFocus.bindAsEventListener(this);
    this.onBlurEL   = this.onBlur.bindAsEventListener(this);
    this.setInputsEL    = this.setInputs.bindAsEventListener(this);
    this.resetInputsEL  = this.resetInputs.bindAsEventListener(this)

    for (var name in this.hints) {
    var i = this.form[name];
    Event.observe(i, 'focus', this.onFocusEL);
    Event.observe(i, 'blur', this.onBlurEL);
    }
    this.setInputs();
    Event.observe(this.form, 'reset', this.setInputsEL);
    Event.observe(this.form, 'submit', this.resetInputsEL);
};

Form.InlineHints.prototype = {
    onFocus:
    function(e) {
        var i = Event.element(e);
        if (i.value.replace(/[\r\n]/g, '') == this.hints[i.name].replace(/[\r\n]/g, '')) {
            i.style.color = '#000';
            i.value = '';
        }
    },
    
    onBlur:
    function(e) {
        var i = Event.element(e);
        if (i.value == '') {
            i.style.color = '#808080';
            i.value = this.hints[i.name];
        }
    },
    
    setInputs:
    function() {
        for (var name in this.hints) {
            var i = this.form[name];
            if (i.value == '' || i.value.replace(/[\r\n]/g, '') == this.hints[name].replace(/[\r\n]/g, '')) {
                i.style.color = '#808080';
                i.value = this.hints[name];
            }
            else {
                i.style.color = '#000';
            }
        }
    },
    
    resetInputs:
    function() {
        for (var name in this.hints) {
            var i = this.form[name];
            if (i.value.replace(/[\r\n]/g, '') == this.hints[name].replace(/[\r\n]/g, '')) {
                i.style.color = '#000';
                i.value = '';
            }
        }
    }
};

function buttonStateHandler(button, enabled) {
 if (button == "prev-arrow-small") 
   $('prev-arrow-small').src = enabled ? "/images/prev-arrow-small-enabled.gif" : "/images/prev-arrow-small-disabled.gif"
 else 
   $('next-arrow-small').src = enabled ? "/images/next-arrow-small-enabled.gif" : "/images/next-arrow-small-disabled.gif"
}

function animHandler(carouselID, status, direction) {
  var region = $(carouselID).down(".carousel-clip-region")
  if (status == "before") {
    Effect.Fade(region, {to: 0.3, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
  if (status == "after") {
    Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
}


function showBox(){
    $('overlay').show();
    center('overlay-box');
    return false;
}

function hideBox(){
    $('overlay-box').hide();
    $('overlay').hide();
    return false;
}

function center(element){
    try {
        element = document.getElementById(element);

    } catch(e) {
        return;
    }

    var my_width = 0;
    var my_height = 0;

    if (typeof(window.innerWidth) == 'number') {
        my_width = window.innerWidth;
        my_height = window.innerHeight;

    } else if (document.documentElement && 
    (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        my_width = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;

    }
     else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        my_width = document.body.clientWidth;
        my_height = document.body.clientHeight;

    }

    element.style.position = 'absolute';
    element.style.zIndex = 10000;

    var scrollY = 0;

    if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;

    } else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;

    } else if (window.pageYOffset) {
        scrollY = window.pageYOffset;

    } else if (window.scrollY) {
        scrollY = window.scrollY;

    }

    var elementDimensions = Element.getDimensions(element);

    var setX = (my_width - elementDimensions.width) / 2;
    var setY = (my_height - elementDimensions.height) / 2 + scrollY;

    setX = (setX < 0) ? 0: setX;
    setY = (setY < 0) ? 0: setY;

    element.style.left = setX + "px";
    element.style.top = setY + "px";

    element.style.display = 'block';
}

function photoButtonStateHandler(button, enabled) {
 if (button == "prev-photo") 
   $('prev-photo').src = enabled ? "/images/prev-photo-enabled.gif" : "/images/prev-photo-disabled.gif"
 else 
   $('next-photo').src = enabled ? "/images/next-photo-enabled.gif" : "/images/next-photo-disabled.gif"
}

function photoAnimHandler(carouselID, status, direction) {
  var region = $(carouselID).down(".carousel-clip-region")
  if (status == "before") {
    Effect.Fade(region, {to: 0.3, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
  if (status == "after") {
    Effect.Fade(region, {to: 1, queue: { position:'end', scope: "carousel" }, duration: 0.2})
  }
}