/* learner.js : Lasoo Learner utility
*  Version 1.0
*
*/

function llTracking(host) {

	this.host=host;
	this.version = "1";
	this.advance=1;
	this.retailerId=1;
	this.pageType="home";
	this.locationId=null;
	this.userId=null;
	this.requestHistory=[];
	
	// This is for preview site
	this.setPreview=function(){
		this.host="http://202.92.83.62/event/save"
	}
	
	// Call this for production site
	this.setProduction=function(){
		this.host="http://leaner.lasoo.com.au"
	}
	
	this.init=function(isProduction, pageType, userId, locationId, retailerId) {
//		if (isProduction) {
//			this.setProduction();
//		} else {
//			this.setPreview();
//		}
		this.setPageType(pageType);
		this.setUserId(userId);
		this.setLocationId(locationId);
		this.setRetailerId(retailerId);
	}
	
//	function LasooOfferImpression(id){
//		llImpression('offer', id);
//	}
	
	this.sharedOnFacebook=function(id){
	     this.offerInteraction(id, "facebook");
	}
	
	this.clikedOnBuynow=function(id){
		this.offerInteraction(id, "buynow");
	}
	
	this.sharedOnTwitter=function(id){
		this.offerInteraction(id, "twitter");
	}
	
	this.sharedViaEmail=function(id){
		this.offerInteraction(id, "email");
	}
	
	this.offerLiked=function(id){
		this.offerInteraction(id, "liked");
	}
	
	this.offerInteraction=function(id, interactionType){
		this.interaction('offer', id, interactionType);
	}
	
	this.offerImpression=function(id){
		this.impression('offer', id);
	}

//	function LasooOfferInteraction(id, interactionType){
//		llInteraction('offer', id, interactionType);
//	}
	
	this.impression=function(objectType, id){
		this.learn(objectType, id, 'impression');
	}
	
	this.interaction=function(objectType, id, interactionType ){
		this.learn(objectType, id, interactionType);
	}
	
	this.getURL=function(objectType, id, interactionType){
	    var params = "retailerId=" + (this.getRetailerId()?this.getRetailerId():"");
	    params += "&eventOn=";
	    params += objectType;
	    params += "&eventType=";
	    params += interactionType;
	    params += "&eventId=";
	    params += id;
	    params += "&pageType=";
	    params += this.getPageType();
	    params += "&locationId=";
	    params += this.getLocationId()?this.getLocationId():"";
	    params += "&userId=";
	    params += this.getUserId()?this.getUserId():"";
	    params += "&advance=";
	    params += this.advance;    
	    params += "&ver=";
	    params += this.version;
	    return params;
	}
	
	// id can be a long or an array of long
	this.getEventURLs=function(objectType, id, interactionType) {
		var result=null;
		if (id) {
			var baseUrl = "/borderimage/lls/"+encodeURIComponent(interactionType)+"/"+encodeURIComponent(objectType);
			if (id instanceof Array) {
				if (id.length>0) {
					var links = [];
					var link = null;
					for (var no in id) {
						if (!link) {
							link=baseUrl;
						}
						link+="/"+encodeURIComponent(id[no])
						if (link.length>1900) {
							links.push(link);
							link=null;
						}
					}
					if (link) {
						links.push(link);
					}
					result = links;
				}
			} else {
				result = baseUrl+"/"+encodeURIComponent(id);
			}
		}
		return result;
	}
	
	// application specific methods which need to customized for integration
	// id can be a long or an array of long
	
	this.learn=function(objectType, id, interactionType){
		var postUrls = this.getEventURLs(objectType, id, interactionType);
		if (postUrls) {
			if (postUrls instanceof Array) {
				for (var no in postUrls) {
					this.sendData(this.host+postUrls[no]);
				}
			} else {
				this.sendData(this.host+postUrls);
			}
		}
	}
	
	// Populate retailer Id here as of now it is going to Lasoo Id only
	this.getRetailerId=function(){
	    return this.retailerId;
	}
	
	this.setRetailerId=function(retailerId){
	    this.retailerId = retailerId;
	}
	
	// Get current page name here
	this.getPageType=function(){
	    return this.pageType;
	}
	
	this.setPageType=function(pageType){
		this.pageType = pageType;
	}	
	
	// Get actual entityr id here as category id, retailer id etc
	this.getLocationId=function(){
	    return this.locationId;
	}
	
	this.setLocationId=function(locationId){
	    this.locationId = locationId;
	}

	// Get user id automatically here
	this.getUserId=function(){
	    return this.userId;
	}
	
	this.setUserId=function(userId) {
		this.userId = userId;
	}
	
	this.sendData=function(url){
	    // make ajax call here to Lasoo Leaner server to capture data
	    // This function assumes that we have jquery included on the page
		if (window.location.href.indexOf("debug=2")>0) {
			alert(url);
		}
		
/*		var ajaxObj = new Image();
		var uniqueId = "mv"+Math.ceil(Math.random()*100000000);
		var requestHistory = this.requestHistory;
		this.requestHistory[uniqueId]=ajaxObj;
		ajaxObj.onload=function () {
			requestHistory[uniqueId]=null;
		}
		ajaxObj.src=url+(url.indexOf("?")>=0?"&":"?")+"refresh="+uniqueId;*/
		
//		$.post(url);
	}
}