
function _extends(sub,base){
	var sub_proto = clone(sub.prototype);
	var base_proto = clone(base.prototype);
	sub.prototype = base_proto;
	for(prop in sub_proto){
		sub.prototype[prop] = sub_proto[prop];
	}
	sub.prototype.__super__ = base.prototype;
}

slideshows = [];
num_slideshows = [];

function SlideShow(container_div){
	/*
	 * CMS slideshow Widget.
	 */
	this.container = container_div;
	this.__init__();
}

SlideShow.prototype.index = 0;

SlideShow.prototype.__init__ = function(){
	this.attach_listeners();
	this.load_slides();
	this.load_parameters();
	this.play();
}

SlideShow.prototype.attach_listeners = function(){
	this.listeners = {
			'container_click':connect(this.container,'onclick',bind(this.next,this))
	};
}

SlideShow.prototype.load_slides = function(){
	this.slides = getElementsByTagAndClassName('*', 'cms_slide_show_slide', this.container);
	forEach(this.slides, function(slide){
		hideElement(slide);
		setStyle(slide, {'visibility':'visible'});
	});
	showElement(this.slides[0]);
}

SlideShow.prototype.play = function(){
	appear(this.container);
	setStyle(this.container,{'height':getElementDimensions(this.slides[this.index],true).h+'px'});
	this.next_deferred = callLater(this.delay,bind(this.next,this))
}

SlideShow.prototype.stop = function(){
	this.next_deferred.cancel();
}

SlideShow.prototype.show = function(index){
	this.toggle_on = toggle(this.slides[index],this.effect,{'queue':{'scope':'img2','position':'break','limit':1},
		'duration':this.duration,
		'afterFinish':bind(function(){
			this.listeners['container_click'] = connect(this.container,'onclick',bind(this.next,this));
			},this)
	});
}

SlideShow.prototype.hide = function(index){
	this.toggle_off = toggle(this.slides[index],this.effect,{'queue':{'scope':'img1','position':'break','limit':1},
		'duration':this.duration
	});
}

SlideShow.prototype.next = function(next_index){
	disconnect(this.listeners['container_click']);
	var last_index = this.index;
	if(this.next_deferred && this.next_deferred.fired == -1){
		this.next_deferred.cancel();
	}
	try{
		this.hide(this.index);
	}
	catch(error){
		log(error);
	}
	try{
		this.index = (this.index+1 < this.slides.length)?this.index+1:0;
		this.show(this.index);
		this.current_page = this.slides[this.index];
	}
	catch(error){
		log(error);
	}
	setStyle(this.container,{'height':getElementDimensions(this.slides[this.index],true).h+'px'});
	signal(this,'next_page',{'current_index':this.index,'last_index':last_index});
	this.next_deferred = callLater(this.delay,bind(this.next,this));
}

SlideShow.prototype.load_parameters = function(){
	setStyle(this.container,{'width':getElementDimensions(this.container.parentNode).w+'px'});
	try{
		this.delay = getFirstElementByTagAndClassName('input', 'duration', this.container).value;
		this.duration = this.delay/2;
		this.effect = getFirstElementByTagAndClassName('input', 'effect', this.container).value;
	}
	catch(error){
		log(error);
		this.delay = 7;
		this.duration = 2.5;
		this.effect = "appear";
	}
}

function NumberedSlideShow(container_div){
	this.container = container_div;
	this.__init__();
}
NumberedSlideShow.prototype.__init__ = function(){
	bind(this.__super__.__init__,this)();
	this.create_numbers();
	addElementClass(this.numbers[0],'selected');
}

NumberedSlideShow.prototype.create_numbers = function(){
	this.numbers = [];
	this.numbers_container = DIV({'class':'numbers_container'});
	forEach(this.slides,bind(function(slide){
		var span = SPAN({'class':'cms_slide_show_number'});
		this.numbers.push(span)
		var index = this.numbers.length;
		this.listeners[['numbers_',this.numbers.length].join("")] = 
			connect(span,'onclick',bind(function(event){
				if(index-1 != this.index){
					removeElementClass(this.numbers[this.index],'selected');
					addElementClass(span,'selected')
					this.hide(this.index);
					this.show(index-1);
				}
				this.index = index-1;
				if(this.next_deferred){
					this.next_deferred.cancel();
				}
				this.next_deferred = callLater(this.delay,bind(this.next,this));
				event.stopPropagation();
			},this));
		span.innerHTML = index;
	},this));
	appendChildNodes(this.numbers_container,this.numbers);
	appendChildNodes(this.container,this.numbers_container);
	connect(this.slides[0].children[0],'onload',bind(function(){
		this.position_numbers();
		disconnectAll(this.slides[0]);
	},this));
	this.position_numbers();
}

NumberedSlideShow.prototype.attach_listeners = function(){
	bind(this.__super__.attach_listeners,this)();
	this.listeners['next'] = connect(this,'next_page',bind(this.next_handler,this));
}

NumberedSlideShow.prototype.next_handler = function(event){
	addElementClass(this.numbers[event.current_index],'selected');
	removeElementClass(this.numbers[event.last_index],'selected');
	this.position_numbers();
}

NumberedSlideShow.prototype.position_numbers = function(){
	try{
	var x = getElementDimensions(this.container).w - getElementDimensions(this.numbers_container).w;
	var y = getElementDimensions(this.container).h - getElementDimensions(this.numbers_container).h - 7;
	setElementPosition(this.numbers_container,{'x':x,
		'y':y});
	}
	catch(error){
		console.info(error);
	}
}

_extends(NumberedSlideShow,SlideShow);

addLoadEvent(function(){
	var slide_show_divs = getElementsByTagAndClassName('*', 'cms_slide_show_widget');
	forEach(slide_show_divs, function(slide_show_div){
		var slide_show_obj = new SlideShow(slide_show_div);
		slideshows.push(slide_show_obj);
	});
	var numbered_slide_show_divs = getElementsByTagAndClassName('*', 'cms_numbered_slide_show_widget');
	forEach(numbered_slide_show_divs, function(slide_show_div){
		var numbred_slide_show_obj = new NumberedSlideShow(slide_show_div);
		num_slideshows.push(numbred_slide_show_obj);
	});
});


