Yevent		= YAHOO.util.Event;
Ydom		= YAHOO.util.Dom;
YWidget		= YAHOO.widget;

SCM_search_authors = function(){

	return{
		init: function(){
			Yevent.onContentReady('authorSearch', this.author_search);
		},

		author_search: function(){

			// retrieve author list from /VCW/publications/get_authors
			var authors				= new YAHOO.util.XHRDataSource("/VCW/publications/get_authors");
			authors.responseType	= YAHOO.util.XHRDataSource.TYPE_TEXT;
			authors.responseSchema	={
				recordDelim:	"|",
				fieldDelim:		"::",
				fields:			["name", "id"]
			};

			authors.maxCacheEntries = 5;

			// set auto complete options
			var search_config		= {
				prehighlightClassName:	"yui-ac-prehighlight",
				useShadow:				false,
				queryDelay:				0,
				minQueryLenght:			0,
				maxResultsDisplayed:	100
			};

			// initialize auto complete
			var author_complete				= new YWidget.AutoComplete("SearchAuthorName", "authorContainer", authors, search_config);
			// create custom request that is needed by CakePHP
			author_complete.generateRequest	= function(sQuery){
				return "/author:" + sQuery;
			};

			author_complete.resultTypeList = false;

			// create and activate drop down
			var toggler			= Ydom.get('toggleAuthor');
			var author_button	= new YWidget.Button({container: toggler});
			var toggle			= function(e){
				if(!Ydom.hasClass(toggler, "open")){
					Ydom.addClass(toggler, "open");
				}

				// toggler is open
				if(author_complete.isContainerOpen()){
					author_complete.collapseContainer();
				}
				else{ // toggler is closed
					author_complete.sendQuery("alltheworld");
					author_complete.expandContainer();
				}

			};

			author_button.on("click", toggle);
			author_complete.containerCollapseEvent.subscribe(function(){Ydom.removeClass(toggler, "open")});

			// set the correct author id on select
			var author_id = Ydom.get('authorID');
			var select_handler	=function(sType, aArgs){
				var myAC	= aArgs[0];
				var elLI	= aArgs[1];
				var oData	= aArgs[2];

				author_id.value = oData.id;
			};
			author_complete.itemSelectEvent.subscribe(select_handler);

			return{
				authors: authors,
				author_complete: author_complete
			};

		}
	};

}();

SCM_search_authors.init();

SCM_show_all = function(){

	return{
		init: function(){
			Yevent.onContentReady('show_all', this.load_all);
		},

		load_all: function(){
			var redirect_button = Ydom.get('show_all');
			var redirect_url	= Ydom.getAttribute(redirect_button, 'ref');

			Yevent.on(redirect_button, 'click', function(){
				document.location.href = redirect_url;
			});
		}
	}

}();

SCM_show_all.init();
