/*Jquery*/

var sliderBlocked = false;
var logosBlocked = false;

$(document).ready(function(){
	
	$(".box.portal ul").addClass("stylized");

   	$(".events td").live("click", function() {
   		if(($(this).find("a")[0] != undefined) && ($(this).find("a")[0] != null)) window.location = $(this).find("a").attr("href");
   	});
   
    //Main slider actions
    $(".line-photo-slider").ready(function() {
      var that = $(".line-photo-slider");
      
      if((that[0] != null) && (that[0] != undefined)) {
        if(that.find("li").size() < 5) that.find(".next").css("display","none");
        else that.find("li").eq(3).addClass("last");
      }
    });
    
    
    $(".line-photo-slider .next").live("click", function() {
   	  var that = $(this);
      var parent = $(this).parents(".line-photo-slider");
      var current = parent.find("li.last");
      var next = null;
      
      if(!sliderBlocked) {
      		sliderBlocked = true;
      		
      		var margin = current.width() + parseInt(current.css("margin-right"));
      		
      		next = current.next();
      		current.removeClass("last");
      		next.addClass("last");

			parent.find(".line").animate({'margin-left': '-' + margin + 'px'}, animationDuration, function() {
				var cln = $(this).find("li").first().clone();
				$(this).find("ul").append(cln);
				$(this).find("li").first().remove();
				$(this).css("margin-left","0");
				sliderBlocked = false;
			});
      }
    });
    
    // logos Slider
     $(".line-logos-slider").ready(function() {
      var that = $(".line-logos-slider");
      
      if((that[0] != null) && (that[0] != undefined)) {
        if(that.find("li").size() < 6) that.find(".next").css("display","none");
        else that.find("li").eq(3).addClass("last");
      }
    });
    
    
    $(".line-logos-slider .next").live("click", function() {
   	  var that = $(this);
      var parent = $(this).parents(".line-logos-slider");
      var current = parent.find("li.last");
      var next = null;
      
      if(!logosBlocked) {
      		logosBlocked = true;
      		
      		var margin = current.width() + parseInt(current.css("margin-right"));
      		
      		next = current.next();
      		current.removeClass("last");
      		next.addClass("last");

			parent.find(".line").animate({'margin-left': '-' + margin + 'px'}, animationDuration, function() {
				var cln = $(this).find("li").first().clone();
				$(this).find("ul").append(cln);
				$(this).find("li").first().remove();
				$(this).css("margin-left","0");
				logosBlocked = false;
			});
      }
    });
    
    $("form#reservation_form").submit(function() {
    	$.ajax({
            type: "POST",
            url: "/portal/reservation_ajax_send/",
            data: $(this).serialize(),
            dataType: 'json',
            beforeSend: function() {
          		$("form#reservation_form div.loader span").text("Przetwarzanie rezerwacji...");
                $("form#reservation_form div.loader").show();
            },
            success: function(data){
          	  	$("form#reservation_form div.loader").hide();
                if(data.errors) {
					$('label').removeClass('errors');
					$('small.errors').remove();
					$('small').show();
					if(data.errors.name) {
					    $("label[for='id_name']").addClass('errors').next('small').hide()
					    	.after("<small class='errors'>(" + data.errors.name + ")</small>");
					}
					if(data.errors.street) {
					  $("label[for='id_street']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.street + ")</small>");
					}
					if(data.errors.code) {
					  $("label[for='id_code']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.code + ")</small>");
					}
					else if(data.errors.city) {
					  $("label[for='id_code']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.city + ")</small>");
					}
					if(data.errors.phone) {
					  $("label[for='id_phone']").addClass('errors').next('small').hide()
					    	.after("<small class='errors'>(" + data.errors.phone + ")</small>");
					}
					if(data.errors.email) {
					  $("label[for='id_email']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.email + ")</small>");
					}
					if(data.errors.date_from) {
					  $("label[for='id_period']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.date_from + ")</small>");
					}
					else if(data.errors.date_to) {
						$("label[for='id_period']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.date_to + ")</small>");
					}
					if(data.errors.type) {
						$("label[for='id_type']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.type + ")</small>");
					}
					if(data.errors.accept) {
						$("label[for='id_accept']").addClass('errors');
					}
					if(data.errors.subject) {
					  $("label[for='id_subject']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.subject + ")</small>");
					}
					if(data.errors.room) {
					  $("label[for='id_room']").addClass('errors').next('small').hide()
					  	.after("<small class='errors'>(" + data.errors.room + ")</small>");
					}
                }
                else {
                	$('label').removeClass('errors');
					$('small.errors').remove();
					$('small').show();
  					$("form#reservation_form")[0].reset();
  					$("form#reservation_form").parents(".viewer").fadeOut(animationDuration);
                }
            }
  		});
        return false;
    });
});
