/**
 * @author Rob
 */
Element.implement({
	extra_dimensions: function(dir,padding,border,margin) {
	    if(typeof(dir)=='undefined') dir = 'x';
	    if(typeof(padding)=='undefined') padding = true;
	    if(typeof(border)=='undefined') border = true;
	    if(typeof(margin)=='undefined') margin = false;
	    var x = 0;
	    if(dir=='x') {
	            dir = ['left','right'];
	    } else {
	            dir = ['top','bottom'];
	    }
	    if(padding) {
	            x += this.getStyle('padding-' + dir[0]).toInt();
	            x += this.getStyle('padding-' + dir[1]).toInt();
	    }
	    if(border) {
	            x += this.getStyle('border-' + dir[0] + '-width').toInt();
	            x += this.getStyle('border-' + dir[1] + '-width').toInt();
	    }
	    if(margin) {
	            x += this.getStyle('margin-' + dir[0]).toInt();
	            x += this.getStyle('margin-' + dir[1]).toInt();
	    }
	    return x;
	}
});
window.addEvent('load', function() {
	new Ajax('/ajax/hp_caroussel.php', {
		method: 'get',
		onComplete: function(text, html){
			
			$('carshow').setHTML(text);
			if($('carshow')) {
				var items = $('carshow').getElements('div.car');
				var width = items.length * 105;
				var wrappert = new Element('div').setStyles({
					width: $('carshow').getSize().size.x.toInt(),
					height: $('carshow').getSize().size.y.toInt()
				}).injectAfter($('carshow'));
				$('carshow').injectInside(wrappert);
				$('carshow').setStyles({
					width: width,
					margin: 0
				});
				wrappert.setProperty('id', 'carshow_wrapper');
				new slider($('carshow_wrapper'));
			}
		
		}
	}).request();
	
	
	
});
var slider = new Class({
	Implements: [Options, Events],
	options: {
		height: false,
		width: 105,
		interval: 2500,
		selector: 'div.car',
		rotate: false
	},
	initialize: function(container, options) {
		//this.setOptions(options);
		this.container = container;
		if(this.container != null) {
			this.objects = this.container.getElements(this.options.selector);
			if(this.objects.length > 1) {
				if(this.objects.length > 2 && this.options.rotate) {
					this.objects[0].clone().inject(this.container);
					this.objects = this.container.getElements(this.options.selector);
				}
				this.current = 0;
				this.fx = new Fx.Scroll(this.container);
				this.start_timer();
				this.objects.each( function(el) {
					el.addEvents({
						mouseenter: function() {
							this.stop_timer();
						}.bind(this),
						mouseleave: function() {
							this.start_timer();
						}.bind(this)
					});
				}.bind(this));
				
//				this.previous = new Element('div').addClass('previous').injectAfter(this.container);
//				this.previous.addEvents({
//					mouseenter: function() {
//						this.stop_timer();
//					}.bind(this),
//					mouseleave: function(){
//						this.start_timer();
//					}.bind(this),
//					click: function() {
//						this.goto_previous();
//					}.bind(this)
//				});
//				this.next = new Element('div').addClass('next').injectAfter(this.container);
//				this.next.addEvents({
//					mouseenter: function() {
//						this.stop_timer();
//					}.bind(this),
//					mouseleave: function(){
//						this.start_timer();
//					}.bind(this),
//					click: function() {
//						this.goto_next();
//					}.bind(this)
//				});
			}
		}
	},
	start_timer: function() {
		$clear(this.timer);
		this.timer = (function() { this.walk(); }.bind(this)).periodical(this.options.interval);
	},
	stop_timer: function() {
		$clear(this.timer);
	},
	walkto: function(next, manual) {
		var previous = this.current;
		if(typeof(manual)=='undefined') manual = true;
		if(next >= this.objects.length || next < 0) next = 0;
		if (next != this.current) {
			this.objects[this.current].removeClass('screen');
			var nextpos = next * (this.options.height != false ? this.options.height : this.options.width);
			if (this.objects.length > 2 && next == 0 && !manual) {
				this.fx.set(0, 0);
				previous = 0;
				next = 1;
				nextpos = (this.options.height != false ? this.options.height : this.options.width);
			}
			if (this.options.height != false) {
				this.fx.scrollTo(0, nextpos);
			}
			else {
				this.fx.scrollTo(nextpos, 0);
			}
			this.current = next;
			this.objects[this.current].addClass('screen');
		}
	},
	walk: function() {
		var next = this.current + 1;
		this.walkto(next, false);
	},
	goto_next: function() {
		var next = this.current + 1;
		this.walkto(next, true);
	},
	goto_previous: function() {
		var next = this.current - 1;
		this.walkto(next, true);
	}
});
