var show_modal = true;

var showLoader = function(){
	if(show_modal)
	{
		dialog = $("#dialog-message").dialog({
			modal: true,
			resizable: false,
			draggable: false,
			minHeight: 10,
			height: 76,
			width: 60,
			open: function(){
				$('.ui-dialog-titlebar').hide();
			},
			close: function(){
				$('.ui-dialog-titlebar').show();
			}
		});
	}
}

var hideLoader = function(){
	if(show_modal)
	{
		dialog.dialog('destroy');
	}
}

$(document).ready(function(){
	//$(document).ajaxStart(showLoader);
	
	//$(document).ajaxStop(hideLoader);
   
	if( $('.anythingSlider').length ){
		$('.anythingSlider').anythingSlider({
                easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
                autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
                delay: 10000,                    // How long between slide transitions in AutoPlay mode
                startStopped: false,            // If autoPlay is on, this can force it to start stopped
                animationTime: 5000,             // How long the slide transition takes
                hashTags: true,                 // Should links change the hashtag in the URL?
                buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
        		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        		startText: "Go",             // Start text
		        stopText: "Stop",               // Stop text
		        navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
            });
	}
   
	if( $('.anythingSliderSmall').length ){
		$('.anythingSliderSmall').anythingSlider({
                easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
                autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
                delay: 3000,                    // How long between slide transitions in AutoPlay mode
                startStopped: false,            // If autoPlay is on, this can force it to start stopped
                animationTime: 600,             // How long the slide transition takes
                hashTags: true,                 // Should links change the hashtag in the URL?
                buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
               pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
               itemsToClone: 8,
               startText: "",             // Start text
		        stopText: "",               // Stop text
		        navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
            });
	}

	if( $('#player').length )
		flowplayer("player", "/js/flowplayer/flowplayer-3.1.5.swf");
	

	$('.print').click(function(){
		window.print();
		
		return false
	});
	
	$('.menu-gora a').mousedown(function(){
		$(this).parent().css({
			"background-image": "url(/images/menu_active_shade.png)",
			"background-repeat": "repeat-x"
		});
	}).mouseup(function(){
		$(this).parent().css({
			"background-image": "url('/images/menu/oddzielnik.png')",
			"background-repeat": "no-repeat",
			"background-position": "right top"
		});
	});
	
	$('input[type="submit"].disable').each(function(){
		var sub = $(this);

		$(this).closest('form').submit(function(){
			sub.attr('disabled', true)
			sub.val('Wysyłanie')
		})
	});
	
	$("#datepicker.dob").datepicker({
			showOn: 'button',
			buttonImage: '/images/calendar.gif',
			buttonImageOnly: true,
            changeMonth: true,
			changeYear: true,
            yearRange: 'c-20:c+20',
            minDate: '-50Y',
            defaultDate: '-25Y',
            maxDate: '+0d'

		});
		
	if( $('.clearInput').length )
		$('.clearInput').clearingInput();
	
	$('#kontakt_link').click(function(){
		show_modal = false;
		jQuery.facebox({ajax: '/index/kontakt'});
		return false
	});
	
	$('#kontakt').live('submit', function(){
		var err = false;
		var email_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		$('#kontakt .error').removeClass('error');
		$('span.error-msg').hide();
		if( $('#kontakt_imie').val().length < 3 ){
			$('#kontakt_imie').addClass('error');
			$('#kontakt-imie-error').html("Niepoprawne dane");
			$('#kontakt-imie-error').show();
			err = true;
		}
		if( $('#kontakt_mail').val().length < 3){
			$('#kontakt_mail').addClass('error');
			$('#kontakt-mail-error').html("Adres jest zbyt krótki");
			$('#kontakt-mail-error').show();
			err = true;
		}
		else
		{
			if(!email_reg.test($('#kontakt_mail').val()) )
			{
				$('#kontakt_mail').addClass('error');
				$('#kontakt-mail-error').html("Niepoprawne adres e-mail");
				$('#kontakt-mail-error').show();
				err = true;
			}
		}
		
		if( $('#kontakt_tresc').val().length < 3 ){
			$('#kontakt_tresc').addClass('error');
			$('#kontakt-tresc-error').html("Wiadomość jest za krótka");
			$('#kontakt-tresc-error').show();
			err = true;
		}
		
		if(err == true)
		{
			return false;
		}
		
		$('#kontakt input[type="submit"]').val('Wysyłanie').attr('disabled', true);
		
		$.ajax({
    		  url: $('#kontakt').attr('action'),
    		  type: "POST",
    		  data: $('#kontakt').serialize(),
    		  success: function(data){
    		  	 if( data == 'ok' )
    		  	 {
				 	alert('Wiadomość została wysłana.')
				 	$(document).trigger('close.facebox');
					show_modal = true;
    		  	 }
				 else
				 {
					alert('Wystąpił błąd podczas wysyłania wiadomości.');
				 }
    	  	  }
    	  });
		  
		  return false
	});
});

function formatText(index, panel) {
  return index + "";
}

