$(document).ready(function() {

	geAlertPopup = function(_tytul, _text, _button, _middle, _id) {
		var aWidth = 500;
		var aHeight = 60;


		//#
		objMaska = '<div class="fullScreenDiv"></div>';
		objAlert = '';
		objAlert += '<div class="mediaContainer">';
			objAlert += '<div class="mediaClose"></div>',
			objAlert += '<div class="bT">',
				objAlert += '<div class="L"></div><div class="M"></div><div class="R"></div>',
			objAlert += '</div>',
			objAlert += '<div class="bM">',
				objAlert += '<div class="L"></div><div class="M"><div class="mediaContent"><!-- text --></div></div><div class="R"></div>',
			objAlert += '</div>',
			objAlert += '<div class="bB">',
				objAlert += '<div class="L"></div><div class="M"></div><div class="R"></div>',
			objAlert += '</div>',
		objAlert += '</div>';


		//#
		if ( _text ) {
			$('body').append( objMaska + objAlert);
			$('.fullScreenDiv').css({ opacity: '0.40' });
			getPopupSize(aWidth, aHeight);

			tmpTxt = '<div class="boxText">';
				if ( _tytul ) {
					tmpTxt += '<div class="Tytul">'+_tytul+'</div>';
				}

				tmpTxt += '<div class="Text">'+_text+'</div>';

				if ( _button ) {
					tmpTxt += '<div class="Buttons"><a class="btnBlue_20 btnAlert"><span class="bL">&nbsp;</span><span class="bM"><span>' + _button + '</span></span><span class="bR">&nbsp;</span></a></div>';
				}
			tmpTxt += '</div>';

			$('.mediaContent').html( tmpTxt );

			tmpHeight = $('.mediaContent .boxText').height() + parseInt($('.mediaContent .boxText').css('padding-top')) + parseInt($('.mediaContent .boxText').css('padding-bottom'));

			if ( aHeight < tmpHeight ) {
				getPopupSize(aWidth, tmpHeight);
			}
		}


		//#
		$(window).resize(function() {
			middleAlert();
		});


		//#
		$(window).scroll( function() {
		  $('.mediaContainer').animate( {marginTop: $(window).scrollTop()+'px' }, {queue: false, duration: 250} );
		});


		//#
		$('.mediaClose').live('click', function() {
			$('.fullScreenDiv, .mediaContainer').animate( {opacity: 'hide'}, {queue: false, duration: 150} );
			$('.mediaContainer').html('');
			$('.fullScreenDiv').promise().done(function() {
				$('.mediaContainer').remove();
				$('.fullScreenDiv').remove();
			});
		});
                
               $('.btnAlert').live('click', function() {
			window.location.href="/pierwsze-kroki/zmiana-systemu/"+_id;
		});


		//#
		function getPopupSize(_width, _height) {
			$('.mediaContainer .bT .M, .mediaContainer .bB .M').width(_width);
			$('.mediaContainer .bM .L, .mediaContainer .bM .R').height(_height);
			$('.mediaContent').width(aWidth).height(_height);
			$('.fullScreenDiv, .mediaContainer').animate( {opacity: 'show'}, {queue: false, duration: 150} );
			middleAlert();
		}

		//#
		function middleAlert() {
			var topPosition		= $(window).scrollTop();
			var centerContent	= ($(window).width() - $('.mediaContainer').width()) / 2;

			if ( _middle == true ) {
				var middleContent =  ( ($(window).height() - $('.mediaContainer').height()) / 2 ) + topPosition;
			} else {
				var middleContent = 245;
			}

			$('.mediaContainer').css({ left: centerContent+'px', top: middleContent+'px' });

			if ( $(window).width() < $('.pageContainer').width() ) {
				$('.fullScreenDiv').css({ width: $(document).width()+'px', height: $(document).height()+'px' });
			} else {
			  $('.fullScreenDiv').css({ width: '100%', height: $(document).height()+'px' });
			}
		}
	}

	//geAlertPopup('Komunikat ze strony', 'Nieprawidlowy format numeru telefonu ...', 'OK', false); //test

});
