/* ******************************* */
/*          COOKIE TRACKING		   */
/* ******************************* */

function CookieTracking() {
	this._init_track_vars();
	
	if( this._is_cookie( this._track_name ) ){
		this._track_value = this._get_cookie( this._track_name );
	}
	
}

CookieTracking.prototype._track_name;
CookieTracking.prototype._track_value;
CookieTracking.prototype._track_regexp;
CookieTracking.prototype._track_is_set;


CookieTracking.prototype.execute = function () {
	var track = this._get_tracking_by_url();
	if( track ){
		this._set_cookie( this._track_name, track );
	}
};


CookieTracking.prototype._init_track_vars = function (){
	this._track_name = "track";
	this._track_value = 0;
	this._track_regexp = new RegExp(/^\d+$/);
	this._track_is_set = false;
};

CookieTracking.prototype._is_cookie = function ( name ) {
	this._track_is_set = this._get_cookie( name ).match( this._track_regexp ) != null;
	return this._track_is_set;
};

CookieTracking.prototype._get_cookie = function ( name ) {
	return clib_get_cookie ( name );
};

CookieTracking.prototype._set_cookie = function ( name, value ) {
	var d = new Date();
	d.setMinutes( d.getMinutes() + 5 );

	var hostname = window.location.hostname;
	
	clib_set_cookie ( name , value, d, '/', hostname );
};


CookieTracking.prototype._get_tracking_by_url = function () {
	var neoUrl = new RegExp("-(\\d+)\\.html$");
	var url = window.location.href.split('?');

	// new style url 
	if( result = url[0].match( neoUrl ) ){
		return result[1];
	}
	// old style url 
	else {
		if( url.length == 2 ){
			var args = url[1].split('&');
			
			var pattern = new RegExp("i_campaign=(\\d*)");
			for(var index = 0; index < args.length; index++ ){
				if( result = args[index].match( pattern ) ){
					return result[1];
				}
			}
		}
	}
	
	return false;
};