$(document).ready(function() {
   
   // system messages
   $("#systemMsg").slideDown("slow");
   setTimeout("$('#systemMsg').slideUp('slow')",3000);
   
   // toggle slide for advanced search
   $("#advSearch").click(function() {
		$(".advHidden").slideToggle("slow");						  
	});
   
   
   // switcher 'magazynowe'
   $("#radioWarehouse").click(function() {
		$("#fType").attr("disabled", "disabled").hide();
		$("#fTypeW").removeAttr("disabled").show();
		$("#fBrand").attr("disabled", "disabled").parent().fadeTo("slow", 0.25);
		$("#fDrive").attr("disabled", "disabled").parent().fadeTo("slow", 0.25);
		$("#fLoadFrom").attr("disabled", "disabled").parent().fadeTo("slow", 0.25);
		$("#fLoadTo").attr("disabled", "disabled");
		$("#fYearFrom").attr("disabled", "disabled").parent().fadeTo("slow", 0.25);
		$("#fYearTo").attr("disabled", "disabled");
		$("#fHeightFrom").attr("disabled", "disabled").parent().fadeTo("slow", 0.25);
		$("#fHeightTo").attr("disabled", "disabled");
		$("#advSearch").hide();
		$(".advHidden #fMHFrom").attr("disabled", "disabled");
		$(".advHidden #fMHTo").attr("disabled", "disabled");
		$(".advHidden:visible").hide();
		$(".advHidden div").each(function(){
			$(this).children("select").attr("disabled", "disabled");
		});
		$(".advHidden label").each(function(){
			$(this).children("input").attr("disabled", "disabled");
		});
	});
   
   // switcher 'widlowe czolowe'
   $("#radioForklifts").click(function() {
		$("#fType").removeAttr("disabled").show();
		$("#fTypeW").attr("disabled", "disabled").hide();
		$("#fBrand").removeAttr("disabled").parent().fadeTo("slow", 1);
		$("#fDrive").removeAttr("disabled").parent().fadeTo("slow", 1);
		$("#fLoadFrom").removeAttr("disabled").parent().fadeTo("slow", 1);
		$("#fLoadTo").removeAttr("disabled");
		$("#fYearFrom").removeAttr("disabled").parent().fadeTo("slow", 1);
		$("#fYearTo").removeAttr("disabled");
		$("#fHeightFrom").removeAttr("disabled").parent().fadeTo("slow", 1);
		$("#fHeightTo").removeAttr("disabled");
		$("#advSearch").fadeTo("slow", 1);
		$(".advHidden #fMHFrom").removeAttr("disabled");
		$(".advHidden #fMHTo").removeAttr("disabled");
		$(".advHidden div").each(function(){
			$(this).children("select").removeAttr("disabled");
		});
		$(".advHidden label").each(function(){
			$(this).children("input").removeAttr("disabled");
		});
	});
   
   // registration
   $("#regForm input:text").each(function() {
		$(this).blur(function() {
			$("#hint:visible").hide();
			$("#hint2:visible").hide();
		});
	});
   $("#regForm input:password").each(function() {
		$(this).blur(function() {
			$("#hint:visible").hide();
			$("#hint2:visible").hide();
		});
	});
   $("#regForm select").each(function() {
		$(this).blur(function() {
			$("#hint:visible").hide();
			$("#hint2:visible").hide();
		});
	});
   $("#fMail").focus(function(){
		$("#hint:hidden").html('<strong>Pole wymagane.</strong><br />Wprowadź adres e-mail.').fadeIn("slow");
	});
   $("#fPass").focus(function(){
		$("#hint:hidden").html('<strong>Pole wymagane.</strong><br />Wymyśl hasło trudne do złamania. Najlepiej kobminację znaków alfanumerycznych').fadeIn("slow");
	});
   $("#fPassConfirm").focus(function() {
		$("#hint:hidden").html('<strong>Pole wymagane.</strong><br />Potwierdź hasło.').fadeIn("slow");
	});
   $("#fUsername").focus(function() {
		$("#hint2:hidden").html('<strong>Pole wymagane.</strong><br />Wprowadź swoje imię i nazwisko lub nazwę firmy.').fadeIn("slow");
	});
   $("#fCounty").focus(function() {
		$("#hint2:hidden").html('<strong>Pole wymagane.</strong><br />Wybierz województwo.').fadeIn("slow");
	});
   $("#fCity").focus(function() {
		$("#hint2:hidden").html('Wprowadź nazwę miejscowości w której mieszkasz.').fadeIn("slow");
	});
   $("#fPost").focus(function() {
		$("#hint2:hidden").html('Wprowadź kod pocztowy w formacie <strong>xx-xxx</strong>.<br />Przykład: <strong>01-123</strong>').fadeIn("slow");
	});
   $("#fAddress").focus(function() {
		$("#hint2:hidden").html('Podaj adres w formacie: ulica, numer domu, numer mieszkania.').fadeIn("slow");
	});
   $("#fTel").focus(function() {
		$("#hint2:hidden").html('Wprowadź numer telefonu stacjonarnego w formacie: <strong>(xx)xxxxxxx</strong>.<br />Przykład: <strong>(22)1234567</strong>').fadeIn("slow");
	});
   $("#fMobile").focus(function() {
		$("#hint2:hidden").html('Wprowadź numer telefonu komórkowego w formacie <strong>xxxxxxxxx</strong>.<br />Przykład: <strong>601123456</strong>').fadeIn("slow");
	});
   
   $("#annType").change(function(){
	    window.location = $("#annType option:selected").val();
	});
   
   $("#annList tr:odd").not('.expired').each(function() {
		$(this).addClass('odd');
	});
	
   $(".nice_table tr:odd").not('.expired').each(function() {
		$(this).addClass('odd');
	});

	$("a.delete").click( function() {
		var agree = confirm('Czy na pewno chcesz usunąć ten obiekt?');
		if(agree){return true;}
		else {return false;}
	});
	
	// adding announcement switcher
	$("#rWarehouse").parent('label').click(function(){
		$("#formForkflifts:visible").hide();
		$("#formWarehouse:hidden").fadeIn("slow");		
	});
	$("#rForklifts").parent('label').click(function(){
		$("#formWarehouse:visible").hide();
		$("#formForkflifts:hidden").fadeIn("slow");		
	});
	
	// select all
	$(".select_all").toggle(function(){
		$('input:checkbox').attr('checked','checked');
		$(this).empty().text('Odznacz wszystkie');
	}, function(){
		$('input:checkbox').removeAttr('checked');
		$(this).text('').append('<strong>Zaznacz wszystkie</strong>');
	});

	// limit chars
	var textArea = $('textarea.limit').attr('value');
	var length = 0;
	if (textArea) {
		length = textArea.length;
	}
	var result = 255 - length;
	$("textarea.limit").siblings('small').children('strong').text('' + result);
	
	$("textarea.limit").keyup(function(event){
		result = 255 - $('textarea.limit').attr('value').length;
		$("textarea.limit").siblings('small').children('strong').text('' + result);
		if(result < 0)
		{
			$(this).css('border', '2px solid red');
			$("textarea.limit").siblings('small').children('strong').text('' + result + " - tekst zostanie obcięty");
		}
		else {
		    $(this).css('border', '1px solid #B2B2B2');
		}
			
	});
	
	// keywords
	if(document.getElementById("fKeywords")) {
		var DOMlimit = $("#fKeywords").siblings('label').children('span');
		var limit = $("#fKeywords").siblings('label').children('span').text();
		
		var keywords = $("#fKeywords").val();
		DOMlimit.text("" + (limit - keywords.split(" ").length + 1));
		limit = limit - keywords.split(" ").length + 1;
	}
	
	$("#fKeywords").bind('keyup', function(key){
		if (key.keyCode == 32 || key.keyCode == 13) {
			limit -= 1;
			if (limit < 0) {limit = 0;}
			$("#fKeywords").siblings('label').children('span').text('' + limit);
		}		
	});
	
	var options = {
            upload_url: "/account/upload",
            flash_url: '/public/swfs/swfupload.swf',
            button_image_url: '/public/images/blankButton.png',
            button_text: "<font face='Arial' size='13pt'>Wybierz plik</font>",
            file_size_limit: '200 KB',
            file_types: '*.jpg;*.gif;*.png',
            file_types_description: 'Pliki graficzne',
            disableDuringUpload: 'input[type="submit"]',
            debug: false
        };
	try {
	    $("#regForm .photos #fPhoto").makeAsyncUploader(options);
    	$("#regForm .photos #fPhoto2").makeAsyncUploader(options);
    	$("#regForm .photos #fPhoto3").makeAsyncUploader(options);
    } catch (e) {}
    
	$("#regForm .photos p a").bind("click", function(){
	    var agree = confirm('Czy na pewno chcesz usunąć ten obiekt?');
	    if (agree) {
	        var url = $(this).attr("href");
	        var parent = $(this).parent();
	        
	        $.ajax({
	          url: url,
	          type: "POST",
	          success: function(msg) {
	            if (msg) {
	                parent.fadeOut("slow");
	            }
	         },
	        
	          error: function() {
	            //console.log("error");
	          }
	        });
	    }
	    return false;
	});
 });