/*
 * Object parser XML v0.2
 * Author: Tommy Lesmeister
 * Company: Goes & Roos
 */
 
/*
 * constructor..
 */
function grobParser( options ) {
	this.fileName = "../huizen/SpecialObjects.xml";
	this.fileNameRoot = "huizen/SpecialObjects.xml";

	this.items = { img:"hoofdfoto", plaats:"plaatsnaam", adres:"straatnaam,huisnummer", prijs:"prijssoort,prijs,prijsconditie" };
	this.sep = ' ';
	this.max_items = false; // Tommy +- 14-04-2010 Toevoeging max items
	
	// over-rule me...
	if( typeof options["items"] != "undefined" ) this.items = options["items"];
	if( typeof options["seperator"] != "undefined" ) this.sep = options["seperator"];
	if( typeof options["filename"] != "undefined" )	this.fileName = options["filename"];
	if( typeof options["max_items"] != "undefined" ) this.max_items = parseInt(options["max_items"]); // Tommy +- 14-04-2010 Toevoeging max items
	// Einde over-rulen
	
	if( window.XMLHttpRequest ) {
		this.xml_obj = new XMLHttpRequest()
	} else {
		this.xml_obj = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	this.xml_obj.open("GET",this.fileName,false);
	this.xml_obj.send("");
	
	
	if( this.xml_obj.readyState == 4 ) {
		if( this.xml_obj.status != 200 ) {
			this.xml_obj.open("GET",this.fileNameRoot,false);
			this.xml_obj.send("");
		}
	}
	
	this.xmlDoc = this.xml_obj.responseXML;

	// begin de iterator
	this.parse();
	
	return this;
}

/*
 * stripHTML, verwijdert alle html tags uit een str.
 */
grobParser.prototype.strip_html = function(src) { return src.replace( /<(?:.|\s)*?>/g, "" ) };

/*
 * get, mischien moeten we nog bij de data kunnen ?
 */
grobParser.prototype.get = function() { return this.xmlDoc; };

/*
 * cast_array, zet een object om naar een array( was de intentie, maar is dubbel werk )
 */
grobParser.prototype.cast_array = function( obj ) {
	arr = [];
	
	for(var a=0,l=this.max_items;a<l;++a)
		arr[arr.length]=obj[a];
	
	return arr;
};

/*
 * obj_rand, haalt een random obj uit de objecten array en returned deze ;)
 */
grobParser.prototype.obj_rand = function( obj ) {
	arr = [];
	arr[arr.length] = obj[ Math.floor( Math.random()*obj.length ) ];
	return arr;
};

grobParser.prototype.hasNodeVal = function( n ) {
	if( typeof n == "undefined" ) return false;
	
	return ( !n.nodeValue && (!n.childNodes||!n.childNodes.item(0).nodeValue) ) ? false : true;
};

/*
 * parser, iterate door de xmlnodes en gooit ze in een div
 */
grobParser.prototype.parse = function() {
	var node, nodes = this.xmlDoc.getElementsByTagName("object");
	var tmp, content = "";
	
	// Als er geen nodes gevonden zijn hoeven we niks te doen toch?
	if( nodes.length <= 0 ) {
		document.getElementById( "specContainer" ).style.display = 'none';
		return;
	}
	
	// Tommy +- 16-04-2010, toevoeging voor een random object
	if( this.max_items==1 ) {
		nodes = this.obj_rand(nodes);
	}
	
	//alert( this.max_items );

	// Tommy +- 16-04-2010, toevoeging voor een limiet opgeven.
	if( this.max_items>=2 ) {
		if( this.max_items < nodes.length ) { // BugFix, items die kleiner of gelijk zijn aan de lengte mogen gecast worden anders niet.
			nodes = this.cast_array( nodes );
		}
	}
	
	// Loop door de objecten heen
	for(var n=0,l=nodes.length;n<l;++n) {
		node = nodes[n];
		
		className = (n%2==0)?"spec_odd":"spec_even";
		tmp = node.getElementsByTagName("deeplink")[0].childNodes[0].nodeValue; // was link
		
		// 25-03-2010 Tommy toevoeging:
		if(n==0) className += " firstSpec";
		if(n==nodes.length-1) className += " lastSpec";
		
		content += '<div onclick="window.location=\''+tmp+'\'"class="'+className+'">';
		
		for(var i in this.items) {
			// TEMP Fix voor TEKST
			if( this.items[i]=="" ) continue;
			
			if(i=='img') {
				content += '<img class="spec_img" src="'+node.getElementsByTagName(this.items[i])[0].childNodes[0].nodeValue+'" alt="">';
				continue;
			}
			
			content += '<div class="spec_'+i+'">';
			
			if(this.items[i].indexOf(",") != -1) {
				tmp = this.items[i].split(",");
				for(var t=0,tl=tmp.length;t<tl;++t) {
					if(tmp[t]=="prijs") content += "&euro; ";
					
					if(node.getElementsByTagName(tmp[t]).length>0) 
					//{
					if( tmp[t]=="pic0tekst" ) {
							if( node.getElementsByTagName(tmp[t])[0].childNodes[0].nodeValue.indexOf("KENMERKEN") != -1 ) continue;
					}
					//}
						content += this.strip_html( node.getElementsByTagName(tmp[t])[0].childNodes[0].nodeValue ) + this.sep;
				}
			} else {
				if( this.hasNodeVal( node.getElementsByTagName(this.items[i])[0] ) ) 				
					content += this.strip_html( node.getElementsByTagName(this.items[i])[0].childNodes[0].nodeValue );
			}
			content += '</div>';
		}
		content += '</div>';
	}
	
	document.getElementById( "specContainer" ).innerHTML += content;
};
/*
var grSpecialInit = function() {
	var grSpecial = new grobParser({});// { items:{ img:"hoofdfoto", plaats:"plaatsnaam" } } );
};


if (window.addEventListener) {
	window.addEventListener( "load",  grSpecialInit, false );
} else if( window.attachEvent ) {
	window.attachEvent( "onload",  grSpecialInit );
} else {
	window.onload = function() {  grSpecialInit(); }
}
*/