var homeRotator = function(container) {
	var me = this;
	me.container = container;

	this.curIndex  = 0;
	this.count     = 0;
	this.timeout   = false;
	this.period    = 2500;
	this.paused    = false;

	this.getItems = function() {
		if (typeof me.items == 'undefined') {
			var items = $('DL', me.container);//#content .news 
			me.items = items;
			me.count = items.length;
		}

		return me.items;
	};

	this.getNavItems = function() {
		if (typeof me.navItems == 'undefined') {
			var items = $('LI', me.container);//#content .news 
			me.navItems = items;
		}

		return me.navItems;
	};

	this.next = function() {
		me.curIndex = me.curIndex < me.count - 1
			        ? parseInt(me.curIndex) + 1
			        : 0;

		me.setCurrent(me.curIndex);
		me.startRotation();
	};

	this.startRotation = function() {
		me.timeout = window.setTimeout(me.next, me.period);
	};

	this.stopRotation = function() {
		window.clearTimeout(me.timeout);
	};

	this.setCurrent = function(index) {
		var items     = me.getItems();
		var navItems  = me.getNavItems();
		var count     = me.count;
		var className = 'cur';

		items.removeClass(className)
		items.eq(index).addClass(className);

		navItems.removeClass(className)
		navItems.eq(index).addClass(className);

		me.curIndex = index;
	};

	this.playPause = function(button) {
		if (typeof button != 'undefined') {
			var parent = $($(button).get(0).parentNode);
			var playClass  = 'play';
			var pauseClass = 'pause';

			if (parent.hasClass(pauseClass)) {
				parent.removeClass(pauseClass);
				parent.addClass(playClass);
				me.stopRotation();
				me.paused = true;
			}
			else {
				parent.removeClass(playClass);
				parent.addClass(pauseClass);
				me.startRotation();
				me.paused = false;
			}
		}

		return false;
	};

	me.setCurrent(0);
	me.startRotation();

	var mouseover = function() {
		me.stopRotation();
		var param = site.getParamFromElementClass('i', this);
		me.setCurrent(param);
	};

	var mouseout = function() {
		if (!me.paused) {
			me.startRotation();
		}
	};

	for (var i = 0; i < me.count; i++) {
		me.getItems().eq(i).addClass('i-' + i);
		me.getNavItems().eq(i).addClass('i-' + i).mouseover(mouseover).mouseout(mouseout);
	}

	$('.nav .pause A', container).click(function() {
		return me.playPause(this);
	});
};

var site = {};

site.rotators = new Array;

site.getElementClasses = function(el) {
	var str = el.className;
	str = str.replace(/\s+/g, ' ');
	str = $.trim(str);
	var classes = str.split(' ');

	return classes;
};

site.getParamFromElementClass = function(param, el) {
	var classes = site.getElementClasses(el);
	var count   = classes.length;
	var length  = param.length;
	var item;

	for (var i = 0; i < count; i++) {
		item = classes[i];
		if (item.substr(0, length) == param && item.substr(length, 1) == '-' && item.length > length + 1) {
			return item.substr(length + 1);
			break;
		}
	}

	return false;
};

site.initHomeRotation = function() {
	$('#content .news').each(function() {
		site.rotators[site.rotators.length] = new homeRotator(this);
	});
};

site.initLightbox = function() {
	$('A.lightbox').lightBox();
};

site.initRadio = function() {
	$('#app-1 .blk.live A').click(function() {
		var url = this.href;
		var width  = 220;
		var height = 40;
		var left = (screen.availWidth  - width)  / 2;
		var top  = (screen.availHeight - height) / 2;
		var mypopup = window.open(url, 'radio', 'toolbar=no,scrollbars=no,directories=no,status=no,menubar=no,resizable=yes,width='
		            + width + ',height=' + height +  ',left=' + left + ',top=' + top);

		return false;
	});
};

$(document).ready(function() {
	site.initHomeRotation();
	site.initLightbox();
	//site.initRadio();
});