var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function initXMLHTTP()
{
    var xmlhttp=false
    if (!xmlhttp && typeof XMLHttpRequest!="undefined") 
    {
        try { xmlhttp=new XMLHttpRequest();} 
        catch(e) { xmlhttp=false; }
    }
    if (!xmlhttp && window.createRequest) 
    {
        try { xmlhttp=window.createRequest(); } 
        catch(e) { xmlhttp=false; }
    }
    return xmlhttp;
}

function getPosts(feed,div)
{
    var xmlhttp=initXMLHTTP();
    if(!xmlhttp) return;

    xmlhttp.open("GET", feed, true);
    xmlhttp.onreadystatechange=function() 
    {
        if(xmlhttp.readyState==4) {
			
			
            var r=xmlhttp.responseText;
            if(r=="") return;
			
			var foot="<div class=\"copy\">&copy; 2003-2009 virtualization.info<br />"+
					 "<a href=\"http://www.virtualization.info/?full\">Go to full version</a></div>";
					 
            var list="<div id=\"list\"><ul>";
            var posts="";
            var i=0;

            var re=/<published>(.+?)-(.+?)-(.+?)T(.|\n)+?<title.+?>((.|\n)+?)<\/title><content.+?>((.|\n)+?)<\/content><link.+?rel='edit'.+?type='application\/atom\+xml'.+?href='(.+?)'.+?\/>/gim;
            
            var ar;
			//date = 1+2+3 - title = 5 - content = 7 - editurl = 9
           
            while ((ar=re.exec(r))!=null)
            { 
            	var postid=ar[9].replace("http://www.blogger.com/feeds/13562567/posts/default/","");
				list+="<li id=\"row"+i+"\"";
				if (i>9) list+=" style=\"display:none\"";
				list+=" onclick=\"showPost("+i+",false,'"+postid+"')\" ontouchstart=\"setActive(this)\" ontouchend=\"remActive(this)\" ontouchmove=\"cancActive(this)\"><div class=\"row\">"+
						ar[5]+"</div><div class=\"date\">"+
						months[parseInt(ar[2],10)-1]+" "+ar[3]+", "+ar[1]+"</div><div class=\"n\"></div></li>";
						
				posts+="<div id=\"post"+i+"\" class=\"post\">"+
						"<h3>"+ar[5]+"</h3><h4>"+months[parseInt(ar[2],10)-1]+" "+ar[3]+", "+ar[1]+
						"</h4><div class=\"main\">"+ref(ar[7])+"</div>" +
						"<div class=\"backli\" onclick=\"showPost("+i+",true,null)\" ontouchstart=\"setActive(this)\" ontouchend=\"remActive(this)\" ontouchmove=\"cancActive(this)\">Back to News Summary</div>"+foot+"</div>";
				i++;
				
			}

			if (i>9)
				list+="<li class=\"moreli\" onclick=\"showMore(this)\" ontouchstart=\"setActive(this)\" ontouchend=\"remActive(this)\" ontouchmove=\"cancActive(this)\">More News</li>";
			list+="</ul>"+foot+"</div>";	
				
      		try{ 
      			document.getElementById(div).innerHTML=list+posts; 
				document.getElementById("content").setAttribute("loaded", "true");
				window.scrollTo(0, 1);
				
      		}catch(e){}
      			
        }
    }
    xmlhttp.send(null);
}

function showMore(li)
{
	for(var i=10;i<100;i++)
	{
		try{document.getElementById("row"+i).style.display="block";}catch(e){break;}
	}
	li.style.display="none";
}

function ref(str)
{
	return str.replace(/&lt;/igm,"<").replace(/&gt;/igm,">").replace(/&amp;#160;/igm, " ");
}

function showPost(id, backward, postid)
{
	var l=document.getElementById("list");
	l.className = (backward ? "sLC" : "sCL");
	
		
	var p=document.getElementById("post"+id);
	p.style.display="block";
	p.className = (backward ? "post sCR" : "post sRC");
	
	l.style.left= (backward ? "0" : "-490px");
	p.style.left=(backward ? "490px" : "0");
	if (backward)
		setTimeout(function(){p.style.display="none";}, 600);
	setTimeout(function() {window.scrollTo(0, 1);}, 450);

}

function setActive(el){el.tag=setTimeout(function(){el.setAttribute("active","true");},100);}
function remActive(el){setTimeout(function(){el.removeAttribute("active");},100);}
function cancActive(el){clearTimeout(el.tag);}

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
    window.onload = func;
  else 
  {
    window.onload = function() 
    {
      if (oldonload) oldonload();
      func();
    }
  }
}

addLoadEvent(function(){
	getPosts("http://www.virtualization.info/feed.xml","content");
});

window.addEventListener("orientationchange", function() {
	var orientation = window.orientation;
	switch (orientation) {
		case 90:
		case -90:
			document.body.setAttribute("orient", "landscape");
			break;	
		default:
			document.body.setAttribute("orient", "portrait");
			break;
}}, false);

