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 = '';
    // Show exit confirm status true/false - default = true
    this.showExitConfirm = false;
    // Exit page redirect location
    this.exitPageRedirectLocation = '/exit.php';
    // Exit confirm Message
    this.exit_alert_msg  = '';
    // Tag from AB Split
    this.tag_parameter = '';
    // Optional language for exe file
    this.opt_lang = '';
    // Current skin for disabling
    this.current_skin = '';
    // Download page
    this.downloadPage = 'download.html';
    // Track Image URL
    this.trackImgURL = '';
    // Case ERROR string
    this.errorStringURL = '';
	// Empty member
	this.tr_member = 'empty';
	// Empty creferer
	this.creferer = 'empty';
	
    /**
     * Init function: assign required vars, bind events
     */
    this.init = function(variablesObject){
        this.makeErrorString();
        // Setting tag parameter
        this.tag_parameter = variablesObject.tag;
        // Setting optional language
        this.opt_lang = variablesObject.opt_lang;
        // Show exit confirm message
        this.showExitConfirm = variablesObject.redirect_to_exit_page;
        // Setting tracking image URL
        this.trackImgURL = variablesObject.track_img;
		// Setting member from tracking cookie
		this.tr_member = variablesObject.member;
		// Setting creferer
		this.tr_creferer = variablesObject.creferer;
	
        // DOM ready preparing
        $(document).ready(function() {
            tracking.trackImg();
            // Disabling exit confirm for form
            $("form").submit(function(){
                tracking.showExitConfirm = false;
			});
            // Disasbling exit confirm for internal links
            $("a[rel!='external']").click(function(event){
                tracking.showExitConfirm = false;
			});
            //  Enabling exit confirm for external links
            $("a[rel='external']").click(function(event){
                tracking.showExitConfirm = true;
			});
            // Calling to download with parameter name
            $("a[name!='']").click(function(event){
                event.preventDefault();
                var _name = $(this).attr('name');
                var _page = tracking.base_dp.split('+')[3];
                if(_page == 'download'){
                    tracking.directDownload(_name);
                }else{
                    tracking.poputDownloadAndRedirect(_name);    
                }
                
			});
            // Binding event for alert pop
            $('#ap_dlok').click(function (e) {
                e.preventDefault();
                tracking.poputDownloadAndRedirect('alertpop');
            });
        });
        // Unload events
        $(window).unload(function(){
            tracking.exitPageRedirect();
        });
    };
    /** 
   * 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.showExitConfirm = false;
            _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.showExitConfirm = false;
            _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;
    };
    /**
     * Exit method checks if need to display confirm msg and redirect to exit
     */
    this.exitPageRedirect = function(){
        // Aborting function for nopop skin and disabled exitConfirm
        if(this.current_skin == 'nopop' || this.showExitConfirm == false) return false;
        _question = this.exit_alert_msg;
        if(confirm(_question)){
            window.open(this.exitPageRedirectLocation,"","toolbar, location=yes, width=1024, height=728, menubar=yes, status=yes, resizable=yes, scrollbars=yes, directories=yes");
        }
        return true;
    };
    /**
     * Impression counter method
     */
    this.trackImg = function() {
		var _url = this.trackImgURL;
        if(_url == '' || _url == 'undefined') 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;
    };
};

function del_cookie(name) { document.cookie = name +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';};

