


window.addEvent('domready', function() {

if( $('ctx-msg-box') ) {

	
}




if( $('slides') ) {

	mySlideShow = new SlideShow('slides',{
		delay: 3000,
		autoplay: true
	});

}


if( $('ctx_form') ) {
        var myCheck = new FormCheck('ctx_form', {
			display : {
                scrollToFirst : false,
				errorsLocation : 1,
				indicateErrors : 2,
				keepFocusOnError : 1,
				closeTipsButton : 0,
				showErrors : 1

            }
        })

}









	if($("slideshow_images")) {									 
		new Rotating({
			"main": $("slideshow_images"),
			"elements": $$("#slideshow_images li"),
			"nav": $$("#slideshow_nav li"),
			"headers": $$("#slideshow_right .slideshow_item"),
			"time": 3000
		});
	}
	
	
	if($("main_ships")) {
		new Rotating({
			"main": $("main_ships"),
			"elements": $$("#main_ships .ship"),
			"left_arrow": $$("#main_content .left_arrow")[0],
			"right_arrow": $$("#main_content .right_arrow")[0],
			"time": 5000
		});
	}

	
});

var Rotating = new Class({
	
	Implements : [ Options ],
	
	options : {
		"time": 1000,
		"nav": false,
		"left_arrow": false,
		"right_arrow": false,
		"headers": false
	},
	
	
	initialize: function(options){
		
		this.setOptions(options);
		
		this.time = this.options.time;
		
		this.current_el = 0;
		
		this.elements = this.options.elements;
		this.main = this.options.main;
		
		if(this.options.nav){
			this.nav = this.options.nav;
			this.nav[0].addClass("active");
		}
		
		if(this.options.left_arrow){
			this.left_arrow = this.options.left_arrow;
		}
		
		if(this.options.right_arrow){
			this.right_arrow = this.options.right_arrow;
		}
		
		if(this.options.headers){
			this.headers = this.options.headers;
			
			this.headers.fade("hide");
			this.headers[0].fade("show");
		}
		
		this.elements.fade("hide");
		this.elements[0].fade("show");
				
		this.add_events();
		this.function_rotate = this.to_forward.periodical(this.time, this);
	
	},
	
	to_forward: function(){
		var old_current_el = this.current_el;
		if(this.current_el==this.elements.length-1){
			this.current_el = 0;
		}
		else {
			this.current_el+=1;
		}
		
		this.rotate(old_current_el, this.current_el);
	},
	
	to_backward: function(){
		var old_current_el = this.current_el;
		if(this.current_el==0){
			this.current_el = this.elements.length-1;
		}
		else {
			this.current_el-=1;
		}
		
		this.rotate(old_current_el, this.current_el);
	},
	
	add_events: function(){
		var oThis = this;
		
		if(this.nav){
			this.nav.each(function(el, index){
				el.addEvent("click", function(event){
					var event = new Event(event);
					event.preventDefault();
					oThis.set_element(index);
					$clear(oThis.function_rotate);
					oThis.main.removeEvents();
				});
			});
		}
		
		
		if(this.left_arrow){
			this.left_arrow.addEvent("click", function(event){
				var event = new Event(event);
				event.preventDefault();
				oThis.to_backward();
				$clear(oThis.function_rotate);
				oThis.main.removeEvents();
			});
		}
		
		if(this.right_arrow){
			this.right_arrow.addEvent("click", function(event){
				var event = new Event(event);
				event.preventDefault();
				oThis.to_forward();
				$clear(oThis.function_rotate);
				oThis.main.removeEvents();
			});
		}
		
		this.main.addEvent("mouseover", function(){
			$clear(oThis.function_rotate);
		});
		
		this.main.addEvent("mouseout", function(){;
			oThis.function_rotate = oThis.to_forward.periodical(oThis.time, oThis);
		});
	},
	
	rotate: function(old_current_el, new_current_el){
		this.elements[old_current_el].fade("out");
		this.elements[new_current_el].fade("in");
		
		if(this.nav){
			this.nav.removeClass("active");
			this.nav[new_current_el].addClass("active");
		}
		
		if(this.headers){
			this.headers[old_current_el].fade("out");
			this.headers[new_current_el].fade("in");
		}
	},
	
	set_element: function(index){
		this.rotate(this.current_el, index);
		this.current_el = index;
	}
	
	
	
	
	
	
});

		

