/**
 * The EroTracking javascript
 */
function EroTracking() {
    
    this.cookieDomain = function() {
        var array   = window.location.hostname.split(".");
        return array[array.length-2]+"."+array[array.length-1];
    }
    
    this.setCookie = function(name, value, expires, path, domain, secure){
        
        var today = new Date();
        today.setTime( today.getTime() );
        if ( expires )
        {
        expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date( today.getTime() + (expires) );
        
        document.cookie = name + "=" +escape( value ) +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
    
    }
    
    this.readCookie = function (name) {
        var nameEQ = name + "=";
        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 "";
    }

    this.setTracking = function(pname) {
        if (pname == null){pname = 'cid';}
        var track = this.gup(pname);
        
        if (track !="") {
            this.setCookie('erotracking',track,365,"/",this.cookieDomain(),false);
        }
        
    }
    
    this.registerConversion = function(isreal) {
        var skey = "";
        var ck = unescape(this.readCookie('erotracking'));
        if (ck !="") {
            ckey = 'erotracking' + isreal + '_complete_' + this.readCookie('erotracking');
            if (unescape(this.readCookie(ckey)) !=ck) {
                this.setCookie(ckey,ck,365,"/",this.cookieDomain());
                document.writeln('<s'+'cr'+'ipt language="javascript" src="'+this.checkProtocol()+ 'tracker.ero-advertising.com/tracking/conv.php?cid='+ck+'&isreal='+isreal+'&skey='+skey+'"></s'+'cript'+'>');
            }
        }
    }

    this.gup = function(name){
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null ){
            try{
                var results = regex.exec( top.location.href );
                if( results == null ){return "";}
            }catch(_e){return "";}
        }
        return results[1];
    }
    
    this.checkProtocol = function(){
        return ("https:" == document.location.protocol) ? "https://" : "http://";
    }
}

// Are we on either splash1, or splash2?
if(window.location.pathname.search("splash2") >= 0 || window.location.pathname.search("splash1") >= 0) {
    
    // We are, define some cookie functions to make life easier on us.
    function createCookie(name,value,days,domain) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else {
            var expires = "";
        }
        
        if(!domain) {
            var array   = window.location.hostname.split(".");
            var domain  = array[array.length-2]+"."+array[array.length-1];
        }
        
        var domain = "; domain=" + domain;
        document.cookie = name+"="+value+expires+domain +";path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        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 null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    
    function getParam( name )
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null ) {
            return "";
        } else {
            return results[1];
        }
    }
    
    var toggleEroMap = function() {
        jQueryForExternals('#maptoggle').trigger('click');
    };
    
    
    $(function() {
        if(window.location.pathname.search("kaartje") >= 0) {
            var t=setTimeout("toggleEroMap()",300);
        }
    });
    
    // Get the trackingID
    var cid = getParam('cid');
    
    // Did we get an actual trackingID?
    if(cid !== '') {
        
        // We did! Awesome, now let's set a new cookie indicating we are tracking the CID (the value).
        createCookie('ETrackingID', cid, 1);
        
        // And finally, let's load our tracking for the splash index
        try {
            var EA = new EroTracking();
            var qparam = "cid";
            EA.setTracking(qparam);
        } catch(err) {}
    }
} else {
    function createCookie(name,value,days,domain) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else {
            var expires = "";
        }
        
        if(!domain) {
            var array   = window.location.hostname.split(".");
            var domain  = array[array.length-2]+"."+array[array.length-1];
        }
        
        var domain = "; domain=" + domain;
        document.cookie = name+"="+value+expires+domain +";path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        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 null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    
    // Now, let's create a new cookie indicating we want to allow tracking
    if(readCookie('ETrackingID') !== null) {
        
        // And finally, let's load our tracking for the splash index
        document.write(unescape("%3Cscript src='"+ ("https:" == document.location.protocol ? "https://" : "http://") +"tracker.ero-advertising.com/tracking/track.js' type='text/javascript'%3E%3C/script%3E"));
        try {
            var EA = new EroTracking();
            EA.registerConversion(1);
        } catch(err) {}
    }
}

