/**
 * news slider. 
 * news appear and disappear with `period` frequency
 * 
 * sergey@webta.net
 * 
 * using prototype library & scriptaculous
 */
var NewsSlider = new Class.create();

NewsSlider.prototype = {
	period: 	4,	// all in seconds
	hidetime:	1.2,
	showtime:	1.2,
	element: 	null,
	options: 	{},
	news:		[],
	cronid:		0,
	active:		0,
	lastActiveNews:	0,
	storageCapacity:5, 	// news
	newsPointer:	0,
	lastDate:		"",
	
	initialize: function(element) {
		var element = $(element);
		var options = Object.extend({
			period: 	this.period,
			hidetime: 	this.hidetime,
			showtime: 	this.showtime,
			itemClass:	"NewsSlider-Items"
		}, arguments[1] || {});
		
		this.options = options;
		this.element = element;
	},
	
	setup: function(startSlider, unactive) {
		// setup news slider
		var options = this.options;
		var slider = this.element;
		
		if (!slider || typeof(slider) != 'object') return false;
		
		var Items = slider.childNodes;
		if (!Items) return false;
		
		// collect all news
		for(var i=0; i<Items.length; i++){
			var div = Items[i];
			if (div.className == options.itemClass) {
				this.news.push(div);
			}
		}
		
		if (startSlider)
			this.startSlider();
			
		if (this.news.length && !unactive)
			this.showNews(this.news[0]);
		
	},
	
	setLastDate: function(ld) {
		// set last date of last news on page
		this.lastDate = ld;
	},
	
	refreshNews: function() {
		// ajax query on database to get new portion of news
		var objParams = {
			method: 	'get', 
			onSuccess: 	this.onGetNewsSuccess, 
			asynchronous: true, 
			parameters: "lastdate=" + this.lastDate + "&rand=" + Math.random(),
			onFailure: 	this.onGetNewsFailure,
			callerObj: 	this
		};
		
		var req = new Ajax.Request("/server/get_news.php", objParams);
	},
	
	onGetNewsSuccess: function(response, json, callerObj, params) {
		var ajaxres = AJAXResponse.unserialize(response);
		var last = callerObj.news.length - 1;
		
		$H(callerObj.news).each(function(News){
			try {
				if (News[0] != callerObj.lastActiveNews)
				{
					News[1].parentNode.removeChild(News[1]);
					News[1] = null;
				}
				else
				{
					callerObj.hideNews(News[1]);
				}
			} catch (err) {}
		});
		

		$H(ajaxres.data).each(function(Item){
			var NewsDiv = document.createElement("DIV");
				NewsDiv.className = callerObj.options.itemClass;
				
			try {
				NewsDiv.appendChild(callerObj.createNews(Item[1]));
				callerObj.element.appendChild(NewsDiv);
			} catch(err) {}

		});
		
		callerObj.lastActiveNews = 0;
		callerObj.news = [];
		callerObj.setup();
	},

	onGetNewsFailure: function(response, json, callerObj, params) {
		//callerObj.startSlider();
	},
	
	stopSlider: function() {
		// stop rotating news
		if (this.cronid)
			clearTimeout(this.cronid);
		this.active = 0;
	},
	
	startSlider: function() {
		// start transition effect in `period` seconds
		this.stopSlider();
		var callerObj = this;
		
		this.cronid = setTimeout(
			function(){
				callerObj.rotateNews()
			}
		, this.period * 1000);
		
		this.active = 1;
	},
	
	rotateNews: function(direction){
		lastActiveNews = this.lastActiveNews;
		var storage = this.news;
		
		var newsNum;
		var total = storage.length;
		callerObj = this;
		
		// check if storage not empty
		if (total < 1) {
			this.stopSlider();
			return false;
		}

		// check direction and rotate lastactive number
		switch (direction)
		{
			case true: // other direction >> down
				newsNum = (!lastActiveNews) ? total - 1 : lastActiveNews - 1;
				break;
			default: 
				newsNum = (lastActiveNews==null || (lastActiveNews==total-1)) ? 0 : Math.min(lastActiveNews + 1, total - 1);
		}
		
		// hide last active news
		if (lastActiveNews != null)
		{
			var anews = storage[lastActiveNews];
			this.hideNews(anews, 
				function(){
					anews.style.display = 'none';
				}
			);
		}
		
		// if last news in storage
		if (newsNum == total-1) 
			this.refreshNews();
		
		
		// show news
		this.showNews(storage[newsNum],
			function(){
					callerObj.startSlider();
			}
		);
		
		//rorate last active news in storage
		this.lastActiveNews = newsNum;
	},
	
	createNews: function(news) {
		// get news block(object) from news array(object)
		var div1 = document.createElement("DIV");
			div1.className = "NewsBlock";
			
		var div2 = document.createElement("DIV");
			div2.className = "blue";
			div2.innerHTML = news.dtformat;
			this.lastDate = news.date;
			
		var div3 = document.createElement("DIV");
			div3.className = "title";
			div3.innerHTML = news.title;
			
		var div4 = document.createElement("DIV");
			div4.className = "news";
			div4.innerHTML = news.small;

		var div5 = document.createElement("DIV");
			div5.className = "ReadMore";
			div5.innerHTML = '<a href="/' + news.url + '"><img title="Read more..." src="/images/read-more.gif" /></a>';
			
		div1.appendChild(div2);			
		div1.appendChild(div3);			
		div1.appendChild(div4);			
		div1.appendChild(div5);			
		
		return div1;
	},
	
	showNews: function(news, callback){
		// show news transitional
		callerObj = this;
		var callback = (typeof(callback) == 'function') ? callback : function(){};
		
		Element.setOpacity(news, 0);
		news.style.display = 'block';
		new Effect.Opacity(news, {
			duration: callerObj.showtime, 
			from: 0,
			to: 1,
			afterFinish: callback
		});
	},
	
	hideNews: function(news, callback){
		// hide news transitional
		callerObj = this;
		var callback = (typeof(callback) == 'function') ? callback : function(){};
		
		new Effect.Opacity(news, {
			duration: callerObj.hidetime, 
			from: 1,
			to: 0,
			afterFinish: callback
		});
	}
};

