/* erkar DIVi hamar scroller */

function scroller(id){
	var d = document;
	this.init = function(){
		this.div = d.getElementById(id);
		this.p = this.div.parentNode;
		this.v = 0; //ARAGUTIUN@
		
		this.createArrows();
		this.attachWheel();
		
		this.div.scroller = this; //Kpcnenq DOMin
	};
	this.createArrows = function(){
		var up = d.createElement('div');
		up.className = 'uparrow';
		var down = d.createElement('div');
		down.className = 'downarrow';
		
		up.onclick = this.getFunc(60);
		up.onmouseover = this.getFunc(20);
		up.onmouseout = this.getFunc(0);
		down.onclick = this.getFunc(-60);
		down.onmouseover = this.getFunc(-20);
		down.onmouseout = this.getFunc(0);
		
		this.p.appendChild(up);
		this.p.appendChild(down);
	};
	this.attachWheel = function(){
		if(window.addEventListener){ // FFi wheel-@ .... dard a...
			var func = function(ev){d.getElementById(id).scroller.scroll(-ev.detail*40);};
			this.div.addEventListener('DOMMouseScroll', func, false); 
		}else{ //IE-in@ vochinch :)
			var func = function(){d.getElementById(id).scroller.scroll(window.event.wheelDelta);};
			this.div.onmousewheel=func;
		}
	};
	this.getFunc = function(v){
		// veradarcnum a anonym FUNKCIA, vor@ kareli a kpcnel EVENTnerin kam orinak setTimeout-in
		return function(){d.getElementById(id).scroller.scroll(v);};
	};
	
	//Public
	this.scroll = function(v){
		if(this.div.offsetHeight < this.p.offsetHeight)
			// ete karch a lentinner@ voch mi scroll
			return;

		if(v!==false) this.v = v; //ete V ekel a (orinak eventneri jamanak)
		if(this.v==0) return false;
	
		var oldsize = parseInt(this.div.style.top);
		if(isNaN(oldsize)) oldsize = 0;
		var newsize = oldsize + this.v;
			
		if(newsize>10) newsize=10; //vor shat chijni
		var limit = this.p.offsetHeight-this.div.offsetHeight;
		if(newsize<limit) newsize=limit; //vor shat chbarcrana
			
		this.div.style.top = newsize + 'px';
			
		//erb vor click a arvel, Vn piti popoxvi
		if(this.v==70)this.v=20; if(this.v==-70)this.v=-20;
		if(this.v==100)this.v=70; if(this.v==-100)this.v=-70;
		if(this.v==60)this.v=100; if(this.v==-60)this.v=-100; 
		
		//... naev erb vor WHEEL a arvel
		if(this.v>=120 || this.v<=-120) this.v=0;
		
		setTimeout(this.getFunc(false), 40);
	};
	
	this.init();
}

function hscroller(id){ //es horizonakan carusel a
	var d = document;
	this.init = function(){
		this.e = d.getElementById(id);
		this.p = this.e.parentNode;
		
		this.determineWidth();
		this.createArrows();
		
		this.e.hscroller = this; //Kpcnenq DOMin
	};
	this.determineWidth = function(){
		var x = this.e.childNodes;
		var w=0,a;
		for(var i=0;i<x.length;i++){
			a=parseInt(x[i].offsetWidth);
			if(!isNaN(a)) w+=a;
		}
		
		this.e.style.width=w+'px';
	};
	this.createArrows = function(){
		var left = d.createElement('div');
		left.className = 'leftarrow';
		var right = d.createElement('div');
		right.className = 'rightarrow';
		left.innerHTML = "&lt;";
		right.innerHTML = "&gt;";
		
		left.onclick = this.getFunc(+600);
		right.onclick = this.getFunc(-600);
		
		this.e.style.left = "15px";
		
		this.p.appendChild(left);
		this.p.appendChild(right);
	};
	this.getFunc = function(v){
		// veradarcnum a anonym FUNKCIA, vor@ kareli a kpcnel EVENTnerin kam orinak setTimeout-in
		return function(){d.getElementById(id).hscroller.scroll(v);};
	};
	
	this.getStyle = function(s){
		return Math.round( parseFloat(this.e.style[s])*100 ) / 100;
	};
	this.changeStyle = function(s, v, measure){
		var a = this.getStyle(s);
		this.e.style[s] = a+v + (measure?measure:"");
	};
		
	this.scroll = function(v){
		var from = this.getStyle("left");
		var width = this.e.offsetWidth;
		var pwidth = this.p.offsetWidth;
		
		var to = from+v;
		
		if(width<pwidth) return false;
		if(v<0 && to + width < pwidth) to=pwidth-width-15;
		if(v<0 && to + width < 0) return false;
		if(v>0 && from >=15) return false;
		if(v>0 && to>15) to=15;
		this.scroll2(from, to);
	};
	this.scroll2 = function(from, to){
		var n = from>to ? -1 : +1;
		this.changeStyle("left", n * 30, 'px');
		if ( n*(this.getStyle("left")-to) >= 0 )
			this.e.style["left"] = to + 'px';
		if (this.getStyle("left")==to)
			return;
		window.setTimeout(function(){d.getElementById(id).hscroller.scroll2(from, to)}, 20);
	};
	

	this.init();
}