$.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 = navoptions.setTimeout();
		}
	},
	setTimeout: function() {
		navoptions.debug('starting/continuing dia');
		navoptions.timer = setTimeout(navoptions.iterate, navoptions.speed);
		return navoptions.timer;
	},
	clearTimeout: function() {
		navoptions.debug('stopping dia');
		return clearTimeout(navoptions.timer);
	},
	debug: function(msg) {
		if ( window.console && window.console.log ) {
			window.console.log(msg);
		}
	}
};

$(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
	navoptions.debug('$(select): ' + $('select').selectbox({
		animationSpeed: 'fast'
	}).length);

	$('input[type=radio], input[type=checkbox]').checkbox({empty: '/images/layout/empty.png'});

	navoptions.length = $('#nav li').length;

	$('#nav a').mouseenter(function() {
		// enter //
		var index = parseInt(this.getAttribute('rel') || 0);
		navoptions.select(index);
		navoptions.clearTimeout();
	}).mouseleave(function() {
		// leave //
		if ( navoptions.auto ) {
			navoptions.setTimeout();
		}
	});

	$('#themas').mouseenter(function() {
		// enter //
		navoptions.clearTimeout();
	}).mouseleave(function() {
		// leave //
		if ( navoptions.auto ) {
			navoptions.clearTimeout();
			navoptions.setTimeout();
		}
	});

	$('.colorbox#gallery a').colorbox({current:"{current}/{total}"});
	
	/**
		banner display homepage 
	*/
	if( $('#banner ul li').length > 0 ){	
		$('#banner ul').before('<div id="banner-nav">').cycle({ 
			fx: 'fade', 
			speed: 1000, 
			timeout: 6000, 
			pager:  '#banner-nav', 
			pagerAnchorBuilder: function(idx, slide) { 
				// return selector string for existing anchor 
				return '<a href="#" class="pager" title="' + (idx + 1) + '">' + idx + '</a>'; 
			} 		
		}); 
	}
	

}); // onReady
