if (top != self) { top.location = location; }

$(function(){

	$('html').addClass('js');

	// about nula
	$("#about").columnize({	columns:2, balance:true, height:300 });

	// menus and slide ups
	$(".revilient").hide();
	var visible = false;

	function hideInfotext(){
		if (!$.browser.mozilla) {
			$("#text.panel").fadeOut("slow", function(){ $(this).removeClass("positioner"); });
		} else {
			$("#text.panel").animate({top:"100%"});
		}
		visible = false;
	};

	$(".revilator").click(function(e){
        e.preventDefault();
		$(this).next().slideToggle();
		$(this).parents().siblings().children('.revilient').slideUp();
		if (visible) { hideInfotext(); }
	});

	$(".showhideinfo").click(function(e){
        e.preventDefault();
		$(".revilient").slideUp();
		if (!visible) {
			if (!$.browser.mozilla) {
				$("#text.panel").hide().addClass("positioner").fadeIn("slow");
			} else {
				slide_upto = ($(window).height() - $("#text.panel").height()) - 72;
				$("#text.panel").animate({top: slide_upto});
			}
			visible = true;
		} else {
			hideInfotext();
		}
		return false;
	}).hover(
		function(){ $('nav a.showhideinfo').addClass('hoverer'); },
		function(){ $('nav a.showhideinfo').removeClass('hoverer'); }
	);

    // synch function menu rover with nav menu hilights
    $('#functions .revilient').find('a').hover(
    	function(){
    		markClass = $(this).attr('class');
    		$('nav a.'+markClass).addClass("hoverer");
    	},
    	function(){
    		markClass = $(this).attr('class');
    		$('nav a.'+markClass).removeClass("hoverer");
    	}
    );

	// mailing list
	var open = false;
	$('#email, #mailinglist').click(function(e){
		e.preventDefault();
		$("footer").slideToggle();
		open = true;
		return false;
	});
	$('#closeaddemail').click(function(e){
        e.preventDefault();
		$("footer").slideToggle();
		open = false;
		form.data('formstatus','idle'); // needed?
		$('#signup-email').val("");
	});

	$("#signup-email").focus(function(){
	    this.select();
	});

	//http://net.tutsplus.com/tutorials/javascript-ajax/building-a-sleek-ajax-signup-form/
	$('#signup').submit(function() {
		if($(this).data('formstatus') !== 'submitting') {
			var form = $(this),
				formData = form.serialize(),
				formUrl = form.attr('action'),
				formMethod = form.attr('method'),
				responseMsg = $('#signup-response');
			form.data('formstatus','submitting');
			responseMsg.hide().addClass('response-waiting').text('accessing…').fadeIn(333);
			$.ajax({
				url: formUrl,
				type: formMethod,
				data: formData,
				success:function(data) {
					var responseData = jQuery.parseJSON(data),
						klass = '';
					switch(responseData.status) {
						case 'error': klass = 'response-error'; break;
						case 'success': klass = 'response-success'; break;
					}
					responseMsg.fadeOut(333,function() {
						$(this).removeClass('response-waiting').addClass(klass).text(responseData.message)
						.fadeIn(333,function() {
						   setTimeout(function() {
							   responseMsg.fadeOut(333,function() {
							       $(this).removeClass(klass);
								   form.data('formstatus','idle');
							   });
						   },3000)
						});
					});
				}
			});
		}
		return false; //prevent form from submitting
	});

	// email defuscator
	$(".email").defuscate();

	// tooltips
	$('.tiptip').tipTip({maxWidth:"500px"});

});

//canvas clock
function clock(){
	var now = new Date();
	var ctx = document.getElementById('polaris').getContext('2d');
	ctx.save(); ctx.clearRect(0,0,400,400); ctx.translate(200,200); ctx.scale(2,2); ctx.rotate(-Math.PI/2);
	ctx.lineWidth = 1; ctx.lineCap = "butt";
	var milliSec = now.getMilliseconds();
	var sec = now.getSeconds(); sec = milliSec/1000+sec;
	var min = now.getMinutes(); min = sec/60 + min;
	var hr  = now.getHours(); hr = min/60 + hr;
	var secPer = sec/60; var minPer = min/60; var hrPer = hr/24;
	writeTime(ctx,80,hrPer); writeTime(ctx,84,minPer); writeTime(ctx,88,secPer);
	ctx.restore();
}
function writeTime(ctx,radius,per){
	ctx.save();
	ctx.strokeStyle = 'rgba(239,240,241,1)';
	ctx.beginPath();
	partialCircle(ctx,0,0,radius,per);
	ctx.stroke();
	ctx.restore();
}
function partialCircle(ctx,x,y,rad,percentage){
	ctx.arc(x,y,rad,0,percentage*(Math.PI*2),false);
	return ctx;
}
setInterval(clock,66);

