$(document).ready(function() {
	
	var install_path = "";
	var ajax_path = install_path + "/tools/ajax";
	var par_url = window.location.search;

	if ( $('#galeria').length ) {	
		$('#galeria a[rel="lightbox"]').lightBox({
			imageLoading: '/simg/lightbox/lightbox-ico-loading.gif',
			imageBtnClose: '/simg/lightbox/lightbox-btn-close.gif',
			imageBtnPrev: '/simg/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/simg/lightbox/lightbox-btn-next.gif',
			txtImage: 'Zdjęcie',
			txtOf: 'z'
	   });
	}
	
	var jTmp;
	$('a.toggle-block').each(function() {
		if ($($(this).attr('href')).length) {
			$($(this).attr('href')).css('display', 'none');
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideToggle(200);
				return false;
			});
		}
	});
	$('a.show-block').each(function() {
		if ($($(this).attr('href')).length) {
			$($(this).attr('href')).css('display', 'none');
			$('a.hide-block[href="' + $(this).attr('href') + '"]').css('display', 'none');
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideDown(200);
				$('a.hide-block[href="' + $(this).attr('href') + '"]').css('display', 'block');
				$(this).css('display', 'none');
				if ($(this).attr('href') == '#advanced-search') {
					jTmp = $(this).closest('form').find('.simple-search-submit').remove();
				}
				return false;
			});
		}
	});
	$('a.hide-block').each(function() {
		if ($($(this).attr('href')).length) {
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideUp(200);
				$('a.show-block[href="' + $(this).attr('href') + '"]').css('display', 'block');
				$(this).css('display', 'none');
				if ($(this).attr('href') == '#advanced-search') {
					jTmp = $(this).closest('form').find('.more:first').before(jTmp);
				}
				return false;
			});
		}
	});

	/*
	 * ZAKLADKI
	 */
	$('.tabs').tabs();
	if ( adv ) {
		$('.tabs').tabs('option', 'selected', 1);
	}
	$('.ui-tabs-panel').each(function() {
		$('a[href="#' + $(this).attr('id') + '"]').each(function() {
			if ($(this).closest('.ui-tabs').length) {
				return;
			}
			$(this).click(function() {
				$('.ui-tabs a[href="' + $(this).attr('href') + '"]').click();
				return true;
			});
		});
	});
	
	
	
	var form_error_message = '<p class="blad"><strong>Uwaga!</strong></p><p class="blad">Formularz nie został wysłany ponieważ nie wszystkie wymagane pola zostały wypełnione.<br />Proszę uzupełnić poniższe pola i wysłać formularz ponownie!</p>';
	
	function formError(e, validator, mv, form_error_name) {
		var errors = validator.numberOfInvalids();
		if (errors) {
			var e = '<div class="codeblock"><ol>';
			$.each(validator.invalidElements(), function(i, j){
				var o = $(j).parent();
				e = e + '<li>' + $(o).attr('title') + '</li>';
			});
			e = e + '</ol></div>';
			$(form_error_name).html(form_error_message + e);
			$(form_error_name).show();
		} else {
			$(form_error_name).hide();
		}
	}
	
	// recommend
	var options = { 
		        //target:        '#output1',   // target element(s) to be updated with server response 
				beforeSubmit: function() {
					return $('#polec').validate().form();
				},
		        success:       showResponse,  // post-submit callback 
		 
		        // other available options: 
		        //url:       url         // override for form's 'action' attribute 
		        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
		        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		        clearForm: true,        // clear all form fields after successful submit 
		        resetForm: true        // reset the form after successful submit 
		 
		        // $.ajax options can be used here too, for example: 
		        //timeout:   3000 
    }; 
	if ( $('#polec').length ) {	 
    	// bind form using 'ajaxForm' 
    	$('#polec').ajaxForm(options);

    	jQuery.validator.messages.required = "";
        $("#polec").validate({
        	focusInvalid: false,

    		invalidHandler: function(e, validator) {
        		formError(e, validator, 'polec', '#error');
    		},

    		submitHandler: function(form) {
    			$("#error").hide();
    			$('#alr').addClass('ajax-loading');
    		}    	
    	});
	}  

    function showResponse(responseText, statusText)  { 
     
        //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        //    '\n\nThe output div should have already been updated with the responseText.');
        $('#alr').removeClass('ajax-loading');
        if ( responseText ) {
        	alert("Wiadomość została wysłana!"); 
        	$('#polec').stop(true, true).slideToggle(200);
        }
    }     

    //info contact form
    if ( $('#f-kontakt').length ) {
		jQuery.validator.messages.required = "";
	    $("#f-kontakt").validate({
	    	focusInvalid: false,
	
			invalidHandler: function(e, validator) {
				formError(e, validator, 'bf', '#error');
			},
			submitHandler: function(form) {
				form.submit();
			}    	
		});
    }
    
    //info access form
    if ( $('#f-zgloszenie').length ) {
		jQuery.validator.messages.required = "";
	    $("#f-zgloszenie").validate({
	    	focusInvalid: false,
	
			invalidHandler: function(e, validator) {
				formError(e, validator, 'bf', '#error');
			},
			submitHandler: function(form) {
				form.submit();
			}    	
		});
    }
    
    if ( $('#ms').length ) {
    	$('#ms').submit(function() {
    		if ( $('#mo').val() == "zglos" ) {
    			location.href = install_path + "/strefa-pracodawcow/zglos/";
    		}
    		if ( $('#mo').val() == "wyszukaj" ) {
    			location.href = install_path + "/strefa-pracodawcow/baza-cv/";
    		}
    		if ( $('#mo').val() == "zaloguj" ) {
    			location.href = install_path + "/strefa-pracodawcow/";
    		}
    		return false;
    	});
    }
    
    if ( $('#ms2').length ) {
    	$('#ms2').submit(function() {
    		if ( $('#mo2').val() == "zloz" ) {
    			location.href = install_path + "/baza-cv/nowe-cv/";
    		}
    		if ( $('#mo2').val() == "zaloguj" ) {
    			location.href = install_path + "/baza-cv/";
    		}
    		return false;
    	});
    }
    
    //print
	$("#print").click(function() {
		window.print();
	});
	
	//back
	$("#back").click(function() {
		window.history.back();
	});
	$("#back2").click(function() {
		window.history.back();
	});

	$("#zaznacz").click(function() {
		$(".wybor").attr('checked', true);

	});
        
	$("#odznacz").click(function() {
		$(".wybor").attr('checked', false);

	});
        
	$("#wybierz").click(function() {
            var wybor = "";
            $(".wybor:checked").each(function () {
                wybor += (wybor?',':'') + $(this).val();
            });
            if ( wybor == "" ) {
                alert("Proszę wybrać conajmniej jeden profil CV");
            } else {
                var answer = confirm("Wysłać zapytanie o wybrane profile CV do PUP Iława?");
                if (answer) {
                    //location.href = install_path + "/strefa-pracodawcow/baza-cv/zapytanie/" + wybor + "/";
                    //var url = (window.location.href.replace(/\/zapytanie\//i,"")).replace(/CV[0-9]*(,|)/g,"");
                    var url = window.location.href.replace(/baza-cv\//i,"baza-cv/zapytanie/" + wybor + "/");
                    location.href = url;
                }
            }
	});
        
	if ( $('#potwierdzenie').length ) {
		$('#potwierdzenie-ch').click(function() {

			if ( $("#potwierdzenie-ch").attr("checked") ) {
				$('#potwierdzenie').val(1);
			} else {
				$('#potwierdzenie').val('');
			}
		})
	};
	
});

function clearDefault(element) {
	if ( element.defaultValue == element.value ) {
		element.value = "";
	}
}

function setDefault(element) {
	if ( element.value == "" ) {
		element.value = element.defaultValue;
	}
}
