/* JS
-------------------------------------------------- */
document.getElementsByTagName("html")[0].className = "js";

/* IE6 flicker fix
-------------------------------------------------- */
/*@cc_on
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
@*/

/* Dialog
-------------------------------------------------- */
function showModal(target){
	var modalwrap = $("#modalwrap");

	$("#overlay").css("display","block");
	$("#" + target).css("display","block");
	modalwrap.fadeIn(150);

	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		$("html").css("overflow","hidden");
	} else {
		modalwrap.css("margin-top","-" + modalwrap.height() / 2 + "px");
	}

	escape();
	return false;
}
function hideModal(){
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("html").css("overflow","");
	}
	$("#modalwrap").fadeOut(150, function(){
		$("#overlay, div.modalpopup").css("display","none");
	});
	return false;
}

/* Escape
-------------------------------------------------- */
function escape(){
	document.onkeyup = function(e) {
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if (code == 27){
			hideModal();
			hidePhoto();
		}
	}
}

/* Photo
-------------------------------------------------- */
function loadPhoto(){
	$("<img />")
		.load(function(){
			$("#modalphoto").append($(this));
			showPhoto.init();
		})
		.attr("src",  $("#photo").children("a").attr("href") );
}
var showPhoto = {
	init : function(){
		$("#photo").children("a").click(this.clickIt);
	},
	clickIt : function(){
		var overlay = $("#overlay");
		var modalphoto = $("#modalphoto");
		var photo = modalphoto.find("img");
		var pw = photo.width();
		var ph = photo.height();

		overlay
			.css({
				display: "block",
				opacity: "0"
			})
			.fadeTo(250, 0.75, function(){
				modalphoto
					.addClass("ready")
					.animate({
						width: pw + "px",
						height: ph + "px",
						marginTop: - ph / 2 + "px",
						marginLeft: - pw / 2 + "px",
						opacity: "1"
					}, 250 );
			});
		modalphoto.one("click", function(){ hidePhoto(); });
		escape();
		return false;
	}
};
function hidePhoto(){
	var modalphoto = $("#modalphoto");

	modalphoto.animate({
		opacity: 0,
		width: 0,
		height: 0,
		marginTop: "0",
		marginLeft: "0"
	}, 500, function(){
		$("#overlay").fadeOut(250, function(){
			modalphoto.removeClass();
		});
	});
	return false;
}

/* jQuery
-------------------------------------------------- */
var setRadio = {
	init : function(){
		$("fieldset").find("input:radio").click(this.clickIt);
		$("fieldset").find("input:radio:checked").parent("li").addClass("checked");
	},
	clickIt : function(){
		$(this).parents("ul").children(".checked").removeClass("checked");
		$(this).parent("li").addClass("checked");
	}
};
function showLoader() {
	$("#loader").show();
}
function hideLoader() {
	$("#loader").hide();
}
function introSlide() {
	var introslide = $("#introslide");
	introslide.children().eq(0).appendTo(introslide).show();
	setInterval(function() {
		introslide.children().eq(0).hide().appendTo(introslide).fadeIn("slow");
	}, 4000);
}

/* equal heights */

jQuery.fn.equalHeight = function () {
	var height    = 0;
	var maxHeight = 0;

	// Store the tallest element's height
	this.each(function () {
		height    = jQuery(this).outerHeight();
		maxHeight = (height > maxHeight) ? height : maxHeight;
	});

	// Set element's min-height to tallest element's height
	return this.each(function () {
		var t         = jQuery(this);
		var minHeight = maxHeight - (t.outerHeight() - t.height());
		var property  = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';

		t.css(property, minHeight + 'px');
	});
};

/* =init
-------------------------------------------------- */
$(function(){

	setRadio.init();

});


var removeErrorClasses = function()
{
	$('.valid').each(function()
	{
		if($(this).parent().children('.error').length == 0)
		{
			$(this).parent().removeClass('error');
		}
	});
};

var defaultInvalidHandler = function(form, validator)
{
	var errors = validator.numberOfInvalids();
	if (errors) 
	{ 
		$("#error-notice").show(); 
		removeErrorClasses();
	} 
	else { $("#error-notice").hide(); }
	
	removeErrorClasses(); 
};

var defaultErrorPlacement = function(error, element) 
{
	if(error.html() != '')
	{				
		element.parent().addClass('error');
	}
};

Date.prototype.age=function(at)
{
	var value = new Date(this.getTime());
	var age = at.getFullYear() - value.getFullYear();
	value = value.setFullYear(at.getFullYear());
	
	if (at < value) --age;
	return age;
};

function getAge(Y,M,D)
{
	if(Y == 0 || M == 0 || D == 0) { return 0; }
	var now=new Date(),m=now.getMonth()+1,d=now.getDate();
	return now.getFullYear()-Y+(M>m?-1:M==m&&D>d?-1:0);
}

function showLoader() { $("#loader").show(); }
function hideLoader() {	$("#loader").hide(); }

var setKVMtest = {
	init : function(){
		$("#kvm-test").find("input:radio").click(this.clickIt);
	},
	clickIt : function()
	{
		$(this).parents("li:eq(0)").find("input:radio").not(this).parent().removeClass("checked");
		$(this).parent().addClass("checked");
	}
};

function getKVMtest() {	$("#kvm-test").find("input:radio:checked").parent().addClass("checked"); }