/**
 * ResultList component.
 *	
 */

function ResultList($element) {
	ResultList.baseConstructor.call(this, $element);

	this.initDOMElems();
	this.initPageNavigation();

	this.listElementTemplate = ResultList.defaultListElementTemplate;
	// extra for author42-integration
	var context = parent.document.getElementById("externalIFrame");
	if(context != null){
		this.listElementTemplate = ResultList.author42ListElementTemplate;
	}
	// end

	this.reset();
}

ResultList.extend(Component);

ResultList.LOADING_MESSAGE="loading_message";

/*

	PUBLIC 
	methods and event handlers

*/

ResultList.prototype.reset = function(){		
	this.displayResults();
}

ResultList.prototype.displayResults = function(results, message, currentPage, numPages) {
	
	if(results == undefined){
		results=[];
	}
	
	if(numPages == undefined || currentPage==undefined || numPages<2){
		this.$pageNavigation.hide();
	} else {
		this.$pageNavigation.show();
		this.pageNavigation.reset(numPages, currentPage);
	}
	
	this.displayMessage(message);
	this.createTiles(results);
	
	// add "next page" link
	if(currentPage<numPages){

		this.$resultList.addClass("clearfix");
		this.$resultList.append("<button id='nextPage' style='float:right; margin-right:20px;'>Next page</button>");
		var _this=this;
		$("#nextPage").click(function(){_this.onPageChanged(++currentPage);return false;});
	}
}

ResultList.prototype.displayMessage = function(msg, text){
	if(msg==ResultList.LOADING_MESSAGE){
		this.$resultListTitle.empty().append("<span class=\"resultListLoadingMessage\">Searching for: "+ text + "...</span>");
		this.$resultList.hide();
	} else {
		this.$resultList.fadeIn();
		this.$resultListTitle.empty().append(msg);
	};
}

ResultList.prototype.setListElementTemplate = function(htmlTemplate){
	this.listElementTemplate=htmlTemplate;
}

/*
	"OVERWRITEABLES" 
*/

// triggered when a result tile is clicked
// @param searchResult: a structured search result object passed from the serviceConnector


ResultList.prototype.onPageChanged = function(pageNum){
	console.log("ResultList onPageChanged "+pageNum);
}

/*
	
	List tile templates
	
*/

// normal search result display
ResultList.defaultListElementTemplate = ''
	+ '<li>'
	
	+ '<a class="searchResultLink" style="background-image:url('+MACEConstants.rootURL+'components/search/php/getThumbnail.php?lomId={%=id%});" title="View this content" onclick="app.SessionTracker.saveAction(\'resultList\', \'goToPage\', \'{%=url%}\', \'{%=id%}\');" href="{%=url%}" target="_blank">'	

	+ '<span class="title"><span class="repository showTooltip" title="{%=catalog.description%}">{%=catalog.name%}</span>{%=shortTitle%}</span>'
	+ '<span class="description">{%=description%}</span>'

 	+ '<a class="metaDataBar clearfix" title="View and edit MACE metadata" href="'+MACEConstants.detailsBaseSrc + '{%=id%}">'	
	
	+ '<div class="viewMetadata">more info</div>'
	+ '</a>'

	+ '</a></li>';

// for author42 context
ResultList.author42ListElementTemplate = ''
	+ '<li>'
	
	+ '<a class="searchResultLink" style="background-image:url('+MACEConstants.rootURL+'components/search/php/getThumbnail.php?lomId={%=id%});" title="View this content" onclick="app.SessionTracker.saveAction(\'resultList\', \'goToPage\', \'{%=url%}\', \'{%=id%}\');" href="{%=url%}" target="_blank">'	

	+ '<span class="title"><span class="repository showTooltip" title="{%=catalog.description%}">{%=catalog.name%}</span>{%=shortTitle%}</span>'
	+ '<span class="description">{%=description%}</span>'
	+ '</a>'

	+ '<div style="text-align: right; font-size: 0.9em; padding: 0.3em 1em; border: 1px solid #999999; border-left-color: #FFF; border-top-color: #FFF; background-color: #EEEEEE; ">select for use <input type="checkbox" name="inputauthor42" value="{%=url%}" onclick="if(this.checked) parent.takeSelected(this.value); else parent.deleteSelected(this.value);" /></div>'
	
	+ '</li>';
	
// for user page
ResultList.bookmarkListElementTemplate = ''
	+ '<li>'
	
	+ '<a class="searchResultLink" style="background-image:url('+MACEConstants.rootURL+'components/search/php/getThumbnail.php?lomId={%=id%});" title="View this content" onclick="app.SessionTracker.saveAction(\'resultList\', \'goToPage\', \'{%=url%}\', \'{%=id%}\');" href="{%=url%}" target="_blank">'	

	+ '<span class="title"><span class="repository showTooltip" title="{%=catalog.description%}">{%=catalog.name%}</span>{%=title%}</span>'
	+ '<div class="tags clearfix">'
	+ '{% var tagArray = tags.split(","); %}'
	+ '{% for (var i = 0; i < tagArray.length; i++) { %}'
	+ '{% if (tagArray[i].length > 0) { %}'
	+ '<span>{%= tagArray[i] %}</span>'
	+ '{% } %}'
	+ '{% } %}</div>'
	+ '<span class="description">{%=description%}</span>'

	
 	+ '<a class="metaDataBar clearfix" title="View and edit MACE metadata" href="'+MACEConstants.detailsBaseSrc + '{%=id%}">'	
	//+ '<div class="rating_{%=myRating%} rating " title="Rating:{%=myRating%}">&nbsp;</div>'	
	+ '<div class="viewMetadata">more info</div>'
	+ '</a>'

	+ '</a></li>';
	/*
	+ '<li>'
	
 	+ '<a class="metaDataBar clearfix" title="View and edit MACE metadata" href="' + MACEConstants.detailsBaseSrc + '{%=id%}">'	
	+ '<div class="repository" style="background-image:url({%=catalog.iconURL%})">{%=catalog.name%}</div>'
	+ '<div class="rating_{%=myRating%} rating " title="Rating:{%=myRating%}">&nbsp;</div>'
	+ '</a>'
	
	+ '<a class="searchResultLink" title="View this content" onclick="app.SessionTracker.saveAction(\'resultList\', \'goToPage\', \'{%=url%}\', \'{%=id%}\');" href="{%=url%}" target="_blank">'	
	+ '<span class="title" title="{%=title%}">{%=title%}</span>'
	+ '<div class="tags clearfix">'
	+ '{% var tagArray = tags.split(","); %}'
	+ '{% for (var i = 0; i < tagArray.length; i++) { %}'
	+ '{% if (tagArray[i].length > 0) { %}'
	+ '<span>{%= tagArray[i] %}</span>'
	+ '{% } %}'
	+ '{% } %}</div>'
	
	+ '</a></li>';
	*/
/*

	INTERNAL

*/

ResultList.prototype.initDOMElems = function(){
	this.$element.empty();
	
	this.$element.append('<div class="resultListHeader"></div>');
	this.$header=$(".resultListHeader", this.$element);
	
	this.$header.append('<div class="pageNavigation"></div>');
	this.$pageNavigation=$(".pageNavigation", this.$element);
	
	this.$header.append('<div class="resultListTitle"></div>');
	this.$resultListTitle=$(".resultListTitle", this.$element);
	
	this.$element.append('<div class="searchResults"><ul class="resultList"></ul></div>');	
	this.$resultList=$(".searchResults > ul", this.$element);
}

ResultList.prototype.initPageNavigation = function(){
	this.pageNavigation=new PageNavigation(this.$pageNavigation, this);

	var _this=this;
	this.pageNavigation.onPageChanged = function(pageNum){
		_this.onPageChanged(pageNum);
	}

}

ResultList.prototype.createTiles = function(results){
	this.$resultList.empty();
	
	for (var i = 0; i < results.length; i++) {
		// REVISIT: HACK for old ALOE urls
		results[i].url=results[i].url.split("http://mace-project").join("http://portal.mace-project");
		
		var htmlString = tmpl(this.listElementTemplate, results[i]);
		var t = this.$resultList.append(htmlString);
	}	
	
	// add tooltip
	this.$resultList.find("a").tipsy({fade: true, gravity:"s"});

}
