/**
 * @author buenger
 */
var StepButtonManager = {
	
	buttons: null,
	container: null,
	containerId: null,
	numPages: null,
	currentPage: null,
	loadedPageButtons: 0,
	left: null,
	
	setNumPages: function (a_numPages, a_firstRun) {
		this.buttons = [];
		this.numPages = a_numPages;
		var i, button, obj;
		for (i = 0; i < a_numPages-1; i++) {
			obj = URL_MAP['pix']['page'+(i+1)];
			button = new StepButton(obj['active'], obj['inactive'], obj['completed'], a_firstRun);
			button.addEvent("pageClick", this.handlePageClick.bind(this));
			if (a_firstRun == undefined) button.addEvent("creationComplete", this.setupButtons.bind(this));
			this.buttons.push(button);
		}
		
		obj = URL_MAP['pix']['mail'];
		button = new StepButton(obj['active'], obj['inactive'], obj['completed'], true);
		button.addEvent("creationComplete", this.setupButtons.bind(this));
		this.buttons.push(button);
		
		if (a_firstRun != undefined) {
			this.loadedPageButtons = this.numPages;
			this.setupButtons();
		}
	},
	
	handleWindowLoad: function () {
		this.container = $(this.containerId);
		this.container.offsetRightOld = 0;
		this.clear();
	},
	
	clear: function () {
		this.container.empty();
	},
	
	setupButtons: function () {
		
		this.loadedPageButtons++;
		if (this.loadedPageButtons < this.numPages-1) return;
		
		this.buttons.each(function (a_item, a_index){
			a_item.id = a_index;
			a_item.element.inject(this.container);
		}.bind(this));
		if (this.currentPage != null) this.setPage(this.currentPage);
	},
	
	setPage: function (a_pageNum) {
		this.currentPage = a_pageNum;
		if (!this.buttons[0].element) return;
		this.buttons.each(function (a_item, a_index){			
			if (a_item.id < a_pageNum) a_item.setCompleted();
			else if (a_item.id == a_pageNum) a_item.setActive();
			else a_item.setInactive();
		}.bind(this));
	},
	
	init: function (a_containerId) {
		$extend(this, new Events());
		
		this.containerId = a_containerId;
		window.addEvent("load", this.handleWindowLoad.bind(this));
		
	},
	
	setRight: function (a_number) {
		if(false === $defined(this.container)) {
			return;
		}
		var oro = parseInt(this.container.getStyle('right')) || 0;
		if(a_number) {
			this.container.offsetRightOld = this.container.getStyle("right");
			this.container.style.right = a_number+"px";
		//} else if(this.container.offsetRightOld !== null) {
		} else if($defined(oro)) {
			this.container.style.right = this.container.offsetRightOld;
			this.container.offsetRightOld = null;
		}
	},
	
	handlePageClick: function (a_id) {
		this.fireEvent ("pageClick", a_id);
		return false;
	}
}

