/**
 * Tracker class
 * @param {String} account
 * @param {JSONObject} opts.platform
 * @param {String} opts.platform
 * @param {boolean} opts.noInstallTracking
 */
FeedValue = function (account, opts) {
	this.CAT_WIDGET = 1;
	this.CAT_SITE = 3;
	this.CAT_RSS = 3;
	
	this.trackURI = 'http://tracker.feedvalue.com/0.3/';
	
	if(this.parseAccount(account)) {
		return;
	}
	
	this.opts=opts==null?{}:opts;
	
	if(this.opts.fv_camp) {
		this.fv_camp = this.opts.fv_camp;
	}
	switch(this.eCat) {
		case this.CAT_WIDGET : this.initWidget(); break;
		default: break;
	}
};

/**
 * Parse a account number
 * @return {boolean} true if there is an error
 * @param {String} account
 * @param {boolean} simpleCheck if true does not save parsed value and simply check if the account number is well formated
 */
FeedValue.prototype.parseAccount = function (account,simpleCheck) {
	var t;
	var error = false;
	if(account==null) {
		error = true;
	} else {
		t = account.split('-');
		if (t.length!=4) error = true;
		else {
			if(t[0]!='FV') error = true;			
		}
	}
	
	if(!error && !simpleCheck) {
		this.client = t[1];
		this.eCat = parseInt(t[2]);
		this.eNum = t[3];
		this.account=account;
	}
	
	return error;
}

/**
 * Init Feed Value tracker for widget tracking
 */
FeedValue.prototype.initWidget = function () {	
	this.currentWidget = this.account;
	this.pl=this.opts.platform || 'none';
	if (!this.opts.noInstallTracking) {
		if (this.opts.dontWaitOnload) {
			this.getWidgetInstall();
		} else {
			this._onload(this._bind(this.getWidgetInstall, this));
		}
		
	}

} 

/**
 * Add an install tracking beacon from the current widget to widget_dest
 * @param {Object} widget_dest
 */
FeedValue.prototype.addInstall = function (widget_dest, opt) {
	if(opt && opt.fv_camp) {
		this.fv_camp =opt.fv_camp;
	}
	var head = document.getElementsByTagName("head")[0]; 
	if(this.parseAccount(widget_dest,true)) return;
	var ifr = document.getElementById('fv_tracking_src');
	if (ifr) head.removeChild(ifr);
	var tW = widget_dest;
	var cW = this.currentWidget;
	var url = this.trackURI+'track_src.html?pl='+this.pl+'&cW='+encodeURI(cW)+'&tW='+encodeURI(tW);
	if(opt && opt.iSlogan!=null) url+='&iSl='+opt.iSlogan;
	if(this.fv_camp) {
		url+='&fv_camp='+this.fv_camp;
	}
	this.createIframe('fv_tracking_dest',url);
}

/**
 * Track if the current widget is the target of an install click
 */
FeedValue.prototype.getWidgetInstall = function () {	
	var cW = this.currentWidget;
	var url = this.trackURI+'track_dest.html?pl='+this.pl+'&cW='+encodeURI(cW);
	if(this.fv_camp) {
		url+='&fv_camp='+this.fv_camp;
	}
	this.createIframe('fv_tracking_src',url);
}

/**
 * Create a tracking iframe in the head
 * @param {Object} id
 * @param {Object} url
 */
FeedValue.prototype.createIframe = function (id,url) {
	var head = document.getElementsByTagName("head")[0]; 
	var ifr = document.getElementById(id);
	if (ifr) head.removeChild(ifr);
	ifr = document.createElement('iframe');
	ifr.id=id;
	ifr.src=url;
	ifr.setAttribute('height','0');
	ifr.setAttribute('width','0');
	ifr.style.display='none';
	head.appendChild(ifr);
}

/**
 * create an anonym method with the given scope
 * @private
 * @param {Object} fn
 * @param {Object} scope
 */
FeedValue.prototype._bind =function (fn,scope) {
	var __fn = fn;
	return function () {
		return __fn.apply(scope, arguments)
		
	}
}

/**
 * execute the method on the page onload
 * @param {Object} func
 */
FeedValue.prototype._onload = function(func){
	if (this.opts.onLoadFired) {
		func();
	}else {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		}
		else {
			window.onload = function(){
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}
}