function xpath(doc,path,node){
	var nodes;
	
	if(doc.evaluate){
		if(node!=null){
			nodes=doc.evaluate(path,node,null,XPathResult.ANY_TYPE,null);
		}else{
			nodes=doc.evaluate(path,doc,null,XPathResult.ANY_TYPE,null);
		}
		
		var temp=new Array();
		var node=nodes.iterateNext();
		while(node){
			temp.push(node);
			node=nodes.iterateNext();
		}
		
		nodes=temp;
	}else{
		if(node!=null){
			nodes=node.selectNodes(path);
		}else{
			nodes=doc.selectNodes(path);
		}
	}
	
	return nodes;
}
function XML_load(url,callback){
	var created=false;
	var request;
	
	if(window.XMLHttpRequest){		
		request=new XMLHttpRequest();
		created=true;
	}else if(window.ActiveXObject){
		request=new ActiveXObject("Microsoft.XMLHTTP");
		created=true;
 	}
	
	if(created==true){
		request.onreadystatechange=function(){
			XML_onReadystatechange(request,callback);
		}
		request.open("GET",url);
		request.send(null);
	}else{
		alert("cant create xml parser");
	}
}
function XML_onReadystatechange(request,callback){
	if(request.readyState==4){
		callback(request);
	}
}

Menu=function(){
	this.menu_document=null;
	this.div=null;
	this.menu_id="menu-ul";
}
Menu.prototype.build=function(menu_document,div){
	this.menu_document=menu_document;
	this.div=div;
	
	this.clear();
	
	var menu_node=xpath(menu_document,"menu")[0];
	var menu=this.create_menu(menu_node);
	
	this.show_menu(menu);	
}
Menu.prototype.clear=function(){
	var menu_ul=document.getElementById(this.menu_id);
	if(menu_ul!=null){
		this.div.removeChild(menu_ul);
	}
}
Menu.prototype.show_menu=function(menu){
	this.div.appendChild(menu);
}
Menu.prototype.create_menu=function(node){
	var ul=document.createElement("ul");
	ul.id="menu-ul";
	ul.className="horizontal-display";
	
	var items=xpath(this.menu_document,"item",node);
	var item;
	
	var back_node=node.parentNode;
	
	if(back_node.parentNode==null){
		back_node=null;
	}
	
	if(back_node!=null){
		var back_li=document.createElement("li");
		var back_li_text_node=document.createTextNode("back");
		back_li.className="back";
		back_li.appendChild(back_li_text_node);
		
		var p=this;
		back_li.onclick=function(event){
			var menu=p.create_menu(back_node);
			p.clear();
			p.show_menu(menu);
		}
		
		ul.appendChild(back_li);
	}
	
	for(var i=0;i<items.length;i++){
		item=this.create_item(items[i]);
		
		if(i==items.length-1){
			item.className="last";
		}
		
		ul.appendChild(item);
	}
	
	return ul;
}
Menu.prototype.create_item=function(node){
	var li=document.createElement("li");
	
	var title_node=node.getElementsByTagName("title")[0];
	var title_text=title_node.firstChild.nodeValue;
	var title_text_node=document.createTextNode(title_text);
	li.appendChild(title_text_node);
	
	var link_nodes;
	var video_nodes;
	var item_nodes;
	
	video_nodes=node.getElementsByTagName("video");
	
	if(video_nodes.length>0){
		li.onclick=function(event){
			change_video(video_nodes[0].firstChild.nodeValue);
		}
	}
	
	item_nodes=node.getElementsByTagName("item");
		
	var p=this;
	if(item_nodes.length>0){			
		li.onclick=function(event){
			var menu=p.create_menu(node);
			p.clear();
			p.show_menu(menu);
		}
	}
	
	link_nodes=node.getElementsByTagName("link");
	
	if(link_nodes.length>0){
		li.onclick=null;
		li.removeChild(title_text_node);
		var li_a=document.createElement("a");
		li_a.setAttribute("href",link_nodes[0].firstChild.nodeValue);
		li_a.appendChild(title_text_node);
		li.appendChild(li_a);
	}
	
	return li;
}


News_Parser=function(){
}
News_Parser.prototype.build=function(data,div){
	var items_array=data.getElementsByTagName("item");
	
	var i;
	var news_box;
	
	for(i=0;i<items_array.length;i++){
		news_box=this.create_newsbox(items_array[i]);
		div.appendChild(news_box);
	}
}
News_Parser.prototype.create_newsbox=function(node){
	var news_box=document.createElement("div");
	news_box.id=node.getElementsByTagName("guid")[0].firstChild.nodeValue;
	news_box.className="news-box";
	
	var color=node.getElementsByTagName("color")[0];
	
	if(color==null){
		color=node.getElementsByTagName("rmedia:color")[0];
	}
	
	var bgColor=node.getElementsByTagName("background-color")[0];
	
	if(bgColor==null){
		bgColor=node.getElementsByTagName("rmedia:background-color")[0];
	}
	
	if(color!=null){
		news_box.style.color=color.firstChild.nodeValue;
	}
	
	if(bgColor!=null){
		news_box.style.backgroundColor=bgColor.firstChild.nodeValue;
	}
	
	var title=document.createElement("h1");
	title.className="title";
	var title_text=node.getElementsByTagName("title")[0].firstChild.nodeValue
	var title_text_node=document.createTextNode(title_text);
	title.appendChild(title_text_node);
	news_box.appendChild(title);
	
	var date=document.createElement("div");
	date.className="date";
	var date_text=node.getElementsByTagName("pubDate")[0].firstChild.nodeValue
	var date_text_node=document.createTextNode(date_text);
	date.appendChild(date_text_node);
	news_box.appendChild(date);
	
	var img=document.createElement("img");
	img.className="news-image";
	img.src="news/"+news_box.id+".jpg";
	img.alt=news_box.id;
	news_box.appendChild(img);
	
	var info=document.createElement("p");
	info.className="info";
	var info_text=node.getElementsByTagName("description")[0].firstChild.nodeValue
	
	//var info_text_node=document.createTextNode(info_text);
	//info.appendChild(info_text_node);
	
	//I dont like using innerHTML
	info.innerHTML=info_text;
	news_box.appendChild(info);
	
	return news_box;
}


function main(){
	var no_cache=new Date();
	
	var menu="/menu.xml?nocache="+no_cache.getTime();
	//var news_feed="/news/rss.xml?nocache="+no_cache.getTime();
	
	XML_load(menu,menu_onLoad);
	//XML_load(news_feed,rss_onLoad);
}
function menu_onLoad(request){
	var data=request.responseXML;
	
	var navigation_div=document.getElementById("navigation");
	
	var menu=new Menu();
	menu.build(data,navigation_div);
}
function rss_onLoad(request){
	var data=request.responseXML;
	
	var news_div=document.getElementById("news-content");
	
	if(news_div!=null){
		var news=new News_Parser();
		news.build(data,news_div);
	}
}
function change_video(video){
	var video_object=document.getElementById("video");
	
	if(video_object!=null){
		video_object.set_video(video);
	}else{
		window.location="/?video="+video;
	}
}

window.onload=main;