function testy_zawodowe() {

	// aktualny status serwisu : off-line lub on-line
	var page_online = (page_path == '.') ? false : true;
	
	// każdorazowe wczytanie podstawowych skryptów
	init_required_scripts();
	
	// wczytanie biuletynu mailowego
	init_field_mail();
    
	// dodanie kodu odpowiedzialnego za statystyki google
	if (page_online == true) init_google_analytics('UA-8664559-1');
	
    // wywołanie funkcji naprawiającej bugi pod ie
    if ($.browser.msie) init_repair_bugs();

}


function init_field_mail() {

    // zapamiętanie domyślnych tekstów pól
	var txt_field_name = 'Twoje imię';
	var txt_field_mail = 'Adres e-mail';
	
	// po kliknięciu w pole - wymazuje tekst
	$('#m_name').bind("focus",  function() {
		var f_value = $(this).val();
		if (f_value == txt_field_name) $(this).val('');
		$(this).css(schema_style.input.on);
	}).bind("blur",  function() {
		var f_value = $(this).val();
		if (f_value == txt_field_name || f_value == '' || f_value == ' ') $(this).val(txt_field_name);
		$(this).css(schema_style.input.off);
	});
	
	$('#m_mail').bind("focus",  function() {
		var f_value = $(this).val();
		if (f_value == txt_field_mail) $(this).val('');
		$(this).css(schema_style.input.on);
	}).bind("blur",  function() {
		var f_value = $(this).val();
		if (f_value == txt_field_mail || f_value == '' || f_value == ' ') $(this).val(txt_field_mail);
		$(this).css(schema_style.input.off);
	});
	
	// biuletyn mailowy
	$('#mailer').bind("submit",  function() {
	
		var f_name = $('#m_name').val();
		var f_mail = $('#m_mail').val();
	
		// sprawdzenie poprawności dla imienia
		if (f_name == '' || f_name == txt_field_name || f_name.length < 2 || f_name.length > 20) {
			alert('Proszę wpisać swoje prawdziwe imię.');
			$('#m_name').focus(); return false;
		}
		
		// sprawdzenie poprawności dla adresu mail
		if (f_mail == '' || f_mail == txt_field_mail || f_mail.length < 2 || f_mail.length > 30) {
			alert('Proszę wpisać swoje prawdziwy adres e-mail.');
			$('#m_mail').focus(); return false;
		} else if (!is_mail(f_mail)) {
			alert('Proszę podać prawidłowy adres e-mail.');
			$('#m_mail').focus(); return false;
		}
		
		// zablokowanie przycisku wysyłania
	    $('#m_send').attr('disabled', 'disabled');
		
		// płynne ukrycie mailera
		$('#mailer').hide(500, save_mail);
		
		// wysłanie zapytania przez ajax i zapisanie maila
		function save_mail() {
		    $('#mailer').html('Zapisywanie zgłoszenia w bazie danych&hellip;').show(500);
			
			$.ajax({
				type: "POST",
				url: page_path+"/ajax/mailer.php",
				cache: true,
				data: "f_name=" + f_name + "&f_mail=" + f_mail,
				success: function(msg) {
				    $('#mailer').hide(500, function () {
					
						$('#mailer').html('<p>Cześć, <strong>' + msg + '</strong>! Proces zapisu <strong>przebiegł pomyślnie</strong>. Dziękujemy za zapisanie się na naszą listę!</p><p><br />Wysłaliśmy do Ciebie mail z prośbą o potwierdzenie zapisu. Jeśli nie otworzysz podanego w mailu linku w ciągu 7 dni, Twoje podanie zostanie odrzucone.</p>');
						$('#mailer').show(700);
					
					});
				}
			});
		}
		
		return false;
	});
}



function init_required_scripts() {

	// jeśli nie istnieje objekt 'read_in' - stworzenie sztucznej kopii
    if (typeof read_in == 'undefined') {
		read_in = new Array;
    }

	// nadanie każdemu elementowi label wartości ID zgodnej z FOR
 	$('label').each(function(i) {
		$(this).attr('id', 'label_'+$(this).attr('for'));
 	});

	// nadanie każdemu linkowi otwieranemu w nowym oknie, odpowiedniej klasy css
 	$('a').each(function(i) {
		if (($(this).attr('target') == '_blank') && !($(this).attr('class'))) {
			$(this).attr('class', 'new_window');
		}
 	});

	
	// skrypt odpowiedzialny za przesłanie wyszukiwania zgodnie z mod_rewrite strony
	$('div#search form').bind("submit",  function() {
	
		// wyszukiwana fraza
		var search_input = $('div#search input:text');
		var search_value = search_input.attr('value');
	
		// sprawdzenie, czy podano poszukiwaną frazę
		if (search_value.length < 3) {
			alert('Proszę wpisać poszukiwaną frazę (co najmniej 3 znaki).');
			search_input.focus();
			return false;
		}
	
	});
	

    if (in_array(read_in, "formularz")) {

		// dodanie interakcji dla pól formularzy - input
		$('input:text, textarea').focus(function() {
			$(this).css(schema_style.input.on);
			var label_id = 'label_'+$(this).attr('id');
			$('#'+label_id).css(schema_style.input.on);
		});

		$('input:text, textarea').blur(function() {
			$(this).css(schema_style.input.off);
			var label_id = 'label_'+$(this).attr('id');
			$('#'+label_id).css(schema_style.input.off);
		});


		// dodanie interakcji dla pól formularzy - button
		$('input:submit, input:button, input:reset').mouseover(function() {
			$(this).css(schema_style.button.on);
		});

		$('input:submit, input:button, input:reset').mouseout(function() {
			$(this).css(schema_style.button.off);
		});
	

		// nadanie pierwszemu elementowi formularza zdarzenia 'focus'
		if (typeof auto_focus == 'undefined') $("div.left input:text:first").focus();
	
    }
	
	
	if (in_array(read_in, "corners:[n]")) {
	
		// dodanie zaokrąglonych rogów dla wskazanych elementów
		$(get_args('corners')).corner("15px");
	
	}
	
	
    if (in_array(read_in, "captcha")) {

		// dodanie interakcji do zabezpieczenia captcha
		$('#img_captcha').attr('title', 'Kliknij, aby wygenerować nowy kod zabezpieczający.').css('cursor', 'hand');
		$('#img_captcha').bind("click", function() {
			this.src = page_path+'/libs/securimage/securimage_show.php?' + Math.random();
			$('#form_captcha').attr('value', '').focus();
		});

    }
	
	
    if (in_array(read_in, "statute")) {

		// dodanie przyjaznych ikonek
		if (!$.browser.msie) {
			$('input:checkbox').checkbox();
		}
	
		// dodanie reguł walidujących akceptację regulaminu
		$("#statute").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element.parent("div").append("<div></div>") ).css('padding-left', '33px');
			},
			rules: {
				form_statute: {
					required: true
				}
			},
			messages: {
				form_statute: {
					required: 'Proszę zapoznać się z Regulaminem i zaakceptować założone warunki.'
				}
			}
		});
	
    }


    if (in_array(read_in, "test")) {

		// zmiana koloru tła po najechaniu na komórkę
		$('div.row_one').bind("mouseover", function() {
			$(this).css(schema_style.test_one.on)
		}).bind("mouseout", function() {
			$(this).css(schema_style.test_one.off)
		});

		$('div.row_two').bind("mouseover", function() {
			$(this).css(schema_style.test_two.on)
		}).bind("mouseout", function() {
			$(this).css(schema_style.test_two.off)
		});
	
		// zaokrąglenie rogów div pytań z wyjątkiem IE - komplikacje z jquery.checkbox
		if (!$.browser.msie) {
			$('div.row_one, div.row_two').corner("15px");
		}

    }

	
    if (in_array(read_in, "tabelka")) {
	
		$('tr').bind("mouseover", function() {
			$(this).css(schema_style.table.on)
		}).bind("mouseout", function() {
			$(this).css(schema_style.table.off)
		});
	
	}
	
	
    // załadowanie tabsów
    if (in_array(read_in, "tabs:[n]")) {

		// sprawdzenie, czy nie ma pustych tabów
		// jeśli zostanie odnaleziony pusty tab - dodanie atrybutu disabled
		var get_tabs_list = $(get_args('tabs') + "> ul").children();
		var empty_tabs = new Array();
	
		get_tabs_list.each(function(i) {
			var tab_list_id = $(this).contents().attr("href");
			    tab_list_id = jQuery.trim($(tab_list_id).text());
				
			if (tab_list_id == '') {
				empty_tabs.push(i+1);
			}
		});
		
		var empty_tabs = empty_tabs.join(',');
		
	    // zainicjowanie biblioteki
		$(get_args('tabs')).tabs( {fxFade: true, fxSpeed: 'fast', disabled: [empty_tabs] } );
	
	}
	

    // jeśli zostanie zmieniona wartość select - przesłanie formularza
    if (in_array(read_in, "select:[n]")) {
	
		$(get_args('select')).bind("change",  function() {
	
			var new_location;
	
			if (typeof file_name != 'undefined') {
			
				if (file_name == 'testy-zawodowe' || file_name == 'psychologia-pracy') {
					new_location = file_name + '/' + $(this).attr('value');
				} else {
					new_location = null;
				}
			
			} else {
				new_location = null;
			}
		
			if (page_path == '.') new_location = null;
		
			if (new_location != null) {
				window.location.href = 'http://testy-zawodowe.pl/' + new_location;
			} else {
				$(this).parents('form').submit();
			}

		});
	}
	
	
    // testy składające się z kilku pytań
    if (in_array(read_in, "multiple:[n]")) {
	
		var multiple_num = get_args('multiple');
	
		// dodanie hover dla tekstu odpowiedzi
		if (multiple_num == 1) {
			$el_obj = 'div.test label';
		} else {
			$el_obj = 'ul.test label';
		}
	
		$($el_obj).bind("mouseover", function() {
			$(this).css(schema_style.text_label.on);
		}).bind("mouseout", function() {
			$(this).css(schema_style.text_label.off);
		});
	
		// zamiana standardowych formularzy na ładniejsze
		if (multiple_num == 1) {
			$('input:checkbox').checkbox();
		} else {
	
			$('ul.test input:radio').checkbox();
	
			// określenie ilości odpowiedzi w pytaniu
			var how_much_in_quest = multiple_num || 3;
	
			// walidacja formularza
			$('input:submit').bind("click", function() {
				var num_required_checked = $("input:radio").length / how_much_in_quest;
				var num_now_checked = $("input:radio:checked").length;
		
				if (num_required_checked != num_now_checked) {
					alert("Proszę odpowiedzieć na wszystkie pytania!");
					return false;
				}
			});
	
		}

    }
	
	
	

	// testy polegające na wystawianiu ocen
    if (in_array(read_in, "rating")) {
	
		// zamiana elementów radio na serduszka :)
		$('form#test input:radio').rating();

		// walidacja formularza
		$('input:submit').bind("click", function() {
			err = false;
			
			$("input:hidden:input_star").each(function() {
				if (!$(this).val()) {
					alert("Proszę odpowiedzieć na wszystkie pytania!");
					err = true;
					return false;
				}
			});
			
			if (err) return false;
		});
	
    }
	
	
    if (in_array(read_in, "img_zoom:[n]")) {
		var img_to_zoom = $(get_args('img_zoom'));
		img_to_zoom.flyout({loadingSrc: page_path+'/images/thumb-loading.gif', loader: 'loader'});
	}


    if (in_array(read_in, "kontakt")) {

	// stworzenie nowej metody dla validatora
	$.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            if (regexp.constructor != RegExp)
                regexp = new RegExp(regexp);
            else if (regexp.global)
                regexp.lastIndex = 0;
            return this.optional(element) || regexp.test(value);
        },
        "Proszę wprowadzić pełne imię i nazwisko."
	);
	
		// dodanie reguł walidujących formularz kontaktowy
		$("#zostaw_wiadomosc").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element.parent("div").append("<div></div>") );
			},
			rules: {
				form_nazwisko: {
					required: true,
					regex: /^[a-zA-Z'.\s]{2,}\x20[a-zA-Z'.\s]{2,}$/
				},
				form_temat: {
					minlength: 5,
					maxlength: 255
				},
				form_tresc: {
					required: true,
					minlength: 30,
					maxlength: 12255
				},
				form_mail: {
					email: true
				},
				form_captcha: {
					required: true,
					minlength: 4,
					maxlength: 6,
					remote: page_path+"/ajax/captcha.php"
				}
			},
			messages: {
				form_captcha: {
					remote: 'Proszę wprowadzić prawidłowy kod zabezpieczający.'
				}
			}
		});
    }


    if (in_array(read_in, "test_podsumowanie")) {

		// dodanie reguł walidujących formularz kontaktowy
		$("#test").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element.parent("td").append("<div></div>") );
			},
			rules: {
				form_nazwisko: {
					required: true,
					minlength: 2,
					maxlength: 22
				},
				form_mail: {
					email: true
				},
				form_sms: {
					required: true,
					remote: page_path+"/ajax/sms.php"
				}
			},
			messages: {
				form_sms: {
					remote: 'Proszę wprowadzić prawidłowy kod dostępu.'
				}
			}
		});
		
		// dodanie colorbox dla reklamacji
		$("#reclamation").colorbox({width:"540px", height:"500px", iframe:true, title:"Formularz reklamacyjny dla usług SMS typu premium"}); return false;
		
    }
	

}


function init_google_analytics(GAID) {
  var head = document.getElementsByTagName('head')[0];
  var scr = document.createElement('script');
  
  scr.type = 'text/javascript';
  scr.src = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'ga.js';
  var once = 0;
  var timer = setInterval(runMe, 2000);
  
  function runMe() { 
    if (scr.readyState == 'loading' || once) return;
    clearInterval(timer);
    once++;
    var pageTracker = _gat._getTracker(GAID);
	pageTracker._trackPageview();
  }
  
  scr['onload'] = runMe;
  scr['onreadystatechange'] = runMe;
  head.appendChild(scr);
}




/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

function in_array(array, value) {
  if (array && value) {
  for (var i=0; i<array.length; ++i) {
  
    if (value.search('[n]') != -1) {
	  var read = value.slice(0, value.length-4);
	  if (array.join(',').search(read) != -1)
	    return true;
    };
	
    if (array[i] == value)
      return true;
    }
	
  return false;
  }
}

function get_args(readin) {
  var readin_string = read_in.join(',');
  var readin_kotwica = readin_string.search(readin);
  if (readin_kotwica != -1) {
    var readin_start = readin_string.slice(readin_kotwica+readin.length+2);
    var readin_end = readin_start.indexOf(']');
    return readin_start.slice(0, readin_end);
  }
}

function is_int(input){
  return typeof(input)=='number'&&parseInt(input)==input;
}


function is_mail(mail) {

	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(mail);

}


// po wczytaniu zawartości serwisu, załadowanie kodu javascript
$(document).ready(testy_zawodowe);