//inputbox.js
var InputBox = new Class({
	Implements: Options,

	element: null,

	options: {
		text: 'Iskanje...',
		url: false,
		method: 'post'
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = element;

        this.element.setStyle('color', '#bbb');
        
		if(this.options.text == 'Iskanje...' && this.element.get('value')){
			this.options.text = this.element.get('value');
		}

		this.element.addEvents({
			'focus': this.focus.bind(this),
			'blur': this.blur.bind(this)
		});

		this.reset();
	},

	focus: function(){
        this.element.setStyle('color', '#333');
		if(this.element.get('value') == this.options.text){
			this.element.set('value', '');
		}
	},

	blur: function(){
		if(this.element.get('value') == ''){
			this.element.setStyle('color', '#bbb');
			this.element.set('value',  this.options.text);
		}
	},

	reset: function(){
		this.focus();
		this.blur();
	}
});

window.addEvent('domready', function(){
	var search = new InputBox($('s'), {text: 'Iskanje...'});

	var lightbox = new XtLightbox('a[rel=facebox]', {
                                    loop: true,
                                    adaptorOptions: {
                                        Image: {
                                            lightboxCompat: false
                                        }
                                    }
                                });
	
	/* settings */
	var matches = $$('#matches .match');
	var currentIndex = 0;

	matches.each(function(el,i){ 
		if(i > 0) {
		  el.set('opacity',0);
		}
		//el.addEvent('click', function(){ show(); });
	});
	
	/* settings */
	var standings = $$('#standings .standing');
	var standingsIndex = 0;
	
	standings[0].setStyles({'opacity': 1, 'visibility': 'visible'});

	standings.each(function(el,i){ 
		if(i > 0) {
		  el.set('opacity',0);
		}
	});
	
	var fr = new Request.HTML({
			url: '/wp-content/themes/real2012/front/forum.php',
			update: $('forum'),
			useSpinner: true,
			method: 'GET',
			spinnerOptions: { 
				containerPosition: { 
					relativeTo: $('forum'), 
					position: 'center'
				}
			}
		});
	
	var forumRefresh = function(){
		fr.send();
	}

	var show = function() {
		matches[currentIndex].fade('out');
		matches[currentIndex = currentIndex < matches.length - 1 ? currentIndex+1 : 0].fade('in');
	};
	
	var showStandings = function() {
		standings[standingsIndex].fade('out');
		standings[standingsIndex = standingsIndex < standings.length - 1 ? standingsIndex+1 : 0].fade('in');
	};
	
	window.addEvent('load',function(){
		if(matches.length > 0){
			show.periodical(5000);
		}
		if(standings.length > 0){
			showStandings.periodical(10000);
		}
		if($('forum') != null){
			forumRefresh();
			forumRefresh.periodical(30000);
		}
	});
	
	/* For zebra striping */
	$$(".ftable tr:nth-child(odd)").addClass("odd-row");
	/* For cell text alignment */
	$$(".ftable td:first-child, .ftable th:first-child").addClass("first");
	/* For removing the last border */
	$$(".ftable td:last-child, .ftable th:last-child").addClass("last");
	
	konami = new Konami()
	konami.code = function() {
		new Element('script', {
			type: 'text/javascript',
			src: 'http://www.m-ms.dk/spaceheroes/js/spaceship.js'
		}).inject(document.body);
	};
	konami.load();
});

