var Layout = {
	init: function() {
		// TODO: Add autocomplete and move this to a separate file
		$('magnify').observe('click', function(e) {
			// Needs to go away much faster
			$('login').hide();
			
			new Effect.toggle('search', 'slide', {duration: .2});//$('search').toggle();
			
			if( $('search').getStyle('display') != 'none' ) {
				Layout.outsideOf = 'search';
			}

			e.stop();
		});
		
		// Need to work on soft clicking
		//$$('a').invoke('observe', 'click', Layout.softClick);
		
		Login.init();
		Player.init();
	},
	
	loading: function(is_loading) {
		if( is_loading ) {
			$('header_loading').show();
		} else {
			$('header_loading').hide();
		}
	},
	
	softClick: function(e) {
		//Layout.debug(e.target.getOffsetParent());
		var link = '';
		
		if( e.target.tagName == 'A' ) {
			e.target.getOffsetParent();
			link = e.target.readAttribute('href');
		} else {
			link = e.target.parentNode.readAttribute('href'); 
		}
		
		if( e.target.readAttribute('target') != '_blank' &&
			!e.target.hasClassName('no-follow')
		) {
			new Ajax.Request('/soft/page?link=' + link, {
				method : 'post',
				onComplete : Layout.softResponse
			});
			e.stop();
		}

		e.stop();
	},
	
	debug: function(msg) {
		if($('debug')) {
			$('debug').innerHTML += msg + "\n\n";
			
			if( $('debug-div') ) $('debug-div').show();
		}
	},
	
	softResponse: function(response) {
		if( response.status == 200 ) {
			var res = response.responseJSON;
			
			if( res && res.errors && res.errors.length ) {
				document.location = res.link;
			} else {
				$('content').innerHTML = response.responseText;
			}
		} else {
			alert('Oops');
		}
	}
};
document.observe('dom:loaded', Layout.init);