$(document).ready(function(){

	for (i=0; i<document.links.length;) {
		document.links[i].onfocus=document.links[i++].blur;
	}

	$(".szkolenie .gallery").each(function (i){ //galeria szkolen
		$('#'+this.id+' a').lightBox();
	});

	$(function() {
        $('.productColumn .gallery a').lightBox(); //galerie towarow
    });
	
	$(function() {
        $('#gallery a').lightBox(); //galeria aroma derm
    });
	
	$(function() {
        $('.galeria').lightBox(); //galerie stron
    });
	    
	if($('#topFotosBox').length > 0){
      	// animacja topow
		var top_len = $('.topFoto').length;
		var top_numer = 0;
		setInterval(function() {
		    
		    if(top_numer == top_len) { 
		      top_numer = 1; 
		    } else { 
		      top_numer++; 
		    }
		    top_changer(top_numer, top_len);      
		
		}, 7000);
	}
	
	var img_count = $("div.productColumn div.gallery a").length;
	if(img_count>1){
		var act_img = 0;
		var next_img = 1;
		setInterval(function() {
			$("#img_"+act_img).fadeOut(1000);
			setTimeout(function() {
				$("#img_"+next_img).fadeIn(1000);
				act_img = next_img;
				next_img++;
				next_img = next_img%img_count;
			},1000);
		}, 7000)
	}
});

function enter_log(e){
	if(e.keyCode == 13) $('#log_form').submit();
}

function enter_search(e){
	if(e.keyCode == 13) szukaj();
}

function top_changer(top_numer, top_len) {

  	var top_nastepny = (top_numer < top_len) ? top_numer+1 : 1;
	$("#top_foto"+top_numer).fadeOut(2000);
	setTimeout(function() {
    $("#top_foto"+top_nastepny).fadeIn(2000);
  },2200);

}

function towary(cat, page, quest){

	//WERSJA Z AJAXEM DO KATEGORII
	//var sub_set = $('.sub_no'+cat).size(); //sub_no# is set
	/*
	if( sub_set > 0 ){	
		if( $('.sub_no'+cat+':visible').size()==0 ){
			$('.sub').slideUp('fast');
			$('.sub_no'+cat).slideDown('fast');
			$('.menuMain_li').css('background-color', '#F3B642');
			$('.menuMain_subli').css('background-color', '#F5C466');
		}
		$.post($("base").attr("href")+"towary/cattegory.html", {
			cat: cat
		}, function(data){
			$('#right').html(data);
			//hide_info();
		});
   	}
   	else{
		$.post($("base").attr("href")+"towary/session.html", {
			page: page,
			quest: quest
		}, function(data){
			window.location.href=$("base").attr("href")+'towary/lista/'+cat+'.html';
		});
   	}
	*/
	
	
	//WERSJA Z LINKIEM BEZPOŚREDNIM DO KAŻDEJ KATEGORII
	$.post($("base").attr("href")+"towary/session.html", {
		page: page,
		quest: quest
	}, function(data){
		if(cat) window.location.href=$("base").attr("href")+'towary/lista/'+cat+'.html';
		else window.location.href=$("base").attr("href")+'towary/lista.html';
	});
}

function zamowienia_strona(page){
	$.post($("base").attr("href")+"towary/page.html", {
		page: page
	}, function(data){
		window.location.href=$("base").attr("href")+'towary.html';
	});	
}

function szukaj(){
	var quest = $('#search_input').val();
	if(quest) towary('', 1, quest);
}

function show_log_form(){
	$('#loginButt').show();
	$('#logowButt').hide();
		
	$('#searchContainer').animate({"left": "-15px"}, "slow", "swing");
	$('#searchContainer').animate({"width": "200px"}, "slow", "swing");
}

function logowanie()
{
	document.log_form.submit();
}

function clear_val(wsk, str){
	$(wsk).attr('type', 'password');
	if($(wsk).val() == str) $(wsk).val('');
}

function input_val(wsk, str){
	if(!$(wsk).val()){
		$(wsk).attr('type', 'text');
		$(wsk).val(str);
	}
}

function koszyk_dodaj_fade(id, atr, cena) {

	//wielkosc okna
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	//polozenie na stronie
	var xPos, yPos;
	if (self.pageYOffset) {
		yPos = self.pageYOffset;
		xPos = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yPos = document.documentElement.scrollTop;
		xPos = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yPos = document.body.scrollTop;
		xPos = document.body.scrollLeft;	
	}

	var str = '<div id="info_preloader" style="display: none; z-index: 1; opacity: 0.7; filter: alpha(opacity=70); background-color: #ffffff; position: absolute; left: 0px; top: 0px; width: '+pageWidth+'px; height: '+pageHeight+'px;">';
	str += '</div>';
	str += '<div id="info_content" class="tableOptions" style="left: '+(parseInt(xPos+windowWidth/2)-150)+'px; top: '+(parseInt(yPos+windowHeight/2)-100)+'px;">';
	if(id!=0){
		str += '<form method="post" name="koszyk_input_form" action="klient/koszyk_dodaj/'+id+'/'+atr+'.html">';
		str += '<div class="potwierdzenie" style="width: 300px; text-align: center;">';
		str += '<div class="text_unit" style="padding-top: 3px">Cena: <strong>'+cena+' zł</strong></div>';
		str += '<div class="text_unit">Ilość: <input style="width: 50px;" type="text" name="ilosc" value="1" /></div>';
		str += '<div class="clear">&nbsp;</div><div class="linki">';
		str += '<a class="button" id="koszyk_submit" style="margin: 8px 3px 0 92px;" href="javascript:koszyk_submit();">Dodaj</a>';
		str += '<a class="button" style="margin: 8px 3px 0 3px;" href="javascript:hide_info();">Anuluj</a>';
		str += '</div></div></form><br clear="all" />';
	}
	else{ //dla niedostepnego towaru
		str += '<div class="potwierdzenie" style="width: 300px; text-align: center;">';
		str += '<br /><strong>towar niedostępny!</strong><br /><br />';
		str += '<a class="button" style="float: none;" href="javascript:hide_info();">OK</a>';
		str += '<div class="clear">&nbsp;</div>';
		str += '</div><br clear="all" />';
	}
	str += '</div>';

	$("body").append(str);


	jQuery("#info_preloader").fadeIn(200, function(){
		jQuery("#info_content").fadeIn(200);
	});

}

function koszyk_submit(){
	$('#koszyk_submit').attr('href', 'javascript:void(0);');
	document.koszyk_input_form.submit();
}

/*
function koszyk_dodaj(){
	$.post($("base").attr("href")+"towary/koszyk.html", {
		page: page
	}, function(data){
		window.location.href=$("base").attr("href")+'towary.html';
	});		
}
*/

function hide_info(){

	jQuery(document).ready(function(){
		jQuery("#info_content").fadeOut(200,function(){
			jQuery("#info_preloader").fadeOut(200,function(){
				jQuery("#info_preloader").empty();
				jQuery("#info_preloader").remove();
				jQuery("#info_content").empty();
				jQuery("#info_content").remove();
			});
		});
	});
}

function koszyk_ilosc(wsk, id, atr){
	
	var ilosc = $(wsk).val();
	$.post($("base").attr("href")+"klient/koszyk_ilosc.html", {
		id: id,
		atr: atr,
		ilosc: ilosc
	}, function(data){
		window.location.href=$("base").attr("href")+'klient/koszyk.html';
	});
}

function koszyk_usun(id, atr){
	$.post($("base").attr("href")+"klient/koszyk_usun.html", {
		id: id,
		atr: atr
	}, function(data){
		window.location.href=$("base").attr("href")+'klient/koszyk.html';
	});
}

function koszyk_dostawa(id){
	$.post($("base").attr("href")+"klient/koszyk_dostawa.html", {
		id: id
	}, function(data){
		window.location.href=$("base").attr("href")+'klient/koszyk.html';
	});
}

function zamowienie(){
	$('#realizujZamowienie_butt').attr('href', 'javascript:void(0);');
	$.post($("base").attr("href")+"klient/zamowienie.html", {}, 
	function(data){
		window.location.href=$("base").attr("href")+'klient/komunikat.html';
	});
}

function preloader_show() {
	//wielkosc okna
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	//polozenie na stronie
	var xPos, yPos;
	if (self.pageYOffset) {
		yPos = self.pageYOffset;
		xPos = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yPos = document.documentElement.scrollTop;
		xPos = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yPos = document.body.scrollTop;
		xPos = document.body.scrollLeft;	
	}
	jQuery("#info_content").empty();
	jQuery("#info_content").remove();
	var str = '<div id="info_preloader" style="display: block; z-index: 1; opacity: 0.9; filter: alpha(opacity=70); background-color: #ffffff; position: absolute; left: 0px; top: 0px; width: '+pageWidth+'px; height: '+pageHeight+'px;">';
	str += '<img src="media/dom/gfx/preloader.gif" style="float: none; position: absolute;  z-index: 2; left: '+(parseInt(xPos+windowWidth/2)-20)+'px; top: '+(parseInt(yPos+windowHeight/2)-20)+'px; opacity: 0.9;" />';
	str += '</div>';
	
	$("body").append(str);
}