$(document).ready( function() {

	makeNewsletter.construct(); 
	
	$("img.sub").click( function() {
			var el = $("#sub"+$(this).attr("rel"));
			if(el.css("display") == 'none')
			{
				el.css("display","");
				$(this).attr("src","images/navig/drz_minus.gif");
			}
			else
			{
				el.css("display","none");
				$(this).attr("src","images/navig/drz_plus.gif");
			}
			return false;
	});
	
	$("a.sub").click( function() {
			var el = $("#sub"+$(this).prev("img.sub").attr("rel"));
			if(el.css("display") == 'none')
			{
				el.css("display","");
				$(this).prev("img.sub").attr("src","images/navig/drz_minus.gif");
			}
			else
			{
				el.css("display","none");
				$(this).prev("img.sub").attr("src","images/navig/drz_plus.gif");
			}
			return false;
	});
	
	$("#contact_submit").click( function () {
		$(".form-sending-info p").html('');
		$('.form-sending-info ul li').remove();
		var flag = 0;
		if($('#form_name').val()=='')
		{
			flag = 1;
			$(".form-sending-info ul").append('<li>Podaj imię i nazwisko</li>');
		}
		if(!validate( $("#form_email").val() ))
		{
			flag = 1;
			$(".form-sending-info ul").append('<li>Nieprawidłowy e-mail</li>');
			
		}
		if($('#form_content').val()=='')
		{
			flag = 1;
			$(".form-sending-info ul").append('<li>Podaj treść wiadomości</li>');
		}
		if($('#form_dzial').val()==0)
		{
			flag = 1;
			$(".form-sending-info ul").append('<li>Wybierz dział kontaktowy</li>');
		}
		if(flag == 0)
			getContactResponse();	
		else
		{
			$(".form-sending-info p").html('<strong>Niestety, w formularzu wystąpiły błędy:</strong>');
		}		
		return false;
	});
	
	$(".job-offer h3").click(
		function() {
			toggleJob($(this));
			return false;
		}); 
});


	
function toggleJob(el) {

			var state = el.next("div").css("display");
			if(state == 'none')
			{
				el.next("div").show("slow");
			}
			else
			{
				el.next("div").hide("slow");
			}
			
};
	
function getContactResponse() {

			var content_show = 'form_name='+$('#form_name').val()+'&form_email='+$('#form_email').val()+'&form_content='+$('#form_content').val()+'&form_dzial='+$('#form_dzial').val();
			$.ajax({  
				type: "post",
				url: "index.php?menu=kontakt&ajax=1",
				data: content_show,  
				cache: false, 
				beforeSend: function(){
				},
				success: function(data){
					$(".form-sending-info p").html('<strong>Wiadomość została wysłana. Dziękujemy!</strong>');		
					$("#contact_submit").css('display','none');				
				},
				error: function() {
					$(".form-sending-info p").html('<strong>Nie udało się wysłać wiadomości, spróbuj ponownie.</strong>');	
				}
			}); 

}


function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
      
   }
   else
   	return true;
}


var makeNewsletter = new Object({
	
	

	construct: function() {
	
	$('#newsletter_add').click(	function() {
			if($(".group-options:checked").length == 0)
				$("#newsletter_email").val('Musisz wybrać temat(y)');
			else
				makeNewsletter.sendForm($(this));
			return false;
	}); 
	

	
	},
	
	sendForm: function(el) {

			var content_show = "menu=newsletter&email="+$("#newsletter_email").attr("value");
			$(".group-options:checked").each(function() {
				content_show += "&"+$(this).attr("name")+"=1";
			});

			$.ajax({  
				method: "get",
				url: "index.php",
				data: content_show+'&ajax=1',  
				cache: false, 
				beforeSend: function(){
					$("#newsletter_email").attr('value','łączę z serwerem...');
				},
				success: function(html){ 	
					$("#newsletter_email").attr('value',html);
				},
				error: function() {
				}
			}); 

	}

});


function popup(url)
{
	window.open( url, "drukuj", "status = 1, height=600, width=490, resizable=no, menubar=no, toolbar=no" );
}
