
var Page = {
	init: function() {
		Page.timerOn = false;
		Page.timer = null;
		Page.navImages = $('#navimages > a > img');
		Page.linkBars = $('#linkbar > ul');
		Page.enableNavBar();
		Page.enableLang();
		Page.enableFade();
		Page.enableImageboxes();
	},
	startTimer: function() {
		if (!Page.timerOn) {
			Page.timer = window.setTimeout(function() {
				Page.linkBars.hide();
				Page.navImages.each(function() {
					this.setState('');
				});
				if ($('.linkbar_urgent')) {
					$('.linkbar_urgent').each(function() {
						$(this).css({ display: 'inline' });
					})
				}
			},2000);
			Page.timerOn = true;
		}
	},
	stopTimer: function() {
		if (Page.timerOn) {
			window.clearTimeout(Page.timer);
			Page.timer = null;
			Page.timerOn = false;
		}
	},
	enableNavBar: function() {
		Page.navImages.each(function() {
			$.extend(this,imgState,{
				displayLinks: function() {
					Page.linkBars.hide();
					$('#linkbar_'+this.id).show();
				}
			});
		});
		Page.navImages.hover(
			function() {
				if ($('.linkbar_urgent')) {
					$('.linkbar_urgent').each(function() {
						$(this).css({ display: 'none' });
					})
				}
				Page.navImages.each(function() {
					this.setState('');
				});
				this.setState('-over');
				this.displayLinks();
				Page.stopTimer();
			},
			function() {
				Page.startTimer();
			}
		);
		Page.linkBars.hover(
			Page.stopTimer,
			Page.startTimer
		);
	},
	enableLang: function() {
		langImages = $('#langselect > a > img');
		langImages.each(function() {
			$.extend(this,imgState);
		});
		langImages.hover(
			function() {
				if (this.getState() != '-on') {
					this.setState('-over');
				}
			},
			function() {
				if (this.getState != '-on') {
					this.setState('');
				}
			}
		);
	},
	enableFade: function() {
		var rotators = $('.rotator');
		rotators.each(function () {
			var rand = Math.floor(Math.random()*2000);
			$(this).innerfade({
				speed: 6000+rand, 
				timeout: 8000+rand, 
				containerheight: '336px'
			});
		});
	},
	enableImageboxes: function() {
		var imgboxes = $('.imgbox');
		imgboxes.each(function() {
			$.extend(this,imgBar);
			this.bootStrap();
		});
	}
}

var imgState = {
	setState: function(state) {
		var src = this.src.split('/').pop().split('.')[0].split('-')[0];
		this.src = 'static/images/ui/'+src+state+'.png';
	},
	getState: function() {
		var srcArr = this.src.split('/').pop().split('.')[0].split('-');
		return (srcArr[1] ? srcArr[1] : '');
	}
}

var imgBar = {
	totalWidth: 0,
	bootStrap: function() {
		var box = this;
		this.childImgs = $(this).find('.imgBarImg');
		this.advanceButton = $(this).find('.advanceButton')[0];
		$(this.advanceButton).bind('click',function() {
			box.advance();
		});
		this.retreatButton = $(this).find('.retreatButton')[0];
		$(this.retreatButton).bind('click',function() {
			box.retreat();
		});
		this.maxWidth = $(this).width() - ($(this.advanceButton).width() + $(this.retreatButton).width() + 4);
		this.getTotalWidth();
		if (this.totalWidth > this.maxWidth) {
			this.advanceButton.style.display = 'inline';
		}
	},
	getTotalWidth: function() {
		this.totalWidth = 0;
		var box = this;
		this.childImgs.each(function() {
			if (this.style.display != 'none') {
				box.totalWidth += (this.width + 1);
			}
		});
	},
	advance: function() {
		this.childImgs.each(function() {
			if (this.style.display != 'none') {
				this.style.display = 'none';
				return false;
			}
		});
		this.retreatButton.style.display = 'inline';
		this.getTotalWidth();
		if (this.totalWidth <= this.maxWidth) {
			this.advanceButton.style.display = 'none';
		}
	},
	retreat: function() {
		var revImgs = this.childImgs.reverse();
		var box = this;
		revImgs.each(function() {
			if (this.style.display != 'inline') {
				this.style.display = 'inline';
				if (this == revImgs[revImgs.length-1]) {
					box.retreatButton.style.display = 'none';
				}
				return false;
			}
		});
		this.getTotalWidth();
		if (this.totalWidth > this.maxWidth) {
			this.advanceButton.style.display = 'inline';
		}
		this.childImgs.reverse();
	}
}

$(document).ready(function() {
	jQuery.fn.reverse = [].reverse;
	Page.init();
});
