

if( typeof(Domestika) != "object") {
    Domestika = {}
}

if( typeof(Domestika.Analytics) != "object" ) {
    Domestika.Analytics = {}
}

Domestika.Analytics.track = function(domain_name) {

    var owa_params = new Object();
    owa_params["site_id"] = domain_name;
    /**
     * Logs Page View
     *
     * Takes owa_param object which is defined by the logging tag.
     *
     * @param owa_params Object
     */
    var owa_pv = new OWA.pageView(owa_params);
    owa_pv.log();
};


/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

};


/**
 * Javascript Tracking Library
 *
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version		$Revision$
 * @since		owa 1.0.0
 */

var OWA = {};

OWA.log = function() {

	this.id = '';
};

OWA.log.prototype = {

    // private method for issuing logging request
    _makeAjaxRequest : function (properties) {

    	var bug;
    	var get;
    	var init;

    	url = this._assembleRequestUrl(properties);

		if (window.XMLHttpRequest){

			// If IE7, Mozilla, Safari, etc: Use native object
			var ajax = new XMLHttpRequest()

		}

		else {

			if (window.ActiveXObject){

		          // ...otherwise, use the ActiveX control for IE5.x and IE6
		          var ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}

		}


		ajax.open("GET", url, false);
		ajax.send(null);

		// Uninitialize variable.
		init = null;

		return;
    },

    // private method for issuing logging request
    _makeRequest : function (properties) {

    	var bug;
    	var url;

    	url = this._assembleRequestUrl(properties);

	   	bug = "<img src=\"" + url + "\" height=\"1\" width=\"1\">";

	   	document.write(bug);

        return;
    },

    _assembleRequestUrl : function(properties) {

    	var get;
    	var log_url;

    	get = '';

   		log_url = 'http://track.domestika.com/public/log.php?';

    	//assemble query string
	    for(param in properties) {  // print out the params


			value = '';

	  		if (typeof properties[param] != 'undefined') {

    			value = Url.encode(this._base64_encode(properties[param]+''));

	    	} else {

    			value = '';

    		}

    		get = get + "owa_" + param + "=" + value + "&";
		}

		// add some radomness for cache busting
		return log_url + get + Math.round(100*Math.random());

    },

    _base64_encode : function(decStr) {

		  var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
		  var bits;
		  var dual;
		  var i = 0;
		  var encOut = '';

		  while(decStr.length >= i + 3) {
		    bits = (decStr.charCodeAt(i++) & 0xff) <<16 |
		           (decStr.charCodeAt(i++) & 0xff) <<8 |
		            decStr.charCodeAt(i++) & 0xff;

		    encOut += base64s.charAt((bits & 0x00fc0000) >>18) +
		              base64s.charAt((bits & 0x0003f000) >>12) +
		              base64s.charAt((bits & 0x00000fc0) >> 6) +
		              base64s.charAt((bits & 0x0000003f));
		  }

		  if(decStr.length -i > 0 && decStr.length -i < 3) {
		    dual = Boolean(decStr.length -i -1);

		    bits = ((decStr.charCodeAt(i++) & 0xff) <<16) |
		           (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);

		    encOut += base64s.charAt((bits & 0x00fc0000) >>18) +
		              base64s.charAt((bits & 0x0003f000) >>12) +
		              (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
		              '=';
		  }

		  return(encOut);
		}

};

// OWA Page View object /////////////////////////////////////

OWA.pageView = function(caller_params) {

	this.properties = new Object();

	for(param in caller_params) {  // print out the params

		this.properties[param] = caller_params[param];

    }

	this._setProperties();

	return;
};

OWA.pageView.prototype = {

	// public method for setting logging request properties
    _setProperties : function () {

		this.properties["event"] = "base.page_request";
    	this.properties["action"] = "base.processRequest";

		if (typeof this.properties["page_uri"] == 'undefined') {
			this.properties["page_url"] = document.URL;
		}
		if (typeof this.properties["page_title"] == 'undefined') {
			this.properties["page_title"] = document.title;
		}

		if (typeof this.properties["referer"] == 'undefined') {
			this.properties["referer"] = document.referrer;
		}

        return;
    },

    log : function() {

    	logger = new OWA.log;
    	return logger._makeRequest(this.properties);

    }

};


