var firmy = new Object();

firmy.scroll = function(id, distance)
{
	window.scrollTo(document.getElementById(id).offsetLeft, document.getElementById(id).offsetTop+distance);
}

firmy.animate = function(id, zwin, rozwin)
{
	o = document.getElementById('ob'+id);
	
	if (o.style.display == 'none')
	{
		$('#ob'+id).fadeIn("fast", function(){
			$('#ob'+id).animate({ height: 'show', opacity: 'show' }, 'fast');
		});
		$('#zm'+id).removeClass('sortOrderBottom').addClass('sortOrderTop').text(zwin);
	}
	else
	{
		$('#ob'+id).fadeOut("fast", function(){
			$('#ob'+id).animate({ height: 'hide', opacity: 'hide' }, 'fast');
		});
		$('#zm'+id).removeClass('sortOrderTop').addClass('sortOrderBottom').text(rozwin);
	}
}

firmy.copyAddressData = function() 
{
	var langFx = $("#wybrany_lang").val();
	
	if ($("#lang_"+langFx+"_f_firma").val() != undefined && $("#lang_"+langFx+"_nazwa").val() != undefined)
	{
		$("#lang_"+langFx+"_f_firma").val($("#lang_"+langFx+"_nazwa").val())
	}
	if ($("#f_adres").val() != undefined && $("#pole_adres").val() != undefined)
	{
		$("#f_adres").val($("#pole_adres").val())
	}
	if ($("#f_kod_pocztowy").val() != undefined && $("#pole_kod_pocztowy").val() != undefined)
	{
		$("#f_kod_pocztowy").val($("#pole_kod_pocztowy").val());
	}
	if ($("#f_miejscowosc").val() != undefined && $("#pole_miejscowosc").val() != undefined)
	{
		$("#f_miejscowosc").val($("#pole_miejscowosc").val());
	}
}

firmy.deleteEntry = function(id, count, prefix) 
{
	for (i = 1; i <= count; i++)
	{
		if (id == i)
		{
			if (document.getElementById(prefix+id).style.display=="none")
				document.getElementById(prefix+id).style.display="block";
			else
				document.getElementById(prefix+id).style.display="none";
		}
		else
			document.getElementById(prefix+i).style.display="none";
	}
}

firmy.categories = function(lang) 
{
	$(".kategorie_checkbox_"+lang+" input").click(function() {
		var checkboxClass = $(this).attr("class");
		var checkboxChecked = this.checked;
		var checkedCheckboxes = new Array();
		
		$(".kategorie_checkbox_"+lang+" input:checked").each(function(){
			var checkboxId = $(this).attr("class").split("_")[3];
			var addCheckbox = true;
			
			for (i=0;i<checkedCheckboxes.length;i++)
			{
				if (checkedCheckboxes[i] == checkboxId)
				{
					addCheckbox = false;
				}
			}
			
			if (addCheckbox)
			{
				checkedCheckboxes[checkedCheckboxes.length] = checkboxId;
			}
		});
		
		if (checkboxChecked && checkedCheckboxes.length > 3)
		{
			this.checked = false;
		}
		
		$("."+checkboxClass).each(function(){
			if (checkboxChecked)
			{
				if (checkedCheckboxes.length < 4)
				{
					this.checked = checkboxChecked;
				}
			}
			else
			{
				this.checked = checkboxChecked;
			}
		});
	});
}

firmy.generujKategoriePowiazane = function(id, id_kategorii, tabela)
{
	var ajaxLink = $("#linkPodkategoriePowiazane_"+tabela).val();
	
	$("#wyszukiwarka_"+tabela+" select[name^='k"+id+"']").find("option").remove();
	
	$.post(ajaxLink,
		{ kategoria: id_kategorii },
			function(data){
				if (data)
				{
					$("#wyszukiwarka_"+tabela+" select[name^='k"+id+"']").parent().parent().show();
					$("#wyszukiwarka_"+tabela+" select[name^='k"+id+"']").append(data);
				}
				else
					$("#wyszukiwarka_"+tabela+" select[name^='k"+id+"']").parent().parent().hide();
			}
		);
}
firmy.zawezajMiejscowosci = function(id_wojewodztwa, tabela)
{
	var ajaxLink = $("#linkZawezanieMiejscowosci_"+tabela).val();
	
	$("#wyszukiwarka_"+tabela+" select[name^='msc']").find("option").remove();
	$.post(ajaxLink,
		{ wojewodztwo: id_wojewodztwa },
		function(data){
			if (data)
				$("#wyszukiwarka_"+tabela+" select[name^='msc']").append(data);
		}
	);
}

firmy.countChecked = function(typ,obj,jqHeadMsg,jqMsg)
{
	var typ = typ;
	var linkCountChecked = $("#linkCountChecked").val();
	var id = obj.id;
	
	$.ajax({
		type: "POST",
		url: linkCountChecked,
		data: { typ: typ },
		dataType: "html",
		success: 
			function(data){
				if(data)
				{
					if(data>=5 && $("#"+id).attr("checked") == true)
					{
						$("#"+id).attr("checked", "");
						$.jqpooop({
							Id:"alertt",
							Width:400,
							Height:170,
							Ajax:'',
							Mensaje: jqMsg,
							Headmsg: jqHeadMsg
						});
					}
					else
					{
						firmy.saveChecked(typ);
					}
				}
			}
	});
}

firmy.saveChecked = function(typ)
{
	var typ = typ;
	var inp_checked = '';
	var inp_unchecked = '';
	var linkSaveChecked = $("#linkSaveChecked").val();
	var linkFindChecked = $("#linkFindChecked").val();
	var ilosc = true;
	
	$("input.c:checkbox").each(function(){ 
	
		inp_id = this.id.split("_");
		
		if(this.checked)
		{
			inp_checked += inp_id[1]+':';
			inp_unchecked += '';
		}
		else
		{
			inp_checked += '';
			inp_unchecked += inp_id[1]+':';
		}
	});

	$.ajax({
		type: "POST",
		url: linkSaveChecked,
		data: { typ: typ, inp_checked: inp_checked, inp_unchecked: inp_unchecked }
	});
}

firmy.findChecked = function(typ)
{
	var typ = typ;
	var linkFindChecked = $("#linkFindChecked").val();
	
	$.ajax({
		type: "POST",
		url: linkFindChecked,
		data: { typ: typ },
		dataType: "html",
		success: 
			function(data){
				if(data)
				{
					tab = data.split(":");
					
					for (i=0;i<tab.length;i++)
					{
						$("input#c_"+tab[i]).attr("checked","checked");
					}
				}
			}
	});
}

/* koszyk i zamowienia */

zamowienia = 
{
	dodajDoKoszyka : function(callerObj, typ, id, id_firmy)
	{
		 $.ajax({
		   type: "POST",
		   url: ajaxLinks.Firma_DodajDoKoszyka,
		   data: "typ="+typ+"&id="+id+"&id_firmy="+id_firmy,
		   success: function(msg){
			 if (msg == 'ok')
		     {
		     	if (typ == 'promuj_firma' || typ == 'firma')
		     	{
		     		window.location = "/twoje-konto/modul,KonferencjeKoszyk.html";
		     		$(callerObj).after('<span class="add">'+Lang.dodano_do_koszyka+'</span>').hide();
		     	}
		     	else
		     	{
			     	var infoBox = $(' \
							<div class="question" > \
								<div class="green">'+Lang.dodano_do_koszyka+'</div>  \
							</div>	\
					');
			     	$(callerObj).before(infoBox);
			     	setTimeout(function(){infoBox.fadeOut("slow", function(){$(this).remove()})}, 1200);
			     	
					//zamien ikonkę na off
//					var srcArr = $('img' ,callerObj).attr('src').split('.');
//					$('img' ,callerObj).attr('src',srcArr[0] + '_off.' + srcArr[1] );
			     	
			     	//zmiana klasy na off
			     	$(callerObj).addClass('button-orange-shadow');
			    }
		     }
		     else if (msg == 'juz_zamowione' )
		   		alert(Lang.element_juz_zamowiony);
		   	 else
		   		alert(Lang.ajax_err);
		     
		   },
		   error: function(){
		   	alert(Lang.ajax_err);
		   }
		 });
	
		return false;
	},	
	pokarzMiesiacePromocji : function(callerObj, ident)	
	{
		 $.ajax({
		   type: "POST",
		   url: ajaxLinks.Firma_pokarzMiesiacePromocji,
		   data: "ident="+ident,
		   success: function(msg){
		   	alert(msg);
// 		     if (msg == 'ok')
// 		     	$(callerObj).after('<span class="add">'+Lang.dodano_do_koszyka+'</span>').hide();
// 		     else if (msg == 'juz_zamowione' )
// 		   		alert(Lang.element_juz_zamowiony);
// 		   	 else
// 		   		alert(Lang.ajax_err);
		     
		   },
		   error: function(){
		   	alert(Lang.ajax_err);
		   }
		 });
	
		return false;	
	}
	
}


fotoPreloader = function()
{
	var preloaderTrue = false;
	$("input[id^=zdjecie_][type=file]").each(function(){
		if($(this).val())
			preloaderTrue = true;
	});
	
	if(preloaderTrue)
		$("#layout").show();	
}
