(function($) {
	$.fn.empty_value = function(empty_value, reset, class_name)
	{
		if (typeof empty_value 	!= 'string')  { empty_value = ''; }
		if (typeof reset 		!= 'boolean') { reset = true; }
		if (typeof class_name 	!= 'string')  { class_name = 'empty'; }
		
		return this.each(function()
		{
			$(this).focus(function()
			{
				if ($(this).hasClass(class_name))
				{
					$(this).removeClass(class_name);
					if (reset) { $(this).val('').attr('title', ''); }
				}
			}).blur(function()
			{
				var val = $(this).val();
				if (!val || val == empty_value)
				{
					$(this).addClass(class_name).val(empty_value).attr('title', $(this).val());
				}
			}).blur();
		});
	};
})(jQuery);

function splitFile(file) {
	var fileName = file.substr(0,file.lastIndexOf("."));
	fileName = fileName.replace(/\_over/g,"");
	fileName = fileName.replace(/\_aktiv/g,"");
	
	var ending = file.substr(file.lastIndexOf(".") + 1,file.length);
	
	return [fileName,ending];
}

function goToMonth(el) {
	$('#start').val('');
	submitForm();
}

function selectDistrict(el) {
	submitForm();
}

function submitForm() {
	$('#form1').submit();
}

function validateForm() {
	var valid = true;
	$('.cleanform input, .cleanform textarea').each(function() {
		if (!valid) {
			return;
		}

		var $$ = $(this);
		var val = ($$.hasClass("confirm_remove") ? $$.is(":checked") : $$.val());
		var name = $$.attr("name");
				
		if ((!val || val == "") && (name.indexOf("phone") == -1)) {
			alert(i18n['fieldMissing']);
			$$.focus();
			valid = false;
			return;
		}

		if (name.indexOf("email") != -1) {
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(val)) {
				alert(i18n['emailInvalid']);
				$$.focus();
				valid = false;
				return;
			}
		}
	});
	return valid;
}

function validatePressForm() {
		var valid = true;
	$('.cleanform input, .cleanform textarea').each(function() {
		if (!valid) {
			return;
		}

		var $$ = $(this);
		var val = ($$.hasClass("confirm_remove") ? $$.is(":checked") : $$.val());
		var name = $$.attr("name");
		if ((!val || val == "") && (name.indexOf("comment") == -1)) {
			alert(i18n['fieldMissing']);
			$$.focus();
			valid = false;
			return;
		}

		if (name.indexOf("email") != -1) {
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(val)) {
				alert(i18n['emailInvalid']);
				$$.focus();
				valid = false;
				return;
			}
		}
	});
	return valid;
}

function limitChars($textarea,maxLength) {
   maxLength = maxLength || 200;
   var length = $textarea.val().length;
   
   if(length > maxLength) {
      $textarea.val($textarea.val().substring(0,maxLength));
	}
}

$(document).ready(function() {
	$('#searchWord').empty_value(i18n["search"], true, 'searchDefaultText');

	$("img.hoverImage").mouseover(function() {
		var oldSrc = $(this).attr("src");
		var parts = splitFile(oldSrc);
		var newSrc = parts[0] + "_over." + parts[1];
		$(this).attr("src",newSrc);
	});
	$("img.hoverImage").mouseout(function() {
		var oldSrc = $(this).attr("src");
		var parts = splitFile(oldSrc);
		var newSrc;
		
		if ($(this).parent().is(".activeNavigation")) {
			newSrc = parts[0] + "_aktiv." + parts[1];
		}
		else {
			newSrc = parts[0] + "." + parts[1];
		}
		
		$(this).attr("src",newSrc);
	});
	$("a.activeNavigation").each(function() {
		$(this).children("img").each(function() {
			var oldSrc = $(this).attr("src");
			var parts = splitFile(oldSrc);
			var newSrc = parts[0] + "_aktiv." + parts[1];
			$(this).attr("src",newSrc);
		});
	});
	$('a[@rel*=lightbox]').lightBox({
		imageBtnPrev : "../pics/" + lang +  "/lightbox-btn-prev.gif",
		imageBtnNext : "../pics/" + lang +  "/lightbox-btn-next.gif",
		imageBtnClose : "../pics/" + lang +  "/lightbox-btn-close.gif",
		imageLoading : "../pics/lightbox-ico-loading.gif",
		txtImage : i18n["image"],
		txtOf : " / "
	});
	$('a[@rel*=lightbox]').each(function() {
		var title = $(this).attr("title");
		if (!title) {
			return;
		}
		var lastSl = title.lastIndexOf("/");
		if (title && lastSl > -1) {
			var firstTitle = title.substring(0,lastSl);
			var secondTitle = title.substring(lastSl + 1,title.length);
			title = secondTitle + " (" + firstTitle + ")";
			$(this).attr("title",title);
		}
	});
	$('a[@rel*=lightbox] > img').each(function() {
		var title = $(this).attr("title");
		if (!title) {
			return;
		}
		var lastSl = title.lastIndexOf("/");
		if (title && lastSl > -1) {
			title = title.substring(0,lastSl);
			var nextSl = title.lastIndexOf("/");
			if (nextSl > -1) {
				title = title.substring(0,nextSl);
			}
		}
		
		title+= " / " + i18n["clickToEnlargeImg"];
		$(this).attr("title",title);
	});

	$("div.freespaceItemDim, .futureOver").css("opacity",0.8);
	$("div.freespaceNoExpand, .futureBox").hover(function() {
		$(this).find(".freespaceItemDim, .futureOver").fadeOut("fast");
	},
	function() {
		$(this).find(".freespaceItemDim, .futureOver").fadeIn("fast");
	});

	var animTime = 300;
	var targetSize = 179;
	var orgSize = 85;
	var orgMargin = 5;

	$(".freespaceExpand").hover(function() {
		var $$ = $(this);
		if ($$.hasClass("inExpansion") || $$.hasClass("inCollapse")) {
			$$.find(".freespaceItemDim").show();
			return;
		}
		if ($$.hasClass("freespaceEven")) {
			$$.css("marginRight",0);
			$$.next(".freespaceItemContainer").animate({width:0},animTime,function() {
				$(this).hide();
			});
		}
		else{
			$$.prev(".freespaceItemContainer").css("marginRight",0);
			$$.prev(".freespaceItemContainer").animate({width:0},animTime,function() {
				$(this).hide();
			});
		}
		$$.addClass("inExpansion");
		$$.find(".freespaceItemDim, .freespaceItemBg").hide();
		$$.animate({width: targetSize},animTime,function() {
			$$.removeClass("inExpansion");
		});
	},
	function() {
		var $$ = $(this);
		if ($$.hasClass("inCollapse")) {
			$$.find(".freespaceItemDim, .freespaceItemBg").show();
			return;
		}
		$$.addClass("inCollapse");
		$$.animate({width: orgSize},animTime,function() {
			$$.find(".freespaceItemDim, .freespaceItemBg").show();
			$$.removeClass("inCollapse");
		});

		if ($$.hasClass("freespaceEven")) {
			$$.next(".freespaceItemContainer").animate({width:orgSize},animTime,function() {
				$(this).show();
				$$.css("marginRight",orgMargin);
			});
		}
		else{
			$$.prev(".freespaceItemContainer").animate({width:orgSize},animTime,function() {
				$(this).show();
				$(this).css("marginRight",orgMargin);
			});
		}
	});
	
	$("#actual_events_item2").hover(function() {
		var $$ = $(this);
		if(!$$.is(':animated') ) {
			$(this).animate({
				height: 40
			}, animTime );
			$$.html($('#hiddenEventContent').html());
		}
	},
	function() {
		$(this).animate({ 
			height: 24
		}, animTime );
		
		$(this).html($('#actual_events_item2_temp').html());
	});
	
	$('a[rel*=facebox]').facebox({
		loading_image : '../pics/lightbox-ico-loading.gif',
		close_image : '../pics/' + lang + '/lightbox-btn-close.gif'
	});

	$('#selectMonth').bind("change", function(event) {
		goToMonth($(this));
   });
   $('#selectMonth').bind("blur", function(event) {
		goToMonth($(this));
   });

   $('#selectYear').bind("change", function(event) {
     goToMonth($(this));
   });
   $('#selectYear').bind("blur", function(event) {
      goToMonth($(this));
   });

   $('#selectDistrict').bind("change", function(event) {
     selectDistrict($(this));
   });  
   $('#selectDistrict').bind("blur", function(event) {
      selectDistrict($(this));
   });
   
   $(".cleanform input:first").focus();
	$("#selectedLightbox").click();
	
	$("div.landesfestumzugBlock").hover(function() {
		$(this).css("borderColor","#A00030");
		$(this).css("color","#A00030");
	},
	function() {
		$(this).css("borderColor","#605843");
		$(this).css("color","#605843");
	}
	);
});
