jQuery(document).ready(function() {

function sr_ABBR() {
	$('#CONTENT abbr').each( function() {
		var sr_title = $(this).attr( 'title' );
		sr_title_index = sr_title.indexOf(' - ');
		$(this).append('<span class="abbr-box"><span class="abbr-top"></span><span class="abbr-title">' + sr_title.substr(0, sr_title_index) + ' -</span> ' + sr_title.substr(sr_title_index+3) + '<span class="abbr-bottom"></span></span>');
//		$(this).append('<span class="abbr-box"><span class="abbr-top"></span><span class="abbr-title">' + $(this).html() + ' -</span> ' + $(this).attr( 'title' ) + '<span class="abbr-bottom"></span></span>');
		$(this).attr( 'title', '' );
	});

	if ( !($.browser.msie && ( $.browser.version.slice(0,1)<'9' ) ) ) {
		$('#CONTENT abbr').hover( function() {
			$(this).find('.abbr-box').stop().fadeTo(300,'1');
		}, function() {
			$(this).find('.abbr-box').stop().fadeTo(600,'0', function() {
				$(this).hide();
			});
		});
	}
	else {
		$('#CONTENT abbr').hover( function() {
			$(this).find('.abbr-box').show();
		}, function() {
			$(this).find('.abbr-box').hide();
		});
	}
}
sr_ABBR();

if ( $('#CONTENT').hasClass('PAGE')) {
		$TABMENU_href_index = window.location.href.indexOf('#');

		if ( $TABMENU_href_index > 0 ) {
			$TABMENU_href = window.location.href;
			if ( $TABMENU_href.indexOf('#og%C3%B3lnie')=='-1' ) {
				$TABMENU_href = window.location.href.substr(0,$TABMENU_href_index) + '/' + window.location.href.substr($TABMENU_href_index+1);

				$('.ajax > section:not(.image),.ajax > a').fadeTo(0, '0');
				$('#TABMENU li').removeClass('current_page_item');
				$('#TABMENU a').each( function() {
					if ( $(this).attr('href') == $TABMENU_href )
					$(this).parent().addClass('current_page_item');
				});

				$.ajax({
					url: $TABMENU_href,
				  success: function(data){
						$('.ajax > .text').html( $('.ajax > .text',data).html() );
						$('.ajax > .edit').attr('href',( $('.ajax > .edit',data).attr('href') ));
						$('.ajax > .links').html( $('.ajax > .links',data).html() );
						$('.ajax > section:not(.image),.ajax > a').fadeTo(500, '1');
						$('#TABMENU').addClass('active');
						sr_ABBR();
					}
				});
			}
		}
	}

	$('#TABMENU a').each( function() {
		$(this).attr('title','');
	})
	$('#TABMENU a').click( function(e) {
		if ( !$('#TABMENU').hasClass('active') || ( $(".ajax #TABMENU li.current_page_item").index('.ajax #TABMENU li') == $(this).parent().index('.ajax #TABMENU li') ) ) {
			e.preventDefault();
			return;
		}

		$('.ajax section:not(.image)').fadeTo(500, '0');
		$('#TABMENU li').removeClass('current_page_item');
		$(this).parent().addClass('current_page_item');

		$('#TABMENU').removeClass('active');
		var sr_url = $(this).attr('href');
		if ( $(this).find('.middle').html()=='Ogólnie' ) sr_url = '#ogólnie';
		else sr_url = '#' + sr_url.substr(sr_url.lastIndexOf('/')+1);

		$.ajax({
			url: $(this).attr('href'),
		  success: function(data){
				$('.ajax > section:not(.image),.ajax > a').fadeTo(100, '0', function() {
					$('.ajax > .text').html( $('.ajax > .text',data).html() );
					$('.ajax > .edit').attr('href',( $('.ajax > .edit',data).attr('href') ));
					$('.ajax > .links').html( $('.ajax > .links',data).html() );
					$('.ajax > section:not(.image),.ajax > a').fadeTo(500, '1');
					$('#TABMENU').addClass('active');
					window.location = sr_url;
					sr_ABBR();
				});
			}
		});
		e.preventDefault();
	})

	$('#INFO .button').toggle( function() {
		$('#INFO aside').slideUp(1000);
		$('#INFO .button').removeClass('hide').addClass('show');
	}, function() {
		$('#INFO aside').slideDown(1000);
		$('#INFO .button').removeClass('show').addClass('hide');
	});

	var TABMENU_zindex = 100;
	$('#CONTENT #TABMENU li').each( function() {
		$(this).css('z-index', TABMENU_zindex--);
	});

//	$('#CONTENT .LEFT > #SUBMENU > ul > li > ul > li > a').click( function() {
//		$temp = $(this).addClass('close').next();
//		if ( $temp.is(':visible') ) $temp.slideUp(1500).prev().removeClass('close');
//		else {
//			$('#CONTENT .LEFT > #SUBMENU > ul > li > ul > li > ul:visible').each( function() {
//				$(this).slideUp(1500).prev().removeClass('close');
//			});
//			$temp.slideDown(1500);
//		}
//
//		return false;
//	});

	$(window).scroll( function() {
		if ( $('#SEND-EMAIL').is(':visible') ) $('#SEND-EMAIL').css('top', $(window).scrollTop()+($(window).height()-$('#SEND-EMAIL form').height())/3 );
		if ( $('#SEND-APPLICATION').is(':visible') ) $('#SEND-APPLICATION').css('top', $(window).scrollTop()+($(window).height()-$('#SEND-APPLICATION > div').height())/3 );
	})

	$($('#send-info')).click(function() {
		$('#SEND-EMAIL-overlay').fadeTo(1000, '0.8');
		$('#SEND-EMAIL').delay(500).css('top',$(window).scrollTop()-500).show().animate({'top': ($(window).scrollTop()+($(window).height()-$('#SEND-EMAIL form').height())/3)}, 1000);
	})
	$($('#SEND-EMAIL .close')).click(function() {
		$('#SEND-EMAIL').animate({'top': ($(window).scrollTop()+$(window).height())}, 700, function() {$(this).hide();});
		$('#SEND-EMAIL-overlay').fadeTo(1000, '0',function(){$(this).hide();});
	})

	$('#SEND-EMAIL form').append('<input type="text" name="js" value="1" />');

	$('#SEND-EMAIL input[type=text],#SEND-EMAIL textarea').each( function() {
		$(this).attr( 'special', $(this).attr('title') );
		if ( !$(this).val() ) $(this).val( $(this).attr('special') );
	});

	$('#SEND-EMAIL input[type=text],#SEND-EMAIL textarea').focus( function() {
		if ( $(this).val()== $(this).attr('special') ) $(this).attr('value','');
	});
	$('#SEND-EMAIL input[type=text],#SEND-EMAIL textarea').blur( function() {
		if ( !$(this).val() ) $(this).attr('value',$(this).attr('special'));
	});

	$('#SEND-EMAIL input[type=text],#SEND-EMAIL textarea').click( function() {
		$('#SEND-EMAIL #perror').html('');
		$("#SEND-EMAIL .validate").removeClass('validate');
	});

	$('#SEND-EMAIL form').submit(function() {
		if ( !$("#SEND-EMAIL input[name=psurname]").val()||$("#SEND-EMAIL input[name=psurname]").val()==$("#SEND-EMAIL input[name=psurname]").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#perror').html('<p class="error-r">Please enter your firstname and surname.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#perror').html('<p class="error-r">Bitte geben Sie Ihren Namen.</p>');
			else $(this).find('#perror').html('<p class="error-r">Proszę podać imię i nazwisko.</p>');
			$("#SEND-EMAIL input[name=psurname]").addClass('validate');
			return false;
		}
		var email = $("#SEND-EMAIL input[name=pemail]").val();
		if ( email==$("#SEND-EMAIL input[name=pemail]").attr('special') || ( email.indexOf( "@" ) < 1 ) || ( email.indexOf( "@" ) != email.lastIndexOf( "@" ) ) || ( email.indexOf( ".@" ) > -1 ) || ( email.indexOf( "@." ) > -1 ) || ( email.indexOf( ".." ) > -1 ) || ( email.indexOf( "." ) ==0 ) || ( email.lastIndexOf( "." ) > email.length - 2 ) || ( email.lastIndexOf( "@" ) + 1 >= email.lastIndexOf( "." ) ) ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#perror').html('<p class="error-r">Please enter a valid e-mail address.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#perror').html('<p class="error-r">Bitte geben Sie eine gültige E-Mail.</p>');
			else $(this).find('#perror').html('<p class="error-r">Proszę podać prawidłowy adres e-mail.</p>');
			$("#SEND-EMAIL input[name=pemail]").addClass('validate');
			return false;
		}
		if ( !$("#SEND-EMAIL textarea").val() || $("#SEND-EMAIL textarea").val()==$("#SEND-EMAIL textarea").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#perror').html('<p class="error-r">Please add your message.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#perror').html('<p class="error-r">Bitte fügen Sie Ihre Nachricht.</p>');
			else $(this).find('#perror').html('<p class="error-r">Proszę uzupełnić treść wiadomości.</p>');
			$("#SEND-EMAIL textarea").addClass('validate');
			return false;
		}

		$('#SEND-EMAIL input').each(function() {
			if ( $(this).val()==$(this).attr('special') ) $(this).attr('value','');
		})
		$('#SEND-EMAIL textarea').each(function() {
			if ( $(this).val()==$(this).attr('special') ) $(this).attr('value','');
		})

		if ( $('html').attr('lang') == 'en-US' ) $('#SEND-EMAIL form #perror').html('<p class="error-b">Sending...</p>');
		else if ( $('html').attr('lang') == 'de-DE' ) $('#SEND-EMAIL form #perror').html('<p class="error-b">Weiter senden...</p>');
		else $('#SEND-EMAIL form #perror').html('<p class="error-b">Trwa wysyłanie...</p>');

		$.ajax({
			type: "POST",
			url: $(location).attr('href'),
			data: $(this).serialize(),
			dataType: 'html',
			success: function(data) {$('#SEND-EMAIL form #perror').html( $('#perror',data).html() );},
			error: function() {
				if ( $('html').attr('lang') == 'en-US' ) $('#SEND-EMAIL form #perror').html('<p class="error-r">You can not send messages.</p>');
				else if ( $('html').attr('lang') == 'de-DE' ) $('#SEND-EMAIL form #perror').html('<p class="error-r">Sie können keine Nachrichten senden.</p>');
				else $('#SEND-EMAIL form #perror').html('<p class="error-r">Nie można wysłać wiadomości.</p>');
			}
		});
		return false;
	});


if ( $('#CONTENT').hasClass('SIMPLE') ) {
	$($('#button-send-cv,.button-send-cv')).click(function(e) {
		$('#SEND-EMAIL-overlay').fadeTo(1000, '0.8');
		$('#SEND-APPLICATION').delay(500).css('top',$(window).scrollTop()-900).show().animate({'top': ($(window).scrollTop()+($(window).height()-$('#SEND-APPLICATION > div').height())/3)}, 1200);
		e.preventDefault();
	})
	$($('#SEND-APPLICATION .close')).click(function(e) {
		$('#SEND-APPLICATION').animate({'top': ($(window).scrollTop()+$(window).height())}, 700, function() {$(this).hide();});
		$('#SEND-EMAIL-overlay').fadeTo(1000, '0',function(){$(this).hide();});
		e.preventDefault();
	})
}

if ( $('body').hasClass('iframe-career') ) {
	$('#APPLICATION-FORM form').append('<input type="text" name="js" value="1" />');

	$('#APPLICATION-FORM input[type=text],#APPLICATION-FORM textarea').each( function() {
		$(this).attr( 'special', $(this).attr('title') );
		if ( !$(this).val() ) $(this).val( $(this).attr('special') );
	});

	$('#APPLICATION-FORM input[type=text],#APPLICATION-FORM textarea').focus( function() {
		if ( $(this).val()== $(this).attr('special') ) $(this).attr('value','');
	});
	$('#APPLICATION-FORM input[type=text],#APPLICATION-FORM textarea').blur( function() {
		if ( !$(this).val() ) $(this).attr('value',$(this).attr('special'));
	});

	$('#APPLICATION-FORM input[type=text],#APPLICATION-FORM input[type=checkbox],#APPLICATION-FORM textarea,#APPLICATION-FORM .files,#APPLICATION-FORM .delete').click( function() {
		$('#APPLICATION-FORM #error').html('');
		$("#APPLICATION-FORM .validate").removeClass('validate');
	});

	$('#APPLICATION-FORM .files').append('<div class="file"><span class="box"></span><div>załącz plik*<input id="file" name="file[]" type="file" /></div><span class="delete"></span><br class="clear" /></div>');
	function sr_files() {
		$('#APPLICATION-FORM .file input[type=file]').change( function() {
			var APPLICATION_box = $(this).parent().parent().find('.box');
			if (!APPLICATION_box.html())
				if ( $('#APPLICATION-FORM .files .file').size()<2 ) {
//				if ( $(window).height() > $('#APPLICATION-FORM form').height() + 70 ) {
					$('#APPLICATION-FORM .files').append('<div class="file"><span class="box"></span><div>załącz plik*<input id="file" name="file[]" type="file" /></div><span class="delete"></span><br class="clear" /></div>');
//					$('#APPLICATION-FORM').animate({'top': ($(window).scrollTop()+($(window).height()-$('#APPLICATION-FORM form').height())/3)}, 500);
					sr_files();
				}
			$(APPLICATION_box).html($(this).val()).parent().find('.delete').show();
		});
		$('#APPLICATION-FORM .file .delete').click( function() {
			if ( $('#APPLICATION-FORM .files .delete:visible').size()==2 ) {
//			if ( $(window).height() > $('#APPLICATION-FORM form').height() + 70 ) {
				$('#APPLICATION-FORM .files').append('<div class="file"><span class="box"></span><div>załącz plik*<input id="file" name="file[]" type="file" /></div><span class="delete"></span><br class="clear" /></div>');
				sr_files();
			}
//			$('#APPLICATION-FORM').animate({'top': ($(window).scrollTop()+($(window).height()-$('#APPLICATION-FORM form').height())/3)}, 500);
			$(this).parent().remove();
		});
	}
	sr_files();
	$('#APPLICATION-FORM .file,#APPLICATION-FORM .delete').bind('selectstart.disableTextSelect', function() {
		return false;
	});
	$('#APPLICATION-FORM .file,#APPLICATION-FORM .delete').bind('mousedown.disableTextSelect', function() {
		return false;
	});

	$('#APPLICATION-FORM input[name=phone]').keypress( function(e) {
		if ( ( ( e.which < 48 ) || ( e.which > 57 ) ) && ( e.which != 8 ) && ( e.which != 13 ) ) e.preventDefault();
	});

	$('#APPLICATION-FORM form').submit(function() {
		if ( !$("#APPLICATION-FORM input[name=surname]").val()||$("#APPLICATION-FORM input[name=surname]").val()==$("#APPLICATION-FORM input[name=surname]").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please enter your firstname and surname.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte geben Sie Ihren Namen.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę podać imię i nazwisko.</p>');
			$("#APPLICATION-FORM input[name=surname]").addClass('validate');
			return false;
		}
		var email = $("#APPLICATION-FORM input[name=email]").val();
		if ( email==$("#APPLICATION-FORM input[name=email]").attr('special') || ( email.indexOf( "@" ) < 1 ) || ( email.indexOf( "@" ) != email.lastIndexOf( "@" ) ) || ( email.indexOf( ".@" ) > -1 ) || ( email.indexOf( "@." ) > -1 ) || ( email.indexOf( ".." ) > -1 ) || ( email.indexOf( "." ) ==0 ) || ( email.lastIndexOf( "." ) > email.length - 2 ) || ( email.lastIndexOf( "@" ) + 1 >= email.lastIndexOf( "." ) ) ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please enter a valid e-mail address.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte geben Sie eine gültige E-Mail.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę podać prawidłowy adres e-mail.</p>');
			$("#APPLICATION-FORM input[name=email]").addClass('validate');
			return false;
		}
		if ( !$("#APPLICATION-FORM input[name=phone]").val() || $("#APPLICATION-FORM input[name=phone]").val()==$("#APPLICATION-FORM input[name=phone]").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please enter a valid phone number.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte geben Sie eine gültige Telefonnummer.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę podać prawidłowy numer telefonu.</p>');
			$("#APPLICATION-FORM input[name=phone]").addClass('validate');
			return false;
		}
		if ( !$("#APPLICATION-FORM input[name=topic]").val()||$("#APPLICATION-FORM input[name=topic]").val()==$("#APPLICATION-FORM input[name=topic]").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please fill in the message subject.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte füllen Sie in den Betreff der Nachricht.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę uzupełnić temat wiadomości.</p>');
			$("#APPLICATION-FORM input[name=topic]").addClass('validate');
			return false;
		}
		if ( !$("#APPLICATION-FORM textarea").val() || $("#APPLICATION-FORM textarea").val()==$("#APPLICATION-FORM textarea").attr('special') ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please add your message.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte fügen Sie Ihre Nachricht.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę uzupełnić treść wiadomości.</p>');
			$("#APPLICATION-FORM textarea").addClass('validate');
			return false;
		}
		if ( !$("#APPLICATION-FORM input[type=file]").val() ) {
			if ( $('html').attr('lang') == 'en-US' ) $(this).find('#error').html('<p class="error-r">Please attach your CV.</p>');
			else if ( $('html').attr('lang') == 'de-DE' ) $(this).find('#error').html('<p class="error-r">Bitte fügen Sie Ihren Lebenslauf.</p>');
			else $(this).find('#error').html('<p class="error-r">Proszę załączyć swoje CV.</p>');
			$("#APPLICATION-FORM .file span").addClass('validate');
			return false;
		}

		$('#APPLICATION-FORM input').each(function() {
			if ( $(this).val()==$(this).attr('special') ) $(this).attr('value','');
		})
		$('#APPLICATION-FORM textarea').each(function() {
			if ( $(this).val()==$(this).attr('special') ) $(this).attr('value','');
		})

		if ( $('html').attr('lang') == 'en-US' ) $('#APPLICATION-FORM #error').html('<p class="error-b">Sending the application...</p>');
		else if ( $('html').attr('lang') == 'de-DE' ) $('#APPLICATION-FORM #error').html('<p class="error-b">Das Senden der Anwendung...</p>');
		else $('#APPLICATION-FORM #error').html('<p class="error-b">Trwa wysyłanie aplikacji...</p>');
	});
}

});
