var handmenu_block = 0;
var menu_hand_width = 362;
var menu_hand_content_width = 316;

function set_columns_styles() {
	$('.left_menu_content').css("display", "none");
	$('.visible .left_menu_content').css("display", "block");
	left_height = 0;
	$('.left_menu_content').each(function() {
		if ($(this).actual("height") > left_height) {
			left_height = $(this).actual("height"); 
		}
	});
	left_height += ($('#left_menu > li').length * $('#left_menu > li .category').height()) + 70; 
	$('#right_column').css("min-height", left_height + "px");
	$('#center_column').css("min-height", left_height + "px");
}

function check_handmenu_position() {
	if ($('.handmenu').length > 0) {
		if ( ($('.handmenu').offset().top + $('.handmenu').height()) > $(window).height() ) {
			$('.handmenu').css("position", "absolute");	
		}
		else {
			$('.handmenu').css("position", "fixed");
		}
		
		if ( ($('.handmenu').offset().left - $('.handmenu_toggle').width()) < $('#content').width() ) {
			$("#content_wrap").css("padding-right", $('.handmenu_toggle').width() + "px");
		}
		else {
			$("#content_wrap").css("padding-right", "0");
		}
	}
}

function set_facebookbox_postition() {
	$('#facebook_box_wrap').css("top", "-" + $('#facebook_box_wrap').actual("height") + "px");
}

function show_facebookbox() {
	fb_pos = $('#facebook_box_wrap').position().top;
	fb_ht = $('#facebook_box_wrap').actual("height");
	if (fb_pos == 0) {
		$('#facebook_box_wrap').animate({
			"top": -fb_ht
		}, 600);	
		$('#facebook').animate({
			"top" : 0
		}, 600);
	}
	else {
		$('#facebook_box_wrap').animate({
			"top": 0
		}, 600);	
		$('#facebook').animate({
			"top" : fb_ht
		}, 600);
	}
}

$(document).ready(function() {
	set_facebookbox_postition();
	set_columns_styles();
	check_handmenu_position(); 
	
	$(window).resize(function () {
		check_handmenu_position();
    });
	
	$('#facebook_link').click(function(e) {
		e.preventDefault();
		show_facebookbox();
	});
	
	$('#left_menu .category').click(function() {
		el = $(this);
		if (el.parent().find('.left_menu_content').css("display") == "none") {
			$('.visible .left_menu_content').slideUp(function(){
				$(this).closest('li').removeClass('visible');
				el.closest('li').addClass('visible').find('.left_menu_content').slideDown();
			});
		}
	});	
	$('.handmenu_toggle').click(function() {
		if (handmenu_block == 0) {
			handmenu_block = 1;
			toggle_width = $(this).width();
			menu_width = $(this).parent().width();
			if (menu_width > toggle_width) {
				menu_width = toggle_width;
				menu_content = 0;
			}
			else {
				menu_width = menu_hand_width;
				menu_content = menu_hand_content_width;
			}
			$(this).parent().animate({
				"width": menu_width
			}, 600, function() {
				handmenu_block = 0;
			});
			$(this).parent().find('.handmenu_content_wrap').animate({
				"width": menu_content
			}, 600);
			$(this).parent().find('.handmenu_content').animate({
				"width": menu_content
			}, 600);
		}
	});
	
	//KONSULTACJE AJAX FORM
   $('#konsultacje_form').submit(function() {
      if(validate_all($(this))) { 
	  	$form_wrap = $(this).parent();
         $form_wrap.html("").append($('<div></div>').addClass("ajax_loader"));
		 var act_url = $(this).attr("action") + '/1/';
		 data_serial = $(this).serialize();
         $.ajax({
            type: "POST",  
            url: act_url,  
            data: data_serial,  
            success: function(data){
               switch (data) {
                  case '1' :
                     $form_wrap.html("<p>Dziękujemy. Zgłoszenie zostało przyjęte.</p>");
                     break;
                  default :
                     $form_wrap.html("<p>Wystąpił błąd podczas wysyłania zgłoszenia.<br /> Spróbuj ponownie później.</p>");     
               }
            }  
         });  
         return false;
      }
      else {
         return false;
      }  
   });  
});

