$.fn.item = function(n) { return this[n]; };

function toggleLoginFrame() {
	$('#loginframe').toggle();
	return false;
}

var navoptions = {
	auto: false,
	speed: 5000,
	selected: 0,
	length: -1,
	timer: -1,
	select: function(index) {
		navoptions.unselect();
		$($('#nav a')[index]).addClass('active');
		$($('#themas li')[index]).show();
		navoptions.selected = index;
	},
	unselect: function() {
		$($('#nav a')[navoptions.selected]).removeClass('active');
		$($('#themas li')[navoptions.selected]).hide();
	},
	iterate: function() {
		var index = (navoptions.selected+1) % navoptions.length;
		navoptions.select(index);
		if ( navoptions.auto ) {
			clearTimeout(navoptions.timer);
			navoptions.timer = setTimeout(navoptions.iterate, navoptions.speed);
		}
	},
	debug: function(msg) {
		if ( window.console && window.console.log ) {
			window.console.log(msg);
		}
	}
};

$(document).ready(function() {

	// default values of textboxes
	with ( $('input[defvalue]') ) {
		focus(function() {
			if ( this.value == this.getAttribute('defvalue') ) {
				this.value = '';
			}
		});
		blur(function() {
			if ( this.value == '' ) {
				this.value = this.getAttribute('defvalue');
			}
		});
		for ( var i=0; length>i; i++ ) {
			if ( !item(i).getAttribute('value') ) {
				item(i).setAttribute('value', item(i).getAttribute('defvalue'));
			}
		}
	}

	// custom selectboxes layouts
	$('select').selectbox({
		animationSpeed: 'fast'
	});

	$('input[type=radio], input[type=checkbox]').checkbox({empty: '/images/layout/empty.png'});

	navoptions.length = $('#nav li').length;

	$('#nav a').hover(function() {
		// mouseover //
		var index = parseInt(this.getAttribute('rel') || 0);
		navoptions.select(index);
		clearTimeout(navoptions.timer);
		navoptions.debug('stop dia');
	}, function() {
		// mouseout //
		if ( navoptions.auto ) {
			clearTimeout(navoptions.timer);
			navoptions.timer = setTimeout(navoptions.iterate, navoptions.speed);
			navoptions.debug('start dia');
		}
	});

	$('#themas').mouseover(function() {
		// mouseover //
		clearTimeout(navoptions.timer);
		navoptions.debug('stop dia');
	});
	$('#themas').mouseout(function() {
		// mouseout //
		if ( navoptions.auto ) {
			clearTimeout(navoptions.timer);
			navoptions.timer = setTimeout(navoptions.iterate, navoptions.speed);
			navoptions.debug('start dia');
		}
	});

}); // onReady
