
/*-------------------------------------------------------------------------------
  DATA WAREHOUSE INTEGRATION OBJECT

  ***USER ACTIONS***
  This communicates user actions to DW.  To make use of this, add a 'dw' attribute
  to your html element.  Set the value of the 'dw' attribute to one or more of the
  uact.name values.  For example, if you want to DW track the following:

  		<a href="#" onclick="Add Show(); return false;">Add Show</a>

  Add the following to your <a> tag:

  		dw="add_to_my_shows"

  ***LINK OFFS***
  This communicates video link offs to DW.  To make use of this, add the link
  structure to dw_connecter.link_offs.  Onload the link will be prefixed with the
  dw URL.  For example:

  <a href="http://www.nbc.com/30-rock/video/episodes/?vid=1199218">Link Off</a>

  Becomes:

  <a href="http://dw.com.com/redir?siteid=45&amp;destURL=http://www.nbc.com/30-rock/video/episodes/?vid=1199218">Link Off</a>


  -------------------------------------------------------------------------------*/


var dw_connecter = new Object();

// Array of useract values used to track user events.
dw_connecter.uact = [
	{'category':'comments',		'name': 'comment_on_video', 					'value' : 560},
	{'category':'comments',		'name': 'comment_on_news_story', 				'value' :  82},
	{'category':'comments',		'name': 'comment_on_feature', 					'value' :  83},
	{'category':'comments',		'name': 'comment_on_blog', 						'value' :  63},
	
	
	{'category':'favorites',	'name': 'add_to_my_shows', 						'value' :  13},
	{'category':'favorites',	'name': 'remove_from_my_shows', 				'value' : 422},
	{'category':'favorites',	'name': 'add_to_my_people', 					'value' :  69},
	{'category':'favorites',	'name': 'remove_from_my_people', 				'value' : 420},
	
	{'category':'forums',		'name': 'post_to_forum', 						'value' :   5},
	       		
	{'category':'ratings',		'name': 'rate_a_tv_show', 						'value' : 561},
	{'category':'ratings',		'name': 'rate_a_tv_episode', 					'value' : 562},
	{'category':'ratings',		'name': 'rate_a_person', 						'value' : 563},
	{'category':'ratings',		'name': 'thumb_rate_a_tv_show_review', 			'value' : 564},
	{'category':'ratings',		'name': 'thumb_rate_a_tv_episode_review', 		'value' : 565},
	{'category':'ratings',		'name': 'thumb_rate_a_person_review', 			'value' : 566},
	
	{'category':'review',		'name': 'review_a_tv_show', 					'value' : 567},
	{'category':'review',		'name': 'review_a_tv_episode', 					'value' : 568},
	
	{'category':'tivo',			'name': 'tivo_me', 								'value' : 423},
	
	{'category':'ugc',			'name': 'add_a_tv_episode_recap', 				'value' : 569},
	{'category':'ugc',			'name': 'add_a_tv_episode_synopsis', 			'value' : 570},
	
	{'category':'ugc',			'name': 'add_a_celebrity_trivia',				'value' : 664},
	{'category':'ugc',			'name': 'add_a_celebrity_quote',				'value' : 665},
	{'category':'ugc',			'name': 'add_a_tv_episode_allusion',			'value' : 663},
	{'category':'ugc',			'name': 'add_a_tv_episode_note',				'value' : 661},
	{'category':'ugc',			'name': 'add_a_tv_episode_trivia',				'value' : 660},
	{'category':'ugc',			'name': 'add_a_tv_episode_quote',				'value' : 662},
	
	{'category':'ugc',			'name': 'edit_cast_crew', 						'value' : 163},
	{'category':'ugc',			'name': 'edit_summary', 						'value' : 165},
	       		
	{'category':'watchlist',	'name': 'wl_invite_friends', 					'value' : 580},
	{'category':'watchlist',	'name': 'wl_settings',							'value' : 581},
	{'category':'watchlist',	'name': 'wl_about', 							'value' : 582},
	{'category':'watchlist',	'name': 'wl_all_shows', 						'value' : 583},
	{'category':'watchlist',	'name': 'wl_my_shows', 							'value' : 584},
	{'category':'watchlist',	'name': 'wl_friends_shows', 					'value' : 585},
	{'category':'watchlist',	'name': 'wl_friend', 							'value' : 586},
	{'category':'watchlist',	'name': 'wl_friend_info', 						'value' : 588},
	{'category':'watchlist',	'name': 'wl_show_name_header', 					'value' : 589},
	{'category':'watchlist',	'name': 'wl_done_managing', 					'value' : 590},
	{'category':'watchlist',	'name': 'wl_facebook_ad', 						'value' : 591},
	{'category':'watchlist',	'name': 'wl_watch_video_button', 				'value' : 592},
	{'category':'watchlist',	'name': 'wl_friends_watch', 					'value' : 593},
	{'category':'watchlist',	'name': 'wl_show_name', 						'value' : 594},
	{'category':'watchlist',	'name': 'wl_episode_info', 						'value' : 595},
	{'category':'watchlist',	'name': 'wl_tivo_me', 							'value' : 596},
	{'category':'watchlist',	'name': 'wl_plan_to_watch', 					'value' : 597},
	{'category':'watchlist',	'name': 'wl_have_seen', 						'value' : 598},
	{'category':'watchlist',	'name': 'wl_friend_have_show', 					'value' : 599},
	{'category':'watchlist',	'name': 'wl_friends_plans_to_watch', 			'value' : 600},
	{'category':'watchlist',	'name': 'wl_friends_have_seen', 				'value' : 601},
	{'category':'watchlist',	'name': 'wl_friend_avatar', 					'value' : 602},
	{'category':'watchlist',	'name': 'wl_friend_username', 					'value' : 603},
	{'category':'watchlist',	'name': 'wl_friends_watchlist', 				'value' : 604},
	{'category':'watchlist',	'name': 'wl_invite_friends_form', 				'value' : 605},
	{'category':'watchlist',	'name': 'wl_show_listing', 						'value' : 606},
	{'category':'watchlist',	'name': 'wl_play_offsite_full_episode_mini',	'value' : 607},
	{'category':'watchlist',	'name': 'wl_play_full_episode_mini', 			'value' : 608},
	{'category':'watchlist',	'name': 'wl_play_preview_mini', 				'value' : 609},
	{'category':'watchlist',	'name': 'wl_display_show_info', 				'value' : 610},
	{'category':'watchlist',	'name': 'wl_hide_show_info', 					'value' : 611}
];

// An array of link off prefixes to track.
dw_connecter.link_offs = [	
	{'provider':'abc',			'url':'http://abc.go.com/player/'},
	{'provider':'abc',			'url':'http://abc.go.com/watch'},
	{'provider':'animal',		'url':'http://animal.discovery.com/videos/'},
	{'provider':'discovery',	'url':'http://dsc.discovery.com/videos/'},
	{'provider':'ae',			'url':'http://link.aetv.com/services/link/'},
	{'provider':'biography',	'url':'http://link.biography.com/services/link/'},
	{'provider':'history',		'url':'http://link.history.com/services/link/'},
	{'provider':'tlc',			'url':'http://tlc.discovery.com/videos/'},
	{'provider':'crunchy_roll',	'url':'http://www.crunchyroll.com/redirect'},
	{'provider':'nbc',			'url':'http://www.nbc.com/'},
	{'provider':'videosurf',	'url':'http://apis.videosurf.com/'}
];


/*-----------------------------------------------
  Register event handler for firing off DW calls.
  -----------------------------------------------*/
jQuery(document).ready(function() {
	
	//this tracking should only occur in production
	//adding stage too to help prevent weird problems slipping to prod
	if(TV_BASE_URL == 'http://www.tv.com' || TV_BASE_URL == 'http://stagewww.tv.com'){
		// USER ACTIONS
		jQuery('[dw]').live('mousedown',function(){
			var element_dw = jQuery(this).attr('dw');
			for (var x = 0; x < dw_connecter.uact.length; x++){
				//if we have a matching useraction
				if (element_dw.match(dw_connecter.uact[x].name)){
					DW.redir( {useract: dw_connecter.uact[x].value});
					_gaq.push(['_trackEvent', dw_connecter.uact[x].category, dw_connecter.uact[x].name]);
				}
			}
			for (var x = 0; x < dw_connecter.link_offs.length; x++){
				//if we have a matching link-off prefix
				if(element_dw.match(dw_connecter.link_offs[x].provider)){
					_gaq.push(['_trackEvent', dw_connecter.link_offs[x].provider, dw_connecter.link_offs[x].url]);
				}
			}
		});
	
		dw_connecter.link_off_events();
	}

});//----------closes ready----------


dw_connecter.link_off_events = function() {

	// LINK OFFS		
	jQuery('a').each(function(){
		var thisLink = jQuery(this);
		var thisHref = thisLink.attr('href');
		
		for (var x = 0; x < dw_connecter.link_offs.length; x++){
			//for untracked linkoffs
			if(thisLink.is('[href^="'+dw_connecter.link_offs[x].url+'"]')){
				thisLink.attr('href', 'http://dw.com.com/redir?siteid=45&destURL='+thisHref);	
				thisLink.attr('dw',dw_connecter.link_offs[x].provider);
			
			//for tracked linkoffs, just for comparison
			}else if(thisLink.is(':contains('+dw_connecter.link_offs[x].url+')')){
				thisLink.attr('dw',dw_connecter.link_offs[x].provider);
			}
		}
	});
	
	
}

