// Presbox

	// -- SETTINGS ------------------------------------------------------------------------------------
	
	// fade animation time
	var ANIMATION_TIME = 1000;
	
	// items switch period
	var SWITCH_PERIOD = 10000;

	// -----------------------------------------------------------------------------------------------------

function Presbox(masterDiv) {
	
	var self = this; // keep ctx
	this.changedByUser = false;
	
	this.activeItem = -1;

	this.masterDiv = masterDiv;
	this.itemsCount = $(masterDiv).find(".promo").length;
	
	// generate navigation part
	var navPart = "<div><ul class='promoNav'>";
	for(var i=0; i<this.itemsCount; i++) {
		navPart+="<li><a href='#'>"+(i+1)+"</a></li>";
	}
	navPart += "</ul></div>";
	
	if (this.itemsCount >1 ) {
		$(masterDiv).find(".promoTabs").append(navPart);	
	}
	
	// initialize tabs
	var i=0;		
	$(masterDiv).find(".promoTabs").find("a").each(function(i) {					
		var idx = i++;
		$(this).click(function() {									
			self.setActiveItem(idx);
			self.changedByUser = true;
		});				
	});	
	
	$(masterDiv).find(".promoNavi").find("a").eq(0).click( function() {
		self.previousItem();
	});
	
	
	$(masterDiv).find(".promoNavi").find("a").eq(1).click( function() {
		self.nextItem();
	});
		
	this.setActiveItem(0);
				
	
}

Presbox.prototype.switchBox = function() {
	if(this.changedByUser) { // when changed by user, omit this turn
		this.changedByUser = false;
		return;
	}
			
	var nextIdx = this.activeItem+1;
	if(nextIdx >=this.itemsCount) nextIdx = 0;
					
	this.setActiveItem(nextIdx);	
}

Presbox.prototype.getItem = function(idx) {
	return $(this.masterDiv).find(".promo").eq(idx);		
}

Presbox.prototype.getTab = function(idx) {		
	return $(this.masterDiv).find(".promoNav").find("a").eq(idx);	
}

Presbox.prototype.setActiveItem = function(idx) {
	if(this.activeItem>=0) {
		if(this.activeItem==idx) return; // omit, the same
		this.getItem(this.activeItem).fadeOut( ANIMATION_TIME ); // fadeOut old item	
		this.getTab(this.activeItem).removeClass("active");
	}			
	this.activeItem = idx;								
	this.getItem(this.activeItem).fadeIn( ANIMATION_TIME ); // fadeIn current item				
	this.getTab(this.activeItem).addClass("active");		
}

Presbox.prototype.nextItem = function() {
	this.changedByUser = true;
	var nextIdx = this.activeItem+1;
	if(nextIdx >=this.itemsCount) nextIdx = 0;
	this.setActiveItem(nextIdx);
}
	
Presbox.prototype.previousItem = function() {
	this.changedByUser = true;
	var nextIdx = this.activeItem-1;
	if(nextIdx <0) nextIdx = this.itemsCount-1;
	this.setActiveItem(nextIdx);
}
	
$(document).ready(function(){	
	items = $(".promoShow");	
	
	var pBoxes = [];
	
	for(var i=0; i<items.length; i++) {
		var p = new Presbox(items[i]);
		pBoxes.push(p);
	}
					
	// initialize periodical presentation
	window.setInterval( function() {
		for(var i=0; i<pBoxes.length; i++) {
			pBoxes[i].switchBox();
		}
	
	}, SWITCH_PERIOD);
	
});


// searchbox
// document ready - init routines
$(document).ready(function(){
	$("#searchText").focus(function() {
	   if (this.value == "Szukaj...")
		 this.value = "";
	 });
	$("#searchText").blur(function() {
	   if (this.value == "")
		 this.value = "Szukaj...";
	 });
});	