
function SEngine(Authority, QueryParamName){

	this.Authority		= Authority;
	this.QueryParamName	= QueryParamName;
}

function RParam(ParamName, ObjectName){

	this.ParamName	= ParamName;
	this.ObjectName	= ObjectName;
}

function OfferMiner(){

	this.SearchEngines		= new Array(new SEngine("google", "q"), new SEngine("bing", "q"), new SEngine("omreferrer", "q"));
	this.RequestParams		= new Array(new RParam("OM.siteID", "SiteID"), 
										new RParam("OM.uri", "URIPath"),
										new RParam("OM.domain", "Domain"), 
										new RParam("OM.rDomain", "ReferrerDomain"), 
										new RParam("OM.exVisitorID", "ExVisitorID"), 
										new RParam("OM.offerClick", "OfferClick"), 
										new RParam("OM.mailClick", "MailClick"), 
										new RParam("OM.pb", "ProductBasket"), 
										new RParam("OM.pp", "ProductPurchase"), 
										new RParam("OM.ppr", "ProductPrice"), 
										new RParam("OM.pv", "ProductView"), 
										new RParam("OM.q", "SEngineParam"), 
										new RParam("OM.OSB", "OnSiteBanner"), 
										new RParam("OM.OSS", "OnSiteSearch"), 
										new RParam("OM.PPC", "PPC"),
										new RParam("OM.pu", "ProductUnit"),
										new RParam("OM.cur", "Currency"),
										new RParam("OM.evt_t", "EventType"),
										new RParam("OM.inv", "InvoiceNo"),
										new RParam("OM.OSBD", "OnSiteBannerDisplay"),
										new RParam("OM.bid", "BasketID"));
														
	this.URI		= parseUri(document.location.href.toString());
	this.CookieID	= this.CheckCookie();
	
	if(document.referrer && document.referrer.toString() != "")
		this.ReferrerURI	= parseUri(document.referrer.toString());
	
	this.GetMETAParams();
	this.GetURIParams();
}

OfferMiner.prototype.EventType				= null;
OfferMiner.prototype.InvoiceNo				= null;
OfferMiner.prototype.OnSiteBannerDisplay	= null;
OfferMiner.prototype.BasketID				= null;
OfferMiner.prototype.SearchEngines			= null;
OfferMiner.prototype.SEngineParam			= null;
OfferMiner.prototype.Domain					= null;
OfferMiner.prototype.URIPath				= null;
OfferMiner.prototype.ReferrerDomain			= null;
OfferMiner.prototype.RequestParams			= null;
OfferMiner.prototype.Protocol				= null;
OfferMiner.prototype.SiteID					= "15";
OfferMiner.prototype.CookieName				= "OfferMiner_ID";
OfferMiner.prototype.CookieID				= null;
OfferMiner.prototype.ExVisitorID			= null;
OfferMiner.prototype.OfferClick				= null;
OfferMiner.prototype.MailClick				= null;
OfferMiner.prototype.ProductBasket			= null;
OfferMiner.prototype.ProductPurchase		= null;
OfferMiner.prototype.ProductPrice			= null;
OfferMiner.prototype.PPC					= null;
OfferMiner.prototype.ProductView			= null;
OfferMiner.prototype.DisplayAd				= null;
OfferMiner.prototype.OnSiteBanner			= null;
OfferMiner.prototype.OnSiteSearch			= null;
OfferMiner.prototype.OMServerURL			= "sdc.portakalofis.com/";
OfferMiner.prototype.OMGifName				= "om.gif";
OfferMiner.prototype.URI					= null;
OfferMiner.prototype.ReferrerURI			= null;
OfferMiner.prototype.URIParams				= "";
OfferMiner.prototype.RequestParams			= null;
OfferMiner.prototype.ProductUnit			= null;
OfferMiner.prototype.Currency				= null;

OfferMiner.prototype.GetMETAParams = function(){

	var	METAs	= document.getElementsByTagName("meta");
		
	for(var j=0; j<METAs.length; j++){
	
		Found	= false;
	
		for(var i=0; i<this.RequestParams.length; i++){
		
			if(METAs[j].name == this.RequestParams[i].ParamName){
						
				eval("this." + this.RequestParams[i].ObjectName + "='" + METAs[j].content.split("'").join("\'") + "'");
				break;
			}
		}
	}
}

OfferMiner.prototype.GetURIParams = function(){

	var	Found;

	if(this.URI != null){
	
		for(var Key in this.URI.queryKey){
		
			Found	= false;
		
			for(var i=0; i<this.RequestParams.length; i++){
			
				if(Key == this.RequestParams[i].ParamName){
				
					Found	= true;
					eval("this." + this.RequestParams[i].ObjectName + "='" + this.URI.queryKey[Key].split("'").join("\'") + "'");
					break;
				}
			}
			
			if(!Found){
							
				this.URIParams	+= "&" + Key +"="+ encodeURIComponent(unescape(this.URI.queryKey[Key]));
			}
		}
	}
}

OfferMiner.prototype.WriteCookie = function(){

	var	cDate	= new Date();
	var	cValue	= cDate.getTime();
	var	Days	= 1000;

	cDate.setTime(cDate.getTime() + (Days * 24 * 60 * 60 * 1000));
	document.cookie	= this.CookieName + "=" + cValue + "; expires=" + cDate.toGMTString() + "; path=/; domain=" + this.URI.authority;
		
	return cValue;
};

OfferMiner.prototype.CheckCookie = function(){
	
    var	CookieArr	= document.cookie.split(";");
    var	Value		= null;
              
    for(var i=0; i<CookieArr.length; i++){
            
        while(CookieArr[i].charAt(0) == " ")
			CookieArr[i]	= CookieArr[i].substring(1, CookieArr[i].length);

        if (CookieArr[i].indexOf(this.CookieName + "=") == 0)
            Value	= CookieArr[i].substring((this.CookieName + "=").length, CookieArr[i].length);
    }
    
    if(Value == null){
    
		Value	= this.WriteCookie();
    }

    return Value;
};

OfferMiner.prototype.CollectReferrerSerach = function(){

	var	ParamArr	= null;
	var	Value		= null;
	var	Resume		= false;
	var	ParamName	= null;
		
	for(var i=0; i<this.SearchEngines.length; i++){
	
		if(this.ReferrerURI.authority.indexOf(this.SearchEngines[i].Authority) != -1){
								
			Resume		= true;
			ParamName	= this.SearchEngines[i].QueryParamName;
			
			break;
		}
	}
				
	if(Resume && this.URI.query && this.URI.query != null){
	
		for(var Key in this.URI.queryKey){
										
			if(Key == ParamName){
			
				Value	= this.URI.queryKey[Key];
				break;
			}
		}
	}

	return Value;
}

OfferMiner.prototype.SendRequest = function(){

	var	Img				= new Image();
	var	Src				= null;
	var	SEngineParam	= null;
	
	Src		= this.URI.protocol + "://" + this.OMServerURL + this.SiteID + "/" + this.OMGifName + "?OM.cookieID=" + this.CookieID + "&OM.siteID=" + this.SiteID;
	
	if(this.ExVisitorID != null && this.ExVisitorID != "")
		Src	+= "&OM.exVisitorID=" + PrepEncoded(this.ExVisitorID);
	
	if(this.ReferrerDomain == null){
		
		if(this.ReferrerURI != null){
		
			Src	+= "&OM.rDomain=" + document.referrer;
		}
	}
	else{
	
		Src	+= "&OM.rDomain=" + PrepEncoded(this.ReferrerDomain);
	}
		
	if(this.SEngineParam == null){
	
		if(this.ReferrerURI != null){
		
			this.SEngineParam	= this.CollectReferrerSerach();
			
			if(this.SEngineParam != null && this.SEngineParam != "")
				Src	+= "&OM.q=" + PrepEncoded(this.SEngineParam);
		}
	}
	else{
	
		Src	+= "&OM.q=" + PrepEncoded(this.SEngineParam);
	}
	
	if(this.URIPath == null){
	
		if(this.URI != null)
			Src	+= "&OM.uri=" + this.URI.path;
	}
	else{
	
		Src	+= "&OM.uri=" + PrepEncoded(this.URIPath);
	}
	
	if(this.Domain == null){
	
		if(this.URI != null)
			Src	+= "&OM.domain=" + this.URI.authority;
	}
	else{
	
		Src	+= "&OM.domain=" + PrepEncoded(this.Domain);
	}
	
	if(this.OfferClick != null && this.OfferClick != "")
		Src	+= "&OM.offerClick=" + PrepEncoded(this.OfferClick);
	
	if(this.DisplayAd != null && this.DisplayAd != "")
		Src	+= "&OM.displayAd=" + PrepEncoded(this.DisplayAd);
	
	if(this.MailClick != null && this.MailClick != "")
		Src	+= "&OM.mailClick=" + PrepEncoded(this.MailClick);
	
	if(this.ProductBasket != null && this.ProductBasket != "")
		Src	+= "&OM.pb=" + PrepEncoded(this.ProductBasket);
	
	if(this.ProductPurchase != null && this.ProductPurchase != "")
		Src	+= "&OM.pp=" + PrepEncoded(this.ProductPurchase);
	
	if(this.ProductPrice != null && this.ProductPrice != "")
		Src	+= "&OM.ppr=" + PrepEncoded(this.ProductPrice);
	
	if(this.ProductUnit != null && this.ProductUnit != "")
		Src	+= "&OM.pu=" + PrepEncoded(this.ProductUnit);
	
	if(this.ProductView != null && this.ProductView != "")
		Src	+= "&OM.pv=" + PrepEncoded(this.ProductView);
	
	if(this.OnSiteBanner != null && this.OnSiteBanner != "")
		Src	+= "&OM.OSB=" + PrepEncoded(this.OnSiteBanner);
	
	if(this.OnSiteSearch != null && this.OnSiteSearch != "")
		Src	+= "&OM.OSS=" + PrepEncoded(this.OnSiteSearch);
	
	if(this.PPC != null && this.PPC != "")
		Src	+= "&OM.PPC=" + PrepEncoded(this.PPC);
	
	if(this.Currency != null && this.Currency != "")
		Src	+= "&OM.cur=" + PrepEncoded(this.Currency);
	
	if(this.EventType != null && this.EventType != "")
		Src	+= "&OM.evt_t=" + PrepEncoded(this.EventType);
	
	if(this.InvoiceNo != null && this.InvoiceNo != "")
		Src	+= "&OM.inv=" + PrepEncoded(this.InvoiceNo);
	
	if(this.OnSiteBannerDisplay != null && this.OnSiteBannerDisplay != "")
		Src	+= "&OM.OSBD=" + PrepEncoded(this.OnSiteBannerDisplay);
	
	if(this.BasketID != null && this.BasketID != "")
		Src	+= "&OM.bid=" + PrepEncoded(this.BasketID);
	
	if(document.cookie)
		Src	+= "&OM.cookie=" + PrepEncoded(document.cookie);
		
	if(this.URIParams != "")
		Src	+= this.URIParams;
	
	var	Dt	= new Date();
		
	Src	+= "&OM.dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();
	
	Img.src	= Src;	

	//document.write(Src);
	//alert(Src);
};

OfferMiner.prototype.Collect = function(){

	this.SendRequest();
};

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function PrepEncoded(S){

	return encodeURIComponent(unescape(S));
}