/* == FILE INFO ==
 * File Name : hscroll.js
 * Purpose   : This file is part of the "WebEndians Websolution" project.
 *
 * == COPYRIGHT ==
 * Copyright (C) 2008, Stefan Kovachev
 * All Rights Reserved
 *
 * ==  LICENCE  ==
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 * ===============
 */

var webendians = {};

// webendians HScroll <CODE BEGINS HERE>
webendians.hscroll = function(config){
	this.init(config);
};

webendians.hscroll.prototype = {
	cfg: {},

	init: function(config){
		if (!arguments.length) return;
		this.cfg = config;
		this.cfg.busy = true;
		if (this.cfg.initial_offset == null || isNaN(this.cfg.initial_offset)) this.cfg.initial_offset = 0;
		this.cfg.desired_offset = this.cfg.current_offset = this.cfg.initial_offset;
		this.cfg.max_offset = -1 * (this.getWidth() - this.getVisibleWidth()); 
		this.setWidth();
		this.setPosition();
		//this.setTriggerCursor();
		this.cfg.busy = false;
	},
	
	update: function(pcfg){
		if (!arguments.length) return;
		this.cfg.busy = true;
		this.cfg.containers_count = (pcfg.containers_count == null || isNaN(pcfg.containers_count)) ? 0 : pcfg.containers_count;
		this.cfg.desired_offset = this.cfg.current_offset = this.cfg.initial_offset;
		this.cfg.max_offset = -1 * (this.getWidth() - this.getVisibleWidth()); 
		this.setWidth();
		this.setPosition();
		//this.setTriggerCursor();
		this.cfg.busy = false;
	}, 

	getMinWidth: function(){
		return this.getVisibleWidth();
	},
	
	getWidth: function(){
		var calc_width = (this.cfg.ee_width_begin + this.cfg.ee_width_end);
		calc_width += (this.cfg.container_width * this.cfg.containers_count);
		calc_width += (this.cfg.separator_width * (this.cfg.containers_count - 1));
		var min_width = this.getMinWidth();
		if (calc_width < min_width) return min_width;
		return calc_width;
	},

	getVisibleWidth: function(){
		var calc_width = (this.cfg.ee_width_begin + this.cfg.ee_width_end);
		calc_width += (this.cfg.container_width * this.cfg.visible_containers_count);
		calc_width += (this.cfg.separator_width * (this.cfg.visible_containers_count - 1));
		return calc_width;
	},

	setPosition: function(){
		this.cfg.el.style.left = this.cfg.current_offset + 'px';
	},

	setWidth: function(){
		this.cfg.el.style.width = this.getWidth() + 'px';
	},
	
	setTriggerCursor: function(){
		var trp = document.getElementById(this.cfg.trigger_prev), trn = document.getElementById(this.cfg.trigger_next);
		if (!trp || !trn) return;
		
		if (this.cfg.current_offset == this.cfg.initial_offset)
			trp.style.cursor = 'normal';
		else 
			trp.style.cursor = 'pointer';
				
		if (this.cfg.current_offset == this.cfg.max_offset) 	
			trn.style.cursor = 'normal';
		else
			trn.style.cursor = 'pointer';
	},

	startMoving: function(dir){
		this.cfg.direction = (dir == null || isNaN(dir)) ? 0 : dir;

		this.cfg.desired_offset = this.cfg.current_offset;

		// << 1 || -1 >>
		if (this.cfg.direction == 1 || this.cfg.direction == -1) {
			this.cfg.desired_offset = this.cfg.current_offset;
			this.cfg.desired_offset += (this.cfg.direction * (this.cfg.ee_width_begin + this.cfg.container_width + (this.cfg.separator_width - this.cfg.ee_width_begin)));

			if (this.cfg.direction == 1){
				if (this.cfg.desired_offset > this.cfg.initial_offset)
					this.cfg.desired_offset = this.cfg.initial_offset;
			} else {
				if (this.cfg.desired_offset < this.cfg.max_offset)
					this.cfg.desired_offset = this.cfg.max_offset;
			}

			if (this.cfg.desired_offset == this.cfg.current_offset) return;

			var sld = new rutils.slider({
				el: this.cfg.el.style,
				dir: 'h',
				start_pos: this.cfg.current_offset,
				final_pos: this.cfg.desired_offset,
				duration: this.cfg.duration,
				effect: this.cfg.effect
			});
			sld.start();

			this.cfg.current_offset = this.cfg.desired_offset;
			//this.setTriggerCursor();
		}
	}
}
// webendians HScroll <CODE ENDS HERE>
