/*
  Newsbox v1.0 - Yet another Lightbox clone by Davor Bokun

	Based on:
  
  Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Newsbox = {
	init: function(options){
		this.options = $extend({
			resizeDuration: 400,
			resizeTransition: false,	// default transition
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true,
			showCounter: true
		}, options || {});

		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);
    
    this.lang = 'hr';    
    
		this.overlay = new Element('div', {'id': 'newsbOverlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': 'newsbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		this.prevLink = new Element('a', {'id': 'newsbPrevLink', 'href': '#', 'styles': {'display': 'none'}}).injectInside(this.center);
		this.newsframe = new Element('div', {'id': 'newsbFrame'}).injectInside(this.center);
		this.nextLink = this.prevLink.clone().setProperty('id', 'newsbNextLink').injectInside(this.center);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);

		this.bottomContainer = new Element('div', {'id': 'newsbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.bottom = new Element('div', {'id': 'newsbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'newsbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick  = this.close.bind(this); // = this.overlay.onclick
		this.caption = new Element('div', {'id': 'newsbCaption'}).injectInside(this.bottom);
		this.number = new Element('div', {'id': 'newsbNumber'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			newsframe: this.newsframe.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};

//		this.preloadPrev = new Image();
//		this.preloadNext = new Image();
		
	},

	show: function(url, title, lang){
		return this.open([[url, title]], 0, lang);
	},

	open: function(news, newsNum, lang){
	  this.lang = lang;
		this.news = news;
		this.position();
		this.setup(true);
		this.top = 40;//window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.9);
		return this.changeNews(newsNum);
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: this.close(); break; //case 88: case 67: 
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function(){
		return this.changeNews(this.activeNews-1);
	},

	next: function(){
		return this.changeNews(this.activeNews+1);
	},

	changeNews: function(newsNum){
		if (this.step || (newsNum < 0) || (newsNum >= this.news.length)) return false;
		this.step = 1;
		this.activeNews = newsNum;

		this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.fx.newsframe.hide();
		this.center.className = 'newsbLoading';

//		this.preload = new Image();
//		this.preload.onload = this.nextEffect.bind(this);
//		this.preload.src = this.news[newsNum][0];
    var nextEffect = this.nextEffect.bind(this);
    new Ajax('novosti/fetch_article.php',{evalScripts:true,update:this.newsframe,method:'post',data:{id:this.news[this.activeNews][0]},onComplete:nextEffect}).request();
//    window.setTimeout('Newsbox.nextEffect();',700);
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
		  vijest = {'de':'Nachricht ', 'en':'News ', 'fr':'Nouvelle ', 'hr':'Vijest ', 'it':'Notizie '};
		  od = {'de':' von ', 'en':' of ', 'fr':' de ', 'hr':' od ', 'it':' di '};
		
			this.center.className = '';
			this.newsframe.style.width = this.bottom.style.width = 600+'px';
			
			this.newsframe.style.height = this.prevLink.style.height = this.nextLink.style.height = 600+'px';

			this.caption.setHTML(this.news[this.activeNews][1] || '');
			this.number.setHTML((!this.options.showCounter || (this.news.length == 1)) ? '' : vijest[this.lang]+(this.activeNews+1)+od[this.lang]+this.news.length);

//			if (this.activeNews) this.preloadPrev.src = this.news[this.activeNews-1][0];
//			if (this.activeNews != (this.news.length - 1)) this.preloadNext.src = this.news[this.activeNews+1][0];

			if (this.center.clientHeight != this.newsframe.offsetHeight){
				this.fx.resize.start({height: this.newsframe.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.newsframe.offsetWidth){
				this.fx.resize.start({width: this.newsframe.offsetWidth, marginLeft: -(this.newsframe.offsetWidth)/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.newsframe.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			if (this.activeNews) this.prevLink.style.display = '';
			if (this.activeNews != (this.news.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
/*		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
*/
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

window.addEvent('load', Newsbox.init.bind(Newsbox));
