/**
 * CustomWatch extends BrowseWhileWatch
 */
var CustomWatch = BrowseWhileWatch;

CustomWatch.init = function() {
	this.player = $('player');
	
	// First video to play will have JSON metadata present on the page, fetch it and display the video
	if($('jsonWatch')) {
		Metadata.fetchJson('Watch', null, $empty);
	
		if(Metadata.jsonObj.videoId) {
			this.initRelated(Metadata.jsonObj.videoId);	// initialize related videos
			this.showWatch(Metadata.jsonObj.videoId);
		}
	}
	else {
		this.initRelated('');	// just initialize box for related videos
	}
};

CustomWatch.showWatch = function(permalinkId, index) {
	var callback = function() {
		CustomWatch.showDisplay(permalinkId);
	};
	Metadata.fetchJson(index, permalinkId, callback);
	return false;
};

CustomWatch.initRelated = function(permalinkId) {
	this.relatedVideos = new ExpandableBox("relatedVideos", {
		containerElement: $('sliderrel'),
		contentElement: $('sliderrel_expandWrap'),
		stateElement: $('sliderrel_state'),
		titleElement: $('sliderrel_title'),
		state: 1,
		height: 60,
		urlTemplate: VeohUtils.rpcRoot + "video/relatedVideos/permalinkId/-permalinkId-",
		stateClosedClass: 'sp_arrow-down-grey',
		stateOpenedClass: 'sp_arrow-up-grey',
		afterOpen: function() { $('sliderrel_title').set('html', 'Hide Related Videos'); },
		afterClose: function() { $('sliderrel_title').set('html', 'Show Related Videos'); }
	});

	this.relatedVideos.setUrlFromTemplates(["permalinkId"], [permalinkId]);
};
	
CustomWatch.showDisplay = function(permalinkId) {
	if(!Metadata.jsonObj || Metadata.jsonObj == null) {
		return;
	}
	
	// hide any lingering thumb tips
	thumbTips.hide();
	
	// Notify ads object that watch pane is opening, indicating whether or
		// not ads are allowed by publisher
	Ads.handleWatchOpeningEvent( Metadata.jsonObj.allowAds );
	
	// load embed				
	if(Metadata.jsonObj && Metadata.jsonObj.isExternalMedia) {	
		// AOW player
		var embedCode = Metadata.jsonObj.embedCode;
		if(this.player)
			this.player.set('html', embedCode);					
		
		// ABC short form player needs to do special things
		if(Metadata.jsonObj.isABC && Metadata.jsonObj.isFullLength == '') {						
			try {
				launchABCShortFormPlayer();
				
				var oldembed = this.player.innerHTML;
				if(Browser.Engine.trident) {
					// ABC's swf generator uses SWFObject which generates the embed as an <object> and it's params look different
					oldembed = oldembed.replace('width=540', 'width=100%');
					oldembed = oldembed.replace('height=373', 'height=100%');
				}
				else {
					oldembed = oldembed.replace('width="540"', 'width="100%"');
					oldembed = oldembed.replace('height="373"', 'height="100%"');
					oldembed = oldembed.replace('width=\'540\'', 'width="100%"');
					oldembed = oldembed.replace('height=\'373\'"', 'height="100%"');
				}
								
				this.player.set('html', oldembed);
			}
			catch (e) {
				// do nothing, the script isnt available.
			}
		}
	} else {
		CustomWatch.generateEmbedCode(permalinkId, false, CustomRecsType, CustomBrandedChannel); // generates and places embed code on the page
	}	
	
	// resize the player area so it looks nice
	CustomWatch.resizePlayerWithRatio( $defined(CustomPlayerWidth) ? CustomPlayerWidth : 574 );
	
	// update page title
	VeohUtils.updatePageTitle(Metadata.jsonObj.title + (VeohUtils.basePageTitle != '' ? ' | ' + VeohUtils.basePageTitle : ''));
	
	// update related
	if(!$defined(CustomWatch.relatedVideos)) {
		CustomWatch.initRelated(permalinkId);
	}
	CustomWatch.relatedVideos.reset();	// reset so the panels will update with new permalinkId
	CustomWatch.relatedVideos.setUrlFromTemplates(["permalinkId"], [permalinkId]);
				
	if(CustomWatch.relatedVideos.isOpen && CustomWatch.relatedVideos.isOpen()) {
		CustomWatch.relatedVideos.expand();
	}
	
	// Notify ads object that watch pane is fully open, indicating
	// whether or not the player is an external player
	Ads.handleWatchOpenedEvent( Metadata.jsonObj.isExternalMedia );
};

CustomWatch.resizePlayerWithRatio = function(width) {
		thumbTips.hide();
		var vobj = Metadata.jsonObj;
		if(width == null) {
			width = Constants.WATCH_START_WIDTH;
		}
		
		if(CustomPlayerHeight != null) { // CustomPlayerHeight overrides any sort of scaling via ratio
			var height = CustomPlayerHeight;
		}
		else {	
			var ratio = vobj.pWidth / vobj.pHeight;
			var height = parseInt(width / ratio);	
		}
		
		CustomWatch.player.setStyle('width', width);
		CustomWatch.player.tween('height', height);
};

window.addEvent('unload', function() {
	CustomWatch = null;
}); 


var CustomTabs = {
	selectedClassName: 'selected',
	
	select: function(linkObj) {
		var parentLI = $(linkObj).getParent('li');
		var parentUL = parentLI.getParent('ul.tabs');
		var tabs = parentUL.getElements('a');
		
		// first unselect all tabs
		tabs.each(function(el) {
			this.unselect(el);
		}, this);
	
		$(linkObj).addClass(this.selectedClassName);
		
		return VeohUtils.makeRequest(linkObj, $('currentTabContents'));
	},
	
	unselect: function(linkObj) {
		$(linkObj).removeClass(this.selectedClassName);
	}
};