var timer = 0;
//------------------------------Carousel-------------------------------------------------------
var Carousel = function(initData)
{
	this.structure = {
		'nodes':{
			'container':'undefined',
			'window':'undefined',
			'itemsContainer':'undefined', 
			'bar':'undefined' 
		},
		'buttons':
			{
				'node':'undefined',
				'action':'undefined',
				'speed':'undefined',
				'orientation':'undefined'
			},
		'classes':
			{
				'item':'undefined'
			},
		'target'	: {
						'node': 'undefined',
						'control':'undefined'
					}
	}
	;
	
		var self = this;

		this.setProperties(initData);
		this.items = this.nodes.itemsContainer.select(['.'+this.classes.item+'']);

		if (this.items[0]) {	
			var currentWidth = parseInt(this.items[0].offsetWidth * this.items.length);
			var containerWidth = parseInt(this.nodes.window.offsetWidth);
			
			if (containerWidth >= currentWidth) {
				$('carousel-scroll').style.display = 'none';
				$('carousel-left').onmouseover = function() {
					this.style.cursor = 'default';
				};			
				$('carousel-right').onmouseover = function() {
					this.style.cursor = 'default';
				};
				$('nav-bar').onmouseover = function() {
					this.style.cursor = 'default';
				};
				return;
			}
			else {
				$('carousel-scroll').style.display = '';
				//$('carousel-left').style.display = '';
				//$('carousel-right').style.display = '';
			}

		
			this.nodes.itemsContainer = {
				'node'		: this.nodes.itemsContainer,
				'width'		: this.items[0].offsetWidth * this.items.length,
				'height'	: this.items[0].offsetHeight * this.items.length
				};
			this.nodes.itemsContainer.setProperties({
				'maxX' : this.nodes.window.offsetWidth - this.nodes.itemsContainer.width,
				'maxY' : this.nodes.window.offsetHeight - this.nodes.itemsContainer.height});
			this.nodes.itemsContainer.node.style.width   = this.nodes.itemsContainer.width +'px';
			this.nodes.itemsContainer.node.style.height   = this.nodes.itemsContainer.height +'px';
			
	
			
			var buttons = new Button(this.buttons, this);
		}
		/*var target = new Target(structure.target,this.target);*/
}

Object.prototype.setProperties = function(object)
{
	for(var i in object){
		if('function' != typeof object[i])
			this[i] = object[i];
	}
}

//------------------------------Button-------------------------------------------------------
var Button = function(initData, Carousel){
	var self = this;
	self.Carousel = Carousel;
	Carousel.actualBarSize = {
		'x':Carousel.nodes.bar.offsetWidth,
		'y':Carousel.nodes.bar.offsetHeight
	};
	try{
		for(var i = 0; i< initData.length; i++)
		{
			this[i] = initData[i];
			switch(this[i].action)
			{
				case 'up':
					this[i].stream = 1;			
					break;
				case 'down':		
					this[i].stream = -1;	
					break;
				case 'scroll':		
					this[i].stream = 0;	
					this.scroll = this[i];
					break;
				default:	
					break;
			}
			switch(this[i].speed)
			{
				case 'superslow':
					this[i].speed = Carousel.items[0].offsetWidth/100;			
					break;
				case 'slow':		
					this[i].speed = Carousel.items[0].offsetWidth/50;	
					break;
				case 'normal':		
					this[i].speed = Carousel.items[0].offsetWidth/10;	
					break;
				case 'fast':		
					this[i].speed = Carousel.items[0].offsetWidth/5;	
					break;
				case 'superfast':		
					this[i].speed = Carousel.items[0].offsetWidth/2;	
					break;
				default:
					this[i].speed = Carousel.items[0].offsetWidth/10;	
					break;
			}
			if(this[i].action == 'scroll'){
					Carousel.actualBarSize.x -= this[i].node.offsetWidth;
					Carousel.actualBarSize.y -= this[i].node.offsetHeight;
				}
			
			this[i].node.onmousedown =  function(event){
				return self.getProperties(this).initAnimate(window.event||event, self);
			}
		}
		
	this.coef = {
		'x': (Carousel.nodes.itemsContainer.width - Carousel.nodes.window.offsetWidth)/(Carousel.nodes.bar.offsetWidth - this.scroll.node.offsetWidth),
		'y': (Carousel.nodes.itemsContainer.height - Carousel.nodes.window.offsetHeight)/(Carousel.nodes.bar.offsetHeight  - this.scroll.node.offsetHeight)		
	}

		document.onmouseup =  function(){
			clearTimeout(timer);
			document.onmousemove = null;
		}
	}
	catch(e){
		alert("button"+e);
		return;
	}
}
Button.prototype.getProperties = function(key){
	for(var i in this)
	{
		if(key == this[i].node)
			return this[i];
	}
	return false;
}
Object.prototype.initAnimate = function(e, Button)
{
	this.Button = Button;
	this.event = e;
	this.animate();
	return false;
}

Object.prototype.animate = function(){

	var self = this;
	if('scroll' != self.action){
		switch(this.orientation){
			case 'horizontal':
				if((self.stream == 1 && self.Button.Carousel.nodes.itemsContainer.node.offsetLeft >= 0 ) ||
					(self.stream == -1 && self.Button.Carousel.nodes.itemsContainer.node.offsetLeft <= self.Button.Carousel.nodes.itemsContainer.maxX)){
						return;
					}
				self.Button.scroll.moveTo({'x':-self.Button.Carousel.nodes.itemsContainer.node.offsetLeft/self.Button.coef.x});
				self.Button.Carousel.nodes.itemsContainer.moveTo({'x':self.Button.Carousel.nodes.itemsContainer.node.offsetLeft + self.stream*self.speed});
				break;
			case 'vertical':
				if((self.stream == 1 && self.Button.Carousel.nodes.itemsContainer.node.offsetTop >= 0) ||
					(self.stream == -1 && self.Button.Carousel.nodes.itemsContainer.node.offsetTop <= self.Button.Carousel.nodes.itemsContainer.maxY)){
						return;
					}
				self.Button.scroll.moveTo({'y':-self.Button.Carousel.nodes.itemsContainer.node.offsetTop/self.Button.coef.y});
				self.Button.Carousel.nodes.itemsContainer.moveTo({'y':self.Button.Carousel.nodes.itemsContainer.node.offsetTop + self.stream*self.speed});
				break;
			case 'both':	
				self.Button.scroll.moveTo({
					'x':-self.Button.Carousel.nodes.itemsContainer.node.offsetLeft/self.Button.coef.x,
					'y':-self.Button.Carousel.nodes.itemsContainer.node.offsetTop/self.Button.coef.y});	
				self.Button.Carousel.nodes.itemsContainer.moveTo({
					'x':self.Button.Carousel.nodes.itemsContainer.node.offsetLeft + self.stream*self.speed,
					'y':self.Button.Carousel.nodes.itemsContainer.node.offsetTop + self.stream*self.speed});
				break;
		}
		timer = setTimeout(function(){self.animate();}, 1);
	}
	else if(self.Button.Carousel.nodes.itemsContainer.node.offsetWidth > self.Button.Carousel.nodes.window.offsetWidth){
		if('undefined' == typeof self.Button.scroll.offsetX)  
			self.Button.scroll.offsetX = self.Button.scroll.node.cumulativeOffset()[0] - self.Button.scroll.node.offsetLeft;
		var x = self.Button.scroll.offsetX + ((typeof self.event.layerX == 'undefined')?self.event.offsetX:self.event.layerX);
		if('undefined' == typeof self.Button.scroll.offsetY)
			self.Button.scroll.offsetY = this.Button.scroll.node.cumulativeOffset()[1] - this.Button.scroll.node.offsetTop;			
		var y = self.Button.scroll.offsetY + ((typeof self.event.layerY == 'undefined')?self.event.offsetY:self.event.layerY);
		
		document.onmousemove = function(event)
		{
			var event = 'undefined' == typeof window.event?event:window.event;
			var tx = event.clientX - x;
			var ty = event.clientY - y;
			switch(self.orientation)
			{
				case 'horizontal':
					if('' == self.Button.scroll.node.style.left ||
						(0 <= tx
						&& tx <= self.Button.Carousel.actualBarSize.x)){
						self.Button.scroll.moveTo({'x':tx});
						self.Button.Carousel.nodes.itemsContainer.moveTo({'x':-(tx)*self.Button.coef.x});
					}
				break;
				case 'vertical':
					if('' == self.Button.scroll.node.style.top ||
						(0 <= ty &&
						 ty <= self.Button.Carousel.actualBarSize.y)){
						self.Button.scroll.moveTo({'y':ty});
						self.Button.Carousel.nodes.itemsContainer.moveTo({'y':-(ty)*self.Button.coef.y});
					}
				break;
				case 'both':
					if(
						('' == self.scroll.style.top ||
							(0 <= ty &&
							 ty <= self.Button.Carousel.actualBarSize)) || 
						('' == self.scroll.style.top ||
							(0 <= ty  &&
							 ty <= self.Button.Carousel.actualBarSize)
						) 
						 
					){
						self.Button.scroll.moveTo({'x':tx,'y':ty});
						self.Button.Carousel.nodes.itemsContainer.moveTo({
							'x':-(tx)*self.Button.coef.x,
							'y':-(ty)*self.Button.coef.y});
					}
				break;				
			}
		}
	}
}
Object.prototype.moveTo = function(coords)
{
	try{
		if('undefined' != typeof coords.x) 
			this.node.style.left = coords.x + 'px';
		
		if('undefined' != typeof coords.y) 
			this.node.style.top = coords.y + 'px';
	}
	catch(e){alert(e)}
}
	
