﻿// JavaScript Document

function SampleRoll(){
	if(arguments.length<1){
		alert("SampleRoll Object needs one DOM object as argument.");
		return;
	}
	
	var object=null;
	if((typeof(arguments[0])=="string")&&arguments[0].constructor==String){
		object=document.getElementById(arguments[0]);
	}else{
		object=arguments[0];
	}
	if(object){
		this.container=new StyleControl(object);
		this.list=new Array();
		var items=object.getElementsByTagName("li");
		for(var i in items){
			if(items[i].tagName && items[i].tagName.toLowerCase()=="li"){
				var temp=new StyleControl(items[i]);
				this.list.push(temp);
			}
		}
		this.current=1;
		this.prev=0;
		this.next=2;
	}
}

SampleRoll.prototype={
	container:null,
	list:null,
	current:null,
	prev:null,
	next:null,
	
	moveprev:function(){
		this.move("/ajax/latestprojects.php?page="+this.prev,this.Parse);
	},
	movenext:function(){
		this.move("/ajax/latestprojects.php?page="+this.next,this.Parse);
	},
	move:function(url,callback){
		var _xmlhttp=null;
		if (window.XMLHttpRequest) {
        	_xmlhttp = new XMLHttpRequest();
        	_xmlhttp.onreadystatechange = function(){
					if(_xmlhttp.readyState==4){
						if(_xmlhttp.status==200){
							callback(_xmlhttp.responseXML);
						}else{
							alert("Cann't get XML about Latest Projects");
						}
					}
			}
        	_xmlhttp.open("GET", url, true);
        	_xmlhttp.send(null);
    	} else if (window.ActiveXObject) {
        	_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	if (_xmlhttp) {
            	_xmlhttp.onreadystatechange = function(){
						if(_xmlhttp.readyState==4){
							if(_xmlhttp.status==200){
								callback(_xmlhttp.responseXML);
							}else{
								alert("Cann't get XML about Latest Projects");
							}
						}
				}
            	_xmlhttp.open("GET", url, true);
            	_xmlhttp.send(null);
        	}
    	}
	},
	Parse:function(xml){
		var object=eval(ObjectRegister.objectSampleRoll);
		var xmldoc=xml;
		object.current=(xmldoc.getElementsByTagName("current")[0]).firstChild.nodeValue;
		object.prev=(xmldoc.getElementsByTagName("previous")[0]).firstChild.nodeValue;
		object.next=(xmldoc.getElementsByTagName("next")[0]).firstChild.nodeValue;
		var items=xmldoc.getElementsByTagName("item");
		var temp=new Array();
		for(var i=0;i<items.length;i++){
			var id=items[i].getElementsByTagName("id")[0].firstChild.nodeValue;
			var topic=items[i].getElementsByTagName("topic")[0].firstChild.nodeValue;
			var url=items[i].getElementsByTagName("url")[0].firstChild.nodeValue;
			var breviary=items[i].getElementsByTagName("breviary")[0].firstChild.nodeValue;
			var img=document.createElement("img");
			img.src=breviary;
			img.alt=topic;
			var a=document.createElement("a");
			a.href=url;
			a.appendChild(img);
			var li=document.createElement("li");
			li.appendChild(a);
			temp.push(li);
		}
		object.list=temp;
		object.container.clearObject();
		for(var j=0;j<object.list.length;j++){
			object.container.appendObject(object.list[j]);
		}
	}
}
