onloadHooks.push(function(){
	initializeTicker();
});

function initializeTicker() {
	
	//$$(".tickerSlider img").reflect({'height':0.6});
	$$(".slideshow").setStyle('height','185px');
	$$(".tx-kpkimageticker-pi1").setStyle('height','185px');
	
	$$(".tickerContainer").setStyle('height','150px');
	$$(".tickerSlider").setStyle('height','180px');
	$$(".tickerSlider .tickerItem").setStyle('height','180px');
	//$$(".tickerSlider .reflected").setStyle('margin-bottom','2px');

	var scrollSlider = $$(".tickerContainer")[0]; 

	var scroll = new imageTicker(scrollSlider);
}

var imageTicker = new Class({
	Implements: [Options,Events],
	options: {
		startIndex: 1,
		slideInterval: 1600,
		ajaxTarget: page,
		preloadInterval: 10,
	},
	initialize: function(container,options){
		
		this.container = container;
		this.setOptions(options);
		this.createNavigation();
		this.createStructure();
		this.createFx();
		
		this.autoplay();
	},
	createFx: function(){
		this.scrollFx = new Fx.Scroll(this.container,{ duration: 1000 });
		this.jumpFx = new Fx.Scroll(this.container,{ duration: 0 });
	},
	createNavigation: function(){
		this.navigation = this.container.getParent().getChildren('.tickerNavigation')[0];
		this.pause = this.navigation.getChildren('.pause')[0];
		this.play = this.navigation.getChildren('.play')[0];
		this.up = this.navigation.getChildren('.backward')[0];
		this.down = this.navigation.getChildren('.forward')[0];
		this.pause.addEvent('click', function(){
			this.pause.setStyle('display','none');
			this.play.setStyle('display','block');
		    $clear(this.slideshowInt);
		}.bind(this));
		
		this.play.addEvent('click', function(){
			this.pause.setStyle('display','block');
			this.play.setStyle('display','none');
		    this.autoplay();
		}.bind(this));
		
		this.up.addEvent('click', function(){
			
			this.now = this.now-1;
			var slide = this.slides[this.now];
			this.scrollFx.toElement(slide);
			
		}.bind(this));
		
		this.down.addEvent('click', function(){
			
			this.now = this.now+1;
			var slide = this.slides[this.now];
			this.scrollFx.toElement(slide);
			
		}.bind(this));
	},
	createStructure: function(){
		this.slides = this.container.getChildren()[0].getChildren();
		this.width = 0;
		this.slides.each(function(slide){
			this.width = this.width.toInt() + slide.getStyle('width').toInt() + 10;
			slide.addEvent('mouseover', function(){
				this.container.getParent().getChildren('.tickerDescription')[0].set('html',slide.getChildren()[0].get('title'));
			}.bind(this));
		}.bind(this));
		this.container.getChildren()[0].setStyle('width',this.width+'px');
		
		//$$(".tickerSlider img").reflect({'height':0.6});
		//$$(".tickerSlider .tickerItem").setStyle('height','150px');
		//$$(".tickerSlider .reflected").setStyle('margin-bottom','2px');
	},
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
		this.fireEvent('onAutoPlay');
	},
	rotate: function(){
		this.cycleForward();
		this.fireEvent('onRotate');
	},
	cycleForward: function(){
		if($chk(this.now) && this.now < this.slides.length-1) {
			if(this.now > this.slides.length/2) {
				this.jumpSlide(this.options.startIndex);
			} else {
				this.showSlide(this.now+1);
			}
		} else if (this.now) {
			this.showSlide(0); 
		} else if(!$defined(this.now)) {
			this.showSlide(this.options.startIndex);
		}
	},
	jumpSlide: function(iToShow){ 
		var now = this.now;
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];
		
		this.jumpFx.toElement(slide);
		
		this.now = iToShow;
		
		/*if(this.options.preloadInterval+this.now > this.slides.length) {
			this.preloadForward();
		}*/
		/*if(this.now < this.options.preloadInterval) {
			//this.preloadBackward();
		}*/
	},	
	showSlide: function(iToShow){ 
		var now = this.now;
		var currentSlide = this.slides[now];
		var slide = this.slides[iToShow];
		
		this.scrollFx.toElement(slide);
		
		this.now = iToShow;
		
		/*if(this.options.preloadInterval+this.now > this.slides.length) {
			this.preloadForward();
		}*/
		/*if(this.now < this.options.preloadInterval) {
			//this.preloadBackward();
		}*/
	},
	preloadForward: function() {
		req = new Request.HTML({
			url:'index.php?id='+this.options.ajaxTarget+'&tx_kpkimageticker_pi1[ajax]=true', 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				for(i=0;i<responseTree.length;i++) {
					if(responseTree[i].className == 'tickerItem') {
						responseTree[i].inject(this.container.getChildren()[0]);
					}
				}
				this.createStructure();
			}.bind(this)
		});
		req.send();
	},
	preloadBackward: function() {

	},
	
});
