/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
/*\/\/\/\/\/\/\/\/\/\/\ AJAX FUNCTIONS /\/\/\/\/\/\/\/\/\/\/\*/
/*\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/

	var ajax = new sack();
	/*********************************************************************************
	*   [create_function] - funckja do generowania zapytan AJAX
	*
	*	create_function({   'params': {id : '1', nazwa : 'lorem ipsum', .itd...}, 
	*						'type'  : 'POST',
	*						'action': 'ajax/test',
	*						'return_type' : 'html',
	*						'return_object_id' : '#overlayAjax' })
	*
	**********************************************************************************/
	function create_function(setting){
		
		var params = '';
		var appers = '';
		if(setting['return_object_id']) {
				$(setting['return_object_id']).html(''); 
		}
		/** Tworzenie parametrów funkcji **/
		$.each(setting['params'], function(key, value) { 
			params = params + appers + key + '=' + value;
			appers = '&';
		});

		/** Budowanie funkcji ajax **/
		$.ajax({ type: setting['type'],
				 url: url_base + setting['action'],
				 data: params,
				 success: function(html){   
				
					
					if(html == 'refresh'){
						location.href = url_base + url_fullPath;
					}else{
						if(setting['return_object_id']) {
							$(setting['return_object_id']).html(html); 
						}
				 }
				 }
			  })
	}
	
	/********************************************************/
	/****	RATING: Skrypt odpowiedzialny za głosoanie 	 ****/
	/********************************************************/
	function initRating(article, article_id, user_id){
			$("#rat").children().not(":radio").hide();
			$("#rat").stars({
				split: 2,
				oneVoteOnly: true,
				callback: function(ui, type, value){
					$("#rat").hide();
					$("#sum_votes").hide();
					$("#vote_title").hide();
					$("#loader_rating").show();
					
					$.ajax({ type: 'POST',
							 url: url_base + 'ajax/set_rating',
							 data: 'rate=' + value + '&article=' + article + '&article_id=' + article_id + '&user_id=' + user_id,
							 success: function(html){  
									resources = html.split(':');
											ui.select(resources[2]);
											$("#avg").text(resources[0]);
											$("#votes").text('('+resources[1]+' głosy)');
											$("#loader_rating").hide();
											$("#rat").show();
											$("#sum_votes").show();
											$("#vote_title").show();
									 }
							});
				}
			});
			$("#ocena_artykulu").show();
	}	

		
	
	function CheckRating_part(article, article_id, user_id){
			$("#btn_rating_"+article_id).hide();
			$("#ocena_artykulu_"+article_id).hide();
			$.ajax({ type: 'POST',
					 url: url_base + 'ajax/get_rating',
					 data: 'article=' + article + '&article_id=' + article_id + '&user_id=' + user_id,
					 success: function(html){  
									resources = html.split(':');
									$("#rat_"+article_id).stars({
													split: 2,
													disabled: true	
									});

									$("#rat_"+article_id).stars("select", resources[2]);
									$("#ocena_artykulu_"+article_id).show();
							}
					});
	}
	
	function CheckRating(article, article_id, user_id){
			$("#btn_rating").hide();

			$.ajax({ type: 'POST',
					 url: url_base + 'ajax/get_rating',
					 data: 'article=' + article + '&article_id=' + article_id + '&user_id=' + user_id,
					 success: function(html){  

									resources = html.split(':');
									if(resources[3] == 'fail' || user_id == ''){
										$("#rat").stars({
													split: 2,
													disabled: true	
										});
										
										$("#rat").stars("select", resources[2]);
										$("#avg").text(resources[0]);
										$("#votes").text('('+resources[1]+' głosy)');
										$("#ocena_artykulu").show();
										$("#sum_votes").show();
									}else{
										$("#rat").stars("select", resources[2]);
										$("#avg").text(resources[0]);
										$("#votes").text('('+resources[1]+' głosy)');
										$("#ocena_artykulu").show();
										$("#sum_votes").show();
										initRating(article, article_id, user_id);
									}
							}
					});
	}
		
	/********************************************************/
	/****	MODEROWANIE FORUM: USUWANIE POSTÓW 	    	 ****/
	/********************************************************/
	function delete_forum_confirm_box(topic, id_postu, parent_id){
		var odpowiedz = confirm('Czy napewno usunąć post/temat(Usunięcie pierwszego postu powoduje usun)');
			if(odpowiedz){
				ajax.requestFile = url_base + 'ajax/delete_post?id_postu=' + id_postu + '&topic=' + topic;
				ajax.onCompletion = function() { Return_delete_forum_confirm_box(topic, parent_id, id_postu); };
				ajax.runAJAX();
			}else{ /** window.alert('nie gra'); **/ }
	}
	
	function Return_delete_forum_confirm_box(topic, parent_id, id_postu){
		if (parent_id != 0){
			location.href= url_base + 'forum/view_topic/' + topic;
		}else {
			location.href= url_base + 'forum';
		}
	}
	
	/********************************************************/
	/****	LISTA KATEGORII: WYDARZE� i ATRAKCJI     	 ****/
	/********************************************************/
	function ajax_wyd_atr_category(type){

				document.getElementById('aw_select').options.length = 0;
				document.getElementById('aw_select').innerHTML = '';

				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/pobierz_kategorie_wa',
						 data: 'type=' + type,
						 success: function(html){  
						 var obj = document.getElementById('aw_select');
						 eval(html);
						 }
				});
	}

	/********************************************************/
	/****	AUTH: SPRAWDZENIE CZY PODANY LOGIN ISTNIEJE	 ****/
	/********************************************************/
	function ajax_check_user(nazwa){
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/check_user',
						 data: 'nazwa=' + nazwa,
						 success: function(html){ 
							$("#ajax_chk_user").html(html); 
							$("#ajax_chk_user").val('ok');
						 }
						})
	}
	
	/********************************************************/
	/****	SCHOWEK: ZARZ�DZANIE SCHOWKIEM	 	 		 ****/
	/********************************************************/
	function ajax_schowek(step, dzial, id, cat, data){
			    
				if(step == 3 && cat == '')
        {
					alert('Wybierz kategorię.');
					return false;
				}
				else if(step == 3 && data == '')
        {
					alert('Wybierz datę atrakcji.');
					return false;					
				}
        else
        {
					document.getElementById('overlayAjax').innerHTML = '';
				}
				
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/schowek',
						 data: 'step=' + step + '&dzial=' + dzial + '&id=' + id + '&cat=' + cat+ '&data=' + data,
						 success: function(html){ $("#overlayAjax").html(html); }
						})

	}
	/********************************************************/
	/****	SCHOWEK: ZARZ�DZANIE OBSERWOWANYMI	 	 		 ****/
	/********************************************************/
	function ajax_obserwowane(step, dzial, id){
			    document.getElementById('overlayAjax').innerHTML = '';
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/obserwowane',
						 data: 'step=' + step + '&dzial=' + dzial + '&id=' + id,
						 success: function(html){ 
						   $("#overlayAjax").html(html);
						 }
						})
	}

	/********************************************************/
	/****	AKTUALNO�CI: ZAPISYWANIE TAB'a	 	 		 ****/
	/********************************************************/
	function ajax_save_news_tab(tab){
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/save_tab',
						 data: 'tab=' + tab,
						 success: function(msg){ /*alert(msg);*/}
				})
	}

	/********************************************************/
	/**** 	LISTA POWIAT�W	 ****/
	/********************************************************/
	function PobierzListePowiatow(sel, div_id){
			var woj_id = sel.options[sel.selectedIndex].value;
			document.getElementById(div_id).options.length = 0;
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/powiaty',
						 data: 'id_wojewodztwa=' + woj_id,
						 success: function(html){  
						 var obj = document.getElementById(div_id);
						 eval(html);
						 }
				})
	}

	/********************************************************/
	/****	LISTA MIEJSCOWOŚCI 	 ****/
	/********************************************************/
	function PobierzListeMiejscowosci(sel, div_id){
			var id_miejscowosci = sel.options[sel.selectedIndex].value;
			document.getElementById(div_id).options.length = 0;
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/miejscowosci',
						 data: 'id_miejscowosci=' + id_miejscowosci,
						 success: function(html){  
						 var obj = document.getElementById(div_id);
						 eval(html);
						 }
				})
	}
	
	/********************************************************/
	/****	LISTA GMIN 	 ****/
	/********************************************************/
	function PobierzListeGmin(sel, div_id){
			var pow_id = sel.options[sel.selectedIndex].value;
			document.getElementById(div_id).options.length = 0;
			
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/gminy',
						 data: 'id_powiatu=' + pow_id,
						 success: function(html){  
						 var obj = document.getElementById(div_id);
						
						 eval(html);
						 }
				})
			
	}

	/********************************************************/
	/****	LISTA WOJEWODZTW - SZUKANE W/G MIAST(GMIN) 	 ****/
	/********************************************************/
	function ZaznaczWojewodztwo(sel, div_id, diw_id_miejsc){
			var id_miasta = sel.options[sel.selectedIndex].value;
			if(id_miasta.length>0){
				document.getElementById(div_id).options.length = 0;
				document.getElementById(diw_id_miejsc).options.length = 0;
				$.ajax({ type: 'GET',
						 url: url_base + 'ajax/wojewodztwa',
						 data: 'id_miasta=' + id_miasta,
						 success: function(html){  
							var obj = document.getElementById(div_id);
							var obj1 = document.getElementById(diw_id_miejsc);
							eval(html);
						 }
				})
			}
	}

	/********************************************************/
	/****	FORUM: CYTOWANIE POST�W NA FORUM 			 ****/
	/********************************************************/
	function cytujtext(user,topicid) {
			var txt = document.getElementById(topicid).innerHTML;
			txt = txt.replace(/^\s+|\s+$/g, '');
			txt = txt.replace(/<br>/g, '\n');
			txt = txt.replace(/<div class="forum_cytat_content">/g, '[CYTAT]');
			txt = txt.replace(/<\/div>/g, '[/CYTAT]');
			document.getElementById('odpowiedz').value = "[quote user='" + user + "']\n"+txt+"\n[/quote]";
			document.getElementById('odpowiedz').focus();
	}
	
	/********************************************************/
	/****	ZMIANA PHOTO - WYDARZE� i ATRAKCJI 	 	 	 ****/
	/********************************************************/
	function change_photo(photo,id,rels) {
			obrazek = document.getElementById("atrakcje_foto_glowne");
			link = document.getElementById("atrakcje_foto_glowne_link");
			obrazek.src = basepath + rels + id + "/mid_" + photo;
			link.href = basepath + rels + id +  "/big_" + photo;
	}
	
	/********************************************************/
	/****	ZMIANA PHOTO - NOCLEGI 	 	 	 			 ****/
	/********************************************************/
	function change_photo1(photo,id,rels) {
			obrazek = document.getElementById("atrakcje_foto_glowne");
			link = document.getElementById("atrakcje_foto_glowne_link");
			//obrazek.src = basepath + rels + id + "/mid_" + photo;
			//link.href = basepath + rels + id +  "/" + photo;
			obrazek.src =  rels + id + "/mid_" + photo;
			link.href =  rels + id +  "/" + photo;
	}

