/* Requires prototype.lite.js */
var Xhr = Class.create();
Xhr.prototype = {
	initialize: function() {
		this.req = false;
	},
	loadXML: function(url, send, callback, async, method) {		
		method = method == null ? "GET" : method;
		send = send == null ? "" : send;
		async = async == null ? true : async;
		callback = callback == null ? this.dummyCallback : callback; 	
	 	
	    if(window.XMLHttpRequest) {
	    	try {
				this.req = new XMLHttpRequest();
	        } catch(e) {
				this.req = false;
	        }	    
	    } else if(window.ActiveXObject) {
	    	
	       	try {
	        	this.req = new ActiveXObject("Msxml2.XMLHTTP");
	      	} catch(e) {
	        	try {	        		
	          		this.req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} catch(e) {
	          		this.req = false;
	        	}
			}
	    }
		if(this.req) {				
			this.req.onreadystatechange = callback;
			this.req.open(method, url, async);
			this.req.send(send);
			if(async == false) {				
				return this.req.responseXML;
			}
		}
	},
	loadSync: function(url) {	
		return this.loadXML(url, null, null, false, "GET");		
	},
	loadAsync: function(url, callback) {
		return this.loadXML(url, null, callback, true, "GET");
	},
	dummyCallback: function() { /* IE appears to require a callback */ }
};

function xmlCards2JsCards(x) {
	var cards = [];
	var ps = x.getElementsByTagName("postcard");
	for(var i = 0; i < ps.length; i++) {
		var p = ps[i];
		cards[i] = {
			title: p.getAttribute('title'),
			id: p.getAttribute('id'),
			price: p.getAttribute('price'),
			count: p.getAttribute('count'),
			alt: p.getAttribute('alt'),
			src: p.getAttribute('src'),
			images: []
		};
		var pis = p.getElementsByTagName("image");
		for(var j = 0; j < pis.length; j++) {
			cards[i].images[j] = {
				src: pis[j].getAttribute('src'),
				title: pis[j].getAttribute('title'),
				id: pis[j].getAttribute('id')
			};
		}
	}
	return cards;
}

function initNavigation() {
	if(document.images && document.getElementsByTagName && document.getElementById && document.childNodes && document.createElement) {
		imgEffect = new fx.Opacity('cardOverviewImage', { duration: 600 });

		var xreq = new Xhr();
		var xdoc = xreq.loadSync("cards.xml");
		
		postcards = xmlCards2JsCards(xdoc);
		lastNav = null;
		var navs = document.getElementsByTagName("a");
				
		for(var count = i = 0; i < navs.length; i++) {
			var nav = navs[i];			
			if(nav.className == "rollOverItem") {
				var hashId = parseInt(window.location.hash.substring(1,window.location.hash.length));
				hashId = isNaN(hashId) ? 0 : hashId;			
				nav.__listId = count;
				nav.onclick = postCardNav_onclick;							
				if(hashId == count) {										
					nav.onclick();
				}
				++count;
			}
		}		
	}	
}

function checkLoading() {
	$("loader").style.display = "none";	
	imgEffect.custom(0,1);
}

function postCardNav_onclick() {	
	if(lastNav == null) {
		lastNav = this;		
		this.onmouseover();		
	} else {
		lastNav.onmouseout = this.onmouseout;
		lastNav.onmouseout();
	}
		
	lastNav = this;					
	this.onmouseout = null;	
	
	var p = postcards[this.__listId];
	
	$("loader").style.display = "block";	
	
	var img = $("cardOverviewImage");
	imgEffect.hide();
	img.onload = checkLoading;
	img.src = null;
	img.src =  p.src + "section.jpg";
	
	var links = "";
	var pts = p.images;
	
	var thums = $("thums");
	deleteChildren(thums);
	
	for(var j = 0; j < pts.length; j++) {
		var t = pts[j];

		var link = document.createElement("a");
		link.setAttribute("href", p.src + t.src + ".jpg");
		link.setAttribute("rel", "lightbox_cards");
		
		if(t.id == "") {
			link.setAttribute("title", '"' + t.title + '"');
		} else {
			link.setAttribute("title", '"' + t.title + '" | ID: ' + p.id + "-" + t.id);
		}
		
		var img = document.createElement("img");
		img.setAttribute("src", p.src + 'thumbs/' + t.src + "_thumb.jpg");
		img.setAttribute("alt", t.title);
		img.onmouseover = function() { this.className = "on"; };
		img.onmouseout = function() { this.className = "off"; };
		img.className = "off";
		
		link.appendChild(img);
		
		thums.appendChild(link);
	} 
	
	myLightbox.reinit();	
	
	$("cardPrice").innerHTML = p.price;
	$("cardCount").innerHTML = p.count;
	$("cardIdVal").innerHTML = p.id;	
	
	document.title = "PrivatPoste :: " + p.title;
	window.location.hash = this.__listId;
	return false;	
}

function deleteChildren(element) {
	while (element.firstChild) {
	  element.removeChild(element.firstChild);
	}
}
