/*  Prototype JavaScript framework
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
/*--------------------------------------------------------------------------*/
var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

var imgslider = new Object();
//base
imgslider.Base = function(){};
imgslider.Base.prototype = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		transition: imgslider.sinoidal
	}
	Object.extend(this.options, options || {});
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.increase();
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	hide: function() {
		this.now = 0;
		this.increase();
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	}
}

imgslider.Slide = Class.create();
imgslider.Slide.prototype = Object.extend(new imgslider.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.setOptions(options);
		this.now = 0;
	},
	increase: function() {		
		this.el.style.marginLeft = this.now + "px";	
		
	},
	slide: function(howMuch){
		this.clearTimer();
		this.custom(this.now, howMuch * this.options.distance * -1);
	}
});

var featured = {
	vids : null, currentNum : 0, slider : null,
	init : function(){
		this.vids = $('imgdiv').getElementsByTagName('div').length;
		this.slider = new imgslider.Slide($('imgdiv'), {distance: 480, duration: 800});
		
	},
	toggle : function(direction){
		if(!this.slider) this.init();
		var new_num = this.currentNum + direction;
		if(new_num < 0) new_num = 0;
		if(new_num > (this.vids / 3) - 1) new_num = 2;
		this.currentNum = new_num;
		this.slider.slide(this.currentNum);
		
	}
}
imgslider.sinoidal = function(pos){return ((-Math.cos(pos*Math.PI)/2) + 0.5);}
