

/*  easyrec JavaScript API, version 1.0
 *  (c) 2008-2009 Peter Hlavac
 *
 *  This is a client based class to communicate with the REST-API of easyrec.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

var scriptIndex = 0;
var easyrecApiUrl= "http://intralife.researchstudio.at:8080/api/";
var easyrecJSApiUrl= "http://intralife.researchstudio.at:8080/api-js/";

//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////  Item Actions ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

function sendAction(actionType, userId,itemId,sessionId,itemUrl,itemDescription,itemImageUrl, ratingValue){
	includeJavascript(easyrecApiUrl + "1.0/"+ actionType + "?" +
							"tenantid="		+ tenantId +
							"&apikey="		+ apiKey  +
							"&userid="		+ userId +
							"&itemid="   	+ itemId +
							"&sessionid=" 	+ sessionId +
 							"&itemurl="		+ escapeUri(itemUrl) +
 							"&itemdescription="	+ itemDescription +
							"&itemimageurl="	+ escapeUri(itemImageUrl) +
							"&ratingvalue=" + ratingValue);
}
// This function tracks if a user purchases a specific item.
//
function purchaseItem(userId,itemId,sessionId,itemUrl,itemDescription,itemImageUrl){
	includeJavascript(easyrecApiUrl + "1.0/buy?" +
							"tenantid="		+ tenantId +
							"&apikey="		+ apiKey  +
							"&userid="		+ userId +
							"&itemid="   	+ itemId +
							"&sessionid=" 	+ sessionId +
 							"&itemurl="		+ escapeUri(itemUrl) +
 							"&itemdescription="	+ itemDescription +
							"&itemimageurl="	+ escapeUri(itemImageUrl));
}

// This function tracks if a user views a specific item.
function viewItem(userId,itemId,sessionId,itemUrl,itemDescription,itemImageUrl){
	includeJavascript(easyrecApiUrl + "1.0/view?" +
							"tenantid="		+ tenantId +
							"&apikey="		+ apiKey  +
							"&userid="		+ userId +
							"&itemid="   	+ itemId +
							"&sessionid=" 	+ sessionId +
 							"&itemurl="		+ escapeUri(itemUrl) +
 							"&itemdescription="	+ itemDescription +
							"&itemimageurl="	+ escapeUri(itemImageUrl));
}

// This function tracks if a user views a specific item.
function rateItem(userId,itemId,sessionId,itemUrl,itemDescription,itemImageUrl, ratingValue){
	includeJavascript(easyrecApiUrl + "1.0/view?" +
							"tenantid="		+ tenantId +
							"&apikey="		+ apiKey  +
							"&userid="		+ userId +
							"&itemid="   	+ itemId +
							"&sessionid=" 	+ sessionId +
 							"&itemurl="		+ escapeUri(itemUrl) +
 							"&itemdescription="	+ itemDescription +
							"&itemimageurl="	+ escapeUri(itemImageUrl) +
							"&ratingvalue=" + ratingValue);
}

// This functions calls the recommender to retrieve a list
// of similar items to the given one and
// return them in a javascript file
// e.g.
// http://host/api/otherusersalsoviewed.js?tenantid=EASYREC_DEMO&apikey=260b49202287066b8d2449cac92c9888&itemid=42

function getRecommendations(recommendationType, userId,itemId){
	includeJavascript(easyrecJSApiUrl + recommendationType + ".js?" +
							"tenantid="  + tenantId +
							"&apikey=" 	 + apiKey  +
							"&userid="   + userId +
							"&itemid="   + itemId);
}

function callViewSimilarItems(userId,itemId){
	includeJavascript(easyrecJSApiUrl + "otherusersalsoviewed.js?" +
							"tenantid="  + tenantId +
							"&apikey=" 	 + apiKey  +
							"&userid="   + userId +
							"&itemid="   + itemId);
}

function callUserAlsoBought(userId,itemId){
	includeJavascript(easyrecJSApiUrl + "otherusersalsobought.js?" +
							"tenantid="  + tenantId +
							"&apikey=" 	 + apiKey  +
							"&userid="   + userId +
							"&itemid="   + itemId);
}

function callUserAlsoRatedGood(userId,itemId){
	includeJavascript(easyrecJSApiUrl + "itemsratedgoodbyotherusers.js?" +
							"tenantid="  + tenantId +
							"&apikey=" 	 + apiKey  +
							"&userid="   + userId +
							"&itemid="   + itemId);
}


///////////////////////////////// Recommendation WIDGET //////////////////////////////////////////////
function showRecommendations(
			recommendationLayer,
			itemTemplate,
			recommendationtype,
			apikey,
			tenantid,
			itemid,
			userid,
			maxItems,
			prefixString,
			appendString) {


		recommendationUrl = easyrecApiUrl +
				"1.0/json/" + recommendationtype + "?" +
				"&apikey="   + apikey +
				"&tenantid=" + tenantid +
				"&itemid="   + itemid +
				"&userid="   + userid;

		new Ajax.Request(recommendationUrl, {
			method: 'get',
			contentType: 'text/plain',
			onSuccess: function(transport) {


				eval("var recommendation = "+transport.responseText);

				if (recommendation.recommendedItems.length>1) {

					for (var i = 0; i < recommendation.recommendedItems.length && i < maxItems; i++) {

						item = recommendation.recommendedItems[i];
						addItem(i+1, item, recommendationLayer, itemTemplate);
					}

				} else {

					item = recommendation.recommendedItems;
					addItem(1, item, recommendationLayer, itemTemplate);
				}


				recommendationLayer.style.display = "block";
			}
		});
	}


	function addItem(index, item, recommendationLayer, itemTemplate) {

		itemHTML = itemTemplate.innerHTML;
		itemHTML = itemHTML.replace(/easyrec.item.position/g,index);
		itemHTML = itemHTML.replace(/easyrec.item.id/g,item.id);
		itemHTML = itemHTML.replace(/easyrec.item.description/g,unescape(item.description).replace(/^\s+/, '').replace (/\s+$/, ''));
		itemHTML = itemHTML.replace(/easyrec.item.url/g,item.url);
		itemHTML = itemHTML.replace(/http\:\/\/easyrec.org\/img\/blank_easyrec.gif/g,item.imageUrl);

		recommendationLayer.innerHTML += itemHTML;
	}

///////////////////////////////// Recommendation WIDGET END //////////////////////////////////////////////




//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Java Script HELPERS ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

// This function creates a new <SCRIPT> tag with
// the given adress a source.
// (=Dynamic javascript generation)
//
function includeJavascript (jsAdr) {
    var newJs = document.createElement('script');
    newJs.type = 'text/javascript';
    newJs.src = jsAdr;
    newJs.id= scriptIndex++;
    document.getElementsByTagName('head')[0].appendChild(newJs);
}

function removeJavascript(id){
	getNode(a).parentNode.removeChild(getNode(a));
}

function getNode(id){
	return document.getElementById(id);
}

// This function returns the first node
// that matches the given class name.
//
function getNodeByClassName(className) {
	var allPageTags=document.getElementsByTagName("*");
	for (i=0; i<allPageTags.length; i++) {
		if (allPageTags[i].className==className) {
			return allPageTags[i];
		}
	}
}

// This function returns the value of a parameter
// Input: - A list of strings e.g.: [key1=value1][key2=value2]...
//        - The parameter Name e.g.: key2
// Output: The Value of the parameter as a String: e.g. value2
//
function getParameter(parameters, paramName) {
	for (i=0; i<parameters.length; i++) {
		if (parameters[i].split('=')[0]==paramName) {
			return parameters[i].split('=')[1];
		}
	}
}

// This function returns the URL of the current page
// in upper case.
function getHREF() {
	return location.href.toUpperCase();
}

// This funtion returns replaces all % with %25 in the
// given URL.
//
function escapeUri(uri){
	if (uri==null) {
		return ''
	} else {
		return uri.replace(/%/g, "%25");
	}
}

