// JavaScript Document
// dependencies: jquery
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17900492-1']);
if (window.location.host.match(/\.tilibags\.com/)) {
	_gaq.push(['_setDomainName', '.tilibags.com']);
} else {
	_gaq.push(['_setDomainName', 'none']);
}
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setAllowHash', false]);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

document.observe("dom:loaded", function() {
	domains = [
		'tilibags.com',
		'tilibags.3dcartstores.com'
	];

	var links = $$("a");
	links.each(function(link) {
		if (link.host != window.location.host) {
			for (var i=0; i<domains.length; i++) {
				var a = domains[i];
				var b = link.host.substr(link.host.length - a.length, link.host.length);
				if (a == b) { 
					Event.observe(link, 'click', function(event) {
						_gaq.push(['_link', this.href]);
						Event.stop(event);
					});
					return true;
				}
			}
		}
	});

	var forms = $$("form");
	forms.each(function(form) {
		var host;
		if (host = form.action.match(/^.*\/{2}([^\/:]*)/)) {
			host = host[1];
		} else {
			return false;
		}
		if (host != window.location.host) {
			for (var i=0; i<domains.length; i++) {
				var a = domains[i];
				var b = host.substr(host.length - a.length, host.length);
				if (a == b) {
					Event.observe(form, 'submit', function(event) {
						_gaq.push(['_linkByPost', this]);
					});
					return true;
				}
			}
		}
	});
});

