EpginfoModule.prototype.startEpginfo = function(){
	this.epginfoDiv = document.getElementById("epginfo");
	
	this.topDiv = document.createElement("div");
	this.epginfoDiv.appendChild(this.topDiv);
	this.topDiv.style.position = "absolute";
	this.topDiv.style.left = "5px";
	this.topDiv.style.top = "3px";
	//this.topDiv.style.width = (parseInt(this.epginfoDiv.offsetWidth, 10) - 25 + "px");
	this.topDiv.style.width = (parseInt(this.epginfoDiv.offsetWidth, 10) - 3 + "px");
	this.topDiv.style.height = "15px";
	this.topDiv.align = "right";
	this.topDiv.style.backgroundImage = "url(" + epginfoTopImg + ")";
	this.topDiv.style.backgroundRepeat = "no-repeat";
	this.topDiv.style.backgroundPosition = "100% 0%";
	
	var img = document.createElement("img");
	this.topDiv.appendChild(img);
	img.src = folderClosedImg;
	
	var title = document.createElement("span");
	this.topDiv.appendChild(title);
	title.innerHTML = "Root";
	title.style.paddingLeft = "5px";
	title.style.paddingRight = "5px";
	title.style.fontWeight = "bold";
	title.style.fontSize = "1.1em";
		
	this.epgDiv = document.createElement("div");
	this.epginfoDiv.appendChild(this.epgDiv);
	this.epgDiv.className = "epginfolist";
	this.epgDiv.style.position = "absolute";
	this.epgDiv.style.left = "5px";
	this.epgDiv.style.top = "25px";
	//this.epgDiv.style.width = (parseInt(this.epginfoDiv.offsetWidth, 10) - 25 + "px");
	this.epgDiv.style.width = (parseInt(this.epginfoDiv.offsetWidth, 10) - 5 + "px");
	this.epgDiv.style.height = (parseInt(this.epginfoDiv.offsetHeight, 10) - 25 + "px");
}

EpginfoModule.prototype.showFolder = function(folderId){
	var folder = epgFolders.getById(folderId);
	if(folder != null){
		this.topDiv.getElementsByTagName("span")[0].innerHTML = folder.title;
		
		this.epgDiv.innerHTML = "";
		
		this.epginfoDiv.style.overflowX = "hidden";
		this.epginfoDiv.style.overflowY = "hidden";
		this.epgDiv.style.overflowX = "hidden";
		this.epgDiv.style.overflowY = "auto";
		
		var ul = document.createElement("ul");
			
		for(var i=0; i<folder.epgStations.length; ++i){
			ul.appendChild(this.drawItem(epgStations[folder.epgStations[i]], "live"));
			
			if(folder.epgStations.length-1 != i){
				ul.appendChild(this.getSeparator());
			}
		}
		
		if(folder.epgOndemandItems.length > 0){
			ul.appendChild(this.getSeparator());
		}
		
		for(var i=0; i<folder.epgOndemandItems.length; ++i){
			ul.appendChild(this.drawItem(epgOndemandItems[folder.epgOndemandItems[i]], "ondemand"));
			
			if(folder.epgOndemandItems.length-1 != i){
				ul.appendChild(this.getSeparator());
			}
		}
		
		while(this.epgDiv.hasChildNodes()){
			this.epgDiv.removeChild(this.epgDiv.firstChild);
		}
		this.epgDiv.appendChild(ul);
	}
}

EpginfoModule.prototype.getSeparator = function(){
	var separator = document.createElement("li");
	var img = document.createElement("div");
	img.style.backgroundImage = "url(" + borderImg + ")";
	img.style.backgroundRepeat = "repeat-x";
	
	// Line up the border background image properly
	var w = parseInt(this.epgDiv.style.width, 10);
	while(w%6 != 0){
		w--;
	}
	
	img.style.width = w+"px";
		
	img.style.height = "4px";
	img.style.marginLeft = "auto";
	img.style.marginRight = "auto";
	separator.appendChild(img);
	separator.style.paddingTop = "4px";
	separator.style.paddingBottom = "4px";
	return separator;
}

EpginfoModule.prototype.drawItem = function(item, itemType){
	var li = document.createElement("li");
	li.id = item.id + itemType;
	
	var table = document.createElement("table");
	table.style.width = "100%";
	li.appendChild(table);
	
	// Image and title row
	var row1 = document.createElement("tr");
	table.appendChild(row1);

	// Title cell
	var titleCell = document.createElement("td");
	titleCell.style.paddingLeft = "2px";
	//titleCell.style.textAlign = "right";
	titleCell.style.width = "60%";
	
	var link = document.createElement("a");
	titleCell.appendChild(link);
	link.href = "javascript:void(0);";
	link.setAttribute("onclick", "player.EpginfoPlayEvent(" + item.id + ", '" + itemType + "');");
	link.style.fontWeight = "bold";
	link.innerHTML = item.title;
	
	// Image cell
	var imageCell = document.createElement("td");
	imageCell.setAttribute("rowspan", 2);
	imageCell.style.textAlign = "center";
	
	var imageLink = document.createElement("a");
	imageCell.appendChild(imageLink);
	imageLink.href = "javascript:void(0);";
	imageLink.setAttribute("onclick", "player.EpginfoPlayEvent(" + item.id + ", '" + itemType + "');");
	
	var img = document.createElement("img");
	//img.style.width = this.smallImageWidth;
	//img.style.height = this.smallImageHeight;
	if(item.smallImageUrl.length > 0){
		img.src = item.smallImageUrl;
	}
	else if(item.imageUrl.length > 0){
		img.src = item.imageUrl;
	}
	else{
		img.src = placeholderImg;
	}
	imageLink.appendChild(img);
	//img.style.width = img.width;
	//img.style.height = img.height;
	
	// Show/hide link row
	var row2 = document.createElement("tr");
	table.appendChild(row2);
	
	// Show/hide link cell
	var expandCell = document.createElement("td");
	expandCell.style.paddingLeft = "2px";
	//expandCell.style.textAlign = "right";
	expandCell.style.width = "60%";
	
	var expandLink = document.createElement("a");
	expandCell.appendChild(expandLink);
	expandLink.href = "javascript:void(0);";
	expandLink.style.fontSize = "0.9em";
	expandLink.setAttribute("onclick", "epginfo.expandItem('" + item.id + itemType + "');");
	expandLink.innerHTML = "+ show information";
	expandLink.style.color = "#000000";
	expandLink.style.fontWeight = "bold";
	expandCell.appendChild(expandLink);
	
	// Display order.
	row1.appendChild(titleCell);
	row1.appendChild(imageCell);
	
	row2.appendChild(expandCell);
	
	
	// Shown/hidden information
	var ul = document.createElement("ul");
	
	var desc = document.createElement("li");
	desc.innerHTML = item.description;
	desc.style.paddingTop = "5px";
	desc.style.paddingBottom = "5px";
	ul.appendChild(desc);
	
	var playItem = document.createElement("li");
	
	var rightArrow = document.createElement("img");
	playItem.appendChild(rightArrow);
	rightArrow.src = launchArrowRight;
	
	var playLink = document.createElement("a");
	playItem.appendChild(playLink);
	playLink.style.marginLeft = "4px";
	playLink.style.marginRight = "4px";
	playLink.href = "javascript:void(0);";
	playLink.setAttribute("onclick", "player.EpginfoPlayEvent(" + item.id + ", '" + itemType + "');");
	playLink.innerHTML = "Play";
	playLink.style.fontWeight = "bold";
	ul.appendChild(playItem);
	
	var leftArrow = document.createElement("img");
	playItem.appendChild(leftArrow);
	leftArrow.src = launchArrowLeft; 
	
	ul.style.display = "none";
	li.appendChild(ul);
	
	/*if(item.imageUrl.length > 0){
		var img = document.createElement("img");
		img.style.width = this.imageWidth;
		img.style.height = this.imageHeight;
		img.src = item.imageUrl;
		img.setAttribute("onclick", "epginfo.expandItem('" + item.id + itemType + "');");
		li.appendChild(img);
	}
	else{
		var img = document.createElement("img");
		img.style.width = this.imageWidth;
		img.style.height = this.imageHeight;
		img.src = placeholderImg;
		img.setAttribute("onclick", "epginfo.expandItem('" + item.id + itemType + "');");
		li.appendChild(img);
	}
	
	var link = document.createElement("a");
	link.href = "javascript:void(0);";
	link.setAttribute("onclick", "epginfo.expandItem('" + item.id + itemType + "');");
	link.innerHTML = item.title;
	//link.style.fontStyle = "bold";
	li.appendChild(link);
	
	var ul = document.createElement("ul");
	
	var desc = document.createElement("li");
	desc.innerHTML = item.description;
	ul.appendChild(desc);
	
	var playItem = document.createElement("li");
	var playLink = document.createElement("a");
	playLink.href = "javascript:void(0);";
	playLink.setAttribute("onclick", "player.EpginfoPlayEvent(" + item.id + ", '" + itemType + "');");
	playLink.innerHTML = "Play";
	playItem.appendChild(playLink);
	ul.appendChild(playItem);
	
	ul.style.display = "none";
	li.appendChild(ul);*/
		
	// Crazy hack for IE
	li.innerHTML = li.innerHTML;
	
	return li;
}

EpginfoModule.prototype.expandItem = function(elementId){
	var li = document.getElementById(elementId);
	
	if(li){
		var ul = li.getElementsByTagName("ul");
		ul[0].style.display = "block";
		
		var link = li.getElementsByTagName("a");
		link[2].innerHTML = "- hide information";
		link[2].setAttribute("onclick", "epginfo.collapseItem('" + elementId + "');");
		
	/*	var img = li.getElementsByTagName("img");
		if(img.length > 0){
			img[0].setAttribute("onclick", "epginfo.collapseItem('" + elementId + "');");
		}
		else{
			
		}
		
		var link = li.getElementsByTagName("a");
		link[0].setAttribute("onclick", "epginfo.collapseItem('" + elementId + "');");*/
		
		// Crazy hack for IE
		li.innerHTML = li.innerHTML;
	}
}

EpginfoModule.prototype.collapseItem = function(elementId){
	var li = document.getElementById(elementId);
	
	var ul = li.getElementsByTagName("ul");
	ul[0].style.display = "none";
	
	var link = li.getElementsByTagName("a");
	link[2].innerHTML = "+ show information";
	link[2].setAttribute("onclick", "epginfo.expandItem('" + elementId + "');");
		
	/*var img = li.getElementsByTagName("img");
	if(img.length > 0){
		img[0].setAttribute("onclick", "epginfo.expandItem('" + elementId + "');");
	}
	else{
		
	}
	
	var link = li.getElementsByTagName("a");
	link[0].setAttribute("onclick", "epginfo.expandItem('" + elementId + "');");*/
	
	// Crazy hack for IE
	li.innerHTML = li.innerHTML;
}

EpginfoModule.prototype.showItem = function(mediaId, mediaType, actionType, parentId){
	var folder = epgFolders.getById(parentId);
	if(folder != null){
		this.topDiv.getElementsByTagName("span")[0].innerHTML = folder.title;
		
		this.epgDiv.innerHTML = "";
		
		this.epginfoDiv.style.overflowX = "hidden";
		this.epginfoDiv.style.overflowY = "hidden";
		
		var item;
		
		if("live" == mediaType){
			item = epgStations.getById(mediaId);
		}
		else if("ondemand" == mediaType){
			item = epgOndemandItems.getById(mediaId);
		}
		
		var center = document.createElement("center");
		this.epgDiv.appendChild(center);
		
		var img = document.createElement("img");
		//img.style.width = this.largeImageWidth;
		//img.style.height = this.largeImageHeight;
		if(item.smallImageUrl.length > 0){
			img.src = item.largeImageUrl;
		}
		else if(item.imageUrl.length > 0){
			img.src = item.imageUrl;
		}
		else{
			img.src = placeholderImg;
		}
		
		center.appendChild(img);
		//img.style.width = img.width;
		//img.style.height = img.height;
		
		this.epgDiv.appendChild(document.createElement("br"));
		this.epgDiv.appendChild(document.createElement("br"));
		
		var infoheading = document.createElement("span");
		infoheading.style.fontWeight = "bolder";
		infoheading.innerHTML = "Channel information:";
		this.epgDiv.appendChild(infoheading);
		
		this.epgDiv.appendChild(document.createElement("br"));
		this.epgDiv.appendChild(document.createElement("br"));
		
		var infotext = document.createElement("span");
		infotext.innerHTML = item.description;
		this.epgDiv.appendChild(infotext);
	}
	
	//this.showFolder(parentId);
	//this.expandItem(mediaId+mediaType);
	
	/*if("live" == mediaType){
		this.expandItem(mediaId);
	}
	else if("ondemand" == mediaType){
		this.expandItem(mediaItem);
	}
	else{
		alert("Unknown media type.");
	}*/
}

EpginfoModule.prototype.expandStartupItem = function(mediaId, mediaType){
	this.expandItem(mediaId+mediaType);
}

if(epginfo){
	dojo.event.connect('before', window, 'onload', epginfo, 'startEpginfo');
	dojo.event.connect(player, 'EpglistFolderToggleEvent', epginfo, 'showFolder');
	dojo.event.connect(player, 'EpglistClickEvent', epginfo, 'showItem');
	dojo.event.connect('after', player, 'StartupPlayEvent', epginfo, 'expandStartupItem');
	dojo.event.connect(player, 'LaunchDefaultEvent', epginfo, 'expandDefaultItem');
}