EpglistModule.prototype.startEpglist = function(){
	this.epglistDiv = document.getElementById("stationselect");
	
	/*this.epglistDiv = document.createElement("div");
	this.epglistDiv.style.width = this.width + "px";
	this.epglistDiv.style.height = (this.height - 12) + "px";
	this.epglistDiv.style.left = "0px";
	this.epglistDiv.style.top = "6px";
	this.epglistDiv.style.overflowX = "auto";
	this.epglistDiv.style.overflowY = "hidden";
	this.epglistDiv.style.position = "absolute";
	this.mainDiv.appendChild(this.epglistDiv);
	*/
	
	
	this.drawList(epgs[0].root);
	
	var div = document.createElement("div");
	div.style.position = "relative";
	div.style.height = "10px";
	div.style.width = "1px";
	this.epglistDiv.appendChild(div);
	
	//this.epglistDiv.style.backgroundAttachment = "fixed";
	//this.epglistDiv.style.backgroundPosition = "top left";
	//this.epglistDiv.style.backgroundRepeat = "no-repeat";
	
	// Another depressing hack for IE6
	if(!window.XMLHttpRequest){
		this.epglistDiv.style.backgroundPosition = "0px 0px";
	}
}

EpglistModule.prototype.drawList = function(epgId){
	//var epg = epgs.getById(epgId);
	var epg = epgs[0];
	
	if(epg != null){
		var epgroot = epgFolders.getById(epg.root);
		if(epgroot != null){
			for(var i=0; i<epgroot.epgFolders.length; ++i){
				var category = epgFolders.getById(epgroot.epgFolders[i]);
				if(category != null){
					// Move categories down by 7px.
					var cat = this.drawCategory(category, 1);
					cat.style.position = "relative";
					cat.style.top = "7px";
					this.epglistDiv.appendChild(cat);
					
				}
			}

			for(var i=0; i<epgroot.epgStations.length; ++i){
				var station = epgStations.getById(epgroot.epgStations[i]);
				if(station != null){
					this.epglistDiv.appendChild(this.drawStation(station));
				}
			}
			
			for(var i=0; i<epgroot.epgOndemandItems.length; ++i){
				var ondemandItem = epgOndemandItems.getById(epgroot.epgOndemandItems[i]);
				if(ondemandItem != null){
					this.epglistDiv.appendChild(this.drawOndemandItem(epgOndemandItem));
				}
			}
			
			// Crazy hack for IE
			this.epglistDiv.innerHTML = this.epglistDiv.innerHTML;
		}
	}
	this.epglistDiv.innerHTML = this.epglistDiv.innerHTML;
}

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );

EpglistModule.prototype.drawCategory = function(category, depth){
	var div = document.createElement("div");
	div.id = "cat_" + category.id;
	
	var a = document.createElement("a");
	a.href = "#";
	a.setAttribute("onclick", "player.EpglistFolderToggleEvent(" + category.id + "); return false;");
	a.setAttribute("class", "category");
	
	// Another moronic hack for IE
	a.setAttribute("className", "category");
	a.className = "category";
	
	div.appendChild(a);
	
	var img = document.createElement("img");
	img.id = "img_" + category.id;
	
	var margin = (depth-1) * 30 + 1;
	img.style.marginLeft = margin + "px";
	
	if(is_ie6)
	{
	    img.src = "img/Spacer.gif";
        img.style.border = "none";
        img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/epglist/closed.png',sizingMethod='image')";
    }
    else
    {
        img.src = "img/epglist/closed.png";
    }
    
	a.appendChild(img);
	
	var span = document.createElement("span");
	span.innerHTML = category.title;
	a.appendChild(span);
	
	var stationDiv = document.createElement("div");
	stationDiv.id = "stations_" + category.id;
	stationDiv.setAttribute("class", "stationblock");
	
	// Another moronic hack for IE
	stationDiv.setAttribute("className", "stationblock");
	stationDiv.className = "stationblock";
	
	div.appendChild(stationDiv);
	
	for(var i=0; i<category.epgFolders.length; ++i){
		var epgFolder = epgFolders.getById(category.epgFolders[i]);
		if(epgFolder != null){
			stationDiv.appendChild(this.drawCategory(epgFolder, depth+1));
		}
	}
	
	for(var i=0; i<category.epgStations.length; ++i){
		var epgStation = epgStations.getById(category.epgStations[i]);
		if(epgStation != null){
			stationDiv.appendChild(this.drawStation(epgStation, category.id, depth));
		}
	}
	
	for(var i=0; i<category.epgOndemandItems.length; ++i){
		var epgOndemandItem  = epgOndemandItems.getById(category.epgOndemandItems[i]);
		if(epgOndemandItem != null){
			stationDiv.appendChild(this.drawOndemandItem(epgOndemandItem, category.id, depth));
		}
	}
	
	// Crazy hack for IE.
	div.innerHTML = div.innerHTML;
	return div;
}

EpglistModule.prototype.drawStation = function(station, parentId, depth){
//sjekk om arrayet vi lager er større en 2, er det det så deler du, men ikke ellers
 
var link = document.createElement("a");
// maximum length for one line
maxlength = 12;
// split station title into an array
splitstationtitle = station.title.split(" ");

title = "";
// run trough all elements in array, build the string
// if length is > maxlength split the string

if(splitstationtitle.length > 3){
    for(i = 0; i< splitstationtitle.length; i++){
        title += splitstationtitle[i] + " " ;
        if(title.length >= maxlength ){
            title +='<br/>';
            maxlength += maxlength*2;
        } 
    }
station.title = title;
}

// set station title to the new splitted title


link.id = "cat_" + parentId + "_live_" + station.id;
link.setAttribute("class", "station");

	
	// Another moronic hack for IE
	link.setAttribute("className", "station");
	link.className = "station";
	
	link.href = "#";
	link.setAttribute("launchscript", "player.EpglistClickEvent(" + station.id + ", 'live', '" + this.defaultActionType + "', " + parentId + "); return false;");
	link.setAttribute("onclick", link.getAttribute("launchscript"));
	var span = document.createElement("span");
    var margin = depth*30+1;
 	span.style.marginLeft = margin.toString() + "px";
 	
 
	link.appendChild(span);
	span.innerHTML = station.title;

	return link;
}

EpglistModule.prototype.drawOndemandItem = function(ondemandItem, parentId, depth){
	var link = document.createElement("a");
		
	link.id = "cat_" + parentId + "_ondemand_" + ondemandItem.id;
	link.setAttribute("class", "station");
	
	// Another moronic hack for IE
	link.setAttribute("className", "station");
	link.className = "station";
	
	link.href = "#";
	link.setAttribute("launchscript", "player.EpglistClickEvent(" + ondemandItem.id + ", 'ondemand', '" + this.defaultActionType + "', " + parentId + "); return false;");
	link.setAttribute("onclick", link.getAttribute("launchscript"));
	
	var span = document.createElement("span");
	var margin = depth*30;
	span.style.marginLeft = margin.toString() + "px";
	link.appendChild(span);
	span.innerHTML = ondemandItem.title;
	
	return link;
}

EpglistModule.prototype.toggleCategory = function(categoryId){
	var stationDiv = document.getElementById("stations_" + categoryId);
	var img = document.getElementById("img_" + categoryId);
	if(stationDiv){
		if("none" == stationDiv.style.display || "" == stationDiv.style.display){
			stationDiv.style.display = "block";
			
			if(is_ie6) {
                img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/epglist/opened.png',sizingMethod='image')";
            } else {
                img.src = "img/epglist/opened.png";
            }
		}
		else{
			stationDiv.style.display = "none";
			
			if(is_ie6) {
                img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/epglist/closed.png',sizingMethod='image')";
            } else {
                img.src = "img/epglist/closed.png";
            }
			
		}
	}
	else
		alert("ding");
}

EpglistModule.prototype.playEvent = function(mediaId, mediaType, actionType, parentId){
	var links = document.getElementsByTagName("a");
	for(var i=0; i<links.length; ++i){
		if("selected" == links[i].getAttribute("class") || "selected" == links[i].getAttribute("className")){
			var id = parseInt(links[i].id.substring(links[i].id.lastIndexOf('_')+1), 10);
			links[i].setAttribute("class", "station");
			
			// Another moronic hack for IE
			links[i].setAttribute("className", "station");
			links[i].className = "station";
			
			links[i].setAttribute("onclick", links[i].getAttribute("launchscript"));
		}
	}
	
	var playing = document.getElementById("cat_" + parentId + "_" + mediaType + "_" + mediaId);
	if(playing){
		playing.setAttribute("class", "selected");
		
		// Another moronic hack for IE
		playing.setAttribute("className", "selected");
		playing.className = "selected";
		
		playing.setAttribute("onclick", "");
	}
	
	// Crazy hack for IE
	this.epglistDiv.innerHTML = this.epglistDiv.innerHTML;
}

EpglistModule.prototype.expandMediaChain = function(mediaId, mediaType){
	//alert("expanding media chain!");
	
	if("live" == mediaType){
		var station = epgStations.getById(mediaId);
			if(station != null){
			for(var i=0; i<station.parents.length; ++i){
				this.expandParents(station.parents[i]);
				this.playEvent(mediaId, mediaType, '', station.parents[i]);
				break;
			}
			//this.toggleCategory(station.parents[0]);
		}
	}
	else if("ondemand" == mediaType){
		var ondemanditem = epgOndemandItems.getById(mediaId);
		if(ondemanditem != null){
			for(var i=0; i<ondemanditem.parents.length; ++i){
				this.expandParents(ondemanditem.parents[i]);
				this.playEvent(mediaId, mediaType, '', ondemanditem.parents[i]);
				break;
			}
			//this.toggleCategory(ondemanditem.parents[0]);
		}
	}
}

EpglistModule.prototype.expandParents = function(categoryId){
	this.toggleCategory(categoryId);
	var category = epgFolders.getById(categoryId);
	if(category != null){
		for(var i=0; i<category.parents.length; ++i){
			if(!epgs.isRootFolder(category.parents[i])){
				this.expandParents(category.parents[i]);
			}
		}
	}
}

/*EpglistModule.prototype.startDefaultEvent = function(){
//	if(epgStations.first() != null)
//		alert(epgStations.first().title);
//	else
//		alert("null");
}*/

if(epglist){
	dojo.event.connect('before', window, 'onload', epglist, 'startEpglist');
	dojo.event.connect(player, 'EpgmodeClickEvent', epglist, 'drawEpgTree');
	dojo.event.connect(player, 'StartupPlayEvent', epglist, 'expandMediaChain');
	dojo.event.connect(player, 'LaunchDefaultEvent', epglist, 'startDefaultEvent');
	dojo.event.connect(player, 'EpglistClickEvent', epglist, 'playEvent');
	dojo.event.connect(player, 'EpglistFolderToggleEvent', epglist, 'toggleCategory');
}