OneClipVideoListRenderer = {
	'OverrideOneClipImageUrl' : null,
	'OverrideOneClipImagePriority' : 0,
	'MaximumClipList' : 5,
	'SpecialPrerollObject':null,
	'SpecialPrerollVideo':null,
	'SpecialPrerollShown':null,
	'SpecialPrerollPlayOnce':true,
	'SpecialPrerollPriority':0,
	'PrerollFrequency':60,
	'AutoPlay':false,
	'DefaultPath' : 'Feature',
	'ThemePath' : 'http://watch.tsn.ca/themes/TSN/player/onecliptheme.aspx',
	'ErrorMessage':'',
	'arrOneClipVideoList' : new Array(),
	'Redirects' : [],
	'UrlHash' : document.location.hash,
	'UrlPathname' : document.location.pathname,
	'ClipIdFromHash' : /clip(\d+)/,

	'TestRedirect' : function(strTest) {
		for (var i=0; i < OneClipVideoListRenderer.Redirects.length; i++) {
			var objRedirect = OneClipVideoListRenderer.Redirects[i];
			if (
				strTest.match(new RegExp(objRedirect.search)) 
				&& !OneClipVideoListRenderer.UrlPathname.match(new RegExp(objRedirect.url))
				&& !OneClipVideoListRenderer.UrlPathname.match(new RegExp('/(stories|fragment)/'))
				) {
				return objRedirect.url;
			}
		}
	},
	'OverrideOneClipImage' : function(strUrl,intPriority) {
		boolOverrideImage = true;
		if (OneClipVideoListRenderer.OverrideOneClipImagePriority != null && OneClipVideoListRenderer.OverrideOneClipImagePriority > intPriority) {
			boolOverrideImage = false;
		}
		if (boolOverrideImage) {
			OneClipVideoListRenderer.OverrideOneClipImageUrl = strUrl;
			OneClipVideoListRenderer.OverrideOneClipImagePriority = intPriority;
		}
	},
	'GetSpecialPreroll':function() {
		if (!OneClipVideoListRenderer.SpecialPrerollVideo && OneClipVideoListRenderer.SpecialPrerollObject) {
			intPrerollClipId = OneClipVideoListRenderer.SpecialPrerollObject['clipid']; 
			strPrerollTitle = OneClipVideoListRenderer.SpecialPrerollObject['title'];
			strPrerollFormat = OneClipVideoListRenderer.SpecialPrerollObject['format']; 
			OneClipVideoListRenderer.SpecialPrerollVideo = new Video( {ClipId:intPrerollClipId, Title: strPrerollTitle, Format: strPrerollFormat } );
		}
		return OneClipVideoListRenderer.SpecialPrerollVideo;
	},
	'SetSpecialPreroll':function(objClip,intPriority,playOnce) {
		var intPriority = intPriority ? intPriority:1;
		OneClipVideoListRenderer.SpecialPrerollPlayOnce = playOnce === true;
		OneClipVideoListRenderer.SpecialPrerollShown = false;
		if (intPriority > OneClipVideoListRenderer.SpecialPrerollPriority) {
			OneClipVideoListRenderer.SpecialPrerollObject = objClip;
			OneClipVideoListRenderer.SpecialPrerollPriority = intPriority;
		}
	},
	'SetDefaultPath' : function(strPath) {
	    OneClipVideoListRenderer.DefaultPath = strPath == 'feature' ? 'featured' : strPath;
	},
	'AddClip' : function(objDetails) {
	    OneClipVideoListRenderer.arrOneClipVideoList[OneClipVideoListRenderer.arrOneClipVideoList.length] = objDetails;
	},
	'ClearClips' : function() {
	    if (window["Playlist"]) {
//            Playlist.FeaturedVideos = new Array();
	    }
	    OneClipVideoListRenderer.arrOneClipVideoList = new Array();
	},
    'StandardLookRenderer' : function(objTarget,arrDetails) {
		var objContainingLi, objContainingDl, objContainingDt, objAnchor1, objAnchor2, objAnchor3, objDd1, objDd2, objImg;
		var objContainingDiv 	= document.createElement('div');
        var objContainingUl     = document.createElement('ul');
		var intPopupId = null;
		var isAd;
		for(intDetail = 0; intDetail < Math.min(OneClipVideoListRenderer.MaximumClipList,arrDetails.length); intDetail++) {
			objDetails = arrDetails[intDetail];
			isAd = objDetails['isad'] == true;
			intPopupId = objDetails['clipid'];
			strImageHref = objDetails['image'];
			strTitle = objDetails['title'];
			strDescription = objDetails['description'];

	        // Create DOM elements
	        objContainingLi     = document.createElement('li');
	        objContainingDl     = document.createElement('dl');
	        objContainingDt     = document.createElement('dt');
	        objAnchor1          = document.createElement('a');
	        objDd1              = document.createElement('dd');
	        objDd2              = document.createElement('dd');
	        objAnchor2          = document.createElement('a');
	        objImg              = document.createElement('img');

	        // Create text nodes
			objTitleText 	= document.createTextNode(strTitle);
			objDescText 	= document.createTextNode(strDescription);

	        // Configure elements
	        objAnchor1.setAttribute('href','javascript:void(0)');
	        objAnchor1.setAttribute('title','Play now');
	        objAnchor2.setAttribute('href','javascript:void(0)');
	        objAnchor2.setAttribute('title','Play now');

			objDd1.className ='Description';
			objDd2.className = 'Thumbnail';
			objContainingLi.className = isAd ? 'ad' : undefined;
			
			objImg.setAttribute('src',strImageHref + "/80/60/image.ashx");
			objImg.setAttribute('alt',strTitle);
			objImg.setAttribute('width','80');
			objImg.setAttribute('height','60');

			// Set the anchor actions
			objAnchor1.onclick = OneClipVideoListRenderer.Bind(OneClipVideoListRenderer.PlayVideo,objDetails); 
			objAnchor2.onclick = OneClipVideoListRenderer.Bind(OneClipVideoListRenderer.PlayVideo,objDetails); 
			
			// Assemble the pieces
			objContainingUl.appendChild(objContainingLi);
			objContainingLi.appendChild(objContainingDl);
			objContainingDl.appendChild(objDd2);
			objContainingDl.appendChild(objContainingDt);
			objContainingDt.appendChild(objAnchor1);
			objAnchor1.appendChild(objTitleText);
			objContainingDl.appendChild(objDd1)
			objDd1.appendChild(objDescText);
			
			objDd2.appendChild(objAnchor2);
			objAnchor2.appendChild(objImg);
		}
    	objPopupInvite 	= document.createTextNode("Watch more in our video player!");
		objAnchor3 = document.createElement('a');
		objAnchor3.setAttribute('href','javascript:void(0)');
		objAnchor3.setAttribute('title','Open full player');
		objAnchor3.className = 'OpenPlayer';
		objAnchor3.onclick = OneClipVideoListRenderer.Bind(OneClipVideoListRenderer.LaunchPopupPlayer,{'SectionName':OneClipVideoListRenderer.DefaultPath});
		objAnchor3.appendChild(objPopupInvite);

		objTarget.appendChild(objContainingUl);
		objTarget.appendChild(objAnchor3);
    },
	
	'RenderList' : function(fnRenderer) {
		if (OneClipVideoListRenderer.arrOneClipVideoList && OneClipVideoListRenderer.arrOneClipVideoList.length) {
			objTarget = document.getElementById('Playlist');
			fnRenderer(objTarget, OneClipVideoListRenderer.arrOneClipVideoList);
		}
	},
	'Bind' : function(fn,args) {
		return function() { fn(args) };
	},
	'LaunchPopupPlayer' : function(objDetails) {
	    Player.GetInstance().Wait();
		window.open('http://watch.tsn.ca/' + OneClipVideoListRenderer.DefaultPath + '/', 'TSNBroadband','width=1010,height=850,toolbar=0,status=0,scrollbars=1,resizable=1');
		return false;
	},
	'LoadOneClipPlayer' : function() {
        if (document.getElementById("OneClipViewer") && window["Interface"] && OneClipVideoListRenderer.arrOneClipVideoList && OneClipVideoListRenderer.arrOneClipVideoList.length) {
			if (OneClipVideoListRenderer.OverrideOneClipImageUrl) {
				strTopImage = OneClipVideoListRenderer.OverrideOneClipImageUrl;
			}
			else {
				strTopImage = OneClipVideoListRenderer.arrOneClipVideoList[0].image + "/416/312/image.ashx";
			}

			if (window['Interface'] && Interface.DisplayPlayerControllerError) {
//					Interface.DisplayPlayerControllerError( "We're sorry", "There was an error loading your selection. Please try again or load the full player by clicking on the link below." );
				}
			try {
				Interface.PermalinkClicked = function( url ) {
					if( Playlist.GetInstance().Current.IsAd ) {window.open( url , "player" );}
					else {document.location = url;}
				}
			}
			catch(e) {
				OneClipVideoListRenderer.ErrorMessage += e.message;
			}
			
	        // Instantiate the player and create the playlist
			try {
				strAdpg = "/" + adpg;
			}
			catch(e) {
				strAdpg = "/main";
			}
	        Interface.GetInstance().PlayerViewer = document.getElementById("OneClipViewer");
            new Framework( Format.FlashVideo, OneClipVideoListRenderer.PrerollFrequency, "chumtvtsn", "One Clip Player", "ctv.tsnwatch.ca/tradedeadlinevideo ",OneClipVideoListRenderer.ThemePath, strTopImage);

	        // Loop through the videos in the list and push them into the player's playlist.
	        if (OneClipVideoListRenderer && OneClipVideoListRenderer.arrOneClipVideoList && OneClipVideoListRenderer.arrOneClipVideoList.length) {
	            Playlist.FeaturedVideos = new Array();

				var arrAutoPlayClipId = OneClipVideoListRenderer.UrlHash.match(OneClipVideoListRenderer.ClipIdFromHash);
				if (arrAutoPlayClipId) {
					Playlist.GetInstance().Play( new Video( {ClipId:arrAutoPlayClipId[1],Title: 'World Juniors Hockey Championship', Format:'FLV'} ) );
				}

				if(OneClipVideoListRenderer.SpecialPrerollObject) {
					if (OneClipVideoListRenderer.AutoPlay) {
						Playlist.GetInstance().Play( OneClipVideoListRenderer.GetSpecialPreroll() );
						OneClipVideoListRenderer.SpecialPrerollShown = true;
					}
					else {
						Playlist.FeaturedVideos.push( OneClipVideoListRenderer.GetSpecialPreroll() );
					}
				}
				
	            for (intOneClipVideo = 0; intOneClipVideo < OneClipVideoListRenderer.arrOneClipVideoList.length; intOneClipVideo++) {
		            objOneClipVideo = OneClipVideoListRenderer.arrOneClipVideoList[intOneClipVideo];
					if (objOneClipVideo['clipid']) {
						strTitle = objOneClipVideo['title'];
						intClipId = objOneClipVideo['clipid'];
						strFormat = objOneClipVideo['format'] ? objOneClipVideo['format'] : 'FLV';
						strUrl = objOneClipVideo['clippath'] ? objOneClipVideo['clippath'] : undefined;
						vidClip = new Video( {ClipId:intClipId, Title: strTitle, Format:strFormat, Url:strUrl});
						if (OneClipVideoListRenderer.AutoPlay && intOneClipVideo == 0 && !OneClipVideoListRenderer.SpecialPrerollObject) {
							Playlist.GetInstance().Play( vidClip );
						}
						else {
							Playlist.FeaturedVideos.push( vidClip );
						}
					}
		            else if (objOneClipVideo['episodeclips']) {
						for (intEpisodeClip = 0; intEpisodeClip < objOneClipVideo['episodeclips'].length; intEpisodeClip++) {
							objEpisodeClip = objOneClipVideo['episodeclips'][intEpisodeClip];
							strTitle = objEpisodeClip['title'];
							intClipId = objEpisodeClip['clipid'];
							strFormat = objEpisodeClip['format'] ? objEpisodeClip['format'] : 'FLV';
	    		            vidClip = new Video( {ClipId:intClipId, Title: strTitle, Format:strFormat});
							if (OneClipVideoListRenderer.AutoPlay && intEpisodeClip == 0 && !OneClipVideoListRenderer.SpecialPrerollObject) {
								Playlist.GetInstance().Play( vidClip );
							}
							else {
								Playlist.FeaturedVideos.push( vidClip );
							}
						}
		            }
				}
			}
        }
        return false;	
	},
	'PlayVideo' : function(intClip) {
		var vClip;
		var bPlayClip = true;
		var _playlist = OneClipVideoListRenderer.arrOneClipVideoList;
		var intClipId;
		var strFormat;
		var strUrl;
		
	    Player.GetInstance().Wait();
		Playlist.FeaturedVideos = new Array();
		//OneClipVideoListRenderer.AddClip({image:"http://images.ctvdigital.com/images/pub2upload/9/2009_2_13/leafs_021309a.jpg",title:"Frustration Mounts",description:"The Leafs have been struggling of late and the frustration is evident in some of the players.",clipid:139762,clippath:"mms://a180.v45862.c45862.g.vm.akamaistream.net/7/180/45862/44ce8bb3/ctvakamai2.insinc.com/wm/tsn/news/2009/02/13/leafs_021309.wmv",format:'FLV'});
		for (var i=intClip;i<_playlist.length;i++) {
			objClip=_playlist[i];
			intClipId=objClip.clipid;
			strFormat=objClip.format;
			strUrl=objClip.clippath;
			vClip = new Video({ClipId:intClipId,Format:strFormat,Url:strUrl});
			if (bPlayClip) {
				Playlist.GetInstance().Play( vClip );
			}
			else {
				Playlist.FeaturedVideos.push( vClip );
			}
			bPlayClip = false;
		}
		return true;
	}
};


function TradeCentre() {
	// Public variables
	
	// Private variables
	var self = this;
	
	// Public methods
	this.init = function() {
		OneClipVideoListRenderer.ThemePath = 'http://images.tsn.ca/datafiles/xml/OneClip/TradeCentreTheme.xml';
		OneClipVideoListRenderer.ThemePath = 'http://watch.tsn.ca/themes/TSN/player/theme.aspx';
		OneClipVideoListRenderer.AutoPlay = true;
		OneClipVideoListRenderer.LoadOneClipPlayer();
		OneClipVideoListRenderer.RenderList(self.renderer);
	}

	this.renderer = function(o,arrClipList) {
		var objClip;
		var intClipId;
		var strTitle,strDescription,strImage,strFormat;
		var objDescription,objImage,objAnchor,objH2,objDiv;
		var objTarget = document.getElementById('tsnTCVideoList');

		while(objTarget.childNodes.length) {
			objTarget.removeChild(objTarget.childNodes[0]);
		}

		for(var i=0; i<arrClipList.length;i++) {
			objClip = arrClipList[i];
			intClipId = objClip.clipid;
			strTitle = objClip.title;
			strDescription = objClip.description;
			strImage = objClip.image;
			strFormat = objClip.format;

			objDescription = _createElement('p',strDescription);
//			objImage = _createElement('img',null,{'src':strImage + '/80/60/image.ashx','width':'80','height':'60','alt':''});
			objImage = _createElement('img',null,{'src':strImage,'width':'80','height':'60','alt':''});
			objAnchor = _createElement('a',objImage,{'href':'#'});
			objAnchor.onclick = _bind(_triggerPlay,i);
			objH2 = _createElement('h2',strTitle);
			objDiv = _createElement('div',[objH2,objAnchor,objDescription],{'class':'tsnTCClip'});
			
			objTarget.appendChild(objDiv);
		}
	}
	this.rerender = function() {
		OneClipVideoListRenderer.RenderList(self.renderer);
	}

	
	// Private methods
		
	function _triggerPlay(i) {
		OneClipVideoListRenderer.PlayVideo(i);
	}
	function _bind(fn,args) {
		return function(){fn(args)};
	}
	function _createElement(t,v,a){ // t = tagname, v = tag value, a = attribute list
		var objTag = document.createElement(t);
		if (v != undefined && v != null) {
			if (typeof(v) == 'string') {
				v = document.createTextNode(v);
				objTag.appendChild(v)
			}
			else if(v.length > 0) {
				for (var i=0;i<v.length;i++) {
					objTag.appendChild(v[i]);
				}
			}
			else {
				objTag.appendChild(v);
			}
		}
		if (a != undefined) {
			for (var s in a) {
				var n = document.createAttribute(s);
				n.value = a[s];
				objTag.setAttributeNode(n);
			}
		}
		return objTag;
	}
}

