$(document).ready(function(){
	$(document).ajaxStart(function(){
		dialog = $("#dialog-message").dialog({
			modal: true,
			resizable: false,
			draggable: false,
			minHeight: 10,
			open: function(){
				$('.ui-dialog-titlebar').hide();
			},
			close: function(){
				$('.ui-dialog-titlebar').show();
			}
		});
	})
	$(document).ajaxStop(function(){
		dialog.dialog('destroy');
	});
	
	if( $('.anythingSlider').length ){
		$('.anythingSlider').anythingSlider({
                easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
                autoPlay: false,                 // 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
        		startText: "Go",             // Start text
		        stopText: "Stop",               // Stop text
		        navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
            });
            
            $("#slide-jump").click(function(){
                $('.anythingSlider').anythingSlider(6);
            });
	}

	if( $('#player').length )
		flowplayer("player", "/js/flowplayer/flowplayer-3.1.5.swf");
	
	$('#zakladka-rejestracja').click(function(){
		$('#logowanie').css('display', 'none');
		$('#rejestracja').css('display', 'block');
		
		return false
	});
	
	$('#zakladka-logowanie-2').click(function(){
		$('#logowanie').css('display', 'block');
		$('#rejestracja').css('display', 'none');
		
		return false
	});
	
	$('.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(){
		$.fn.colorbox({href:"/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');
		if( $('#kontakt_imie').val().length < 3 ){
			$('#kontakt_imie').addClass('error');
			err = true;
		}
		if( $('#kontakt_mail').val().length < 3 || !email_reg.test($('#kontakt_mail').val()) ){
			$('#kontakt_mail').addClass('error');
			err = true;
		}
		if( $('#kontakt_tresc').val().length < 3 ){
			$('#kontakt_tresc').addClass('error');
			err = true;
		}
		
		if( err ){
			alert('Proszę uzupełnić wszystkie pola.');
			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('Wysłano!')
				else
					alert('Wystąpił błąd podczas wysyłania wiadomości.');
				
				$.fn.colorbox.close()
    	  	  }
    	  });
		  
		  return false
	});
	
	setTimeout(randomLogo, 1000);
});

function formatText(index, panel) {
  return index + "";
}

function randomLogo(){
	var count = $('.dolaczyli-do-nas-firmy a').length;
	var rand;
	do{
		rand = Math.floor(Math.random()*count);
	}while( $('.dolaczyli-do-nas-firmy a:eq('+rand+')').attr('id') == 'l0' );
	var notids = '';
	$('.dolaczyli-do-nas-firmy a').each(function(){
		notids += ':'+$(this).find('img').attr('id').substr(1);
	});
	$.ajax({
		data: 'ids='+notids.substr(1),
		dataType: 'html',
		global: false,
		success: function(data){
			$('.dolaczyli-do-nas-firmy a:eq('+rand+')').animate({
				'opacity': 0
			},{
				duration: 1000,
				complete: function(){
					if (data) {
						$(this).attr('href', $(data).attr('href'));
						$(this).find("img").replaceWith($(data).find("img"))
					}else $(this).remove();
					
					$(this).find("img").load(function(){
						$(this).parent().animate({
							'opacity': 1
						}, 1000)
					});
					
					setTimeout(randomLogo, 1000);
				}
			})
		},
		type: 'POST',
		url: '/getRandomLogo'
	});
}
