
	
var domain_to_catch = 'smartmovies.net';
var tracker_url = 'http://fr.smartmovies.net/d9538/tracker/videos.js';

function SM_harvest_videos() {
	
	// get all links on the current page
	links			= document.getElementsByTagName('A');
	videos			= [];
	known_videos	= [];

	// loop over the links
	for (i = 0 ; i < links.length; i++) {
		
		// if the link targets our site, process it, otherwise continue to the next one
		if (links[i].href && links[i].href.indexOf(domain_to_catch) > 0) {
	
			module_videos	= false;
			view_video		= false;
			
			// get the URL parameters (cf function parseUri() below)
			uri = parseUri(links[i].href);
			
			folders = uri.path.split('/');
			
			// loop over all 'folders' in the url to see if we're in the PDV case (ie module=videos and view=video)
			j = 0;
			while (false == module_videos && j < folders.length ) {
				
				if (folders[j] == 'videos') {
					
					module_videos = true;
					
					// view is always the next parameter after the module
					if (folders[j+1] && folders[j+1] == 'video') {
						
						view_video = true;
					}
				}
				j++;
			}
			
			// we only care about PDVs
			if (module_videos && view_video) {
				
				element		= folders.pop();
				video		= element.split('-');
				video_id	= video.shift();

				// if the video has been already linked on the page, add it to the videos array
				if (typeof known_videos[video_id] == 'undefined') {
					
					known_videos[video_id]	= true;
					videos.push(video_id);
				}
			}
		}
	}
	
	// no need to send a query to say "I've found ... nothing"
	if (0 == videos.length) {
		
		return false;
	}
	
	// build the tracker URL
	url = tracker_url + '?videos=' + videos.join('-');
	
	// seems to be the only solution to call an external file on a different domain without having "security" permissions issues
	document.write('<script type="text/javascript" src="'+url+'"><\/script>');
}

/*
parseUri 1.2.1
(c) 2007 Steven Levithan <stevenlevithan.com>
see http://blog.stevenlevithan.com/archives/parseuri
MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;
	
	while (i--) uri[o.key[i]] = m[i] || "";
		
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	return uri;
};
	
parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
parseUri.options.strictMode = true;

SM_harvest_videos();

