/**
var @side 0 - top, 3 - right, 6 - down, 9 - left
**/
if (!Prototype.Browser.IE)
{
	Node.prototype.swapNode = function (node) {
		var nextSibling = this.nextSibling;
		var parentNode = this.parentNode;
		node.parentNode.replaceChild(this, node);
		parentNode.insertBefore(node, nextSibling);  
	}
}


var smdivcn = {
	__collection : new Array(),
	add : function (smdiv)
	{		
		if (this.__collection[smdiv.node.id] == undefined)
			this.__collection[smdiv.node.id] = smdiv;		
	},
	drop : function (smdiv)
	{
		if (this.__collection[smdiv.node.id] != undefined)
			delete this.__collection[smdiv.node.id];
	},
	get : function (element)
	{
		element = $(element);
		if (element == null)
			return false;
		/*if (this.__collection[element.id] == undefined)			
			this.add(new smart_div(element));*/
		return 	this.__collection[element.id];
	}
};



function smart_div (element)
{
	this.node = $(element);
	this.__offset = 0;
}

smart_div.prototype = {
	setheight : function (height)
	{
		var _height = isNaN(parseInt(height)) ? -1 : parseInt(height);
		if (_height > -1)
			this.node.style.height = _height;
		return this.height();
	},
	height : function ()
	{
		return this.node.getHeight();
	},
	setwidth : function (width)
	{
		var _width = isNaN(parseInt(width)) ? -1 : parseInt(width);
		if (_width > -1)
			this.node.style.width = _width;
		return this.width();
	},
	width : function ()
	{
		return this.node.getWidth();
	},
	setscrollLeft : function (scrollLeft)
	{
		var _scrollLeft = isNaN(parseInt(scrollLeft)) ? -1 : parseInt(scrollLeft);
		if (_scrollLeft > -1)
			this.node.scrollLeft = _scrollLeft;
		return this.scrollLeft();
	},
	setscrollTop : function (scrollTop)
	{
		var _scrollTop = isNaN(parseInt(scrollTop)) ? -1 : parseInt(scrollTop);
		if (_scrollTop > -1)
			this.node.scrollTop = _scrollTop;
		return this.scrollTop();
	},
	scrollWidth : function ()
	{
		return this.node.scrollWidth;
	},
	scrollHeight : function ()
	{
		return this.node.scrollHeight;
	},
	scrollLeft : function ()
	{
		return this.node.scrollLeft;
	},
	scrollTop : function ()
	{
		return this.node.scrollTop;
	},
	moveLeft : function ()
	{
		this.__move(9);
	},
	moveRight : function ()
	{
		this.__move(3);
	},
	moveUp : function ()
	{
		this.__move(0);
	},
	moveDown : function ()
	{
		this.__move(6);
	},
	setOffset : function (offset)
	{
		this.__offset = offset;
	},
	offset : function ()
	{
		return this.__offset;
	},
	__move : function (side)
	{
		switch(side)
		{
			case 0: // up
				if (this.scrollTop() > this.offset())
					this.setscrollTop(this.scrollTop() - this.offset());
				else
					this.setscrollTop(0);
				break;
			case 3: // right
				if ((this.scrollWidth() - this.scrollLeft()) > this.offset())
					this.setscrollLeft(this.scrollLeft() + this.offset());
				else
					this.setscrollLeft(this.scrollWidth());
				break;
			case 6: // down
				if ((this.scrollHeight() - this.scrollTop()) > this.offset())
					this.setscrollTop(this.scrollTop() + this.offset());
				else
					this.setscrollTop(this.scrollHeight());
				break;
			case 9: // left
				if (this.scrollLeft() > this.offset())
					this.setscrollLeft(this.scrollLeft() - this.offset());
				else
					this.setscrollLeft(0);
				break;
		}
	}
}

function swap_div(element)
{
	this.__holder = $(element);
	this.node = this.__holder;
	this.__div_set = new Array();
	this.__case_pos = new Array();
	/*this.__e_node = false;*/
	this.__current = 0;	
	this.show_divs = 2;
	this.init_complete = 0;
	/*this.__p1 = -1;
	this.__p2 = -1;*/
}

swap_div.prototype = {
	
	init : function ()
	{
		var childs = this.__holder.childNodes.length;
		var idx = 0;
		for(var i = 0; i < childs; i++)
		{
			if (this.__holder.childNodes[i].nodeType == 1 && this.__holder.childNodes[i].nodeName == "DIV")
			{
				/*if (this.__p1 == -1)
				{
					this.__p1 = this.__holder.childNodes[i];
				}
				else if (this.__p2 == -1)
				{
					this.__p2 = this.__holder.childNodes[i];
				}*/
				this.__div_set[this.__div_set.length] = this.__holder.childNodes[i];
				els = this.__holder.childNodes[i].childNodes.length - 1;
				for (var k = 0; k < els; ++k) {					
					if (this.__holder.childNodes[i].childNodes[k].nodeName == "DIV"){						
						name = this.__holder.childNodes[i].childNodes[k].id.substr(1);
						this.__case_pos[name] = idx;
					}
				}

				if (this.__div_set.length > this.show_divs)
					Element.hide(this.__holder.childNodes[i]);
				++idx;
			}
		}
		/*this.__e_node = document.createElement("div");
		this.__e_node.style.height = this.__p1.style.height;
		this.__e_node.style.width = this.__p1.style.width;
		this.__holder.appendChild(this.__e_node);*/
		this.init_complete = 1;
	},
	moveRight : function ()
	{
		if (this.init_complete == 0) return;
		if (this.__div_set.length <= this.show_divs)
			return;
		if ((this.__current + this.show_divs) != this.__div_set.length)
		{
			Element.hide(this.__div_set[this.__current]);
			this.__current++;
			Element.show(this.__div_set[this.__current + (this.show_divs - 1)]);
		}
		else
		{
			for (var i = 0; i < this.show_divs ; i++)
			{
				Element.hide(this.__div_set[this.__current + i]);				
			}
			this.__current = 0;
			for (var i = 0; i < this.show_divs ; i++)
			{
				Element.show(this.__div_set[this.__current + i]);				
			}			
		}
	},
	moveLeft : function ()
	{
		if (this.init_complete == 0) return;
		if (this.__div_set.length <= this.show_divs)
			return;

		if (this.__current != 0)
		{
			Element.hide(this.__div_set[this.__current + this.show_divs - 1]);
			this.__current--;
			Element.show(this.__div_set[this.__current]);
		}
		else
		{			
			for (var i = 0; i < this.show_divs ; i++)
			{
				Element.hide(this.__div_set[this.__current + i]);				
			}
			this.__current = this.__div_set.length - this.show_divs;
			for (var i = 0; i < this.show_divs ; i++)
			{
				Element.show(this.__div_set[this.__current + i]);				
			}
		}
	},
	scrollToCase : function (name) {
		if (this.__case_pos[name] != undefined) {
			//alert(this.__case_pos[name]);
			this.scrollToPos(this.__case_pos[name]);
		}
	},
	scrollToPos : function (pos)
	{
		if (this.init_complete == 0) return;
		if (this.__current == pos)
			return;
		
		if (this.__current > pos)
		{		
			var steps = this.__current - pos;			
			for (var i = 0; i < steps; i++)
				this.moveLeft();
		}
		else
		{
			if (4 <= (this.__div_set.length - 1 - pos))
				var steps = pos - this.__current;
			else
				var steps = this.__div_set.length - 1 - this.__current - 4;
			//if ((2*pos - this.__current) >= this.__div_set.length)
			//	var steps = this.__div_set.length - this.show_divs - this.__current;
			//else
			//	var steps = pos - this.__current;
			for (var i = 0; i < steps; i++)
				this.moveRight();
		}
	}	
}