// just in case we want to read GET parameters
if ( typeof gup === 'undefined' ) {
	function gup ( param_name ) {
		param_name = param_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]" + param_name + "=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		
		if( results == null ) {
			return '';
		} else {
			return results[1];
		}
	}
}

// URLs of the shop shortcuts
var base = 'http://www.wolford.com';
var shop_links = [
	'http://www.wolford.com/shop',
	'http://www.wolford.com/shop/',
	'http://www.wolford.com/index.php?id=27',
	'http://www.wolford.com/?id=27',
	'http://www.wolford.com/index.php?id=161',
	'http://www.wolford.com/?id=161',
	'http://www.wolford.com/index.php?id=277',
	'http://www.wolford.com/?id=277'
];

// adds the tracking code to As and AREAs
$(document).ready( function () {
/*
	if ( gup('testing') != 1 ) {
		return;
	}
*/
	
	$('a').each( function (idx, obj) {
		parse_element_href(obj, $(this));
	} );
	$('area').each( function (idx, obj) {
		parse_element_href(obj, $(this));
	} );
} );

// parses the href attribute of the element and, if necessary add the tracking code
var parse_element_href = function (html_element, jquery_object) {
	var href = jquery_object.attr('href') || '';
	if ( ! href.match(/^https?:\/\//) ) {
		href = html_element.href || '';
	}
	var href = $.trim(href);
	
	// if empty href, return
	if (href == '') {
		return;
	}
	
	// check if href is one of the direct shop shortcuts
	for (i = 0; i < shop_links.length; i++) {
		if (href == shop_links[i]) {
			add_shop_link(jquery_object, '/', href);
			return;
		}
	}
	
	// check if href is a shop shortcut (product, category, etc)
	if ( href.match(/^http:\/\/www\.wolford\.com\/shop\/.+$/) ) {
		add_shop_link(jquery_object, href, href);
		return;
	}

	// check if it's a javascript embedded code
	if ( href.match(/^javascript:/) ) {
		return;
	}
	
	// check if href is an external link
	if ( (href != 'http://www.wolford.com') &&
	     (! href.match(/^http:\/\/www\.wolford\.com\/.+$/)) ) {
		add_external_link(jquery_object, href, href);
		return;
	}
	
	// if none of the above, do nothing
};

var add_shop_link = function (obj, path, real_url) {
	if (path != '/') {
		var parts = path.split('/');
		parts[0] = '';
		parts[1] = '';
		parts[2] = '';
		path = parts.join('/').replace(/\/+/, '/');
	} else {
		path = '/shop';
	}
	add_ga_handler(obj, path, real_url);
};

var add_external_link = function (obj, path, real_url) {
	path = path.replace(/^https?:\/\//, '').replace(/[^\w\.\/\&\?'"]/, '-');
	path = '/external/' + path;
	add_ga_handler(obj, path, real_url);
};

var add_ga_handler = function (obj, path, real_url) {
	path = path.replace(/["']/, '');
	obj.attr('onClick', 'recordOutboundLink(this, "' + path + '");return false;');
};

var recordOutboundLink = function (link, path) {
	try {
		var pageTracker=_gat._getTracker("UA-9781863-1");
		pageTracker._trackPageview(path);
		setTimeout('document.location = "' + link.href + '"', 100);
	} catch(err) {}
};

