/***********************************************************************
*
*  Author: joeri.van.hoof@pandora.be
*
*  Based on script Image Thumbnail Viewer II from www.dynamicdrive.com
***********************************************************************/

var subject=""

//Set image border width
var borderwidth=0;

function getimgcode(img, imglink, subject, print, ph1015) {
	var result ="";

	if( print != "" ) {
		result += '<div id="box-gallery-photo-left">';
	} else {
		result += '<div id="box-gallery-photo">';
	}

	if (imglink!="")
		result ='<a href="'+imglink+'">';

	if (subject!="")
		result +='<div id="description">' + subject + '</div>';

	result +='<img src="'+img+'" border="'+borderwidth+'"></img>';

	if (imglink!="")
		result +='</a>';

	result += '</div>';

	if( print != "" ) {
		result += '<div id="box-gallery-items">';
		if(ph1015!=null)
			result += '<a href="' + ph1015 + '">Klik hier voor je gratis 10x15 foto</a>';
		result += '<form name="order" method="post">';
		result += '<input name="itemId" value="' + print + '" type="hidden"/>';
		result += '<input name="color" value="' + subject + '" type="hidden"/>';
		result += '<ul id="box-gallery-ul"><li><input name="size" value="10x15" type="radio">Foto afdruk 10x15 - &euro;1,5</input></li>';
		result += '<li><input name="size" value="13x18" type="radio">Foto afdruk 13x18 - &euro;2,70</input></li>';
		result += '<li><input name="size" value="20x30" type="radio">Foto afdruk 20x30 - &euro;12</input></li>';
		result += '<li><input name="size" value="25x38" type="radio">Foto afdruk 25x38 - &euro;15,30</input></li>';
		result += '<li><input name="size" value="Sleutelhanger" type="radio">Sleutelhanger met foto - &euro;5</input></li>';
		result += '<input src="/style/checkout/cart.gif" onclick="addToCartButtonHandler1CMS(this.form)" type="image">';
		result += '</form>';
		result += '</div>';
	} else {
		result += '<div id="box-gallery-items">';		
/*
		if(ph1015 != undefined && ph1015!="" && !ph1015) {
			alert(ph1015);
			result += '<a href="' + ph1015 + '">Klik hier voor de 10x15 foto.</a>';
		}
*/
		result += '</div>';		
	}

	return result;
}

function getimage(loadarea, img, imglnk, subject, print, ph1015){

	if (document.getElementById){
		var imgobj=document.getElementById(loadarea)

		imgobj.innerHTML=getimgcode(img, imglnk, subject, print, ph1015)

		return false
	}
}

var req;
var batch;

function thumbnailchanger(gallery, action, count) {
	batch = count;

	var url = "/photo?gallery=" + escape(gallery) + "&action=" + escape(action) + "&count=" + escape(count);
	if( window.XMLHttpRequest ) {
		req = new XMLHttpRequest();
	} else if( window.ActiveXObject ) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	req.open("GET", url, true);
	req.onreadystatechange = thumbnailloader;
	req.send(null);
}

function thumbnailloader() {

	if( req.readyState == 4 ) {
		if( req.status == 200 ) {
			var i=0;
			var filelist = req.responseXML.getElementsByTagName("file");
			var response = req.responseXML.getElementsByTagName("result");
			var size = filelist.length;
			var start = parseInt(response[0].getAttribute("start"));
			var stop = response[0].getAttribute("stop");
			var max = response[0].getAttribute("max");
			var mdiv = document.getElementById("box-gallery-thumbnails");
			var result = "";
			for( i=0; i<size; i++) {
				var href = filelist[i].getAttribute('href');
				var path = filelist[i].getAttribute('path');
				var ph1015 = filelist[i].getAttribute('ph1015');
				var description = filelist[i].getAttribute('description');
				var print = filelist[i].getAttribute('print');
				if( path!= null ) {
					result += "<a href=\"";
					if( href != null ) {
						result += href;
					} else {
						result += "#";
					}
					result += "\" onMouseOver=\"getimage('dynloadarea', '";
					result += path;
					result += "','";
					if( href != null )
						result += href;
					result += "','";
					if( description != null )
						result += description;
					result += "','";
					if( print != null )
						result += print;
					result += "','";
					result += ph1015;
					result += "')\"><img border=\"0\" src=\"";
					result += path;
					result += "\" width=\"84\" height=\"52\" style=\"margin: 5px\"></a>\n\r";
				}
				// Display first image automagically
				var href = '';
				if(filelist[0].getAttribute('href') != null )
					href = filelist[0].getAttribute('href');
				var path= '';
				if(filelist[0].getAttribute('path') != null )
					path = filelist[0].getAttribute('path');
				var ph1015 = '';
				if(filelist[0].getAttribute('ph1015') != null )
					ph1015 = filelist[0].getAttribute('ph1015');
				var description = '';
				if(filelist[0].getAttribute('description') != null )
					description = filelist[0].getAttribute('description');
				var print = '';
				if(filelist[0].getAttribute('print') != null )
					print = filelist[0].getAttribute('print');

				getimage('dynloadarea',path, href, description, print, ph1015);
			}
			mdiv.innerHTML = result;
			var countdiv = document.getElementById("box-gallery-count");
			if( countdiv != null )
				countdiv.innerHTML = start + "-" + stop + "/" + max;
		}
	}

} 
