function ImageGallery(div, productId, maxUploadSize, admin) {
	
	this.draw = function() {
		switch(me.mode) {
			case ImageGallery.VIEW: me.getList(); break;
			case ImageGallery.NEW: me.showForm(); break;
		};
	};
	
	this.showForm = function() {
		try {
			var ifrm = null;
			if(BROWSER == IE) {
				ifrm = document.createElement("<iframe name=\"post_target\">");
			} else {
				ifrm = document.createElement("iframe");
				ifrm.name = "post_target";
			}
			ifrm.src = "/lib/image_gallery/saveImage.php";
			ifrm.parentBinding = this;
			ifrm.style.display = "none";

			var div = document.createElement("div");
			div.style.border = "1px solid #999";
			div.style.padding = "10px";
			div.style.backgroundColor = "#e5e5e5";
	
			var input = document.createElement("input");
			input.type = "file";
			input.name = "image";
			div.appendChild(input);
			
			button = document.createElement("button");
			button.appendChild(document.createTextNode("Add Image"));
			button.fileName = input.value;
			
			input = document.createElement("input");
			input.type = "hidden";
			input.name = "productId";
			input.value = me.productId;
			div.appendChild(input);
			
			
			div.appendChild(button);
			var span = document.createElement("span");
			span.style.marginLeft = "10px";
			span.style.fontSize = "14px";
			span.appendChild(document.createTextNode("Max File Size: "+me.maxUploadSize+" bytes"));
			div.appendChild(span)
			
			if(BROWSER == IE) {
				var frm = document.createElement("<form enctype=\"multipart/form-data\">");
			} else {
				var frm = document.createElement("form");
			}
			frm.action = "/lib/image_gallery/saveImage.php";
			frm.method = "post";
			frm.target = "post_target";
			frm.enctype = "multipart/form-data";
			frm.appendChild(div);
			
			me.contentDiv.appendChild(frm);
	
			button.onclick = function() {
				if(!ifrm) {
					alert("don't have an iframe");
					return;
				}
				document.body.appendChild(ifrm);
				frm.submit();
			};
		} catch(err) {
			alert(err);
		}
	}
	
	this.drawGalleryView = function() {
		clearNode(me.contentDiv);
		if(me.imagesResponse && me.imagesResponse.images && me.imagesResponse.images.length) {
			var table = document.createElement("table");
			var tbody = document.createElement("tbody");
			table.appendChild(tbody);
			var tr = document.createElement("tr");
			
			var img;
			var count = 0;
			for(var i=0; i<me.imagesResponse.images.length; i++) {
				var image = me.imagesResponse.images[i];
				img = document.createElement("img");
				img.style.margin = "5px";
				img.src= "/lib/scripts/image.html?w=200&raw_url="+image.img_path;
				img.img_path = image.img_path;
				
				img.onmouseover = function() {
					imagePreview.show('raw',me.productId, 300, this.img_path);
				};
				img.onmouseout = function() {
					imagePreview.hide();
				};
				var td = document.createElement("td");
				td.appendChild(img);
				if(this.admin) {
					td.appendChild(document.createElement("br"));
					var remLink = document.createElement('a');
					remLink.href = "javascript:void(0);";
					remLink.appendChild(document.createTextNode("remove"));
					remLink.image = image;
					remLink.onclick = function() {
						if(confirm("Are you sure you want to remove this Image from the Image Gallery?")) {
							var remIfm = document.createElement("iframe");
							document.body.appendChild(remIfm);
							remIfm.style.display = "none";
							remIfm.src = "/lib/image_gallery/removeImage.php?id="+this.image.id+"&productId="+me.productId;
							me.setMode(me.mode);
						}					
					};
					td.appendChild(remLink);
				}
				if((i) % 3 == 0) {
					tr = document.createElement("tr");
				}
				tr.appendChild(td);
				tbody.appendChild(tr);
			}
			me.contentDiv.appendChild(table);
		} else {
			var h3 = document.createElement("h3");
			h3.appendChild(document.createTextNode("There are no Images in this Gallery"));
			me.contentDiv.appendChild(h3);
		}
		
		if(me.admin)
			me.showForm();
	};
	
	this.setMode = function(mode) {
		showLoadingBar(me.contentDiv, false);
		me.mode = mode;
		me.draw();
	};
	
	this.getList = function() {
		showLoadingBar(me.contentDiv, false);
		xml.list(me.productId, me.getListResponse);
	};
	
	this.getListResponse = function(response) {
		me.imagesResponse = response;
		me.drawGalleryView();
	};
	
	this.init = function() {
		if(!me.div) { throw Exception("No Output Div"); };
		if(!me.productId) { throw Exception("No Product Id"); };
		
		me.div.style.marginTop = "20px";
				
		var titleDiv = document.createElement("div");
		titleDiv.className = "gallery_title";
		titleDiv.appendChild(document.createTextNode("Image Gallery"));
		
		me.div.appendChild(titleDiv);
		
		me.contentDiv = document.createElement("div");
		me.contentDiv.appendChild(document.createTextNode("contentDiv"));
		
		me.div.appendChild(me.contentDiv);
		
		showLoadingBar(me.contentDiv, false);
		me.draw();
	};
	
	var me = this;
	var xml = new ImageGalleryXML();
	this.div = div;
	this.admin = admin;
	this.contentDiv = null;
	this.productId = productId;
	this.mode = ImageGallery.VIEW;
	this.imagesResponse = null;
	this.maxUploadSize = maxUploadSize;
//	this.offset = 0;
//	this.limit = 10;
	var imagePreview = new ImagePreview();
	this.init();
}

ImageGallery.VIEW = 0;
ImageGallery.NEW = 1;