jQuery.noConflict();

function katAlfaCallback(data){
	jQuery("#menu-kat div.alfa").append(data);
}

jQuery(document).ready(function(){

	var menuKat = jQuery("#menu-kat");

	//przyciski do zmiany menu (tree/alfa)
	menuKat.find("p.title a").click(function(){
		menuKat.attr('class', jQuery(this).attr('class'));
		return false;
	});

	jQuery("#nav-tree").treeview({
		animated: "fast",
		//persist: "location",
		persist: "cookie",
		collapsed: true,
		unique: true
	//	toggle: function() { window.console && console.log("%o was toggled", this); }
	});

	//dodaj akcje do liter w alfa
	menuKat.find("p.letters a[href='#']").click(function(){
		var letter = jQuery.trim(jQuery(this).text().replace(/[\[\]]/g, '')),
			node = null;
		nodes = menuKat.find("ul#kat_alfa_"+letter);
		jQuery("#menu-kat div.alfa ul.alfa").hide();
		if(nodes.length){
			//console.log('juz jest "'+letter+'"');
			nodes.show();
		}
		else {
			//console.log('nie ma "'+letter+'" -> zaladuj');
			jQuery.get("ajax/kat_alfa.php?letter="+letter, katAlfaCallback);
		}
		return false;
	});

	var domain=(location.host==='localhost')?'localhost/biall.com.pl':location.host;
	jQuery('a.lightbox').each(function(){
		jQuery(this).lightBox({
			overlayBgColor:'#000',
			overlayOpacity:0.7,
			imageLoading:'http://'+domain+'/images/lightbox/loading.gif',
			imageBtnClose:'http://'+domain+'/images/lightbox/close.gif',
			imageBtnPrev:'http://'+domain+'/images/lightbox/prev.gif',
			imageBtnNext:'http://'+domain+'/images/lightbox/next.gif',
			imageBlank:'http://'+domain+'/images/lightbox/blank.gif',
			containerResizeSpeed:350,
			txtImage:'Obrazek',
			txtOf:'z'
		});
	});
	jQuery('span.gallery').each(function(){
		jQuery(this).find('a.lightbox').lightBox({
			overlayBgColor:'#000',
			overlayOpacity:0.7,
			imageLoading:'http://'+domain+'/images/lightbox/loading.gif',
			imageBtnClose:'http://'+domain+'/images/lightbox/close.gif',
			imageBtnPrev:'http://'+domain+'/images/lightbox/prev.gif',
			imageBtnNext:'http://'+domain+'/images/lightbox/next.gif',
			imageBlank:'http://'+domain+'/images/lightbox/blank.gif',
			containerResizeSpeed:350,
			txtImage:'Obrazek',
			txtOf:'z'
		});
	});


	jQuery('#search_fraza').click(function(){
		if (jQuery(this).val()!=='') jQuery(this).val('');
	});

});


function buyIndeksCallback(data){
	if(data.err){
		alert(data.err);
	}else{
		var n=jQuery('#wrap-buy'+data.ind),
			desc=''+data.szt+' szt.',
			olddesc=n.find('a.basket').text();
		if(data.szt>0){
			n.find('a.basket').removeClass('empty').text(desc).attr('alt',desc);
		}else{
			n.find('a.basket').addClass('empty').text(desc).attr('alt',desc);
		}
		n.removeClass('loading');
	}
}

function buyIndeksAjax(ind,url){
	var n=document.getElementById('buy'+ind),val=n.value;
	if(!n){return true;}
	val=parseInt(val);if(isNaN(val)||val===0){val=1}
	//window.location=''+url+'index.php?sub=user&sub2=buy&add_indeks='+ind+'&add='+val;
	jQuery('#wrap-buy'+ind).addClass('loading');
	jQuery('#buy'+ind).val('');
	jQuery.getJSON(''+url+'ajax/basket.php?add_indeks='+ind+'&add='+val, buyIndeksCallback);

	return false;
}

function buyIndeks(ind,url){
	var n=document.getElementById('buy'+ind),val=n.value;
	if(!n){return true;}
	val=parseInt(val);if(isNaN(val)||val===0){val=1}
	window.location=''+url+'index.php?sub=user&sub2=buy&add_indeks='+ind+'&add='+val;
	return false;
}

