if (!self.khNewsflash) {
	var khNewsflash = {
		get: function() {
			//get a random number
			var news = 0;
			var i = 10;
			while (--i > 0)
			{
				news = Math.floor(khNewsflash.totalItem*Math.random());
				if(news != khNewsflash.currentItem) break;
			}
			khNewsflash.currentItem = news;

			if(!khNewsflash.cachearray[khNewsflash.currentItem]){
				url = khNewsflash.livesite + '/modules/mod_khnewsflash/kh_newsflash/kh-newsflashloader.php?curnews=' + khNewsflash.currentItem + '&loadajax=1&total=' + khNewsflash.totalItem;
				new Ajax(url,{method:'get',onComplete:khNewsflash.update}).request(); 
			}else{
				khNewsflash.update(null);
			}
			return false;	
		},
		
		update: function(request) {
			if (request)
			{
				khNewsflash.cachearray[khNewsflash.currentItem] = request;
			}else{
				request = khNewsflash.cachearray[khNewsflash.currentItem];
			}
			khNewsflash.responce = request.split(khNewsflash.splitter);
			//khNewsflash.currentItem = khNewsflash.responce[1];
			khNewsflash.objs[1].innerHTML = khNewsflash.responce[0];
			khNewsflash.e.animation();
			khNewsflash.timer = setTimeout(khNewsflash.get, khNewsflash.delaytime*1000);	
			return false;	
		},
		
		init: function (){
			khNewsflash.cachearray = new Array(khNewsflash.totalItem);
			khNewsflash.timer = setTimeout(khNewsflash.get, khNewsflash.delaytime*1000);	

			khNewsflash.objs = new Array();
			khNewsflash.objs[0] = document.getElementById("kh-scroll_1");			
			khNewsflash.objs[1] = document.getElementById("kh-scroll_2");
			if (!khNewsflash.objs[0] || !khNewsflash.objs[1])
			{
				return;
			}
			switch(khNewsflash.animation){
				case 'scroll_right':
					khNewsflash.e = new scroll_right(khNewsflash.objs);
					break;
				case 'scroll_up':
					khNewsflash.e = new scroll_up(khNewsflash.objs);
					break;
				case 'scroll_down':
					khNewsflash.e = new scroll_down(khNewsflash.objs);
					break;
				case 'effect_fade':
					khNewsflash.e = new effect_fade(khNewsflash.objs);
					break;
				case 'effect_replace':
					khNewsflash.e = new effect_replace(khNewsflash.objs);
					break;
				case 'scroll_left':
				default:
					khNewsflash.e = new scroll_left(khNewsflash.objs);
					break;
					
			}
		},
		
		swap: function() {
			term = khNewsflash.objs[0];
			khNewsflash.objs[0] = khNewsflash.objs[1];
			khNewsflash.objs[1] = term;
		}
		
	}
	khAddEvent(window,'load', khNewsflash.init);
}

scroll_base = new Class ({
	animation:function () {
		x = new Fx.Elements(khNewsflash.objs, { wait: false, duration: khNewsflash.interval, onComplete:khNewsflash.swap});
		x.start(this.ef);
		return false;
	}
})
scroll_up = scroll_base.extend({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.top=0;
		this.elements[1].style.top='100%';
		this.elements[0].style.display="block";
		this.elements[1].style.display="block";
		this.W = this.elements[0].offsetHeight;
		this.elements[0].style.height = this.W + "px";
		this.elements[1].style.height = this.W + "px";
		
		this.ef = {};
		this.ef[0] = { 'top': [ 0 , -this.W] };
		this.ef[1] = { 'top': [ this.W, 0] };

		return false;
	}

});

scroll_down = scroll_base.extend ({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.top=0;
		this.elements[1].style.top='100%';
		this.elements[0].style.display="block";
		this.elements[1].style.display="block";
		this.W = this.elements[0].offsetHeight;
		this.elements[0].style.height = this.W + "px";
		this.elements[1].style.height = this.W + "px";
		
		this.ef = {};
		this.ef[0] = { 'top': [ 0, this.W] };
		this.ef[1] = { 'top': [ -this.W, 0] };

		return false;
	}
});

scroll_left = scroll_base.extend ({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.left=0;
		this.elements[1].style.left='100%';
		this.elements[0].style.display="block";
		this.elements[1].style.display="block";
		this.W = this.elements[0].offsetWidth;
		this.elements[0].style.width = this.W + "px";
		this.elements[1].style.width = this.W + "px";
		
		this.ef = {};
		this.ef[0] = { 'left': [ 0 , -this.W] };
		this.ef[1] = { 'left': [ this.W, 0] };

		return false;
	}
});

scroll_right = scroll_base.extend ({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.left=0;
		this.elements[1].style.left='100%';
		this.elements[0].style.display="block";
		this.elements[1].style.display="block";
		this.W = this.elements[0].offsetWidth;
		this.elements[0].style.width = this.W + "px";
		this.elements[1].style.width = this.W + "px";
		
		this.ef = {};
		this.ef[0] = { 'left': [ 0 , this.W] };
		this.ef[1] = { 'left': [ -this.W, 0] };

		return false;
	}
});

effect_fade = new Class ({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.display="block";
		this.elements[1].style.display="block";
		new Fx.Style(this.elements[1], 'opacity').set(0);
		return false;
	},

	animation:function () {
		x = new Fx.Elements(khNewsflash.objs, { wait: false, duration: khNewsflash.interval, onComplete:khNewsflash.swap, transition: Fx.Transitions.Expo.easeOut});
		ef = {};
		ef[0] = { 'opacity': [ 1, 0] };
		ef[1] = { 'opacity': [ 0, 1] };
		x.start(ef);
		return false;
	}
})

effect_replace = new Class ({
	initialize: function(objs) {
		this.elements = objs;
		this.elements[0].style.display="block";
		this.elements[1].style.display="none";
		return false;
	},

	animation:function () {
		khNewsflash.swap();
		this.elements[0].style.display="block";
		this.elements[1].style.display="none";
		return false;
	}
})
