// Funkcje js uzywane na podstronach

$(document).ready(function(){   
	// Hover na inputach
	$(".textInput").focus(function () {
		$(this).addClass("hovered").addClass("focus");
	});
	$(".textInput").blur(function () {
		$(this).removeClass("hovered").removeClass("focus");
	});
	$(".textInput").mouseover(function () {
		$(this).not(".hovered").addClass("hovered");
	});
	$(".textInput").mouseout(function () {
		$(this).not(".focus").removeClass("hovered");
	});
	// Szukaj
	$("#searchInput").focus(function() {
		if (this.value == "Wpisz słowa...")
			this.value = "";
	 });
	$("#searchInput").blur(function() {
		if (this.value == "")
			this.value = "Wpisz słowa...";
	 });
	
	//Hover na buttonach
	$(".buttonSubmit").mouseover(function () {
		$(this).addClass("hovered");
	});
	$(".buttonSubmit").mouseout(function () {
		$(this).removeClass("hovered");
	});
	
	
	// Tooltip na strone glowna
	$('#header h1 a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    opacity: 0.85 
	});
	//skasowanie alta - przeszkadza przy tooltipie
	$('#header h1 a img').attr("alt","");

	
	// Logotypy na dole strony
	//$("#row1").fadeTo("fast", .5);
	//$("#row1").hover(function(){
 	//		$(this).fadeTo("fast", 1);
	//	},function(){
  	//		$(this).fadeTo("fast", .5);
	//	});

});

// Presbox

	// -- SETTINGS ------------------------------------------------------------------------------------
	
	// fade animation time
	var ANIMATION_TIME = 500;
	
	// items switch period
	var SWITCH_PERIOD = 12000;

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


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>Usługi</div><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><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();
		self.changedByUser = true;
	});
	
	
	$(masterDiv).find(".promoNavi").find("a").eq(1).click( function() {
		self.nextItem();
		self.changedByUser = true;
	});
		
	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).hide(); // fadeIn current item				
		//this.getItem(this.activeItem).customFadeOut( ANIMATION_TIME ); // fadeOut old item	
		this.getTab(this.activeItem).removeClass("active");
	}			
	this.activeItem = idx;								
	//this.getItem(this.activeItem).customFadeIn( ANIMATION_TIME ); // fadeIn current item				
	this.getItem(this.activeItem).show(); // 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);
});
