$(function(){
	/*
	 * Blur on focus
	 */
	 
	$('a').focus(function() { this.blur(); });
	$('input[@type*=submit]').focus(function() { this.blur(); });
});

function getJournals(volume, openVolumes) {
	var element = volume != null ? $('#blog-volume-' + volume) : $('#journal-list');
	element.hide();
	var html = element.html();
	element.html('Loading...');
	element.fadeIn(80);
	
	var url = volume != null ? "/blog/index/get-journals/volume/" + volume : "/blog/index/get-journals";
	
	$.ajax({
		url: url,
		type: "POST",
		success: function(msg){
			element.fadeOut(80, function() {
				element.html(html);
				element.children('.results').remove();
				element.append(msg);
				element.fadeIn(80);

				if (openVolumes != "" && openVolumes != null) {
					var journalVolumes = openVolumes.split(',');
					for (var i in journalVolumes) {
						getJournals(journalVolumes[i]);
					}
				}	
				
				if (volume != null) {
					element.children('span').attr('onclick', '');
					element.children('span').click(function() {
						collapseJournals(volume);
					});
				} 
			});
      	}
	});
}

function collapseJournals(volume) {
	var element = $('#blog-volume-' + volume);
	$.ajax({
		url: "/blog/index/collapse-journal/volume/" + volume,
		type: "POST",
		success: function(msg){
			element.children('.results').fadeOut(80, function() {
				element.children('.results').remove();
				element.children('span').click(function() {
					getJournals(volume);
				});
			});
		}
	});
}
/*
function addCookie(url, update) {
	var monthYear = url.search("month") == -1 ? "year" : "month";
	if (getCookie('hinzJournals' + monthYear) != "") {
		var cookie = getCookie('hinzJournals' + monthYear);
		cookie += ',' + url + ":" + update;
	} else {
		var cookie = url + ":" + update;
	}
	setCookie('hinzJournals' + monthYear, cookie, 1);
}

function removeCookie(url, update) {
	var monthYear = url.search("month") == -1 ? "year" : "month";
	if (getCookie('hinzJournals' + monthYear) != "") {
		var cookie = getCookie('hinzJournals' + monthYear);
		cookie = cookie.replace("," + url + ":" + update, "");
		cookie = cookie.replace(url + ":" + update, "");
		setCookie('hinzJournals' + monthYear, cookie, 1);
	}
}*/

/* cookie functions from w3schools.com *//*
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}*/
