// Tracking JS

function setTracking()
{
	var pageUrl      = window.location.href.replace("http://", "");
	var urlParts     = pageUrl.split("/mobile-phones/");
	var urlPartsTwo  = urlParts[1].split("/");
	var handsetName  = urlPartsTwo[0];
	var section      = urlPartsTwo[1].replace(".html", "");
	var sectionParts = section.split("?");

	// This is to strip any query string.
	if(sectionParts.length != 1)
	{
		section = sectionParts[0];
	}

	var handsetType = document.getElementById("htype").value;

	var handsetTypeTag = '';

	if(handsetType == 'mbb')
	{
		handsetTypeTag = 'mbb';
	}
	else
	{
		handsetTypeTag = 'deal';
	}

	var trackingCode = '';

	if(section == 'description') {
		trackingCode = '/handset-page/' + handsetName;
	}
	else if(section == 'pay-monthly') {
		trackingCode = '/paym-' + handsetTypeTag + '-page/' + handsetName;
	}
	else if(section == 'pay-as-you-go') {
		trackingCode = '/payg-' + handsetTypeTag + '-page/' + handsetName;
	}
	else if(section == 'reviews') {
		trackingCode = '/reviews-page/' + handsetName;
	}
	else if(section == 'specifications') {
		trackingCode = '/specifications-page/' + handsetName;
	}
	else if(section == 'product-updates') {
		trackingCode = '/product-updates-page/' + handsetName;
	}
	
	urchinTracker(trackingCode);
}

addLoadListener(setTracking);

document.onclick = function(e)
{
	var thisBrowser = identifyBrowser();

	var target = e ? e.target : window.event.srcElement;

	while (target && !/^(a|body)$/i.test(target.nodeName))
	{
		target = target.parentNode;
	}

	if (target && target.getAttribute('rel'))
	{
		// If no width or height is specified.
		if(target.rel == 'globalPopup')
		{
			if(thisBrowser == 'ie6')
			{
				var target = e ? e.target : window.event.srcElement;
				window.open(target.href,'globalIE6Popup','width=500,height=600');
				return false;
			}
			else
			{
				globalPopup(target.href);
				return false;
			}
		}
		else
		{
			// If a width and height is given.
			var targetRel = target.rel.split("-");

			if(thisBrowser == 'ie6' && targetRel[0] == 'globalPopup')
			{
				var target = e ? e.target : window.event.srcElement;
				window.open(target.href,'globalIE6Popup','width=500,height=600');
				return false;
			}
			else
			{
				if(targetRel[0] == 'globalPopup')
				{
					var width    = parseInt(targetRel[1]);
					var height   = parseInt(targetRel[2]);
					var linkHref = target.href;

					if(targetRel[3])
					{
						if(targetRel[3] == 'div')
						{
							var linkHrefRaw = target.href.split('/');
							linkHref = linkHrefRaw[linkHrefRaw.length-1];
						}
					}

					globalPopup(linkHref,width,height);
					return false;
				}
			}
		}
	}
}