var Ridemakerz = Class.create({

    /**
     * Ridemakerz.initialize( container )
     * constructor.
     * The constructor adds google analytics and sets the required Flash version.
     */
    initialize: function() {

        // Flash version information
        this.requiredMajorVersion = 9;
        this.requiredMinorVersion = 0;
        this.requiredRevision = 124;

        this.prodGoogleAnalyticsId = "UA-5449604-2";
        this.qaGoogleAnalyticsId = "UA-5509274-3";

        var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

        this.pageTracker = null;

        Event.observe(window, 'load', this.start.bind(this));       
    },

    start: function() {
        try {
            this.pageTracker = _gat._getTracker(this.getGoogleAnalyticsId());
            this.pageTracker._trackPageview();
        } catch (err) {  }
    },

    getGoogleAnalyticsId: function() {
        if (document.URL.indexOf("play.ridemakerz.com") >= 0) {
            return this.prodGoogleAnalyticsId;
        } else {
            return this.qaGoogleAnalyticsId;
        }
    },

    trackPageview: function(url) {
        try {
            this.pageTracker._trackPageview(url);
        } catch(err) {  }
    },

    // Show swf if minimum flash version is met.  Otherwise, show update alert
    embedRidemakerzSwf: function(swfContainer, flashVersionMessageContainer) {
        var flashVersionString = this.requiredMajorVersion + "." + this.requiredMinorVersion + "." + this.requiredRevision;
        if (swfobject.hasFlashPlayerVersion(flashVersionString)) {
            swfobject.embedSWF("/go/ridemakerz.swf", swfContainer, "800", "450", flashVersionString);
        } else {
            document.getElementById(flashVersionMessageContainer).innerHTML = flashVersionString + " ";
            $('rzupdate').setStyle({display:'inline'});
        }
    },

    createCookieIfNotExists: function(name, value, days) {
        if (this.readCookie(name) == null) {
            this.createCookie(name, value, days);
        }
    },

    createCookie: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        } else {
            var expires = "";
        }
        document.cookie = name + "=" + value+expires + "; path=/";
    },

    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 null;
    },

    eraseCookie: function(name) {
        this.createCookie(name, "", -1);
    }
    
});

var ridemakerz = new Ridemakerz();
ridemakerz.createCookieIfNotExists('referrer_url', document.referrer, 1);
ridemakerz.createCookieIfNotExists('entry_url', document.location, 1);
