$(document).ready( function(){
	var scr = $(window).width();
	
	// nav
	$("#n1 a").each( function(i,a){
		a = $(a);
		var h = a.attr("href"), l = location.pathname;
		if( ( h.length > 1 && l.indexOf(h) == 0 ) || h == l )
			a.addClass("current");
		if(h == l) 
			a.removeAttr("href");
	});
	
	// search
	var i = $("#search input[name='q']"), s = $("#search input[type='submit']").prop("disabled",true), def = i.prop("defaultValue");
	i.focus( function(){if (i.val() == def) i.val("")} );
	i.keyup( function(){s.removeAttr("disabled")} );
	i.blur( function(){
		if ( !i.val() ) {
			i.val( def ); 
			s.prop("disabled", true); 
		}
	});
	$("#search").submit( function(){
		i.val(i.val() + " site:rayofsolaris.net");
	});
	
	if( scr > 860 ) {
		function quotefill(txt){
			var t = $("#top"), q = $('<div id="quote">' + txt + '</div>');
			t.append(q);
			var sp = Math.round((t.height() - q.innerHeight())/2);
			if(sp>2) q.css("top", sp + "px");
		}
		
		var q = utils.session("quote");
		if(q) quotefill(q);
		else $.get("/quote.php", function(txt){
			utils.session("quote",txt);
			quotefill(txt);
		});

		if(document.body.id == "blog"){
			var svg = "<rect fill='#FFF' x='0' y='0' width='50' height='50' />";
			for(var k=0; k<5; k++) for(var j=0; j<5; j++)
				svg = svg + '<circle fill="#682e7c" fill-opacity="' + (Math.round( Math.random() * 10 ) / 10) + '" cx="' + ((10*k) + 5) + '" cy="' + ((10*j) + 5) + '" r="5" />';
			$(".post header").prepend('<div class="icon"><svg width="50px" height="50px" viewBox="-1 -1 51 51">' + svg + "</svg></div>");
		}
	}
	
	function footerPos(){
		var f = $("#bottom"), w = $("#wrap"), gap = $(window).height() - f.offset().top - f.outerHeight();
		if(gap) w.css("min-height", ( w.height() + gap) + "px");
	}
	
	footerPos();
	$(window).resize( footerPos ); 
});

// utils
var utils = {
	session: function(key,val){
		if(!self.sessionStorage) return false;
		if(typeof val == 'undefined') return sessionStorage[key];
		else sessionStorage[key] = val;
	},

	readCookies: function(){
		var cObject = {};
		if(document.cookie){
			var cArray = document.cookie.replace(/\s/g,"").split(";");
			for(var i=0, j; j=cArray[i]; i++){
				var parts = j.match(/([^=]+)=(.+)/i);
				cObject[decodeURIComponent(parts[1])] = decodeURIComponent(parts[2]);
			}
		}
		return cObject;
	}
}
