﻿var Rotator = {
	available : function(){
		this.displayContainer = document.getElementById('main-rotator');
		if(this.displayContainer) return true;
		else return false;
	},
	init : function(){
		var listElms = this.displayContainer.getElementsByTagName('a');
		this.list = new Array();
		for(var i = listElms.length - 1; i > -1; i--){
			this.list[i] = new RotatorItem(listElms[i], i == 0, i );
		}
		this.position = 0;
		this.interval = window.setInterval('Rotator.next()', 5000);
	},
	manualJump : function(e){
		window.clearInterval(Rotator.interval);
		if(isNaN(e)){
			var targ;
			var pos;
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) targ = targ.parentNode;
			for(var i = Rotator.list.length - 1; i > -1; i--){
				if(Rotator.list[i].indicator == targ){
					Rotator.next(Rotator.list[i].position);
					continue;
				}
			}
		}
		else Rotator.next(e);
		Rotator.interval = window.setInterval('Rotator.next()', 5000);
	},
	next : function(newPosition){
		var prevPosition = this.position;
		if(!isNaN(newPosition)) this.position = newPosition;
		else{
			if(this.position == this.list.length - 1) this.position = 0;
			else this.position += 1;
		}
		this.list[prevPosition].setVisible(false);
		this.list[this.position].setVisible(true);
	}
};
var RotatorItem = function(link, visible, position){
	this.maxOpacity = 0.99;
	this.minOpacity = 0;
	this.indicatorSelectedClass = 'selected';
	this.firstClass = 'first';
	this.lastClass = 'last';
	this.anchor = link;
	this.container = document.createElement('div');
	this.image = this.anchor.getElementsByTagName('img')[0];
	if(document.getElementById('ie6css'))
		this.anchor.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.image.src + "', sizingMethod='crop')";
	else
		this.anchor.style.backgroundImage = 'url(' + this.image.src + ')';
	this.image.style.display = 'none';
	this.indicator = document.createElement('a');
	this.indicator.appendChild(document.createTextNode('\u00A0'));
	this.indicator.onclick = Rotator.manualJump;
	this.container.appendChild(this.indicator);
	if(position < 1){
		this.indicator.className = this.indicatorSelectedClass;
		this.container.className = this.firstClass;
	}
	var footer = this.anchor.parentNode.getElementsByTagName('div')[0];
	if(Rotator.list[position + 1]) footer.insertBefore(this.container, Rotator.list[position + 1].container);
	else{
		this.container.className = this.lastClass;
		footer.appendChild(this.container);
	}
	this.position = position;
	this.opacity = (visible)? this.maxOpacity : this.minOpacity;
	this.anchor.style.display = (visible)? 'block' : 'none';
	this.opacityIncrement = 0.05;
	this.opacityInterval = 50;
	this.setVisible = function(visible){
		this.visible = visible;
		window.clearInterval(this.interval);
		this.interval = window.setInterval('Rotator.list[' + this.position + '].' + ((visible)?'increase' : 'decrease') + 'Visibility()', this.opacityInterval);
	};
	this.increaseVisibility = function(){
		if(!this.visible) return;
		if(this.indicator.className == ''){
			this.anchor.style.display = 'block';
			this.indicator.className = this.indicatorSelectedClass;
		}
		if(this.opacity < this.maxOpacity) this.setOpacity(this.opacity + this.opacityIncrement);
		else {
			window.clearInterval(this.interval);
		}
	};
	this.decreaseVisibility = function(){
		if(this.visible) return;
		if(this.indicator.className != '') this.indicator.className = '';
		if(this.opacity == this.minOpacity) this.anchor.style.display = 'none';
		if(this.opacity > this.minOpacity) this.setOpacity(this.opacity - this.opacityIncrement);
		else {
			window.clearInterval(this.interval);
		}
	};
	this.setOpacity = function(value){
		this.opacity = (value > this.maxOpacity)? this.maxOpacity : value;
		this.opacity = (this.opacity < this.minOpacity) ? this.minOpacity : this.opacity;
		this.anchor.style.opacity = this.opacity;
		this.anchor.style.MozOpacity = this.opacity;
		/*
		this.anchor.style.filter = "alpha(opacity=" + (this.opacity * 100) + ")";
		this.image.style.opacity = this.opacity;
		this.image.style.MozOpacity = this.opacity;
		this.image.style.filter = "alpha(opacity=" + (this.opacity * 100) + ")";
		*/
	};
	this.setOpacity(this.opacity);
};
if(Rotator.available()) Rotator.init();
var Countdown = {
	available : function(){
		this.text = document.getElementById('countdown-text');
		if(this.text) return true;
		else return false;
	},
	init : function(){
		this.concertDate = Date.parse(this.text.innerHTML);
		this.adjust();
		this.interval = window.setInterval('Countdown.adjust()', 1000);
	},
	adjust : function(){
		var now = (new Date()).getTime();
		var offset = this.concertDate - now;
		var days = new String(Math.floor(offset / (1000*60*60*24)));
		var hours = new String(Math.floor((offset % (1000*60*60*24)) / (1000*60*60)));
		var minutes = new String(Math.floor((offset % (1000*60*60)) / (1000*60)));
		var seconds = new String(Math.floor((offset % (60*1000)) / 1000));
		while(days.length < 3){days = '0' + days;}
		while(hours.length < 2){hours = '0' + hours;}
		while(minutes.length < 2){minutes = '0' + minutes;}
		while(seconds.length < 2){seconds = '0' + seconds;}
		this.text.innerHTML =  days + ' : ' + hours + ' : ' + minutes + ' : ' + seconds;
	}
};
if(Countdown.available()) Countdown.init();

var Login = {
	available : function(){
		this.username = document.getElementById('login-username');
		this.password = document.getElementById('login-password');
		this.form = document.getElementById('home-login');
		if(this.username && this.password && this.form) return true;
		else return false;
	},
	init : function(){
		this.username.onkeyup = this.trySubmit;
		this.password.onkeyup = this.trySubmit;
	},
	trySubmit : function(e){
		var key;
		if(window.event) key = window.event.keyCode;
		else if (e) key = e.which;
		if(key == 13) Login.form.submit();
	}
};
if(Login.available()) Login.init();
var FlickrContainer = {
	available : function(){
		this.container = document.getElementById('flickr-container');
		if(this.container) return true;
		else return false;
	},
	init : function(){
		var links = this.container.getElementsByTagName('a');
		for(var i = links.length - 1; i > -1; i--){
			links[i].target = '_blank';
		}
	}
};
if(FlickrContainer.available()) FlickrContainer.init();