HD.util.Analytics.track = function(tracking, values) {		
	if (tracking && _hbPageView) {

		// Update defaults.
		var trackingData = tracking.split(';');
		var category = trackingData[0] || '';
		var pagename = trackingData[1] || 'DarkCustom';
		values = values || {};
		
		// Update the category/pagename with dynamic values as necessary.
		for (var prop in values) {
			var mask = new RegExp('\\$\\{' + prop +  '\\}');
			category = category.replace(mask, values[prop]);
			pagename = pagename.replace(mask, values[prop]);
		}
		
		// Track event.
		if (window._hbPageView) {
			_hbPageView(pagename, category);
			//console.log(pagename + ' = ' + category);
		}
	}
};

// Create tracking configuration.
DC = window.DC || {};
DC.Analytics = {
	Blog: {
		landing: '/Products/Motorcycles/Dark+Custom/Blog;${filter}',
		detail: '/Products/Motorcycles/Dark+Custom/Blog/Article/${id};blog_detail',
		videoPlay: '/Products/Motorcycles/Dark+Custom/Blog/Video+Play/${id};blog_video+play',
		rating: '/Products/Motorcycles/Dark+Custom/Blog/Rating/${id};blog_rating',
		comment: '/Products/Motorcycles/Dark+Custom/Blog/Comment/${id};blog_comment',
		socialFacebook: '/Products/Motorcycles/Dark+Custom/Blog/Social+Share/Facebook/${id};blog_share_facebook',
		socialMySpace: '/Products/Motorcycles/Dark+Custom/Blog/Social+Share/MySpace/${id};blog_share_myspace',
		socialBlogger: '/Products/Motorcycles/Dark+Custom/Blog/Social+Share/Blogger/${id};blog_share_blogger',
		socialWordpress: '/Products/Motorcycles/Dark+Custom/Blog/Social+Share/Wordpress/${id};blog_share_wordpress'
	},
	Bikes: {
		landing: '/Products/Motorcycles/Dark+Custom/Bikes;${filter}',
		faceoffVote: '/Products/Motorcycles/Dark+Custom/Bikes/Faceoff/Vote/${date};bike_faceoff_vote',
		faceoffComment: '/Products/Motorcycles/Dark+Custom/Bikes/Faceoff/Comment/${date};bike_faceoff_comment',
		detail: '/Products/Motorcycles/Dark+Custom/Bikes/Article/${category}/${id};bike_article',
		videoPlay: '/Products/Motorcycles/Dark+Custom/Bikes/Video+Play/${category}/${id};bike_video+play',
		rating: '/Products/Motorcycles/Dark+Custom/Bikes/Rating/${category}/${id};bike_rating',
		comment: '/Products/Motorcycles/Dark+Custom/Bikes/Comment/${category}/${id};bike_comment',
		submitForm: '/Products/Motorcycles/Dark+Custom/Bikes/Post/Form;bike_post_form',
		submitConfirm: '/Products/Motorcycles/Dark+Custom/Bikes/Post/Confirmation/${category};bike_post_confirmation',
		modelView: '/Products/Motorcycles/Dark+Custom/Bikes/${model}/${view};bike_${model_pn}',
		socialFacebook: '/Products/Motorcycles/Dark+Custom/Social+Share/Facebook/${id}',
		socialMySpace: '/Products/Motorcycles/Dark+Custom/Social+Share/MySpace/${id}',
		socialBlogger: '/Products/Motorcycles/Dark+Custom/Social+Share/Blogger/${id}',
		socialWordpress: '/Products/Motorcycles/Dark+Custom/Social+Share/Wordpress/${id}',
		socialTwitter: '/Products/Motorcycles/Dark+Custom/Social+Share/Twitter/${id}'
	},
	BikesPromo: {
		socialFacebook: '/Products/Motorcycles/Dark+Custom/Social+Share/Facebook/${category}',
		socialMySpace: '/Products/Motorcycles/Dark+Custom/Social+Share/MySpace/${category}',
		socialTwitter: '/Products/Motorcycles/Dark+Custom/Social+Share/MySpace/${category}',
		socialBlogger: '/Products/Motorcycles/Dark+Custom/Social+Share/Blogger/${category}',
		socialWordpress: '/Products/Motorcycles/Dark+Custom/Social+Share/Wordpress/${category}',
		videoPlay: '/Products/Motorcycles/Dark+Custom/Bikes/${model}/video'
	},
	Videos: {
		landing: '/Products/Motorcycles/Dark+Custom/Vids;${filter}',
		videoPlay: '/Products/Motorcycles/Dark+Custom/Blog/Video+Play/${id}/vid;blog_video+play',
		rating: '/Products/Motorcycles/Dark+Custom/Blog/Rating/${id}/vid;blog_rating',
		comment: '/Products/Motorcycles/Dark+Custom/Blog/Comment/${id}/vid;blog_comment'
	},
	Art: {
		landing: '/Products/Motorcycles/Dark+Custom/Art;art_landing_page',
		detail: '/Products/Motorcycles/Dark+Custom/Art/Detail/${id};art_detail_page',
		videoPlay: '/Products/Motorcycles/Dark+Custom/Art/Video+Play/${id};art_video+play',
		rating: '/Products/Motorcycles/Dark+Custom/Art/Rating/${id};art_rating',
		comment: '/Products/Motorcycles/Dark+Custom/Art/Comment/${id};art_comment',
		submitForm: '/Products/Motorcycles/Dark+Custom/Art/Submit/Form;art_submit_form',
		submitConfirm: '/Products/Motorcycles/Dark+Custom/Art/Submit/Confirmation;art_submit_confirmation'
	},
	Events: {
		landing: '/Products/Motorcycles/Dark+Custom/Events;events_landing_page'
	},
	Downloads: {
		landing: '/Products/Motorcycles/Dark+Custom/Downloads/Wallpapers;downloads_landing_page',
		get: '/Products/Motorcycles/Dark+Custom/Downloads/Wallpapers/${resolution};downloads_get_${name}'
	},
	Social: {
		socialFacebook: '/Products/Motorcycles/Dark+Custom/Social+Share/Facebook/${title}',
		socialMySpace: '/Products/Motorcycles/Dark+Custom/Social+Share/MySpace/${title}',
		socialBlogger: '/Products/Motorcycles/Dark+Custom/Social+Share/Blogger/${title}',
		socialWordpress: '/Products/Motorcycles/Dark+Custom/Social+Share/Wordpress/${title}',
		socialTwitter: '/Products/Motorcycles/Dark+Custom/Social+Share/Twitter/${title}',
		followOverlay: '/Products/Motorcycles/Dark+Custom/Follow',
		followRss: '/Products/Motorcycles/Dark+Custom/Follow/RSS',
		followFacebook: '/Products/Motorcycles/Dark+Custom/Follow/Facebook',
		followTwitter: '/Products/Motorcycles/Dark+Custom/Follow/Twitter',
		followYouTube: '/Products/Motorcycles/Dark+Custom/Follow/YouTube',
		followMyYahoo: '/Products/Motorcycles/Dark+Custom/Follow/MyYahoo',
		followGoogle: '/Products/Motorcycles/Dark+Custom/Follow/Google',
		followMyAOL: '/Products/Motorcycles/Dark+Custom/Follow/MyAOL',
		followPageflakes: '/Products/Motorcycles/Dark+Custom/Follow/Pageflakes',
		followNewsgator: '/Products/Motorcycles/Dark+Custom/Follow/Newsgator',
		followBloglines: '/Products/Motorcycles/Dark+Custom/Follow/Bloglines',
		followNetvibes: '/Products/Motorcycles/Dark+Custom/Follow/Netvibes'
	},
	General: {
		loginForm: '/Products/Motorcycles/Dark+Custom/Login/Form',
		loginConfirm: '/Products/Motorcycles/Dark+Custom/Login/Confirmation',
		createProfileForm: '/Products/Motorcycles/Dark+Custom/Create+Profile/Form',
		createProfileConfirm: '/Products/Motorcycles/Dark+Custom/Create+Profile/Confirmation',
		sendFriendForm: '/Products/Motorcycles/Dark+Custom/Send+To+A+Friend/Form',
		sendFriendConfirm: '/Products/Motorcycles/Dark+Custom/Send+To+A+Friend/Confirmation'
	},
	ArtRebellion: {}
};

// Set override methods.
HD.util.Player.View.prototype.setPlayerListeners = function() {
	var player = HD.get(this.player.id);
	if(player) {		
		player.addControllerListener('PLAY', 'DC.Player.play');
		player.addControllerListener('ITEM', 'DC.Player.play');
		player.addControllerListener('SEEK', 'DC.Player.play');
		player.addModelListener('TIME', 'DC.Player.time');
		
	} else {
		HD.util.Player.View.prototype.setPlayerListeners();
	}
};
DC.Player = {
	ignore: {},
	started: {},
	play: function(data) {
		var videoId = HD.util.Player.View.prototype.playerContainerID;
		var videoMedia = HD.util.Player.View.prototype.data.media;
		
		if (DC.Player.videoTracked != videoId) {
			DarkCustom.engine.dao.notifyObservers('videoPlayed', videoMedia);
			DC.Player.videoTracked = videoId;		
		}
		
	},
	time: function(data) {
		console.log(data);
		if (data.id && data.position >= 0 && data.position <= 0.2 && !DC.Player.ignore[data.id] && DC.Player.started[data.id]) {
			// Inject the videoPlayed event into the main dao engine.
			var player = HD.util.Player.View.views[data.id] || null;
			if (player && player.data && player.data.media) {
				DarkCustom.engine.dao.notifyObservers('videoPlayed', player.data.media);
			}
		
			// Prevent duplicates.
			DC.Player.ignore[data.id] = true;
			setTimeout(function() {
				if (DC.Player.ignore[data.id]) {
					delete DC.Player.ignore[data.id];
				}
			}, 1000);
		}
	}
};

// Set observer handlers.
DarkCustom = window.DarkCustom || {};
DarkCustom.setAnalytics = function() {
	var self = this;
	if (this.analytics) {
		var section = this.analytics.section || '';
		var dateFormat = 'yyyy-mm-dd';
		var trimSection = new RegExp(section+'/','g');
		
		// Fire landing page event.
		if (section.match(/^(Blog|Bikes|Videos|Art|Events|Downloads)$/)) {
			HD.util.Analytics.track(DC.Analytics[section].landing, { filter: 'No_Filter' });
		}
		
		// Create observer.
		var observer = {
			widgets: ['dao', 'view', 'carousel'],
			update: function(eventName, eventData) {
				// Video played.
				if (eventName == 'videoPlayed' && section.match(/^(Blog|Bikes|Videos|Art)$/)) {
					HD.util.Analytics.track(DC.Analytics[section].videoPlay, { id: eventData.mediaId, category: eventData.category.replace(trimSection,'').replace(/\s+/,'+') });
				}
				else if (eventName == 'videoPlayed' && section.match(/^(BikesPromo)$/)) {
					HD.util.Analytics.track(DC.Analytics[section].videoPlay, { model: self.thisBike.name.replace(/\s+/,'+') });
				}
				// Filter changed.
				else if (eventName.match(/^(tags_Set|categories_Set|author_Set|metaTags_Set|dateRange_Set)$/)) {
					var filter = eventData;
					if (eventName.match(/^(tags_Set|categories_Set|metaTags_Set)$/)) {
						filter = filter.join(';').replace(trimSection,'').replace(/\s+/,'+');
					}
					else if (eventName == 'author_Set') {
						filter = filter.userName;
					}
					else if (eventName == 'dateRange_Set') {
						filter = HD.util.Date.format(eventData.startDate, 'mm-yyyy');
					}
					HD.util.Analytics.track(DC.Analytics[section].landing, { filter: filter });
				}
				// Follow overlay opened.
				else if (eventName == 'showFollow') {
					HD.util.Analytics.track(DC.Analytics.Social.followOverlay);
				}
				// Bikes brochure carousel.
				else if (section == 'BikesPromo' && (eventName == 'viewCarouselItemThumb' || eventName == 'scrollCarouselLeft' || eventName == 'scrollCarouselRight')) {
					HD.util.Analytics.track(DC.Analytics.Bikes.modelView, { model: self.thisBike.name.replace(/\s+/,'+'), view: 'photo' });
						switch (self.thisBike.bikeID) {
							case 'iron-883':
								mmConversion(115153);
								break;
							case 'nightster':
								mmConversion(115154);
								break;
							case 'forty-eight':
								mmConversion(115155);
								break;
							case 'street-bob':
								mmConversion(115156);
								break;
							case 'fat-bob':
								mmConversion(115157);
								break;
							case 'blackline':
								mmConversion(115158);
								break;
							case 'cross-bones':
								mmConversion(115159);
								break;
							case 'seventy-two':
								//TODO ADD the correct tracking
								mmConversion(0);
								break;
						}
				}
			}
		};
		
		// Attach to engines.
		this.engine.addObserver(observer);
		if (section == 'Videos') {
			this.seriesEngine.addObserver(observer);
		}
		else if (section == 'BikesPromo') {
			this.carouselEngine.addObserver(observer);
		}
		(function setChromeObserver() {
			if (DC.Chrome && DC.Chrome.blogEngine) {
				DC.Chrome.blogEngine.addObserver(observer);
			}
			else {
				setTimeout(setChromeObserver, 50);
			}
		})();
		
		// Detail viewed.
		HD.DetailWidget.prototype.analyticsString = function(data) {
			if (section.match(/^(Blog|Bikes|Art)$/)) {
				HD.util.Analytics.track(DC.Analytics[section].detail, { id: data.mediaId, category: data.category.replace(trimSection,'').replace(/\s+/,'+') });
			}
			return '';
		};
		// Rating submitted.
		HD.CommunityDAO.prototype.analyticsRatingString = function(data) {
			if (section.match(/^(Blog|Bikes|Videos|Art)$/)) {
				if (section == 'Bikes' && data.category == 'Bikes/Faceoffs') {
					HD.util.Analytics.track(DC.Analytics[section].faceoffVote, { date: HD.util.Date.format(data.uploadedDate, dateFormat) });
				}
				else {
					HD.util.Analytics.track(DC.Analytics[section].rating, { id: data.mediaId, category: data.category.replace(trimSection,'').replace(/\s+/,'+') });
				}
			}
			return '';
		};
		// Comment submitted.
		HD.CommunityDAO.prototype.analyticsCommentString = function(data) {
			if (section.match(/^(Blog|Bikes|Videos|Art)$/)) {
				if (section == 'Bikes' && data.category == 'Bikes/Faceoffs') {
					HD.util.Analytics.track(DC.Analytics[section].faceoffComment, { date: HD.util.Date.format(data.uploadedDate, dateFormat) });
				}
				else {
					HD.util.Analytics.track(DC.Analytics[section].comment, { id: data.mediaId, category: data.category.replace(trimSection,'').replace(/\s+/,'+') });
				}
			}
			return '';
		};
		// Social sharing.
		HD.ShareWidget.prototype.analyticsShareString = function(service, media) {
			return '';
		};
		
		HDSocial.analyticsFacebookString = function(data) {
			if (section.match(/^Bikes$/)) {
				
				var id = 'Generic';
				if (DarkCustom.engine.dao.activeItem && DarkCustom.engine.dao.activeItem.mediaId){
					id = DarkCustom.engine.dao.activeItem.mediaId;
				}
				HD.util.Analytics.track(DC.Analytics[section].socialFacebook, { id: id });
				if(typeof mmConversion=='function') 
					{
						mmConversion(115151);
					};			
			}
			else if (section.match(/^BikesPromo$/)) {
				HD.util.Analytics.track(DC.Analytics[section].socialFacebook, { category: data.category.replace(/\s+/,'+') });
				if(typeof mmConversion=='function') 
					{
						mmConversion(115151);
					};			
			}
			else {
				HD.util.Analytics.track(DC.Analytics.Social.socialFacebook, { section: '', title: 'Generic' });
				if(typeof mmConversion=='function') 
					{
						mmConversion(115151);
					};			
			}
			return '';
		};
		HDSocial.analyticsMyspaceString = function(data) {
			if (section.match(/^Bikes$/)) {
				
				var id = 'Generic';
				if (DarkCustom.engine.dao.activeItem && DarkCustom.engine.dao.activeItem.mediaId){
					id = DarkCustom.engine.dao.activeItem.mediaId;
				}
				HD.util.Analytics.track(DC.Analytics[section].socialMySpace, { id: id });
			}
			else if (section.match(/^BikesPromo$/)) {
				HD.util.Analytics.track(DC.Analytics[section].socialMySpace, { category: data.category.replace(/\s+/,'+') });
			}
			else {
				HD.util.Analytics.track(DC.Analytics.Social.socialMySpace, { section: '', title: 'Generic' });
			}
			return '';
		};
		HDSocial.analyticsTwitterString = function(data) {
			if (section.match(/^Bikes$/)) {
				
				var id = 'Generic';
				if (DarkCustom.engine.dao.activeItem && DarkCustom.engine.dao.activeItem.mediaId){
					id = DarkCustom.engine.dao.activeItem.mediaId;
				}
				HD.util.Analytics.track(DC.Analytics[section].socialTwitter, { id: id });
			}
			else if (section.match(/^BikesPromo$/)) {
				HD.util.Analytics.track(DC.Analytics[section].socialTwitter, { category: data.category.replace(/\s+/,'+') });
			}
			else {
				HD.util.Analytics.track(DC.Analytics.Social.socialTwitter, { section: '', title: 'Generic' });
			}
			return '';
		};
		HDSocial.analyticsBloggerString = function(data) {
			if (section.match(/^Bikes$/)) {
				
				var id = 'Generic';
				if (DarkCustom.engine.dao.activeItem && DarkCustom.engine.dao.activeItem.mediaId){
					id = DarkCustom.engine.dao.activeItem.mediaId;
				}
				HD.util.Analytics.track(DC.Analytics[section].socialBlogger, { id: id });
			}
			else if (section.match(/^BikesPromo$/)) {
				HD.util.Analytics.track(DC.Analytics[section].socialBlogger, { category: data.category.replace(/\s+/,'+') });
			}
			else {
				HD.util.Analytics.track(DC.Analytics.Social.socialBlogger, { section: '', title: 'Generic' });
			}
			return '';
		};
		HDSocial.analyticsWordpressString = function(data) {
			if (section.match(/^Bikes$/)) {
				
				var id = 'Generic';
				if (DarkCustom.engine.dao.activeItem && DarkCustom.engine.dao.activeItem.mediaId){
					id = DarkCustom.engine.dao.activeItem.mediaId;
				}
				HD.util.Analytics.track(DC.Analytics[section].socialWordpress, { id: id });
			}
			else if (section.match(/^BikesPromo$/)) {
				HD.util.Analytics.track(DC.Analytics[section].socialWordpress, { category: data.category.replace(/\s+/,'+') });
			}
			else {
				HD.util.Analytics.track(DC.Analytics.Social.socialWordpress, { section: '', title: 'Generic' });
			}
			return '';
		};
		// Upload content.
		HD.UploadWidget.prototype.analyticsUploadBothString = HD.UploadWidget.prototype.analyticsUploadPhotoString = HD.UploadWidget.prototype.analyticsUploadVideoString = function(data) {
			if (section.match(/^(Bikes|Art)$/)) {
				HD.util.Analytics.track(DC.Analytics[section].submitForm);
			}
			return '';
		};
		HD.UploadWidget.prototype.analyticsUploadConfirmationString = function(contentType, category) {
			if (section.match(/^(Bikes|Art)$/)) {
				HD.util.Analytics.track(DC.Analytics[section].submitConfirm, { category: (category && category.replace(trimSection,'').replace(/\s+/,'+')) || '' });
			}
			return '';
		};
		// User login.
		HD.Profile.View.prototype.analyticsAuthenticateString = function() {
			HD.util.Analytics.track(DC.Analytics.General.loginForm);
			return '';
		};
		HD.Profile.View.prototype.analyticsAuthenticateConfirmString = function() {
			HD.util.Analytics.track(DC.Analytics.General.loginConfirm);
			return '';
		};
		HD.Profile.View.prototype.analyticsRegisterString = function() {
			HD.util.Analytics.track(DC.Analytics.General.createProfileForm);
			return '';
		};
		HD.Profile.View.prototype.analyticsRegisteredString = function() {
			HD.util.Analytics.track(DC.Analytics.General.createProfileConfirm);
			return '';
		};
		// Send to a friend.
		HD.SendFriendWidget.prototype.analyticsSendFriendString = function(data) {
			HD.util.Analytics.track(DC.Analytics.General.sendFriendForm);
			return '';
		};
		HD.SendFriendWidget.prototype.analyticsSendFriendConfirmationString = function(data) {
			HD.util.Analytics.track(DC.Analytics.General.sendFriendConfirm);
			return '';
		};
		
		// Downloads.
		if (section.match(/^(Downloads)$/)) {
			HD.getByClass('download', 'div', HD.get('downloads'), function(downloadEl) {
				var linkEl = downloadEl.getElementsByTagName('a')[0] || null;
				if (linkEl) {
					linkEl.onclick = function() {
						var name = this.href.match(/&imgid=(.+?)&/);
						var resolution = this.parentNode.className.match(/w(\d+)/);
						HD.util.Analytics.track(DC.Analytics[section].get, { name: (name && name[1]) || 'unknown', resolution: (resolution && resolution[1]) || 'unknown' });
					};
				}
			});
		}
		
		// Follow (header/footer).
		var chromeEls = [HD.get('navBar'), HD.get('footer')];
		var applyFollowClick = function(className, parentEl, clickFn) {
			HD.getByClass(className, '*', parentEl, function(el) {
				var linkEl = el.getElementsByTagName('a')[0] || null;
				if (linkEl) {
					linkEl.onclick = function() {
						clickFn.apply(this, arguments);
						return true;
					};
				}
			});
		};
		for (var i = 0; i < chromeEls.length; i++) {
			applyFollowClick('followRss', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followRss);
			});
			applyFollowClick('followFacebook', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followFacebook);
			});
			applyFollowClick('followTwitter', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followTwitter);
			});
			applyFollowClick('followYouTube', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followYouTube);
			});
			applyFollowClick('followMyYahoo', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followMyYahoo);
			});
			applyFollowClick('followGoogle', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followGoogle);
			});
			applyFollowClick('followMyAOL', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followMyAOL);
			});
			applyFollowClick('followPageflakes', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followPageflakes);
			});
			applyFollowClick('followNewsgator', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followNewsgator);
			});
			applyFollowClick('followBloglines', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followBloglines);
			});
			applyFollowClick('followNetvibes', chromeEls[i], function() {
				HD.util.Analytics.track(DC.Analytics.Social.followNetvibes);
			});
		}
	}
};

HD.util.Player.View.prototype.setupCommonPlayerEvents = function () {
	var self = this;
	jwplayer(this.playerContainerID).onPlay(
		 function() {
			 self.notifyObservers('videoPlayed', '');
		 }
	);
	
	jwplayer(this.playerContainerID).onReady(
		 function() {
			 self.setPlayerListeners();
		 }
	);

	
};

/**
 * Stop all active players that are playing videos
 */
HD.util.Player.View.stopAll = function() {
	var allplayers = jwplayer.getPlayers();
	for (var i=0; i<allplayers.length; i++){
		if (jwplayer() && jwplayer(allplayers[i].id)) {
			jwplayer(allplayers[i].id).stop();
			
		}
	}
};

