$(document).ready(function() {


	
	$("#datado").datepicker();
	
	$(".premium:odd").addClass("odd");
	$(".plus:odd").addClass("odd");
	

	$('#usun_logo').click(function()
	{
		$("#formularz_plik").attr("action",$("#formularz_plik").attr('action')+ '&usun=1'); 		
	});

	
	$('form').submit(function() {
		$('input[type=submit]').attr('disabled', 'disabled');
	});
	
   $('.stronnicowanie_input').keypress(function(e)
    {
        code= (e.keyCode ? e.keyCode : e.which);
        if (code == 13) 
        {
        	window.location = $('.stronnicowanie_href').val()+'&strona='+$(this).val();
        }
      
    });

	$.ui.dialog.defaults.bgiframe = true;
	
	$('#post').dialog({ autoOpen: false, 
		 width: 700,
		 height:320,
		 modal: false,
		 resizable: true,
		 closeOnEscape: false,
		 title: 'Dodaj post'});
	
	$('.postbutton').click(function() {
		
		$('#post').dialog("open");
		$("#tekst").htmlarea();
		
	});
	
	$('#dodapost').click(DodajPost);
	function DodajPost()
	{
		$("#tekst").val($("#tekst").htmlarea("toHtmlString"));
		
		if ($('#tekst').val()=='')
		{
			alert('Wpisz tekst');
			return false;
		}
		
		var serialized_form = $('#post_dodaj_form').serialize();
	    $.ajax({
	        data: serialized_form,
	        url: '/forum-post-post.html',
	        dataType: 'json',
	        type: 'post',
	        success: function(j) 
			{
	    		if (j.ok == true)
	            {
	    			window.location = window.location;
	            }
	    		
	    		if (j.ok == false)
	    		{
	    			$('#error').html(j.komunikat);
	    		}
	        }
	    });
	}
	
	$('.watekbutton').click(function() {
		
		$('#watek').dialog({ autoOpen: true, 
			 width: 700,
			 height:360,
			 modal: false,
			 resizable: true,
			 title: 'Dodaj wątek'});
		$("#tresc").htmlarea();
		

	});
	
	$('#dodajwatek').click(DodajWatek);
	function DodajWatek()
	{
		$("#tresc").val($("#tresc").htmlarea("toHtmlString"));
		
		if ($('#nazwa').val()=='')
		{
			alert('Wpisz wątek');
			return false;
		}
		
		if ($('#tresc').val()=='')
		{
			alert('Wpisz tekst');
			return false;
		}
		
		var serialized_form = $('#watek_dodaj_form').serialize();
	    $.ajax({
	        data: serialized_form,
	        url: '/forum-watek-post.html',
	        dataType: 'json',
	        type: 'post',
	        success: function(j) 
			{
	    		if (j.ok == true)
	            {
	    			window.location = window.location;
	            }
	    		
	    		if (j.ok == false)
	    		{
	    			$('#error').html(j.komunikat);
	    		}
	        }
	    });
	    
	  
	}
	
	// ####################################
	// rejestracja
	// ####################################
	
	$('#Rejestruj').click(function() {
		
		$('#formularz_rejestracja').attr('action',$('#formularz_rejestracja').attr('action')+'?k='+$('#kodantyspamowy').val());
		$('#formularz_rejestracja').submit();
		
	});
	
	// ####################################
	// aukcja dodaj
	// ####################################
	
	$('#id_cennik').change(function() {
		 
		$.ajax({
		        url: '/ajax.aukcja_data.html?id='+$(this).val(),
		        dataType: 'json',
		        type: 'post',
		        success: function(j) 
				{
		    		if (j.ok == true)
		            {
		    			$('#datado').attr('value',j.data);
		            }
		        }
		    });		
	});
	
	
	
	// ####################################
	// szukajka
	// ####################################
	$('#szukajw').change(function(){
		var dest = $(this).val();
		$('#form-szukaj').attr('action',dest);
	});
	// -----------------------------------------
	
	
	// ####################################
	// rejestracja
	// ####################################
	if ( $('#rodzaj').val() != 2 )
	{	
		$('#danefirmy').hide();
	}

	$('#rodzaj').change(function() {
		if ($('#rodzaj').val() == 1)		{
			
			$('#danefirmy').hide();
		}
		else
		{			
			$('#danefirmy').show();
		}		
	});
	
	// ####################################
	// oferty pracy
	// ####################################
	
	$('#id_cennik').change(function() {
		if ($('#id_cennik').val() == 9 || $('#id_cennik').val() == 21 || $('#id_cennik').val() == 22)		{
			
			$('#cv').show();
		}
		else
		{			
			$('#cv').hide();
		}		
	});
	
	// ####################################
	// fancy zoom 
	// ####################################
	// $("a.foto_zoom").fancybox({
	// 			'zoomOpacity'			: true,
	// 			'overlayShow'			: false,
	// 			'zoomSpeedIn'			: 500,
	// 			'zoomSpeedOut'			: 500
	// 	});
	
	// lightbox
	$('a.foto_zoom').lightBox();
	// -------------------------------------
	
	// ####################################
	// dodanie lub edycja ogloszenia
	// ####################################
	$('#cenanatelefon').click(function() {
		if ($('#cenanatelefon').is(':checked'))		
		{			
			$('#cena').attr('disabled', 'disabled'); 
			$('#waluta').attr('disabled', 'disabled');
		}
		else
		{			
			$('#cena').removeAttr('disabled');
			$('#waluta').removeAttr('disabled');
		}		
	});
	
	if ($('#cenanatelefon').is(':checked'))		
	{			
		$('#cena').attr('disabled', 'disabled'); 
		$('#waluta').attr('disabled', 'disabled');
	}
	else
	{			
		$('#cena').removeAttr('disabled');
		$('#waluta').removeAttr('disabled');
	}
	
	// ####################################
	// polatnosc
	// ####################################
	$('#platnosc_form').submit(Platnosc);
	function Platnosc()
	{
		
		var serialized_form = $('#platnosc_form').serialize();
	    $.ajax({
	        data: serialized_form,
	        url: '/platnoscpost.html',
	        dataType: 'json',
	        type: 'post',
	        success: function(j) 
			{
				if (j.ok == false)
	            {
	                $('#platnosc_form').submit(Platnosc);
					$('input[type=submit]').removeAttr('disabled');
					$('.komunikat').fadeIn();
	            }
	    		else if (j.ok == true)
	            {
	                $('#send-form #first_name').attr('value',j.first_name);    
	                $('#send-form #last_name').attr('value',j.last_name);    
	                $('#send-form #email').attr('value',j.email);
	                $('#send-form #street').attr('value',j.street);
	                $('#send-form #city').attr('value',j.city);
	                $('#send-form #post_code').attr('value',j.post_code);
	                $('#send-form #pos_id').attr('value',j.pos_id);    
	                $('#send-form #pos_auth_key').attr('value',j.pos_auth_key);    
	                $('#send-form #session_id').attr('value',j.session_id);    
	                $('#send-form #amount').attr('value',j.amount);    
	                $('#send-form #desc').attr('value',j.desc);
	                $('#send-form #desc2').attr('value',j.desc2); 
	                $('#send-form #client_ip').attr('value',j.client_ip);    
	                $('#send-form #sig').attr('value',j.sig);    
	                $('#send-form #ts').attr('value',j.ts);
	                $('#send-form').trigger('submit'); 
	            }
	        }
	    });
	    
	    return false;	
	}

	$('#wybierz-kategorie').click(function() {
		$.nyroModalManual({
	        ajax: {
				url: '/kategorie.html'
			},
			closeButton: "",
	        width: 440,
	        height: 400
		});
		return false;
	});


	// Soth slider
	
	
	$.sothSlider._init('box_wrapper','left',4000);
	
	$('.tab-search-link').click(function() {
		var rel = $(this).attr('rel');
		$('.box-container').css('display','none');
		$('.tab-search-link').removeClass('tsl-selected');
		$('#'+rel+'-box').css('display','block');
		$('#tab-search-'+rel).addClass('tsl-selected');
		return false;
	});
	
	$('.eph-link').click(function(){
		var rel = $(this).attr('rel');
		var class_div = $('#'+rel).attr('class');
		if(class_div=='edit-profile-header') {
			$('#'+rel).attr('class','edit-profile-header-close');
			$('#'+rel+'_content').slideUp('slow');
			//$('#'+rel+'_content').css('display','none');
		} else {
			$('.edit-profile-header').attr('class','edit-profile-header-close');
			$('.edit-profile-content').slideUp('slow');
			//$('.edit-profile-content').css('display','none');
			
			$('#'+rel).attr('class','edit-profile-header');
			$('#'+rel+'_content').slideDown('slow');
			//$('#'+rel+'_content').css('display','block');
		}
	});
	
	$('.dfc-a').click(function(){
		var rel = $(this).attr('rel');
		var answer = confirm("Czy napewno chcesz aktywować usługę?")
		if (answer){
			window.location = "/aktywuj_usluge.html?typ="+rel;
		}
	});
	
	// -----------------------------------------
});




