// Presbox

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

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

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 class='promoNaviP promoNavi'><a href='#'>Poprzednia</a></div>";
	navPart += "<ul class='promoNav'>";
	for(var i=0; i<this.itemsCount; i++) {
		navPart+="<li><a href='#'>"+(i+1)+"</a></li>";
	}
	navPart += "</ul>";
	navPart += "<div class='promoNaviN promoNavi'><a href='#'>Następna</a></div>";
	
	if (this.itemsCount >1 ) {
		$(masterDiv).find(".promoTabs").append(navPart);	
	}
	
	// initialize tabs
	var i=0;		
	$(masterDiv).find(".promoNav").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);
});
$(document).ready(function(){	
	items = $("#promoShow2");		

	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);
});


// Menu
/*$(document).ready(function(){
	$("#menu li").mouseenter(function(){
	    $(this).children().slideDown(300);
	    $(this).siblings().children("ul").slideUp("slow");
	});	
});*/

function menuOver(eventObject) {
	    $(this).children().slideDown(500);
	    $(this).siblings().children("ul").slideUp("slow");
}
function menuOut(eventObject) {
	//do nothing
}
var config = {    
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
     interval: 200, // number = milliseconds for onMouseOver polling interval    
     over: menuOver, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: menuOut // function = onMouseOut callback (REQUIRED)    
};
$(document).ready(function(){
	//$("#menu li").hoverIntent( config );	
});
