window.addEvent('domready', function() {
	if ($('tagcloud')) {
		var list = $$('#tagcloud a');
		list.each(function(element) {
			var fx = new Fx.Styles(element, {
				duration:1,
				wait:false
			});
			element.addEvent('mouseenter', function(){
				fx.start({
					'background-color': '#dadada'
				});
			});
			var fxout = new Fx.Styles(element, {
				duration:750,
				wait:false
			});
			element.addEvent('mouseleave', function(){
				fxout.start({
					'background-color': '#f9f9f9'
				});
			});
		});
	}
	if ($('nav-login')) {

		linbtn = $('nav-login-btn');
		linbtn.addEvent('mouseover', function(e){
			if($('nav-login').hasClass('hidden')) {
				justShow();
			}
		});
		linbtn.addEvent('click', function(e){
			if ($('nav-login').hasClass('hidden')) {
				justShow();
			} else {
				justHide();
			}
			e = new Event(e).stop();
		});

		// set the mouse out and over for the login window
		$('nav-login').addEvent('mouseout', function(e) {
			justHide();
		});
		$('nav-login').addEvent('mouseover', function(e){
			justShow();
		});
	}

	if($('userfeatures')) {
		getFeatures();
	}
});


function justHide() {
	$('nav-login-container').removeClass('active');
	$('nav-login').addClass('hidden');
}

function justShow() {
	$('nav-login-container').addClass('active');
	$('nav-login').removeClass('hidden');
}

function showOnFocus() {
	//	if the element has focus we shoult set the mouseout event to still show the window
	$('nav-login').addEvent('mouseout', function(e) {
		justShow();
	});
}
function hideFormOnClick() {
	// click outside of the window to close the form
	$('main-container').addEvent('click', function() {
		$('nav-login-container').removeClass('active');
		$('nav-login').addClass('hidden');
	});

	// set the mouseout event - you don't need to click outside of the window
	$('nav-login').addEvent('mouseout', function(e) {
		justHide();
	});
}

function getFeatures() {
	url = '?eID=userfeatures&action=show' ;
	ajax = new Ajax(url, {
		method: 'get',
		evalScripts: true,
		update: 'userfeatures',
		onComplete: function(){
			showFeatures();
		}
	});
	ajax.request();
}
function showFeatures() {
	gotBookmarks = false
	mySlide = new Fx.Slide('bookmarkcontent');
	mySlide.hide();
	$('bm-add').addEvent('click', function(e) {
		bookmarks('insert')
	});

	if($('uf-status-info')) {
		statusSlide = new Fx.Slide('uf-status-info');
		statusSlide.hide();
		$('uf-toggle').addEvent('click', function(e) {
			e = new Event(e);
			statusSlide.toggle();
			e.stop();
		});
	}
	$('toggle').addEvent('click', function(e){
		if(!gotBookmarks) {
			if(loggedIN) {
				bookmarks();
			} else {
				complete();
			}
			gotBookmarks = true;
		} else {
			e = new Event(e);
			mySlide.toggle();
			e.stop();
		}
	});
	return true;
}

function complete() {
	if(loggedIN) {
		deleteItems = $$('#bookmarkcontent div.bm-delete');
		deleteItems.addEvent('click', function() {
			bookmarks('delete', this.id);
		});
	}
	mySlide.slideIn();
}


function bookmarks(action, deleteUrl) {
	if(!loggedIN) {
		complete();
		return;
	}
	title = '';
	switch (action) {
		case 'insert':
			title = document.title;
			action = '&action=insert';
			href = escape(myURL);
			break;
		case 'delete':
			action = '&action=delete';
			href = escape(deleteUrl);
			break;
		default:
			action = '';
			href = escape(myURL);
			break;
	}
	url = '?eID=userfeatures' + action +  '&data=' + href ;
	if(title) {
		url += '&title=' + title;
	}
	if(!gotBookmarks || action != 'insert' || action != 'delete' ) {
		ajax = new Ajax(url, {
			method: 'get',
			update: 'bookmarkcontent',
			onComplete: function(){
				complete();
			}
		});
		ajax.request();
		gotBookmarks = true;
	}
}
