/**
 * CSBookmarkClass - Bookmark management
 * 
 * @copyright  CouchSurfing
 */
function CSBookmarkClass() {}
CSBookmarkClass.prototype = {
	data : {},
	show_sorry : function(id) {
		Modalbox.show('/bookmarks/sorry',{title: 'Sorry', width:282, hashUrl:false});
	},
	show_delete : function(id) {
		Modalbox.show('/bookmarks/delete/'+id,{title: 'Delete Bookmark', width:300, hashUrl:false});
	},
	do_delete : function(id) {
		csr('/bookmarks/delete/'+id, {
			csr_options: {
				csr_type: 'CSR.IterativeHashedJSON',
				type: 'json'
			},
			prototype_options: {
				parameters: {action: 'delete'},
				onSuccess: this.deleted.bind(this),
				onFailure: this.error.bind(this)
			}
		});
	},
	deleted : function(data) {
		this.nav_open();
		new Effect.BlindUp($('nav_bookmark'+data.id));
		$('bookmark'+data.id).hide();
		Modalbox.hide();
	},

	show_edit : function(id) {
		Modalbox.show('/bookmarks/edit/'+id,{title: 'Rename Bookmark', width: 330, hashUrl:false});
	},
	edit : function(id, newname) {
		csr('/bookmarks/edit/'+id, {
			csr_options: {
				csr_type: 'CSR.IterativeHashedJSON',
				type: 'json'
			},
			prototype_options: {
				parameters: {name: newname},
				onSuccess: this.saved.bind(this),
				onFailure: this.error.bind(this)
			}
		});
	},
	saved : function(data) {
		this.nav_open();
		var navobj = $('nav_bookmark'+data.id);
		navobj.innerHTML = data.name;
		new Effect.Pulsate(navobj);
		$('bookmark'+data.id).down('a').innerHTML = data.name;
		Modalbox.hide();
	},
	nav_open : function() {
		var navtab = $('nav_bookmarks_add').up('div');
		navtab.style.visibility = 'visible';
		navtab.style.backgroundColor = '#F9B564';
		window.setTimeout('CSBookmark.nav_close()', 3000);
	},
	nav_close : function() {
		var navtab = $('nav_bookmarks_add').up('div');
		navtab.style.visibility = '';
		navtab.style.backgroundColor = '';
	},
	show_create : function() {
		if(!this.data.url)
			this.data.url = location.href.replace(/#.+/, '');
		if(!this.data.title)
			this.data.title = document.title.replace(/CouchSurfing - /, '');
		Modalbox.show(
			'/bookmarks/create',
			{
				params:{url:this.data.url,name:this.data.title},
				title:'CouchSurfing Bookmarks',
				width: 350,
				hashUrl:false
			}
		);
	},
	create : function(url, name) {
		csr('/bookmarks/create', {
			csr_options: {
				csr_type: 'CSR.IterativeHashedJSON',
				type: 'json'
			},
			prototype_options: {
				parameters: {url: url, name: name, action: 'save'},
				onSuccess: this.created.bind(this),
				onFailure: this.error.bind(this)
			}
		});
	},
	created : function(data) {
		this.nav_open();
		Modalbox.hide();
		$('nav_bookmarks_add').up('li').previous().insert('<li class="subitem"><a id="nav_bookmark_new" class="subitem" href="'+data.url+'">'+ data.name +'</a></li>',{position:'after'});
		new Effect.Pulsate('nav_bookmark_new');
	},
	
	update : function(new_title,new_url) {
		this.data.title = new_title;
		if( new_url )
		 	this.data.url = new_url;
	},
	error : function(data) {
		alert('Oops.. something went wrong - please try again.');
		Modalbox.hide();
	},
	check : function(field) {
		if ($(field).value.replace(/ /g,'') != '')
			return true;
		$(field).focus();
		return false;
	}
};

var CSBookmark = new CSBookmarkClass();
