﻿// JavaScript Document
$(function() {
	$("img").each(function(i, n) {
		var maxWidth = n.getAttribute("max-width");
		var maxHeight = n.getAttribute("max-height");
		if (!maxWidth && !maxHeight) return;
		if (n.width > maxWidth || n.height > maxHeight) {
			maxWidth = maxWidth || n.height;
			maxHeight = maxHeight || n.height;
			if (n.width / maxWidth > n.height / maxHeight) {
				n.setAttribute("width", maxWidth);
			} else {
				n.setAttribute("height",maxHeight);
			}
		}
	})
	$("a[@rel*=lightbox]").lightBox();
	$("form").submit(function() {
		var messages = [];
		if (this.dontValidate) return;
		var formValidateGroup = this.validateGroup;
		$.each(this.elements, function(i, n) {
			var v = n.value;
			if (n.getAttribute("required") != undefined && v == "") {
				messages.push(n.getAttribute("required-text"));
			} 
			var compare = n.getAttribute("compare");
			if (compare) {
				var compareTo = document.getElementById(compare);
				if (v != compareTo.value) {
					messages.push(n.getAttribute("compare-text"));
				}
			}
			var r = n.getAttribute("regex");
			if (r) {
				var regex;
				switch(r.toLowerCase()) {
					case "email":
						regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
						break;
					case "number":
						regex = /^\d+$/;
						break;
					default:
						regex = new RegExp(r);
						break;
				}
				if (!regex.test(v)) {
					messages.push(n.getAttribute("regex-text"));
				}
			}
		});

		if (messages.length) {
			alert(messages.join("\n"));
			return false;
		}
	});
	$("*[@postback]").each(function(i, n){
		n.postdata = eval("("+n.postback+")");
		var wireEvent = "click";
		if ("event" in n.postdata) {
			wireEvent = n.postdata.event;
			delete n.postdata.event;
		}
		$(n)[wireEvent](function() {
			var form = $(this).parents("form");
			if (form.length) {
				if ('action' in this.postdata) {
					form[0].action += "?action=" + this.postdata.action;
					delete this.postdata.action;
				}
				for (var name in this.postdata) {
					var field = form.find(name);
					if (!field.length) {
						field = $('<input name="'+name+'" id="'+name+'" type="hidden" />');
						form.append(field);
					}
					field.val(this.postdata[name]);
				}
				var causeValidate = this.getAttribute("cause-validate");
				form[0].dontValidate = (causeValidate == "no" || causeValidate == "false");
				form[0].validateGroup = this.getAttribute("validate-group") || "";
				form.submit();
				return false;
			}
		});
	});
	$(".date-pick").datePicker({clickInput:true}).dpSetStartDate('2000-01-01');
	var cur;
	$("#main-nav .submenu").each(function(i,n) {
		var p = $(n).parent();
		var po = p.offset();
		n.style.top = po.top + 31;
		n.style.left = po.left;
		n.style.width = p.width() + 1;
	});
	$("#main-nav td > a").hover(function(){
		if (cur) cur.fadeOut('fast');
		var t = $(this).next();
		if (t.attr("className") == "submenu") {
			cur=t.slideDown('fast');
		}
	},function(){});
	$("#header").hover(function(){}, function(){if(cur)cur.fadeOut('fast');});
})