var kubis = {

	init : function(){
		this.boxes.init();
		this.news.init();
	},
	
	boxes : {
	
		total : 0,
		current : 0,
		shift : 0,
		timeout : null,
		direction : 1,
		
		init : function() {
			this.total = $('#boxHolder a').length;
			
			var box = $('#boxHolder a').eq(0);
			this.shift = this.nopx(box.css('width')) + this.nopx(box.css('marginRight'));
			
			$('#boxHolder').css('width', this.shift*this.total+'px');
			
			if(this.total>3) { //show the arrows
				$('#boxes a.arrow').show();
				
				$('#boxHolder a').mouseover(function(){ kubis.boxes.stopTick(); });
				$('#boxHolder a').mouseout(function(){ kubis.boxes.startTick(); });
				
				$('#arrow_left').click(function(){
											kubis.boxes.prev();
											kubis.boxes.restartTick();
											$(this).blur();
											return false;
										});
				$('#arrow_right').click(function(){
											kubis.boxes.next();
											kubis.boxes.restartTick();
											$(this).blur();
											return false;
										});
				
				this.startTick();
			}
		},
		
		startTick : function(){
			this.timeout = window.setTimeout('kubis.boxes.tick()', 5000);
		},
		
		stopTick : function(){
			window.clearTimeout(this.timeout);
		},
		
		restartTick : function(){
			this.stopTick();
			this.startTick();
		},
		
		tick : function(){
			var next = this.current + this.direction;
			if(next==this.total-3 || next==0){
				this.direction *= -1;
			}
			this.move(next);
			this.startTick();
		},
		
		nopx : function(val){ //remove the px suffix
			return kubis.nopx(val);	
		},
		
		prev : function(){
			if(this.current>0){
				this.move(this.current-1);
				if(this.current>0){
					this.direction = -1;
				}
			}
		},
		
		next : function(){
			if(this.current<(this.total-3)){
				this.move(this.current+1);
				if(this.current<(this.total-3)){
					this.direction = 1;
				}
			}
		},
		
		move : function(num){
		
			num = Math.min(this.total-3, Math.max(0, num));
		
			if(num==0){
				$('#arrow_left img').attr('src', 'images/arrow-left_inactive.png');
				this.direction = 1;
			}
			else {
				$('#arrow_left img').attr('src', 'images/arrow-left_active.png');
			}
			
			if(num==this.total-3){
				$('#arrow_right img').attr('src', 'images/arrow-right_inactive.png');
				this.direction = -1;
			}
			else {
				$('#arrow_right img').attr('src', 'images/arrow-right_active.png');
			}
			
			$('#boxHolder').animate({left : -num*this.shift});
			
			this.current = num;
		}

	},
	
	news : {
	
		total : 0,
		current : 0,
		shift : 0,
		
		init : function(){
			this.total = $('#news .news').length;
			
			var news = $('#news .news').eq(0);
			this.shift = this.nopx(news.css('height')) + this.nopx(news.css('margin-bottom'));
			
			if(this.total>3){
				$('#arrow_down').show();
				$('#arrow_up').show();
				
				$('#arrow_down').mouseover(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-down_hover.jpg');
											});
				$('#arrow_down').mouseout(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-down.jpg');
											});
				$('#arrow_up').mouseover(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-up_hover.jpg');
											});
				$('#arrow_up').mouseout(function(){
												$(this).children('img').eq(0).attr('src', 'images/arrow-up.jpg');
											});							
				
				$('#arrow_down').click(function(){
											kubis.news.next();
											$(this).blur();
											return false;
										});
										
				$('#arrow_up').click(function(){
											kubis.news.prev();
											$(this).blur();
											return false;
										});
			}
		},
		
		next : function(){
			if(this.current<(this.total-3)){
				this.move(this.current+1);
			}
		},
		
		prev : function(){
			if(this.current>0){
				this.move(this.current-1);
			}
		},
		
		move : function(num){
		
			num = Math.min(this.total-3, Math.max(0, num));
		
			if(num==0){
				//$('#arrow_up').hide();
			}
			else {
				//$('#arrow_up').show();
			}
			if(num==(this.total-3)){
				//$('#arrow_down').hide();
			}
			else {
				//$('#arrow_down').show();
			}
			
			$('#newsHolder').animate({top : -num*this.shift+'px'});
			
			this.current = num;
		},
		
		nopx : function(val){
			return kubis.nopx(val);
		}
		
	},
	
	nopx : function(val){
		var len = val.length;
		return Math.round(val.substring(0, len-2));	
	}

}

$(function(){

	kubis.init();

});
