var jar = new CookieJar();

/* -- ls widget -- */
var lsWidget = function() {
	this.item_selector = 'div';
	this.button_back = 'lsback';
	this.button_next = 'lsnext';
	this.slider      = 'slider';
	this.slider_ele  = null;
	this.step_val    = 1;
	this.stops       = [];
	this.stop_idx    = 0;
	this.stop_last   = null;
	this.view_block  = null;
	this.cookie      = 'lswidx';
}
lsWidget.prototype.init = function(month,year){
	this.slider_ele = $(this.slider);
	if($(this.button_back)) {
		var action = function(){this.back()}
		$(this.button_back).observe('click',action.bindAsEventListener(this));
	}	
	if($(this.button_next)) {
		var action = function(){this.next()}
		$(this.button_next).observe('click',action.bindAsEventListener(this));
	}
	var panes = $(this.slider).select(this.item_selector);
	var totalWidth = 0;
	for(i=0;i<panes.length;i++)
	{
		this.stops.push(-(totalWidth));
		var d = panes[i].getWidth();
		totalWidth += d;
		var ml = parseFloat(Element.getStyle(panes[i],'margin-left'));
		var mr = parseFloat(Element.getStyle(panes[i],'margin-right'));
		totalWidth += ml;
		totalWidth += mr;
	}
	
	this.stop_last = this.stops.length;
	if(this.view_block!=null) {
		this.stop_last += -(this.view_block);
	}

	$(this.slider).setStyle({'position':'absolute','display':'block','top':0,'left':0,'width':(totalWidth+500)+'px'});	
	var n = jar.get(this.cookie);
	if(!isNaN(n)){
		n = new Number(n)
		if(n<=this.stops.length && n>=0) {
			this.stop_idx=n;
		}
	}
}
lsWidget.prototype.last = function(){
	this.step(0);
}
lsWidget.prototype.back = function(){
	this.step(-(this.step_val));
}
lsWidget.prototype.next = function(){
	this.step(this.step_val);
}
lsWidget.prototype.step = function(n){
	if(n>=1) {
		if( this.stop_idx == this.stop_last ) {
			this.stop_idx = 0;
		} else if ( (this.stop_idx+this.step_val) > this.stop_last) {
			this.stop_idx = this.stop_last;
		} else {
			this.stop_idx += this.step_val;
		}
	} else if(n<=-1) {
		if( this.stop_idx == 0 ) {
			this.stop_idx = this.stop_last;
		} else if ( (this.stop_idx-this.step_val) < 0) {
			this.stop_idx = 0;
		} else {
			this.stop_idx += -(this.step_val);
		}
	}
	jar.remove(this.cookie);
	jar.put(this.cookie,this.stop_idx);
	var pos = this.stops[this.stop_idx];
	new Effect.Move(this.slider_ele, {
	   x: pos, 
	   transition: Effect.Transitions.sinoidal,
	   mode: 'absolute'
	});
}
