Position.scrollX = function(){
  return (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0);
}

var Loader = {
  _assets: $H({}),
  _checkInterval: null,
  _options: {},
  _update: function() {
    var allLoaded = true;
    Loader._assets.each(function(a){
      if(!a[1].complete && a[1].image.complete) {
        a[1].complete = true;
        a[1].completed_at = new Date().getTime();;
        if(a[1].options.onComplete) a[1].options.onComplete(a[0]);
      }
      if(!a[1].complete && !a[1].image.complete) allLoaded = false;
    });
    if(allLoaded) {
      clearInterval(Loader._checkInterval);
      Loader._checkInterval = null;
      if(Loader._options && Loader._options.onComplete) Loader._options.onComplete();
      Loader._options = null;
    }
  },
  initialize: function() {
    var options = arguments[0] || {};
    Loader._options = options;
  },
  cacheOrLoad: function(url) {
    var options = arguments[1] || {};
    if(this.isLoaded(url)) {
      if(options.onComplete) options.onComplete();
    } else {
      this.load(url, options);
    }
  },
  load: function(url) {
    if(Loader._assets[url]) return;
    var options = arguments[1] || {};
    var a = {};
    a.image = new Image();
    a.image.src = url;
    a.complete = false;
    a.options  = options;
    a.loaded_at = new Date().getTime();
    Event.observe(a.image, 'error', function(){ Loader.error(url) });
    Loader._assets[url] = a;
    if(!Loader._checkInterval) Loader._checkInterval = setInterval(Loader._update,10);
  },
  error: function(url) {
    var asset = Loader._assets[url];
    asset.complete = true;
    if(asset.options.onComplete) asset.options.onComplete('/images/empty.gif');
  },
  stats: function(url) {
    return (Loader._assets[url]._complete ?
      (Loader._assets[url]._completed_at - Loader._assets[url]._loaded_at) : null);
  },
  isQueued: function(url) {
    return !!(Loader._assets[url]);
  },
  isLoaded: function(url) {
    return (Loader._assets[url] && Loader._assets[url].complete);
  },
  reset: function() {
    Loader._assets = $H({});
  }
};

Event.localPointer = function(event){
  var p = [Event.pointerX(event), Event.pointerY(event)];
  var element = arguments[1] || Event.element(event);
  var e = Position.page($(element));
  return [
    p[0]-(e[0]+(window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0)),
    p[1]-(e[1]+(window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop  || 0))];
};

Position.getPageSize = function() {
  var xScroll, yScroll;
  if (window.scrollMaxX) {  
    xScroll = window.innerWidth  + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else {
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } 
  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
  pageHeight = Math.max(windowHeight, yScroll);
  pageWidth = Math.max(windowWidth, xScroll);
  return { page: { width: pageWidth, height: pageHeight }, window: { width: windowWidth, height: windowHeight } };
}
Effect.HScroll = Class.create();
 Object.extend(Object.extend(Effect.HScroll.prototype, Effect.Base.prototype), {
   initialize: function(delta) {
     this.scrollStart = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
     var w = Position.getPageSize();
     if(this.scrollStart + delta < 0) delta = -this.scrollStart;
     if(this.scrollStart + delta > (w.page.width-w.window.width)) delta = (w.page.width-w.window.width) - this.scrollStart;
     this.delta   = delta;
     this.start(arguments[1] || {});
   },
   update: function(position) {
     Position.prepare();
     window.scrollTo(this.scrollStart + (position*this.delta), 0); 
   }
 });
Effect.MoveRight = Class.create();
Object.extend(Object.extend(Effect.MoveRight.prototype, Effect.Base.prototype), {
  initialize: function(element, delta) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      x:    delta
    }, arguments[2] || {});
    this.start(options);
  },
  setup: function() {
    this.originalRight = this.options.initialRight || parseFloat(this.element.getStyle('left') || '0');
  },
  update: function(position) {
    this.element.setStyle({
      left: Math.round(this.options.x  * position + this.originalRight) + 'px'
    });
  }
});
Effect.NO_CLIP = ((window.opera || navigator.userAgent.indexOf('KHTML') > -1) ? '' : 'rect(auto, auto, auto, auto)');
Effect.slideLeft = function(ref, counter) {
	return Effect.slide(ref, counter, 'left');
};
Effect.slide = function(ref, counter, direction) {
	if(typeof(direction) != 'string'){
		return false;
	}
	direction = direction.toLowerCase();
	var direct = "";
	switch(direction){
		case 'top':
			// fall through
		case 'bottom':
			direct = "marginTop";
			break;
		case 'right':
			// fall through
		case 'left':
			direct = "marginLeft";
			break;
		default:
			return false;
	}
	var cP = Math.pow(Math.sin(Math.PI*counter/200),0.75);
	if(isNaN(cP)){
		cP = 0;
	}
	if (typeof ref.__zp_origmargin == 'undefined') {
		ref.__zp_origmargin = ref.style[direct];
	}
	if(counter == 100){
		ref.style.clip = Effect.NO_CLIP;
	} else {
		switch(direction){
			case 'top':
				ref.style.clip = 'rect(' +
					(ref.offsetHeight*(1 - cP)) + 'px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';
				ref.style.marginTop = '-' + (ref.offsetHeight * (1 - cP)) + 'px';
				break;
			case 'right':
				ref.style.clip = 'rect(' +
					'0px, ' +
					(ref.offsetWidth * cP) + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';
				ref.style.marginLeft = (ref.offsetWidth * (1 - cP)) + 'px';
				break;
			case 'bottom':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight*cP + 'px, ' +
					'0px' +
				')';
				ref.style.marginTop = (ref.offsetHeight * (1 - cP)) + 'px';
				break;
			case 'left':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					ref.offsetWidth*(1 - cP) + 'px' +
				')';
				ref.style.marginLeft = "-" + (ref.offsetWidth * (1 - cP)) + 'px';
				break;
		}
	}
	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
	}
};
Effects = {};
Effects.show = function(ref, animSpeed, effects, onFinish) {Effects.init(ref, true, animSpeed, effects, onFinish);};
Effects.hide = function(ref, animSpeed, effects, onFinish) {Effects.init(ref, false, animSpeed, effects, onFinish);};
Effects.init = function(ref, show, animSpeed, effects, onFinish){
	if(typeof ref == "string"){
		ref = document.getElementById(ref);
	}
	if(ref == null){
		return null;
	}
	ref.animations = [];
	if(effects == null || effects.length == 0){
		ref.style.display = show ? "" : "none";
		if(onFinish != null){
			onFinish();
		}
		return null;
	}
	if(typeof effects == "string")
		effects = [effects];
	for(var i = 0; i < effects.length; i++){
		var effect = null;
		switch(effects[i]){
			case 'slideLeft':
				effect = Effect.slideLeft;
				break;
		}
		if(effect != null){
			ref.animations.push(effect);
		}
	}
	if(ref.animations.length != 0 && ref.running == null) {
		ref.running = true;
		Effects.run(ref, animSpeed, show, null, onFinish);
	}
};
Effects.run = function(ref, animSpeed, show, currVal, onFinish) {
	if(animSpeed == null)
		animSpeed = 10;
	if(currVal < 0){
		currVal = 0;
	}
	if(currVal > 100){
		currVal = 100;
	}
	if(currVal == null) {
		if(show){
			currVal = 0
			if(ref.style.display == "none"){
				ref.style.display = '';
			}
		}
		else {
			currVal = 100;
		}
	}
	currVal += (show ? 1 : -1) * animSpeed;
	for (var i = 0; i < ref.animations.length; i++) {
		ref.animations[i](ref, currVal);
	}
	if (currVal <= 0 || currVal >= 100) {
		ref.running = null;
		if(onFinish != null){
			onFinish();
		}
		return;
	}
	else {
		setTimeout(function() {
			Effects.run(ref, animSpeed, show, currVal, onFinish);
		}, 50);
	}
};
Effects.apply = function(ref, effect, params){
	if(ref == null || effect == null) {
		return;
	}
	if(typeof ref == "string") {
		ref = document.getElementById(ref);
	}
	if(ref == null) {
		return;
	}
};
function Slideshow(slideshow, timeout) {
  this.slides = [];
  var nl = $(slideshow).getElementsByTagName('div');
  for (var i = 0; i < nl.length; i++) {
    if (Element.hasClassName(nl[i], 'slide')) {
      this.slides.push(nl[i]);
    }
  }
  this.timeout = timeout;
  this.current = 0;
  for (var i = 0; i < this.slides.length; i++) {
    this.slides[i].style.zIndex = this.slides.length - i;
  }
  Element.show(slideshow);
  setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
}
Slideshow.prototype = {
  next: function() {
    for (var i = 0; i < this.slides.length; i++) {
      var slide = this.slides[(this.current + i) % this.slides.length];
      slide.style.zIndex = this.slides.length - i;
    }
    Effect.Fade(this.slides[this.current], {
	   duration: 2.0,
      afterFinish: function(effect) {
        effect.element.style.zIndex = 0;
        Element.show(effect.element);
        Element.setOpacity(effect.element, 1);
        effect.element.style.top = '-580px';
      }
    });

    this.current = (this.current + 1) % this.slides.length;
    new Effect.Move(this.slides[this.current],{y: 580,duration: 3.0, queue: 'end'});
    setTimeout((function(){this.next();}).bind(this), this.timeout + 850);
  }
}