var Notelist = Class.create({
	initialize : function(){
		var notelist = this;
		var id;
		var type;
		var checked;
	},
	updateNotelist : function(){
		new Ajax.Request(ROOT_PATH + 'helpers/ajaxnote.php?notelistaction=' + this.type + '&id=' + this.id + '&checked=' + this.checked , {
			method: 'get',
			onComplete: showNotelistCount
		});
		
		// try to switch all links
		try{
			if(this.checked){
				$$('.removeFrom' + this.type.capitalize() + 'Notelist' + this.id).invoke('show');
				$$('.addTo' + this.type.capitalize() + 'Notelist' + this.id).invoke('hide');
			} else {
				$$('.removeFrom' + this.type.capitalize() + 'Notelist' + this.id).invoke('hide');
				$$('.addTo' + this.type.capitalize() + 'Notelist' + this.id).invoke('show');
			}
		}catch(e){
			console.log(e)
		}
	},
	addFromInput : function(event, type){
		var elm = Event.findElement(event, "input");
		this.id = elm.value;
		this.type = type;
		this.checked = elm.checked;
		this.updateNotelist();
	},
	addFromLink : function(id, type, checked){
		this.id = id;
		this.type = type;
		this.checked = checked;
		this.updateNotelist();
	}
});

function showNotelistCount(ajaxResponse){
	$$('span[class=notelistCount]').invoke('update', array_sum(ajaxResponse.responseJSON.notelistCount));
	$$('span[class=notelistJobsCount]').invoke('update', ajaxResponse.responseJSON.notelistCount.jobs);
	$$('span[class=notelistProfilesCount]').invoke('update', ajaxResponse.responseJSON.notelistCount.profiles);
	$$('span[class=notelistStatementsCount]').invoke('update', ajaxResponse.responseJSON.notelistCount.statements);
	$$('span[class=notelistPostsCount]').invoke('update', ajaxResponse.responseJSON.notelistCount.posts);
}

function addAsBookmark(title, url){
	if(window.sidebar){
		window.sidebar.addPanel(title, url, '_content');
	} else if(window.external){
		window.external.addFavorite(url, title);
	}
}

function array_sum(array){
	var key, sum = 0;
	
	if(typeof array !== 'object'){
		return null;
	}
	
	for(key in array){
		sum += (array[key] * 1);
	}	
	return sum;
}