// adding ajax switch between calendar months
function getMonth(obj, y, m){

  $.cookie("lesk_month",String(y) + String(m),{ path: '/', expires: 730});
  $("#calendar").load("calendar.inc.php?month=" + String(y) + String(m));

}



$(document).ready(function(){

  Cufon.replace("#header #headLinks li dt a", {
    fontFamily: "Verlag Condensed Book"
  });

  $("#headLinks dd").each(function(){
    $("a:last", this).css("border", "none");
  });

  $("dt, dd", "#header #headLinks li").corner({
    tl: { radius: 5 },
    tr: { radius: 5 },
    bl: { radius: 5 },
    br: { radius: 5 },
    antiAlias: true
  });

  $("#headLinks dl:has(dd) dt a").click(function(){
    return false;
  });


  if ($("#comments form#fAddComment").length > 0) {

	$("#fAddComment").validate({
		rules: {
			cName:	"required",
			cEmail:	{
					required:	true,
					email:	true
					},
			cText:	"required"
		},
		submitHandler: function(form){
			$(form).replaceWith('<div id="ajaxed"></div>');
			$(form).ajaxSubmit({
				target:	"#ajaxed",
				success: function() {
					$(form).resetForm();
				}
				});
		},
		highlight: 	function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").addClass("errorLabel");
		},
		unhighlight: 	function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
		},
		errorPlacement:	function() {
			return false;
		}
	});

  }

 if ($("#comments form#fAddDoYouKnow").length > 0) {

	$("#fAddDoYouKnow").validate({
		rules: {
			cName:	"required",
			cEmail:	{
					required:	true,
					email:	true
					},
			cText:	"required"
		},
		submitHandler: function(form){
			$(form).replaceWith('<div id="ajaxed"></div>');
			$(form).ajaxSubmit({
				target:	"#ajaxed",
				success: function() {
					$(form).resetForm();
				}
				});
		},
		highlight: 	function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").addClass("errorLabel");
		},
		unhighlight: 	function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
		},
		errorPlacement:	function() {
			return false;
		}
	});

  }


  if ($("form#fNewsletterIn, form#fNewsletterOut").length > 0) {

	$("#fNewsletterIn").validate({
		rules: {
			nEmail1:	{
					required:	true,
					email:	true
					}
		},
		submitHandler: function(form){
			$("div.panel_bg").append('<div id="ajaxed"></div>');
			$("div.panel_bg #ajaxed").css("padding","0 20px");
			$(form).ajaxSubmit({
				target:	"#ajaxed",
				success: function() {
					$(form).resetForm();
				}
				});
		},
		highlight: 	function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").addClass("errorLabel");
		},
		unhighlight: 	function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
		},
		errorPlacement:	function() {
			return false;
		}
	});

	$("#fNewsletterOut").validate({
		rules: {
			nEmail2:	{
					required:	true,
					email:	true
					}
		},
		submitHandler: function(form){
			$("div.panel_bg").append('<div id="ajaxed"></div>');
			$("div.panel_bg #ajaxed").css("padding","0 20px");
			$(form).ajaxSubmit({
				target:	"#ajaxed",
				success: function() {
					$(form).resetForm();
				}
				});
		},
		highlight: 	function(element, errorClass) {
			$(element).addClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").addClass("errorLabel");
		},
		unhighlight: 	function(element, errorClass) {
			$(element).removeClass(errorClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
		},
		errorPlacement:	function() {
			return false;
		}
	});


  }


  // target blank to external links
  $("a[href^=http://]").attr("target","_blank");


  // show alt text under .pContent images
  $("div.bigCol .pContent p img[alt!='']").each(function(){
	if (this.getAttribute("width") && this.getAttribute("height")) {
		$(this).wrap('<span class="imgContent"></span>');
		$(this).parent().append($("<em>" + $(this).attr("alt") + "</em>"));
		$(this).parent().width($(this).width() + 6);
	}
  });



  // jcarousel gallery
  if ($.fn.jcarousel && $("#gallery").length > 0) {
  $("#gallery").jcarousel();
  $("#gallery a").fancybox();
  }


  // glosuj
  if ($("#glosuj").length > 0) {
  $("#glosuj a.glosuj_btn").click(function(){
	$.ajax({
		url: "_glosuj2.php",
		type: "POST",
		data: "count=" + parseInt($("#glosuj span strong").html()),
		success: function(html){
			$("#glosuj span, #glosuj em").remove();
			$("#glosuj").append(html);
		},
		error: function(){
			alert("Error!");
		}
	});
	return false;
  });
  }


  $("#wiki li a.selected").corner({
    tl: { radius: 5 },
    tr: { radius: 5 },
    bl: { radius: 0 },
    br: { radius: 0 },
    antiAlias: true
  });


  if ($.fn.fancybox && $("div.gallery-row").length) {
    $("div.gallery-row a:not(.more)").fancybox();
  }

  if ($.fn.fancybox && $("a.iframe").length) {
    $("a.iframe").fancybox({
      "width": 700,
      "height": 540,
      "hideOnContentClick": false,
      "type": "iframe"
    });
  }


  var f = $("form[id^=fAdd]");
  if (f.attr("id") === "fAddGallery") {

    $('#fileInput').uploadify({
      'uploader'  : 'uploadify/scripts/uploadify.swf',
      'script'    : 'uploadify/scripts/uploadify_gallery.php?SESSID=' + $("input[name=SESSID]", f).val(),
      'cancelImg' : 'uploadify/cancel.png',
      'buttonImg' : 'uploadify/button2_' + $("input[name=language]").val() + '.gif',
      'folder'    : 'uploadify/temp',
      'width'     : '260',
      'height'    : '48',
      'sizeLimit' : '2097152',
      'fileExt'   : '*.gif;*.jpg;*.png',
      'fileDesc'  : 'Pliki graficzne (*.gif, *.jpg, *.png)',
      'multi'     : 'true',
      'queueSizeLimit'  : '10',
      onSelectOnce: function(event, data){
        if ($("input[name=fileCount]", f).val() === "0") {
          $("input[name=fileCount]", f).val(data.fileCount);
        }
      },
      onCancel: function(event, queueID, fileObj, data){
        $("input[name=fileCount]", f).val(data.fileCount);
      },
      onAllComplete: function(){
        f.addClass("filled");
        $("div.submit > input", f).click();
      }
    });

  } else if (f.attr("id") === "fAddGallery2") {

    $('#fileInput').uploadify({
      'uploader'  : 'uploadify/scripts/uploadify.swf',
      'script'    : 'uploadify/scripts/uploadify_support.php?SESSID=' + $("input[name=SESSID]", f).val(),
      'cancelImg' : 'uploadify/cancel.png',
      'buttonImg' : 'uploadify/button_' + $("input[name=language]").val() + '.gif',
      'folder'    : 'uploadify/temp',
      'width'     : '260',
      'height'    : '48',
      'sizeLimit' : '2097152',
      'fileExt'   : '*.gif;*.jpg;*.png',
      'fileDesc'  : 'Pliki graficzne (*.gif, *.jpg, *.png)',
      onSelectOnce: function(event, data){
        if ($("input[name=fileCount]", f).val() === "0") {
          $("input[name=fileCount]", f).val(data.fileCount);
        }
      },
      onCancel: function(event, queueID, fileObj, data){
        $("input[name=fileCount]", f).val(data.fileCount);
      },
      onAllComplete: function(){
        f.addClass("filled");
        $("div.submit > input", f).click();
      }
    });

  } else if (f.attr("id") === "fAddWiki") {

    $('#fileInput').uploadify({
      'uploader'  : 'uploadify/scripts/uploadify.swf',
      'script'    : 'uploadify/scripts/uploadify_place.php?SESSID=' + $("input[name=SESSID]", f).val(),
      'cancelImg' : 'uploadify/cancel.png',
      'buttonImg' : 'uploadify/button2_' + $("input[name=language]").val() + '.gif',
      'folder'    : 'uploadify/temp',
      'width'     : '260',
      'height'    : '48',
      'sizeLimit' : '2097152',
      'fileExt'   : '*.gif;*.jpg;*.png',
      'fileDesc'  : 'Pliki graficzne (*.gif, *.jpg, *.png)',
      'multi'     : 'true',
      'queueSizeLimit'  : '10',
      onSelectOnce: function(event, data){
        if ($("input[name=fileCount]", f).val() === "0") {
          $("input[name=fileCount]", f).val(data.fileCount);
        }
      },
      onCancel: function(event, queueID, fileObj, data){
        $("input[name=fileCount]", f).val(data.fileCount);
      },
      onAllComplete: function(){
        f.addClass("filled");
        $("div.submit > input", f).click();
      }
    });

  } else if (f.attr("id") === "fAddEvent") {

    $('#fileInput').uploadify({
      'uploader'  : 'uploadify/scripts/uploadify.swf',
      'script'    : 'uploadify/scripts/uploadify_event.php?SESSID=' + $("input[name=SESSID]", f).val(),
      'cancelImg' : 'uploadify/cancel.png',
      'buttonImg' : 'uploadify/button_' + $("input[name=language]").val() + '.gif',
      'folder'    : 'uploadify/temp',
      'width'     : '260',
      'height'    : '48',
      'sizeLimit' : '2097152',
      'fileExt'   : '*.gif;*.jpg;*.png',
      'fileDesc'  : 'Pliki graficzne (*.gif, *.jpg, *.png)',
      onSelectOnce: function(event, data){
        if ($("input[name=fileCount]", f).val() === "0") {
          $("input[name=fileCount]", f).val(data.fileCount);
        }
      },
      onCancel: function(event, queueID, fileObj, data){
        $("input[name=fileCount]", f).val(data.fileCount);
      },
      onAllComplete: function(){
        f.addClass("filled");
        $("div.submit > input", f).click();
      }
    });

  }
  

  if ($.fn.validate) {

    $.validator.addMethod('dependsOn', function (value, el, params) {
			return !$(params.el).is(params.being) || $(el).is(':filled');
		}, 'This field is required.');

    $("#fAddGallery, #fAddGallery2, #fAddWiki, #fAddEvent").validate({
			rules: {
        wname: "required",
        wemail: {
          required: true,
          email: true
        },
        waddress: "required",
        wtitle: "required",
        wplace: {
          dependsOn: {
            el: "#wplace2-id",
            being: ":not(:filled)"
          }
        },
        wplace2: {
          dependsOn: {
            el: "#wplace-id option:first",
            being: ":selected"
          }
        },
        fileCount: {
          required: true,
          notEqualTo: "0"
        }
			},
      highlight: 	function(element, errorClass) {
        if ($(element).attr("name") === "fileCount") {
          alert( ($("input[name=language]").val() === "en") ? "Please select at least one file." : "Wybierz co najmniej jeden plik z dysku.");
        } else {
          $(element).addClass(errorClass);
  				$(element.form).find("label[for=" + element.id + "]").addClass("errorLabel");
        }
			},
			unhighlight: 	function(element, errorClass) {
				$(element).removeClass(errorClass);
				$(element.form).find("label[for=" + element.id + "]").removeClass("errorLabel");
			},
			errorPlacement:	function() {
				return false;
			},
			submitHandler: function(form) {
        if ($(form).hasClass("filled")) {
          $(form).submit();
        } else {
          $("#fileInput").uploadifyUpload();
        }
      }
		});
  }
  
  if ($("input[name^=wdate]").length) {
    var dates = $('#wdate1-id, #wdate2-id').datepicker({
			onSelect: function(selectedDate) {
				var option = this.id == "wdate1-id" ? "minDate" : "maxDate";
				var instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
				dates.not(this).datepicker("option", option, date);
			}
		});
  }

});