//hoverident.js
var HoverIdent = new Class({
	Implements: [Options, Events],
	
	parent: null,
	
	options: {
		childs: 'li',
		property: 'margin-left',
		duration: 'short',
		value: {
			over: '10px',
			leave: '0px'
		}
	},
	
	initialize: function(parent, options){
		this.setOptions(options);
		var opts = this.options;
		
		parent.getElements(this.options.childs + ':not(.current_page_item)').each(function(el){
			el.set('tween', {duration: this.options.duration});
			el.addEvents({
				'mouseover': function(){
					this.tween(opts.property, opts.value.over);
				},
				'mouseleave': function(){
					this.tween(opts.property, opts.value.leave);
				}
			});
		}.bind(this));
	}
});

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

	element: null,

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

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

		if(this.options.text == 'Search...' && 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(){
		if(this.element.get('value') == this.options.text){
			this.element.set('value', '');
		}
	},

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

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

/*
 *	QuickBox (Another Lightbox clone)
 *
 *	Version: 1.0
 *	Documentation: AndrewPlummer.com (http://www.andrewplummer.com/code/quickbox/)
 *	Heavily inspired by: Slimbox by Christophe Beyls (http://www.digitalia.be)
 *	Written for: Mootools 1.2
 *	License: MIT-style License
 *	
 *	Copyright (c) 2008 Andrew Plummer
 */
var QuickBox=new Class({Implements:Options,options:{resizeDuration:600,resizeTransition:Fx.Transitions.Circ.easeOut,initialWidth:250,initialHeight:250,padding:10,animateCaption:true,counter:"Slika {NUM} od {TOTAL}",selector:"a[rel=facebox]"},initialize:function(a){this.setOptions(a);this.anchors=$$(this.options.selector);this.anchors.each(function(c){c.store("caption",c.get("title")||c.getElement("img").get("title"));c.addEvent("click",this.open.bindWithEvent(this,c))},this);this.overlay=new Element("div",{id:"qbOverlay",events:{click:this.close.bindWithEvent(this)}}).inject(document.body,"top");this.quickBox=new Element("div",{id:"qbBox",styles:{width:this.options.initialWidth,height:this.options.initialHeight,marginLeft:-(this.options.initialWidth/2),position:"absolute"}}).inject(document.body,"top");this.prevLink=new Element("a",{id:"qbPrev",href:"#"}).inject(this.quickBox);this.nextLink=this.prevLink.clone().setProperty("id","qbNext").injectInside(this.quickBox);this.stage=new Element("div",{id:"qbStage"}).inject(this.quickBox);this.prevLink.addEvent("click",this.changeImage.bindWithEvent(this,-1));this.nextLink.addEvent("click",this.changeImage.bindWithEvent(this,1));this.bottom=new Element("div",{id:"qbBottom"}).inject(this.quickBox);this.closeButton=new Element("div",{id:"qbClose",events:{click:this.close.bindWithEvent(this)}}).inject(this.bottom);this.caption=new Element("div",{id:"qbCaption"}).inject(this.bottom);this.counter=new Element("div",{id:"qbCounter"}).inject(this.bottom);var b=this.nextEffect.bind(this);this.fx={overlay:new Fx.Tween(this.overlay,{property:"opacity"}),resize:new Fx.Morph(this.quickBox,{duration:this.options.resizeDuration,transition:this.options.resizeTransition,onComplete:b}),show:new Fx.Tween(this.stage,{property:"opacity",onComplete:b}),bottom:new Fx.Tween(this.bottom,{property:"top",duration:400,onComplete:b})};this.active=false;document.addEvent("mousewheel",this.mouseWheelListener.bindWithEvent(this));document.addEvent("keydown",this.keyboardListener.bindWithEvent(this))},open:function(e,d){this.active=true;var b=window.getSize();var a=window.getScroll();var c=window.getScrollSize();var g=Math.round((b.y<768)?b.y/36:b.y/10);var f=a.y+g;this.overlay.setStyles({opacity:0,display:"block",width:c.x,height:c.y});this.quickBox.setStyles({display:"block",top:f});this.fx.overlay.start(0.8);this.startLoad(d);return false},startLoad:function(b,a){if(!b){return}var c=new Asset.image(b.get("href"),{onload:function(){if(!a&&this.currentLink==b){this.nextEffect()}}.bind(this)});if(!a){this.stage.addClass("loading");this.stage.setStyle("display","block");this.stage.empty();this.bottom.setStyle("opacity",0);this.prevLink.setStyle("display","none");this.nextLink.setStyle("display","none");this.currentLink=b;this.currentCaption=b.retrieve("caption");this.currentImage=c;this.currentIndex=this.anchors.indexOf(b);this.step=1}},keyboardListener:function(a){if(!this.active){return}if(a.key!="f5"){a.preventDefault()}switch(a.key){case"esc":case"x":case"q":this.close();break;case"b":case"p":case"left":this.changeImage(a,-1);break;case"f":case"n":case"right":this.changeImage(a,1)}},mouseWheelListener:function(a){if(!this.active){return}if(a.wheel>0){this.changeImage(a,-1)}if(a.wheel<0){this.changeImage(a,1)}},changeImage:function(c,b){c.preventDefault();var a=this.anchors[this.currentIndex+b];if(!a){return false}for(var d in this.fx){this.fx[d].cancel()}this.startLoad(a)},nextEffect:function(){switch(this.step++){case 1:var b=this.currentImage.width+this.options.padding*2;var d=this.currentImage.height+this.options.padding*2;this.prevLink.setStyle("height",d);this.nextLink.setStyle("height",d);this.fx.resize.start({width:b,height:d,marginLeft:-(this.currentImage.width/2)});break;case 2:this.stage.removeClass("loading");this.stage.setStyle("opacity",0);this.currentImage.setStyle("margin",this.options.padding);this.currentImage.inject(this.stage);this.fx.show.start(1);break;case 3:this.prevLink.setStyle("display","block");this.nextLink.setStyle("display","block");if(this.options.animateCaption){if(this.options.counter){var e=this.anchors.length;var c=this.currentIndex+1;var f=this.options.counter;f=f.replace(/\{NUM\}/,c);f=f.replace(/\{TOTAL\}/,e);this.counter.set("text",f)}this.caption.set("text",this.currentCaption);var a=this.bottom.getStyle("height").toInt();this.bottom.setStyles({opacity:1,top:-a});this.fx.bottom.start(0)}break;case 4:this.startLoad(this.anchors[this.currentIndex-1],true);this.startLoad(this.anchors[this.currentIndex+1],true);break}},close:function(){this.quickBox.setStyle("display","none");this.overlay.fade("out");this.active=false}});

//main
window.addEvent('load', function(){
	$$('#bright img, #bleft img, .overfade').setStyle('opacity', 0.4);

	function banners(){
		if(($('bright').getPosition().y * 2 + parseInt($$('#bright img').getStyle('height'))) > window.getSize().y){
			$$('#bright img, #bleft img').setStyle('height', Math.min(600, window.getSize().y / 3 * 2.2) + 'px');
		} else {
			$$('#bright img, #bleft img').setStyle('height', '600px');
		}

		var toff = Math.max(144, (window.getSize().y - parseInt($$('#bright img').getStyle('height'))) / 2);
		$('bleft').setStyle('top', toff);
		$('bright').setStyle('top', toff);

		var we = window.getSize().x - $('wrap').getSize().x;
		var orl = $('wrap').getPosition().x - 120;
		$('bleft').setStyle('left', orl + 'px');
		$('bright').setStyle('right', orl + 'px');

		if(window.getSize().x < 1240){
			$('bleft').hide();
			$('bright').hide();
		} else {
			$('bleft').show();
			$('bright').show();
		}
	}

	$$('#bright img, #bleft img, .overfade').set('tween', {duration: 'normal'}).addEvents({
		'mouseover': function(){
			this.tween('opacity', 0.8);
		},
		'mouseleave': function(){
			this.tween('opacity', 0.4);
		}
	});

	window.addEvent('resize', function(){
		banners();
	});

	banners();banners();
	
	//tooltips
	new Tips($$('#standings tbody tr[title]'), {className: 'tooltip'});
	
	//zebra table
	$$('table.squad tbody tr:not(.header):odd').setStyle('background', '#EFEFEF');
	
	var search = new InputBox($('s'), {text: 'Iskanje...'});
	var menu = new HoverIdent($('sidemenu'));
	new QuickBox();
});
