window.addEvent('domready',function(){
	/*** Top selector javascript ***/
	var carousel = $('carousel');
	var items = $$('#carousel li');
	var item_width = 470;
	var max_margin = items.length * item_width - item_width;
	var animStatus = 0;
	
	var animation = new Fx.Tween(carousel, {
		duration: 1000,
		onStart: function() {
			animStatus = 1;
		},
		onComplete: function() {
			animStatus = 0;
		}
	});
	
	function next_item(pos){
		if(animStatus == 0) {
			if(pos == -max_margin){
				$$('.selector a').removeClass('active');
				$$('.item1').addClass('active');
				
				animation.start('left', 0);
			} else {
				var newposition = pos - item_width;
				
				var newclass = Math.abs(pos/item_width)+2;
				$$('.selector a').removeClass('active');
				$$('.item'+newclass).addClass('active');
				
				animation.start('left', newposition);
			}
		}
	}
	
	function previous_item(pos){
		if(animStatus == 0) {
			if(pos == 0){
				$$('.selector a').removeClass('active');
				$$('.item5').addClass('active');
					
				animation.start('left', -max_margin);
			} else { 
				var newposition = pos + item_width;
				
				var newclass = Math.abs(pos/item_width);
				$$('.selector a').removeClass('active');
				$$('.item'+newclass).addClass('active');
				
				animation.start('left', newposition);
			}
		}
	}

	$('next').addEvent('click', function(e){
		e.stop();
		var position = parseInt(carousel.getStyle('left'));
		next_item(position);
	});
	
	$('prev').addEvent('click', function(e){
		e.stop();
		var position = parseInt(carousel.getStyle('left'));
		previous_item(position);
	});
	
	$$('.selector a').addEvent('click', function(e){
		e.stop();
		
		if(animStatus == 0) {
			togoto = this.getProperty('rel');
			
			if(this.getProperty('class')!='active') {
				$$('.selector a').removeClass('active');
				this.addClass('active');
				
				var newposition = (item_width * togoto) - item_width;
				
				animation.start('left', -newposition);
			}
		}
	});
	
	/*** Testimonial javascript ***/
	var test_text = $('test_text');
	var items_test = $$('#test_text li');
	var item_width_test = 310;
	var max_margin_test = items_test.length * item_width_test - item_width_test;
	var animation_test = new Fx.Tween(test_text, {duration: 1000});
	
	function next_item_test(pos){
		animation_test.start('opacity',0).chain(function() {
			if(pos == -max_margin_test){
				test_text.setStyle('left',0);
			} else {
				var newposition = pos - item_width_test;
				
				test_text.setStyle('left', newposition);
			}
			animation_test.start('opacity',1);
		});
	}
	
	var repeat_test = function() {
		var position = parseInt(test_text.getStyle('left'));
		next_item_test(position);
	};
	repeat_test.periodical(15000);
	
	/*** About us javascript ***/
	var about_text = $('about_text');
	var items_about = $$('#about_text li');
	var item_width_about = 430;
	var animation_about = new Fx.Tween(about_text, {duration: 500});
	
	$$('.aboutleft .jslink').addEvent('click',function(e) {
		e.stop();
		
		var position = parseInt(about_text.getStyle('left'));
		var newPos = parseInt(this.getProperty('rel')) * item_width_about - item_width_about;
		
		if(-position != newPos) {
			animation_about.start('opacity',0).chain(function() {
				about_text.setStyle('left', -newPos);
				animation_about.start('opacity',1);
			});
			
			$$('.aboutleft .jslink').removeClass('active');
			this.addClass('active');
		}
	});
});