
// SWFAddress callback when path changes. Relay to internal method.
SWFAddress.onChange = function() {  
	SDC_DigitalShorts.pathChange(SWFAddress.getPathNames());
}


// Brightcove callback when player is loaded to page. Relay to internal method.
function onTemplateLoaded(experienceID) {
	SDC_DigitalShorts.initPlayer(experienceID);
}

function onMediaComplete() {
	SDC_DigitalShorts.onMediaComplete();
}


function onContentLoad(e) {
	SDC_DigitalShorts.onContentLoad();
}


var SDC_DigitalShorts = {

	CURRENT_GROUP		: ''
	, CURRENT_PLAYLIST	: ''
	, CURRENT_VIDEO 	: ''
	
	, tracking_dir		: 'digital-shorts'
	, default_group		: 'series'
	, player			: false

	, queue_path		: false

	, is_ie6			: (document.all && !window.opera && !window.XMLHttpRequest)
	
	, promo_cnt			: 0

	, onClick : function(path) {

		if (mode == 'seo') {

			// User coming from google page. Route back to normal site
			document.location = base_dir+'#/'+path;			
		
		} else {

			// Update SWFAddress, it will in turn call SDC_DigitalShorts.pathChange
			SWFAddress.setValue(path);
		
		}

		// Block browser from executing href
		return false;
		
	}
	
	, pathChange : function(path) {

			if (mode == 'seo') { return; } 
			
			if (!path || !path.length) return;

			if (!this.player) {
				
				// Player not initialized yet, store path
				this.queue_path = path;
			
			} else {
			
				if (path.length == 1) {
					this.showGroup(path[0]);
				} else if (path.length >= 2 && (path[1] != this.CURRENT_PLAYLIST)) {
					
					// Deeplink with group and playlist
					
					this.showPlaylistBackground(path[1]);
	
					if (path[0] != this.CURRENT_GROUP) {
						// Need to change both the playlist and the group
						this.showGroup(path[0], path[1]);
					} else {
						// Just change the playlist
						this.showPlaylist(path[1]);
					}
					
				}
	
				// Do we need to play a video?
				if ((path.length == 3) && (path[2] != this.CURRENT_VIDEO)) {
					this.showVideo(path[2]);
				}
			
			}


	}

	, showGroup : function(group, activate_list) {
	
			// Make sure it's lowercase
			group = group.toLowerCase();

			this.CURRENT_GROUP = group;

			var group_item = $('ds_group_'+group);
			
			if (!group_item) {
				alert('Error: Invalid playist group "'+group+'"');
				return false;
			}

			// Hide all groups, then show the desired group
			$$('.playlist_group').invoke('hide');
			$('ds_group_'+group).show();

			// Hide all group content, then show the desired group content
			$$('#ds_player_episode .playlist_groupcontent').invoke('hide');
			$('ds_groupcontent_'+group).show();


			// Remove all current item status and activate this playlist
			//$$('#ds_groupnav LI.current').invoke('removeClassName', 'current');
			$('ds_groupnav').removeClassName('current_series').removeClassName('current_theme').removeClassName('current_artist');
			//$('path_'+group).addClassName('current');
			$('ds_groupnav').addClassName('current_'+group);


			if (!activate_list) {
				// Initial page load. Perform default actions
				// Show first playlist for this group
				var activate_list = $('ds_group_'+group).down('LI').getAttribute('playlist');
			}
			
			this.showPlaylistBackground(activate_list);
			this.showPlaylist(activate_list);
			

	}

	, showPlaylist : function(list) {
	
			this.CURRENT_PLAYLIST = list;

			// Make sure all list are hidden first, then show the desired list
			$$('.playlist_videos').invoke('hide');

			$('playlist_videos_'+this.CURRENT_GROUP+'_'+list).show();

			// Remove all current item status and activate this playlist
			$$('#ds_player_groups .list_content LI.current').invoke('removeClassName', 'current');
			$('path_'+this.CURRENT_GROUP+'_'+list).addClassName('current');

			// Change custom promos
			this.showPromos(list);
			
			var list_name = $('path_'+this.CURRENT_GROUP+'_'+list).down('A').innerHTML || '';
			this._trackAction('Show Playlist: '+list_name);
						

	}

	, showPlaylistBackground : function(list) {

			if ((list_bg = playlist_backgrounds[list])) {

				// Playlist has custom background
				
				if ($('ds_site').style.backgroundImage != list_bg) {
					$('ds_site').removeClassName('default_background');
					$('ds_site').style.backgroundImage = "url('"+list_bg+"')";
				}
				
			} else {

				// Use default background image

				$('ds_site').style.backgroundImage = '';
				$('ds_site').addClassName('default_background');
			
			}
	
	}
	
	, showPromos : function(list) {

			this.promo_cnt 		= 0;
			var promo_template	= new Template(
									'<a href="#{link_url}" target="#{link_target}" class="tile playlist_tile">'
									+ '<div class="content">'
									+ '<div class="inner">'
									+ '<div class="content_title">#{title}</div>'
									+ '<div class="content_inner">#{subtitle}</div>'
									+ '</div>'
									+ '</div>'
									+ '<div class="image"><img src="#{image}" height="102" width="191" /></div>'
									+ '#{impression_image}'
									+ '</a>');

			// Clear all custom tiles
			$('playlist_promos').update('');
	
			// Hide all page tiles
			$$('A.tile').invoke('addClassName', 'tile_hidden');
			
			if (playlist_promos[list]) {

				var _promo_data		= '';
				
				playlist_promos[list].each(function(tile, i) {
				
					if (SDC_DigitalShorts.promo_cnt >= 6 ) return;
					
					// Add double click impression tracking url, it might not be in here.
					var impression_image = (tile.impression_url && tile.impression_url.length) ? ('<img src="'+tile.impression_url+'" height="1" width="1" class="hide" />') : '';
					
					_promo_data += promo_template.evaluate({title: tile.title, subtitle: tile.subtitle, image: tile.image, link_url: tile.link_url, link_target: tile.link_target, impression_image: impression_image});
					
					SDC_DigitalShorts.promo_cnt ++;

				});

				$('playlist_promos').update(_promo_data);

			}


			// Show page tiles
			$$('#ds_features A.page_tile').each(function(tile, i) {

				if (SDC_DigitalShorts.promo_cnt >= 6 ) return;

				tile.removeClassName('tile_hidden');
				SDC_DigitalShorts.promo_cnt ++;

			});
	
	}


	, showVideo : function(id, play) {

//			if (play == undefined) play = true;

			this.CURRENT_VIDEO = id;

			// Remove all current item status and activate this playlist
			$$('.playlist_videos LI.current').invoke('removeClassName', 'current');
			$('path_'+this.CURRENT_GROUP+'_'+this.CURRENT_PLAYLIST+'_'+id).addClassName('current');

			$('video_title').update($('path_'+this.CURRENT_GROUP+'_'+this.CURRENT_PLAYLIST+'_'+id).down('A').innerHTML);
			
			// Make sure the player is visible (hidden on load)
			//$('ds_player_bc').addClassName('active');

			this._trackAction('Play Video');

		   	this.player.loadVideo(id);
			//this.player.play();		
		
		//   	if (!this.player.isPlaying()) { this.player.play(); alert('player not playing, making it so'); }
			
			/*

			// Is the player ready?
			if (this.player && play) {
			   	this.player.loadVideo(id);
			   	if (!this.player.isPlaying()) this.player.play();
			} else {
			   	this.player.queueVideo(id);
			}
			*/

	}

	, onMediaComplete : function() {

			var current_row = $('path_'+this.CURRENT_GROUP+'_'+this.CURRENT_PLAYLIST+'_'+this.CURRENT_VIDEO);
			var next_row 	= current_row.next('LI');
	
			if (next_row) {
				var next_path = this.CURRENT_GROUP+'/'+this.CURRENT_PLAYLIST+'/'+next_row.getAttribute('video');
				SWFAddress.setValue(next_path);		
			}
			
	}

	, onContentLoad : function() {
			
			// Make sure it's playing, sometimes isn't in IE
			if (!this.player.isPlaying()) { this.player.play(); }

	}

	, initPlayer : function(experienceID) {

			var bcplayer 	= brightcove.getExperience(experienceID);
			var bcvideo 	= bcplayer.getModule(APIModules.VIDEO_PLAYER);
			var bcexp 		= bcplayer.getModule(APIModules.EXPERIENCE);

			this.player 	= bcvideo;

			bcvideo.addEventListener(BCMediaEvent.COMPLETE, onMediaComplete); 
			bcexp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad); 

			// Check to see if there is a video waiting to be player (coming from deeplink)

			if (this.queue_path) {
				this.pathChange(this.queue_path);
				this.queue_path = false;
			} else {
			
				// Play first video
				if ((first_vid = $('playlist_scroll').down('LI A'))) {
					this.onClick(first_vid.getAttribute('path'));
				}				
			
			}
	
	}


	, _trackAction: function(action) {
			Sundance.tracking.flashAction(this.tracking_dir, action, 1);
	}
	
	
}

