function add_css_class(selector, class_name) {
	var elements = cssQuery(selector);
	for(var e = 0; e < elements.length; e++) {
		elements[e].className += " " + class_name;
	}
}
var page_width = 960;

function init() {
	add_css_class("ul.utilities li:first-child", "first-child");
	add_css_class("ul.posts li:first-child", "first-child");
	add_css_class("ul.navigation-pages li:first-child", "first-child");
	add_css_class("ul.navigation-months li:first-child", "first-child");
	
	justify();
	display_bg();
}
function roundNumber(rnum, rlength) {
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

var theSize = null;

function allSize() {
	
	var theWidth = getBrowserWidth();
	var theBody = document.getElementsByTagName("body")[0];
	if(theWidth < 1034) {
                                theSize = "m";
    }
    else if(1034 <= theWidth && theWidth < 1240) {
                    theSize = "m";
    }
    else if(1240 <= theWidth && theWidth < 1400) {
                    theSize = "l";
    }
    else {
                    theSize = "xl";
    }

	var pages = cssQuery("div.page");
	for(var e = 0; e < pages.length; e++) {
		pages[e].className = "page " + theSize;
	}
}
function getBrowserWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	} else if (document.body) {
		return document.body.clientWidth;
	}
	return 0;
}
window.onload = window.onresize = function () {
	allSize();
	justify();
}

function justify() {
	var page = cssQuery("div.page");
	for(var e = 0; e < page.length; e++) {
		page_width = page[e].offsetWidth;
	}
	
	var h1 = cssQuery("div.branding h1");
	for(var e = 0; e < h1.length; e++) {
		
		if(page_width < 1024) {
			h1[e].style.letterSpacing = page_width/5800+"em";
		} else if(page_width > 1024 && page_width < 1240) {
			h1[e].style.letterSpacing = page_width/3600+"em";
		} else if(page_width > 1240) {
			h1[e].style.letterSpacing = page_width/3000+"em";
		}
	}
	
	var p = cssQuery("div.branding p");
	for(var e = 0; e < p.length; e++) {
		
		if(page_width < 1024) {
			p[e].style.letterSpacing = page_width/4800+"em";
		} else if(page_width > 1024 && page_width < 1270) {
			p[e].style.letterSpacing = page_width/3400+"em";
		} else if(page_width > 1270) {
			p[e].style.letterSpacing = page_width/2800+"em";
		}
	}
}
function createCookie(name,value,days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if(c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
function display_bg() {
	var cookie = readCookie("idescookie");
	if(cookie) {
		if(cookie < 13) {
			cookie++;
		} else {
			cookie = 1;
		}
		var bd = cssQuery("body");
		for(var e = 0; e < bd.length; e++) {
			bd[e].className = "v-"+cookie;
		}
		createCookie("idescookie",cookie,365);
		
	} else {
		createCookie("idescookie",1,365);
	}
}
/*
window.onresize = function () {
	justify();
}
*/