Page = {
	init: function() {
		Util.externalLinks();
		window.setTimeout(Page.setColorbox, 500);
		Page.setStartboxes();
		
		if($("plan")) {
			Plan.init();
		}
		Search.init();
		Clapable.init();
		
		if($("#overlay").length>0) {
			$("#overlay .close").click(function() {
				$("#overlay").fadeOut("fast");
			});
		}
	},
	setStartboxes: function() {
		if($("#startbox1")) {
			maximum= 0;
			for(var i=1;i<=3;i++) {
				if($("#startbox"+i).height()>maximum) {
					maximum= $("#startbox"+i).height();
				}
			}
			for(var i=1;i<=3;i++) {
				$("#startbox"+i).css("height", maximum);
			}
		}
	},
	setColorbox: function() {
		$(".colorbox").each(function() {
			$(this).css("width", $("img", $(this)).width());
			$(this).css("height", $("img", $(this)).height());
		}).colorbox();
		/* special colorbox */
		$(".special_colorbox").colorbox({iframe:true, innerWidth:1000, innerHeight:640});
		$(".special_contactform").colorbox({iframe:true, innerWidth:430, innerHeight:460});
		$(".special_newsletter").colorbox({iframe:true, innerWidth:350, innerHeight:350});
	}
}

Search = {
	init:function() {
		if($("#event_search_field")) {
			$("#event_search_field").keyup(function() {
				var search= $("#event_search_field").val();
				if(search.length>0) {
					$("#event_search_field_close").fadeIn("fast");
					Search.go(search);
				} else {
					$("#event_search_field_close").fadeOut("fast");
				}
				$("#event_search_field_close").click(function() {
					$("#event_search_field").val("");
					$(this).fadeOut("fast");
					Search.reset();
				});
			});
		}
	},
	reset: function() {
		$("#event_list .tr").each(function() {
			$(this).show();
		});
	},
	go: function(search) {
		$("#event_list .tr").each(function() {
			if(!$(this).attr("class").match("tr_month") && !$(this).attr("title").toLowerCase().match(search.toLowerCase())) {
				$(this).hide();
			} else {
				$(this).show();
			}
		});
	}
}

Clapable = {
	init: function() {
		if($(".clapable")) {
			$(".clapable").each(function() {
				$(".th", $(this)).each(function() {
					$(this).addClass("cursor_pointer");
					$(this).css("width", $(this).width());
				});
				$(".th", $(this)).click(function() {
					var id= $(this).parent().attr("id");
					Clapable.toggle(id);
				})
			});
		}
	},
	toggle: function(id) {
		if($("."+id+":first").css("display")=="none") {
			$("."+id).each(function() {
				Clapable.show($(this));
			});
		} else {
			$("."+id).each(function() {
				Clapable.hide($(this));
			});
		}
	},
	show: function(obj) {
		var height= parseInt(obj.css("height"));
		obj.css("height", 0).show();
		obj.animate({
			height: height
		}, 500, "linear", function() {
			obj.css("height", "auto");
		});
	},
	hide: function(obj) {
		var height= parseInt(obj.height());
		obj.animate({
			height: 0
		}, 500, "linear", function() {
			obj.hide().css("height", height);
		});
	}
}

Plan = {
	init: function() {
		$("#contact_menu .option").click(function() {
			Plan.toggle($(this));
		});
	},
	toggle: function(obj) {
		if(obj.hasClass("active")) {
			obj.removeClass("active");
			$("#plan .detail_"+obj.attr("id").replace(/option_/, '')).fadeOut("fast");
		} else {
			obj.addClass("active");
			$("#plan .detail_"+obj.attr("id").replace(/option_/, '')).fadeIn("fast");
		}
	}
}

Util = {
	externalLinks: function() {
		//Source: http://www.sitepoint.com/print/standards-compliant-world/
		if (!document.getElementsByTagName) {
			return;
		}
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
				anchor.target = "_blank";
			}
		}
	},
	validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  // trim input field
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		if(result) {
			$("#form_error").removeClass("error");
		} else {
			$("#form_error").addClass("error");
		}
		return result;
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null )
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	}
}
