jQuery(document).ready(function($) {
	// shortcut keys
	$(document).keypress(function(ev) {
		var url,
			map = {
				37: ['#prev a', '.previouspostslink'],
//				38: ['#hint a'],
				39: ['#next a', '.nextpostslink'],
			},
			selectors = map[ev.keyCode];

		for ( var i=0; i<selectors.length; i++ ) {
			url = $( selectors[i] ).attr('href');
			if ( url ) {
				window.location.href = url;
				return;
			}
		}
	});

	// autoshrink embeded videos
	$('object').each(function() {
		var $obj = $(this);
		var cWidth = $obj.attr('width');
		var pWidth = $obj.parent().width();

		if (cWidth <= pWidth)
		    return;

		var attr = {
			'width': pWidth,
			'height': Math.floor($obj.attr('height') * (pWidth / cWidth))
		};

		$obj.attr(attr);
		$obj.find('embed').attr(attr);
	});
});


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 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 changeBg(color) {
	document.getElementById('wrapper').style.backgroundColor = color;
}

window.onload = function(e) {
	var color = readCookie("style") || '';

	if ( color )
		changeBg(color);
}

window.onunload = function(e) {
	color = document.getElementById('wrapper').style.backgroundColor;
	createCookie("style", color, 365);
}
