function Trck() {
	this.member = "empty";
	this.profile = "";
	this.creferer = "";
    this.tagtrack = "";

	this.readCookie = function(n) {
		var nameEQ = n + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return false;
	}

	this.initFCookie = function() {
		var bc = this.readCookie('banner_domainclick');
		if (!bc) {
			bc = this.readCookie('banner_click');
			if (!bc) return false;
		}

        // tag tracking cookie
        var tag = this.readCookie('tagtrack');
        if(tag != false) {
            this.tagtrack = tag;
        }

		bc = bc.replace( /%3B/g, ';');
		bc = bc.replace( /%3A/g, ':');

		var bca = bc.split(",");
		this.member = bca[0];
		this.profile = bca[2];
		this.creferer = bca[4];
		return true;
	}

	this.init = function(m, p) {
		this.member = m;
		this.profile = p;
	}

	this.getQuery = function() {
		p="member="+this.member;
		p+=(this.profile!="" ? "&profile="+this.profile: "");
		p+=(this.creferer!="" ? "&creferer="+this.creferer: "");
		return p;
	}
}


var tracking = new function(){
    // Full path to Setup__.exe
    this.base_url = '';
    // Base download place e.g. 'language+template+skin+current_page'
    this.base_dp = '';
    // Tag from AB Split
    this.tag_parameter = '';
    // Optional language for exe file
    this.opt_lang = '';
    // source
    this.csrc = '';
    // tag
    this.ctag = '';
    // Download page
    this.downloadPage = '';
    // is download page
    this.isDownloadPage = 0;
    // Track Image URL
    this.trackImgURL = '';
    // Cookie domain
    this.cookie_domain = '';
    // Case ERROR string
    this.errorStringURL = '';
	this.del_cookie = function(name,domain){
		document.cookie = name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;' + ( ( domain ) ? ";domain=" + domain + ";" : "" );
	};

    var cref, i, cref_part;

	this.trk_mems = new Trck();
	if (this.trk_mems.initFCookie()) {
	      cref = this.trk_mems.creferer.split(';');
	      for (i=0; i < cref.length; i++) {
	           cref_part = cref[i].split(':');
	           switch (cref_part[0]) {
	               case 'source':
	                   this.csrc = cref_part[1];
	                   break;
	               case 'tag':
	                   this.ctag = cref_part[1];
	                   break;
	           }
	      }
	}

    if(this.ctag == '' && this.trk_mems.tagtrack != '') {
       this.ctag = this.trk_mems.tagtrack;
    }

	if (typeof member!="undefined" && typeof profile!="undefined") {
			this.trk_mems.init(member, profile);
	}

	
	
	
    /**
     * Init function: assign required vars, bind events
     */
    this.init = function(){
        this.makeErrorString();
        // DOM ready preparing
        $(document).ready(function() {
    		if( tracking.trackImg() ){
    			tracking.del_cookie('make_impr',tracking.cookie_domain);
    		}
            // Calling to download with parameter name
            $("a[name!='']").click(function(event){
                event.preventDefault();
                var _name = $(this).attr('name');
                if(tracking.isDownloadPage) {
                    tracking.directDownload(_name);
                } else {
                    tracking.poputDownloadAndRedirect(_name);
                }
			});
        });
    };
    this.cencelExit = function() {
        if(typeof(exit) != 'undefined') {
            exit.showExitConfirm = false;
        }
    };
    /** 
   * Combine error string
   */  
    this.makeErrorString = function(){
        this.errorStringURL = this.base_url+'?creferer=dp:'+this.base_dp+'+error';
    };
    /** 
     * Direct Download Method
     * @param dp{string} - download place
     */
    this.directDownload = function(dp){
        try{
            this.cencelExit();
            _url = this.prepareURL(dp);
            window.location = _url;
            return true;
        }
        catch(ex){
          window.location = this.errorStringURL;
        }
        return false;
    };
    /** 
     * Download via popup and redirect parent to download page Method
     * @param dp{string} - download place
     */
    this.poputDownloadAndRedirect = function(dp){
        try{
            this.cencelExit();
            _url = this.prepareURL(dp);
            window.open(_url, "Download","width=1,height=1,top=0,left=0");
            window.location=this.downloadPage;
            return true;
        }
        catch(ex){
            window.location = this.errorStringURL;
        }
        return false;
    };
    /**
     * Impression counter method
     */
    this.trackImg = function() {
		var _url = this.trackImgURL+'&'+this.trk_mems.getQuery();
        if(_url == '' || _url == 'undefined' || this.trk_mems.readCookie('make_impr') == '' ) return false;
        try {
			var img=document.createElement('img');
			img.src = _url;
			img.setAttribute('width', 0);
			img.setAttribute('height', 0);
			img=document.body.appendChild(img);
			img.style.visibility="hidden";
			img.style.display="none";
			return true;
		}
		catch (ex) { }
		return false;
	};
    /**
     * Utility function for creating download exe query
     * @param dp{string} - download place
     */
    this.prepareURL = function(dp){
        var _url = this.base_url;
        _url  += '?creferer=dp:'+this.base_dp+'+'+dp;
        if(this.tag_parameter){
            _url += ";tag:"+this.tag_parameter+";";
        }
        if(this.opt_lang){
            _url = "&opt_lang="+this.opt_lang;
        }
        return _url;
    };
};