var $jq = jQuery.noConflict();

// Use jQuery via $jq(...)
$jq(document).ready(function(){
	
	/*
	Track pdf downloads
	example:
	http://honda.synkron.com/graphics/Synkron-Library/Honda-Sverige/Files/Models/Broschyrer/1_Modeller/Accord.pdf
	*/
	$jq("a[href*=.pdf]").click(function(){
		$href = $jq(this).attr('href');
		$isdev = self.location.href.indexOf('isdev') != -1;
		if($isdev){
			alert('Tracking click on PDF link:\n'+$href);
		}
		if($href.indexOf('http://honda.synkron.com/graphics/Synkron-Library/Honda-') != -1) {
			$href = $href.replace('http://honda.synkron.com/graphics/Synkron-Library/Honda-','');
			$hrefPath = '/'+$href; // ex; Sverige/Files/Models_Broschyrer_1_Modeller/accord_2011.pdf
			$href = $href.split('/');
			if($href.length > 1) {
				$country = $href.shift(); // ex; Sverige
				$hrefLC = $href.shift(); // should be 'Files'
				$hrefLC = $hrefLC.toLowerCase();
				if($hrefLC == 'files') {
					$pdf = $href.pop(); // ex; accord_2011.pdf
					$path = $href.join('_'); // ex; Models_Broschyrer_1_Modeller
					$path = $path.replace('1_','').replace('2_','').replace('3_','').replace('4_','').replace('5_','');
					$path = $path.replace('6_','').replace('7_','').replace('8_','').replace('9_','').replace('10_','');
					
					trackHondaEvent($country+' - Downloads', $path, $pdf);

					// Mediaminds: Track brochure based on tld
					mediaMindsCode = '';
					if(window.location.href.indexOf('honda.se') != -1) {
						mediaMindsCode = '140580';
					}
					else if(window.location.href.indexOf('honda.no') != -1) {
						mediaMindsCode = '148571';
					}
					else if(window.location.href.indexOf('honda.fi') != -1) {
						mediaMindsCode = '141366';
					}
					else if(window.location.href.indexOf('honda.dk') != -1) {
						mediaMindsCode = '132965';
					}
					if(mediaMindsCode != '') {
						mediaMindsTracking(mediaMindsCode);
					}
				}
			}
		}
	});
	
	
	/*
	$jq(window).bind('resize', function() {
	});
	*/
});

function trackHondaEvent($category, $path, $label){
	$isdev = self.location.href.indexOf('isdev') != -1;
	$hasErrors = false;
	/*
	try {
		if($isdev){
			alert('trackHondaEvent::_trackPageview:\n'+$path);
		}
		pageTracker._trackPageview($path);
	} catch(err) {
		if(self.location.href.indexOf('test') != -1){
			$hasErrors = true;
			alert('ERROR:\ntrackHondaEvent::_trackPageview:\n'+$path+'\n\nError:\n'+err.description);
		}
	}
	*/
	try {
		if($isdev){
			alert('trackHondaEvent::_trackEvent:\nCategory: '+$category+'\nAction: '+$path+'\nLabel: '+$label);
		}
		pageTracker._trackEvent($category, $path, $label);
	} catch(err) {
		if(self.location.href.indexOf('test') != -1){
			$hasErrors = true;
			alert('ERROR:\ntrackHondaEvent::_trackEvent:\nCategory: '+$category+'\nAction: '+$path+'\nLabel: '+$label+'\n\nError:\n'+err.description);
		}
	}
	if($isdev && !$hasErrors){
		alert('SUCCESS:\ntrackHondaEvent: No errors reported.');
	}
}

function mediaMindsTracking($id) {
	$isdev = self.location.href.indexOf('isdev') != -1;
	try {
		if($isdev){
			alert('mediaMindsTracking: '+$id);
		}
		var ebRand = Math.random()+'';
		ebRand = ebRand * 1000000;
		url = 'HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=' + $id + '&rnd=' + ebRand;
		jQuery.getScript(url);
	} catch(err) {
		if(self.location.href.indexOf('test') != -1){
			$hasErrors = true;
			alert('ERROR:\nmediaMindsTracking: '+$id+'\n\nError:\n'+err.description);
		}
	}
	if($isdev && !$hasErrors){
		alert('SUCCESS:\nmediaMindsTracking: '+$id+': No errors reported.');
	}
}

