var channelContents = null;
		var loadedCount = 0;
		var thumb_id = 0;

		window.onload = function(){
			request = 'http://ob.octopusmediatechnology.com/API/Nia/channels.svc/json/6a9d345c-b90c-4dc7-988a-639e69acc5b2/Contents/AddChannel/Max/10?method=getChannelContents';
			addRequest = new JSONscriptRequest(request);
			addRequest.buildScriptTag();
			addRequest.addScriptTag();
		}
		
		function checkLoaded()
		{
			loadedCount++;
			if(loadedCount == channelContents.Assets.length){
				if(document.getElementById('omt-loading-indicator').style.display = "block"){
				document.getElementById('omt-loading-indicator').style.display = "none";
				}
				document.getElementById('omt-video-contents').style.display = "block";
			}
		}
		
		function getChannelContents(channelJson)
		{
			channelContents = channelJson;
			addRequest.removeScriptTag();
			loadContainer();
		}	
		
		function loadContainer(){	
			//get a reference to the container div, and title element and setup the width value to be sent to the thumbnail service.
			var tc = document.getElementById('omt-video-contents');
			var width = 160;
			var thumbnailWidth = 0;
			
			// Loop through all of the assets in a channel, and add an image to the container div, setting the alt tag, and mouseover event on each element.
			//channelContents.Assets.length
			for(i = 0;i<channelContents.Assets.length;i++)
			{
				var videoItem = document.createElement('div');
				videoItem.setAttribute("class", "omt-video-item");
				videoItem.setAttribute("className", "omt-video-item"); 
				
				var videoImage = document.createElement('img');
				guid = channelContents.Assets[i].AssetGuid;
				videoImage.src = "http://niathumbnails.octopusmt.com/defaultthumbnail.ashx?assetguid="+guid+"&width="+width;
				videoImage.alt = channelContents.Assets[i].Title;
				videoImage.title = channelContents.Assets[i].Title;
				videoImage.onload = checkLoaded;
				videoImage.id = "octopus-thumbnail-"+thumb_id;
				videoImage.onclick = function(e) { openFlashPlayer(e) };
				videoItem.appendChild(videoImage);
				
				var videoDetails = document.createElement('div');
				videoDetails.setAttribute("class", "omt-video-item-details");
				videoDetails.setAttribute("className", "omt-video-item-details");
				var titleText = document.createElement('p');
				titleText.innerHTML = "<strong>Title </strong>"+channelContents.Assets[i].Title;
				videoDetails.appendChild(titleText);
				
				var playButton = document.createElement('input');
				playButton.type = "button";
				playButton.value = "Play";
				playButton.title = channelContents.Assets[i].Title;
				playButton.id = "play-video-"+thumb_id++;
				playButton.onclick = function(e) { openFlashPlayer(e) };
				videoDetails.appendChild(playButton);
				
				var descTitle = document.createElement('p');
				descTitle.innerHTML = "<strong>Description</strong>";
				videoDetails.appendChild(descTitle);
				
				var desc = channelContents.Assets[i].Description;
				if(desc != null){
					var hasLink = (desc.indexOf("http://") != -1) ? true : false;
					var descriptionText = (hasLink) ? desc.substring(0,desc.indexOf("http://")) : desc;
			
					var descText = document.createElement('p');
					descText.innerHTML = descriptionText;
					descText.setAttribute("class", "omt-video-item-detail-description");
					descText.setAttribute("className", "omt-video-item-detail-description");
					videoDetails.appendChild(descText);
					
					if(hasLink){
						var relTitle = document.createElement('p');
						relTitle.innerHTML = "<strong>Related Link</strong>";
						videoDetails.appendChild(relTitle);
						
						var linkUrl = desc.substring(desc.indexOf("http://"),desc.length);
						var relLink = document.createElement('a');
						relLink.href = linkUrl;
						relLink.target = "_blank";
						relLink.appendChild(document.createTextNode(linkUrl));
						videoDetails.appendChild(relLink);
					}
				}else{
				}
		
				videoItem.appendChild(videoDetails);
				tc.appendChild(videoItem);
			}
		}
		
		function openFlashPlayer(e)
		{
			OctopusMT.OpenFlashWindow(getItem(e).AssetGuid,'omt-video-holder',channelContents.ChannelGuid);
		}
		
		function getItem(e){
			
			var eventType;
			if(e === undefined){
				eventType = event.srcElement;
			}else if(e.target === undefined){
				eventType = event.srcElement;
			}else{
				eventType = e.target;
			}
			
			var asset = null;
				for(i = 0;i<channelContents.Assets.length;i++)
				{
					if(eventType.title == channelContents.Assets[i].Title){
						asset = channelContents.Assets[i];
						break;
					}
				}
			return asset;
		}