var gaExtended;
if (!window.console) {
    window.console = {};
}
console.log = console.log || function () {};
console.info = console.info || console.log;
console.dir = console.dir || console.log;
console.group = console.group || console.log;
console.groupEnd = console.groupEnd || function () {};
(function ($) {
	
	gaExtended = {
		_: {
			_options: {
                debug: false,
                delay: 1000
            },
			_setCampValues :{
				days :183,
				source:null,
				name:null,
				medium:null,
				content:null
			},
			_setCampSource :{
				days :183,
				value: null
			},
			_setCampMedium :{
				days :183,
				value: null
			},
			_setCampName :{
				days :183,
				value: null
			},
			_setCampContent :{
				days :183,
				value: null
			}
			
		},
		
		readcookie: function () {
			var name = arguments[0];
			var re = new RegExp("(?:^| )" + name + "=([^;]*)", "i");
			var matches = document.cookie.match(re);
			return matches && matches.length == 2 ? matches[1] : null;
		},
		savecookie:	function () {
			var name = arguments[0];
			var value = arguments[1];
			var days = arguments[2];
			var date = new Date;
			date.setTime(date.getTime() + (typeof days != "undefined" ? days : 30) * 24 * 60 * 60 * 1000);
			var expires = "; expires=" + date.toGMTString();
			var cookiecontent = name + "=" + value + expires + "; path=/; domain=." + document.domain.split(".").slice(1).join(".");
			if (gaExtended._._options.debug)
				console.log("gaExtended: " + cookiecontent);
			document.cookie = cookiecontent;
		},
		_setCampValues:function(){
			var a = arguments[1];
			var utmz = gaExtended.readcookie("__utmz");
			if (utmz.match(/utmcsr/)) 
				utmz = utmz.replace(/utmcsr=[^\|]*/, "utmcsr="+a.source);
			else utmz += "|utmcsr="+a.source;
			if (utmz.match(/utmcmd/)) 
				utmz = utmz.replace(/utmcmd=[^\|]*/, "utmcmd="+a.medium);
			else utmz += "|utmcmd="+a.medium;
			if (utmz.match(/utmcct/)) 
				utmz = utmz.replace(/utmcct=[^\|]*/, "utmcct="+a.content);
			else utmz += "|utmcct="+a.content;
			if (utmz.match(/utmccn/)) 
				utmz = utmz.replace(/utmccn=[^\|]*/, "utmccn="+a.name);
			else utmz += "|utmccn="+a.name;
			gaExtended.savecookie("__utmz", utmz, a.days);
		},
		_setCampSource: function(){
			var a = arguments[1];
			var utmz = gaExtended.readcookie("__utmz");
			if (utmz.match(/utmcsr/)) 
				utmz = utmz.replace(/utmcsr=[^\|]*/, "utmcsr="+a.value);
			else utmz += "|utmcsr="+a.value;
			gaExtended.savecookie("__utmz", utmz, a.days);
		},
		_setCampMedium: function(){
			var a = arguments[1];
			var utmz = gaExtended.readcookie("__utmz");
			if (utmz.match(/utmcmd/)) 
				utmz = utmz.replace(/utmcmd=[^\|]*/, "utmcmd="+a.value);
			else utmz += "|utmcmd="+a.value;
			gaExtended.savecookie("__utmz", utmz, a.days);
		},
		_setCampContent: function(){
			var a = arguments[1];
			var utmz = gaExtended.readcookie("__utmz");
			if (utmz.match(/utmcct/)) 
				utmz = utmz.replace(/utmcct=[^\|]*/, "utmcct="+a.value);
			else utmz += "|utmcct="+a.value;
			gaExtended.savecookie("__utmz", utmz, a.days);
		},
		_setCampName: function(){
			var a = arguments[1];
			var utmz = gaExtended.readcookie("__utmz");
			if (utmz.match(/utmccn/)) 
				utmz = utmz.replace(/utmccn=[^\|]*/, "utmccn="+a.value);
			else utmz += "|utmccn="+a.value;
			gaExtended.savecookie("__utmz", utmz, a.days);
		}
	};
	Array.prototype.gaExtended = function () {
        this._push = Array.prototype.push;
        this.push = function () {
            for (v = 0; v < arguments.length; v++) {
                var a = arguments[v];
                var tracker = a[0] ? a[0].split(".") : [];
                var method = tracker.length & 2 ? tracker[1] : tracker[0];
                if (gaExtended[method] && method !== "_trackPageview" || method === "_trackPageview" && typeof a[1] === "object") {
                    if (a.length > 1 && typeof a[1] === "object") {
                        a[1] = $.extend({}, gaExtended._[method], a[1]);
                    } 
                    else if (a.length > 1 ){
                    	if(gaExtended._[method])
                    		a[1] = $.extend({}, gaExtended._[method], {value:a[1]});
                    	else
                    		a[1] = $.extend({},{value:a[1]});
                    }
                    else {
                        a[1] = gaExtended._[method];
                    }
                    a[1].tracker = tracker.length & 2 ? tracker[0] + "." : "";
                    if (gaExtended._._options.debug) {
                        console.group("gaExtended." + method);
                        console.dir(a[1]);
                        console.groupEnd();
                    }
                    try {
                    	gaExtended[method].apply(this, a);
                    } catch (e) {
                        console.log("gaExtended: " + e.message);
                    }
                } else {
                    if (gaExtended._._options.debug) {
                        console.group("ga." + a[0]);
                        a[1] ? console.dir(a[1]) : "";
                        console.groupEnd();
                    }
                    this._push.call(this, a);
                }
            }
            return arguments.length;
        };
    };
})(jQuery);
var _gaq = [];
_gaq.gaExtended();
