$.fn.extend({
	
	zIndex: function(e){
		
		return this.each(function(){
			$(this).css('zIndex',e);
		});

	}
	
});

var nextStep = 1;
var nCurrIndex = 0;

function showNexBannerImg(i){


	var oImg = $('div#banner img.bannerImg');
	var oBtn = $('div#banner div.buttons a.btn');
	var oLabelHolder = $("div#banner div.label div.labelHolder");
	var oLabel = oLabelHolder.find("h2");
	var nTotal = oImg.length;
	
	if (!nCurrIndex||nCurrIndex<0){
		// will be undefined on init call
		nCurrIndex = 0;
		oLabelHolder.height(oLabel.height()*oLabel.length);
	}
	
	if (arguments.length>0) {
		nextStep = i;
	} else {
		nextStep = nCurrIndex+1;
	}
	
	if (nextStep==nTotal) {	
		nextStep = 0;
	}
	
	// set our class to active for the slide are going to show
	oBtn.removeClass('active').eq(nextStep).addClass('active');
	
	oLabelHolder.animate({'top':-nextStep*oLabel.height()},1000);

	if (nCurrIndex==nextStep) {
		
		// init just show first image
		oImg.hide().eq(nextStep).show();
		
	} else {
		// change the z index maybe? is this the reshuffle?
		oImg.stop().removeAttr("style").hide().eq(nCurrIndex).zIndex(0).show();

		oImg.eq(nextStep).zIndex(1).fadeIn(1000,function(){

			// hide the current image?
			oImg.eq(nCurrIndex).hide();
			nCurrIndex=nextStep;
			
		});
	
	}
	
	
		
}



$(document).ready(function(){

	// webkit browsers load CSS and JS at the same time so make them wait a bit more
	// if (jQuery.browser.safari && document.readyState != "complete") { setTimeout( arguments.callee, 100 ); return; }
	
		
	$("#villagedd").change(function(){
		window.location = $('#villagedd').val();

	});

	if ($.isFunction($.fn.selectbox)===true) {
		$("select#villages").selectbox({
			dropTime: 0,
			bIncludeFirstOption: false,
			listener: function(e,o){
			
				if (e=='ITEM_SELECTED') {

					var value = $("input#villages").data('oInputHidden').val();

					window.location = 'index.php?id='+value;

				}
			
			} // e = event switch string // o = event variables

		});
		
	}
	
	if ($.isFunction($.fn.hint)===true) {
		$('input[title!=""]').hint();
	}
	
	if ($.isFunction($.fn.colorbox)===true) {
		$('a.galImgThumb').colorbox({
			transition:'fade',
			speed:500
		});
	}
	
	
	
	
	if ($("div#wrapper.home").length>0) {
		
		
		
		// INIT
		showNexBannerImg(0);
		var t = setInterval(function(){ showNexBannerImg(); },4000);
		
		

		$("div#banner div.buttons a.btn").bind('click',function(){
			
			clearInterval(t);
			
			var index = $(this).index();
			showNextBannerImg(index);
			
			return false;
			
		});
	
	}
	
});


