"use strict";
bindToLoad(init);
function id(i) {return document.getElementById(i)}
function bindToLoad(fn) {
	if(window.addEventListener) window.addEventListener("load",fn,false);
	else window.attachEvent("onload",fn) }

var pageid, nav, advjs = (document.getElementsByClassName),
	descs = {home:"The personal website of Samir Shah", archive:"Older pages preserved for posterity", blog:"Random ramblings about stuff that matters to me", code:"Bits and pieces of open source code", jyotisa:"Tools and articles on the subject of Vedic Astrology", photos:"A small collection of photographs taken by me", poems:"A small collection of poems that I've written"};

function init(){
	pageid = document.body.id;
	nav = id("nav");
	if(nav) requestContent("/menus/main.txt", fillNav);
	var f = id("footer"), h = id("header"), w = id("wrap");
	if(descs[pageid]) {
		var d = document.createElement('p');
		d.innerHTML = descs[pageid];
		h.appendChild(d); }
	f.innerHTML='<p><a href="/">Home</a> <a href="/contact/">Contact</a> <a href="/license.php" rel="license">License</a>';
	var gap = window.innerHeight - w.offsetHeight - h.offsetHeight - f.offsetHeight - 41;
	if(gap > 0) w.style.minHeight = (w.offsetHeight + gap) + "px";
	if(pageid=="blog") blogInit();
	if(pageid=="home") homeInit(); }

function fillNav(content) {
	nav.innerHTML = content;
	var links = nav.getElementsByTagName("a");
	for(var i=0,a; a=links[i]; i++) {
		var key = a.id.replace("nav-","");
		if(key && descs[key]) {
			a.title = descs[key];
			if(a.href == location.href || location.pathname.indexOf("/"+key) == 0) a.className = "current"; }
		else if(a.href && document.URL.indexOf(a.href) != -1) a.className = "current";
		if(a.href == location.href) a.removeAttribute('href'); }
	// search
	var i = id("searchinput"), s = id("searchsubmit");
	i.onfocus = function (){if (i.value == i.defaultValue) i.value = ''};
	i.onkeyup = function(){s.disabled = false};
	i.onblur = function (){
		if (!i.value) {
			i.value = i.defaultValue; 
			s.disabled = true; } };
	i.form.onsubmit = function () {i.value="site:rayofsolaris.net "+i.value}; }

function requestContent(url, callback) { // GET
	var req = createXMLHttpObject();
	if(!req) return;
	req.open('GET', url, true);
	req.onreadystatechange = function() {
		if (req.readyState != 4 || (req.status != 200 && req.status != 304) ) return;
		callback(req.responseText); }
	req.send(); }

function ajaxSubmit(url, data_object, callback) { //POST
	//supply data as object with key/value pairs. It will be encoded here.
	var dataArray = [];
	for(var k in data_object) dataArray.push(encodeURIComponent(k)+"="+encodeURIComponent(data_object[k]));
	var data = dataArray.join("&");
	
	var req = createXMLHttpObject();
	if(!req) return;
	req.open('POST', url, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	req.onreadystatechange = function() {
		if (req.readyState != 4 || (req.status != 200 && req.status != 304) ) return;
		callback(req.responseText); }
	req.send(data); }

function blogInit() {
	if(advjs) {
		var pl = document.getElementsByClassName("permalink");
		for (var p in pl) pl[p].title = "Permanent link to this comment";} }

function homeInit(){
	requestContent("/quote.php", function(content) {
		if(!content) return;
		var q = document.createElement("div");
		q.id = "quote";
		q.innerHTML = content;
		id("content").appendChild(q); }); }

function createXMLHttpObject() {
	var x = false;
	try {x = new XMLHttpRequest()}
	catch(e) {try {x = new ActiveXObject("Microsoft.XMLHttp")} catch(e) {} }
	return x}

function session(key,val) {
	if(!self.sessionStorage) return false;
	if(!val) return sessionStorage[key];
	else sessionStorage[key] = val}

function readCookies() {
	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}