
function makePrintImage( ticket, type) {
	var pURL = "print.php?ticket=" + ticket + "&type=" + type;
	pInfo='toolbar=0,';
	pInfo+='location=0,';
	pInfo+='directories=0,';
	pInfo+='status=0,';
	pInfo+='menubar=0,';
	pInfo+='scrollbars=0,';
	pInfo+='resizable=1,';
	pInfo+='width=930,';
	pInfo+='height=600';
	window.open(pURL, 'der Gedankenprojektor', pInfo);
}	

function setMidTicket( ticket ) {
	var midretina = getObj("midretina");
	var mideye = getObj("mideye");
	var midticket = getObj("midticket");
	var y = ticket.slice(0,4);
	var m = ticket.slice(4,6);
	var d = ticket.slice(6,8);
	var p = ticket.slice(8,10);

	// CHANGE THESE TO MID WHEN THE TIME COMES!!!
	midretina.src = "archive/retina/md/" + y + "/" + m + "/" + d + "/retina_" + ticket + ".jpg"
	midretina.onmousedown = function () { makePrintImage(ticket, "retina"); };
	midretina.onmouseover = function () { this.style.cursor = "pointer"; }
	mideye.src = "archive/auge/md/" + y + "/" + m + "/" + d + "/auge_" + ticket + ".jpg"
	mideye.onmousedown = function () { makePrintImage(ticket, "auge"); };
	mideye.onmouseover = function () { this.style.cursor = "pointer"; }
	midticket.innerHTML = "ticket:#" + ticket;
};

		
function ImageDiv (w, h) {
	//this.img = new Image();
	this.img = document.createElement("img");
	this.comment = document.createElement("div");
	this.maindiv = document.createElement("div");
	this.imgsrc= '';
	this.imgoversrc='';
		
	this.img.style.border = "1px solid black";
	this.img.style.width = parseInt(w) +"px";
	this.img.style.height = parseInt(h) +"px";
	this.img.style.position = "absolute";
	this.img.style.left="0px";
	this.img.style.top="0px";
	this.comment.className = "imagepreviewcomment";
	this.comment.style.position = "absolute";
	this.comment.style.top = h + 3 + "px";
	this.comment.innerHTML= w + "-"  + h;
	this.comment.style.fontSize = "10px";
	this.comment.style.width = parseInt(w) +"px";

	this.maindiv.appendChild( this.img );
	this.maindiv.appendChild(this.comment);
}

ImageDiv.prototype.handleOver = function ( e ) {
	this.img.src = this.imgoversrc;
}

ImageDiv.prototype.handleOut = function ( e ) {
	this.img.src = this.imgsrc;
}

ImageDiv.prototype.handleDown = function ( e ) {
	setMidTicket( this.ticket );
}
ImageDiv.prototype.setTicket = function ( ticket) {
	this.comment.innerHTML= "ticket #" + ticket;
	this.ticket = ticket;
	var y = ticket.slice(0,4);
	var m = ticket.slice(4,6);
	var d = ticket.slice(6,8);
	var p = ticket.slice(8,10);
	this.imgoversrc = "archive/retina/sm/" + y + "/" + m + "/" + d + "/retina_" + ticket + ".jpg"
	this.imgsrc = "archive/auge/sm/" + y + "/" + m + "/" + d + "/auge_" + ticket + ".jpg"
	this.img.src = this.imgsrc;
	this.img.onmouseover = createMethodReference(this, "handleOver");
	this.img.onmouseout = createMethodReference(this, "handleOut");
	this.img.onmousedown = createMethodReference(this, "handleDown");
};

function ImageBrowser ( howmany, preview_w, preview_h, spacing ) {
	this.width = (howmany * preview_w) + (howmany -1)*spacing;
	this.height = preview_h + 10;
	this.spacing = spacing;
	this.howmany = howmany;
	
	this.tickets = new Array();
	
	this.maindiv = document.createElement("div");
	this.maindiv.style.width = this.width + "px";
	this.maindiv.style.height = this.height + "px";
	
	// safari doesn't like Image object
	this.point_l = document.createElement("img"); 
	this.point_r = document.createElement("img");

	this.currentindex = 0;

	this.divarray = new Array( this.howmany);
	for (var i=0; i< this.howmany; i++) {
		this.divarray[i] = new ImageDiv( preview_w, preview_h);
		this.divarray[i].maindiv.style.position = "absolute";
		this.divarray[i].maindiv.style.left = ((i*preview_w) + (i*this.spacing)) +"px";
		this.maindiv.appendChild( this.divarray[i].maindiv );
	}
}

ImageBrowser.prototype.setImages = function( e) {
	if (this.tickets.length < this.divarray.length) {
		var dif = this.divarray.length - this.tickets.length;
		for (var i = 0; i < dif; i++ ) {
			this.divarray[this.howmany - i -1].img.style.visibility = "hidden";
			this.divarray[this.howmany - i -1].comment.style.visibility = "hidden";
		}	
	} else { 
		var j=0;
		for(var i=this.currentindex;i< this.currentindex+this.howmany;i++) {
			this.divarray[j++].setTicket ( this.tickets[i] )
		}
	}
	
};

ImageBrowser.prototype.setCurrentIndex= function( index ) {
	if (index <= this.tickets.length - this.howmany && index >=0 ) {
		this.currentindex = index;
	} else {

		if (index > this.tickets.length - this.howmany ) {
			this.currentindex = index;
			while (this.currentindex > this.tickets.length - this.howmany ) 
				this.currentindex--;
		}

		if (index < 0 ) {
			this.currentindex = index;
			while (this.currentindex < 0 ) 
				this.currentindex++;
		}
	}
}
ImageBrowser.prototype.goRight = function( e) {
	this.setCurrentIndex( this.currentindex + 1);
	this.setImages();	
}

ImageBrowser.prototype.goLeft = function( e) {
	this.setCurrentIndex( this.currentindex - 1);
	this.setImages();	
}

ImageBrowser.prototype.setPointers = function( pnt_l_url, pnt_r_url) {
	this.point_l.src = pnt_l_url;
	this.point_r.src = pnt_r_url;
	this.point_l.onmousedown = createMethodReference(this, "goLeft"); 
	this.point_r.onmousedown = createMethodReference(this, "goRight"); 
};

ImageBrowser.prototype.setPosition = function( left, top) {
	this.maindiv.style.position = "absolute";
	//this.maindiv.style.border = "1px solid black";
	this.maindiv.style.left = left + "px";
	this.maindiv.style.top = top + "px";
	
	this.point_l.style.position = "absolute";
	//this.point_l.style.left = (left - this.point_l.width - 10) + "px";
	this.point_l.style.left = (left - 30 - 10) + "px";
	//this.point_l.style.top  = parseInt(top + (this.height - this.point_l.height) / 2.0 ) + "px";
	this.point_l.style.top  = parseInt(top + (this.height - 60) / 2.0 ) + "px";
	
	this.point_r.style.position = "absolute";
	this.point_r.style.left = (left + this.width + 10) + "px";
	//this.point_r.style.top  = parseInt(top + (this.height - this.point_l.height) / 2.0 ) + "px";
	this.point_r.style.top  = parseInt(top + (this.height - 60) / 2.0 ) + "px";
};

ImageBrowser.prototype.fetchXMLData = function( requesturl ) {
	makeRequest( requesturl, parseXML, this, "get", "" );
}
ImageBrowser.prototype.findTicket = function( ticket ) {
	var found = false;
	for(var i =0; i<this.tickets.length; i++){
		if (this.tickets[i] == ticket ) {
			this.setCurrentIndex(i);
			this.setImages();
			setMidTicket( this.tickets[i] );
			found = true;
			return;
		}	
	}
	if ( !found) {
		alert("not found");
	}
}
function parseXML(imgb, results ) {
	checkErrorXML(imgb, results);
	var bilder = results.getElementsByTagName("b");
	for(var i=0; i< bilder.length; i++) {
		var bild = bilder[i];
		var ticket = bild.getAttribute("tkt");	
		imgb.tickets.push( ticket );
		//we know that we get the xml in the right order
		if (i < imgb.howmany) {
			imgb.divarray[i].setTicket( ticket );
		}
	}	
	imgb.setImages();
}


