
function DowImageAction() {
}
DowImageAction.prototype = new DowWidget;


DowImageAction.prototype.postInit = function() {
	this.uploadStatusElem = document.getElementById(this.uid+"_upload_status");
	this.addImageElem = document.getElementById(this.uid+"_add_image");
	
	var self = this;
	window.setTimeout(function() {self.showChosenImage()} , 0);
}


DowImageAction.prototype.showChosenImage = function() {
	var match = window.location.hash.match(/^#_index_(\d+)$/);
	if (match) {
		var imageIndex = match[1];
		var listWidget = gWidgets[this.uid+"_list"];
		var selectedWidget = listWidget.getWidgetItem(imageIndex);
		if (selectedWidget) {
			selectedWidget.getWidgetElement().scrollIntoView();
		}
	}
}

DowImageAction.prototype.setImageData = function (fileUrl , zoom_url , file_id , name , uploadedStr) {
	var listWidget = gWidgets[this.uid+"_list"];
	var newWidget = listWidget.appendItem();
	var subWidget = newWidget.getSubWidget();
	var imageWidget = gWidgets[subWidget.imageUid];
	imageWidget.name = name; //hack
	imageWidget.setImageData(fileUrl , zoom_url , file_id);
	this.setTextValue(this.uploadStatusElem , uploadedStr);
	listWidget.recalcAllSubPositions(); //only now do this (not in appendItem)
	this.sendGallery();
}

DowImageAction.prototype.uploadError = function (errorCode , name , errorMessage) {
	this.setTextValue(this.uploadStatusElem , errorMessage);
}

DowImageAction.prototype.uploading = function (uploadingStr) {
	this.setTextValue(this.uploadStatusElem , uploadingStr);
	document.getElementById("file_upload_form").style.display="none";
}

DowImageAction.prototype.hideForm = function () {
	this.unsetElementClass(this.addImageElem, "on");
}

DowImageAction.prototype.on_addAction = function (who) {
	var isOn = this.hasElementClass(this.addImageElem, "on");
	var uploadWidget = gWidgets["file_upload"];
	if (isOn) {
		uploadWidget.stopEditing();
	} else {
		uploadWidget.setFileType("image");
		uploadWidget.startEditing(this);
		this.setElementClass(this.addImageElem, "on");
		this.setTextValue(this.uploadStatusElem , "");
		document.getElementById("file_upload_form").style.display="";
	}
}


DowImageAction.prototype.on_reorder = function (who) {
	this.sendGallery();
}

DowImageAction.prototype.sendGallery = function() {
	var listWidget = gWidgets[this.uid+"_list"];
	
	var action = this.createEmptyAction("setGenericXML");
	action.setAttribute("what_key","gallery");

	listWidget.appendXMLData(action);
	this.sendAction(action.ownerDocument,null);
}

DowImageAction.prototype.onWidgetModified = function (widget) {
	if (widget.uid == this.uid+"_list") {
		//this.sendGallery(); NO TOO OFTEN
	} else {
		alert("DowImageAction.onWidgetModified: "+widget.uid);
		alert("DowImageAction.onWidgetModified: "+this.uid);
	}
}

DowImageAction.prototype.on_zoom = function (who, zoom_src) {
	gWidgets.zoomed_image.showZoom(zoom_src);
}

DowImageAction.prototype.on_caption = function (who) {
	this.sendGallery();
}

DowImageAction.prototype.on_deleteAction = function (who) {
	gWidgets[this.uid+"_list"].removeItem(who.getWidgetElement());
	this.sendGallery();
}

DowImageAction.prototype.on_editAction = function (who) {
	var imageAndCaption = this.getFirstParentWidget(who.getWidgetElement() , "DIV", "image_and_caption");
	imageAndCaption.toggleCaption();
}



DowImageAction.prototype.on_slide_show = function (who) {
	var listWidget = gWidgets[this.uid+"_list"];
	var slideShow = gWidgets['slide_show'];
	var slideShowData = listWidget.getXMLData();
	slideShow.run(slideShowData);
}


//*******************************

function GalleryList(){
}
GalleryList.prototype = new DowListWidget;

GalleryList.prototype.on_load = function (who) {
	//alert("loaded: "+who);
	var width = who.imageElem.width;
	if (width==0) {
		//alert("error width=0: "+who);
	}
	//this.recalcAllSubPositions();
	var self = this;
	//window.clearTimeout(this.timer);
	if (!this.timer) {
		this.timer = window.setTimeout(function() {self.timer=null; self.recalcAllSubPositions();} , 200);
	}
}

GalleryList.prototype.recalcAllSubPositions = function () {
	var marginWidth = 14;
	var maxHeight=132;
	var maxWidth=198;	
	var self=this;
	var index = 0;
	var nbrCol = 4;
	var topOffset = 25;
	var leftOffset = 100;
	var rowHeight = maxHeight + 26 + marginWidth; //+ captionheight + margin
	var colWidth = 200;
	var maxRow=0;	
	var rowArray = [];
	//var contentWidth = this.getElementSize(this.getWidgetElement()).width; //problem with pi=1 to early
	var contentWidth = 810;
	
	//window.status += "R.";
	
	var doRow = function(row) {
		var nbrElems = row.length;
		var width = 0;
		for (var ii=0; ii<nbrElems; ii++) {
			width += row[ii].img.width;
		}
		width += (nbrElems-1)*marginWidth;
		var margin = Math.floor((contentWidth-width)/2);
		var left = margin;
		for (ii=0; ii<nbrElems; ii++) {
			width = row[ii].img.width;
			//window.status += width + " ";
			row[ii].itemElem.style.left = left +"px";
			left += marginWidth + width;
		}
		for (ii=0; ii<nbrElems; ii++) {
			self.unsetElementClass(row[ii].itemElem,"hidewhenjs");
		}
	}
	
	var doItem = function(itemElem) {
		var row = Math.floor(index / nbrCol);
		var col = index % nbrCol;
		
		var imgs = itemElem.getElementsByTagName("IMG");
		if (imgs.length ==1) { //security
			var imgWidth = imgs[0].width; 
			var imgHeight = imgs[0].height;
			
			if (imgWidth) { // i.e. NOT loaded YET abstain
				var width = imgWidth;
				var height = imgHeight;
				
				//window.status += width + ":" + imgs[0].complete +",";
				if (imgHeight>maxHeight) {
					var width = Math.floor(maxHeight/imgHeight * imgWidth);
					var height = maxHeight;
					imgs[0].width=width;
					imgs[0].height=height;
				} 
				
				//itemElem.style.display="block";
				itemElem.style.position="absolute";
				itemElem.style.top = (topOffset+(row*rowHeight))+"px";
				itemElem.style.width = width+"px";

				rowArray.push({'itemElem':itemElem , 'img':imgs[0] });
				if (col == (nbrCol-1)) {
					doRow(rowArray);
					rowArray=[];
				}
			}

		}
		maxRow=row;
		index++;
	}

	//this.unsetElementClass(this.listElem,"hidewhenjs"); //test here because IE6 does not calculate the width of invisible(display: none) images

	this.itemIterator(doItem);
	doRow(rowArray);
	this.listElem.style.height = (topOffset*2+((maxRow+1)*rowHeight))+"px";
	
	//this.unsetElementClass(this.listElem,"hidewhenjs");
}


//*******************************

function GalleryGrid(){
}
GalleryGrid.prototype = new DowListWidget;


GalleryGrid.prototype.recalcAllSubPositions = function () {
	var maxHeight=132;
	var maxWidth=198;	
	var self=this;
	var index = 0;
	var nbrCol = 4;
	var topOffset = 25;
	var leftOffset = 100;
	var rowHeight = 180;
	var colWidth = 200;
	var maxRow=0;	
	var doItem = function(itemElem) {
		var row = Math.floor(index / nbrCol);
		var col = index % nbrCol;
		
		var imgs = itemElem.getElementsByTagName("IMG");
		if (imgs.length ==1) { //security
			var imgWidth = imgs[0].width; //might have problems img img is not loaded yet
			var imgHeight = imgs[0].height;
			
			var width = imgWidth;
			var height = imgHeight;
			
			if (imgHeight>maxHeight) {
				var width = Math.floor(maxHeight/imgHeight * imgWidth);
				var height = maxHeight;
				imgs[0].width=width;
				imgs[0].height=height;
			} 
			
			var offsetH = -Math.floor(width/2);
			itemElem.style.display="block";
			itemElem.style.position="absolute";
			itemElem.style.left = (leftOffset+(col*colWidth)+offsetH) +"px";
			itemElem.style.top = (topOffset+(row*rowHeight))+"px";
			itemElem.style.width = width+"px";
		}
		maxRow=row;
		index++;
	}

	this.itemIterator(doItem);
	this.listElem.style.height = (topOffset*2+((maxRow+1)*rowHeight))+"px";
}


//*******************************



function UPControlPanel() {
}
UPControlPanel.prototype = new DowWidget;

UPControlPanel.prototype.postInit = function () {
	this.toggleElem = document.getElementById(this.uid+"_toggle");
	this.toggleIconElem = document.getElementById(this.uid+"_toggle_icon");
	this.toggleTextAddElem = document.getElementById(this.uid+"_toggle_text_add");
	this.toggleTextRemoveElem = document.getElementById(this.uid+"_toggle_text_remove");
	this.bindEvent(this.toggleElem,"click","toggleAction"); 
}

UPControlPanel.prototype.toggleAction = function (event) {
	this.params.rank=this.params.rank ? 0 : 1;
	this.doRanking(this.params.rank);
	this.updateToggle();
}

UPControlPanel.prototype.doRanking = function(value) {
	var action = this.createEmptyAction("ranking");
	var doc = action.ownerDocument;
	var rankingElem = doc.createElement("rank");
	rankingElem.setAttribute("value",value);
	action.appendChild(rankingElem);
	this.sendAction(action.ownerDocument);
}

UPControlPanel.prototype.updateToggle = function () {
	if (this.toggleIconElem) {
		if (this.params.rank) {
			this.unsetElementClass(this.toggleIconElem , "up_favorite_icon_off");
			this.setElementClass(this.toggleIconElem , "up_favorite_icon_on");
		} else {
			this.setElementClass(this.toggleIconElem , "up_favorite_icon_off");
			this.unsetElementClass(this.toggleIconElem , "up_favorite_icon_on");
		}
	}
	
	if (this.toggleTextAddElem && this.toggleTextRemoveElem) {
		if (this.params.rank) {
			this.toggleTextAddElem.style.display="none";
			this.toggleTextRemoveElem.style.display="inline";
		} else {
			this.toggleTextAddElem.style.display="inline";
			this.toggleTextRemoveElem.style.display="none";
		}
	}
}
