$(document).ready(function(){
	
	var page_url = window.location.href==$("base").attr("href") 
					? $("base").attr("href")+'towary/lista/'+$('#page_type').val()+'.html' 
					: window.location.href;
	
	$("a").each(function (i) {
		if ( $(this).attr('href') == page_url) {
			$(this).css('opacity', '0.5');
			if( $(this).parent().hasClass('subMenuKat') || $(this).parent().hasClass('subsubMenuKat') ) $(this).parent().show();
			if( $(this).parent().parent().hasClass('subMenuKat') ) $(this).parent().parent().show();
		} 
	});
	
	$(".szkolenie .gallery").each(function (i){
		$('#'+this.id+' a').lightBox();
	});

	$(function() {
        $('.photoContainer a').lightBox();
    });
	
	var act_img = 1;
	setInterval(function() {
	
		$("div.photoContainer").each(function (i) {
			if ($(this).children("a.photoLink").length>1) {
				img_changer(this, act_img);
			} 
		});
	
		if(act_img == 1) { 
			act_img = 2; 
		} else { 
			act_img = 1;
		}     
		
	}, 7000);
	
	
});

function img_changer(wsk, id){
	var next_id = (id==1) ? 2 : 1;
	
	$(wsk).children("a.photoLink:nth-child("+id+")").fadeOut(1000);
		setTimeout(function() {
	$(wsk).children("a.photoLink:nth-child("+next_id+")").fadeIn(1000);
	},1000);
}

function showSubmenu(id){
	$('.subMenuKat').slideUp();
	$('#subMenuKat_'+id).slideDown();
}

function showSubsubmenu(id){
	$('.subsubMenuKat').slideUp();
	$('#subsubMenuKat_'+id).slideDown();
}

function koszyk_dodaj(id){
	$.post($("base").attr("href")+"towary/dodaj.html", {
		id: id
	}, function(data){
		if(data!=0) {
			koszyk_dodaj_fade(data);
	        $('.cart_foto a').lightBox();
		}
	});	
}

function koszyk_dodaj_fade(html) {

	//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" 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;"></div>';
	str += '<div id="info_content" class="shadowWindow" style="left: '+(parseInt(xPos+windowWidth/2)-300)+'px; top: '+(parseInt(yPos+windowHeight/2)-150)+'px;">'+html+'</div>';
	$("body").append(str);


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

}

function hide_info(){

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

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

function strona(page){ //strona towarow

	var where = '';
	var cat = '';

	switch( $('#page_type').val() ){
		case 'promocje': where = 'promocja = 1'; break;
		case 'nowosci': where = 'nowosc = 1'; break;
		case 'szukaj':
			var szukaj_param = $('#szukaj_param').val();
			if( szukaj_param ) {
				szukaj_arr = szukaj_param.split('-'); 
				where = "nazwa LIKE '%"+szukaj_arr[0]+"%'"; 
				cat = szukaj_arr[1];
			}
			else { window.location.href = '';}
			break;
		default: cat = $('#page_type').val(); break;
	}
	
	$.post( $("base").attr("href") + "towary.html" , {
		ajax_cat: cat,
		ajax_page: page,
		ajax_where: where,
		ajax_view_name: 'lista'
	}, function(data){
		$('#produkty').html(data);
	});	
}
