// wnditm_photos.js

var gPhotosObjQueue = new List();
var gPhotos_hOffset;
var gPhotos_vOffset;
var gPhotos_lastTop;
var gPhotos_lastLeft;
var gPhotos_col;
var gPhotos_row;
var gPhotos_cols;
var gPhotos_rows;
var gPhotos_layout;

//------------------------------------------------------------
function wnditm_photos_add(itm, wnd)
	{
	var data = new Object();
	data.command = itm.cmd;
	data.itmID = gPhotosObjQueue.add(itm);
	data.wndID = gPhotosObjQueue.add(wnd);
	if (itm.setPHPData) itm.setPHPData(data, wnd);
	data.user_id = wnd.attr("user_id");
	if (!data.user_id) data.user_id = getuserid();
	if (itm.dir) data.dir = itm.dir;
	var dataString = $.toJSON(data);
	delete data;
	wnd_beginWait(wnd);
	$.post(itm.php, {data: dataString}, done_wnditm_photos_add);
	}
	
//----------------------------------------------------------------------
function done_wnditm_photos_add(res)
	{
	var obj = $.evalJSON(res);
	if (!obj.recs) return;
	var recs = $.evalJSON(obj.recs);
	var itm = gPhotosObjQueue.get(obj.itmID);
	var iconsize = "small";
	if (itm.iconsize) iconsize = itm.iconsize;
	var wnd = gPhotosObjQueue.getremove(obj.wndID);
	var dblclick = itm.dblclick;	
	itm.dblclick = undefined;
	
	var div = wnditem_elem_add($("<div></div>"), itm, wnd);
	if (wnd.hasClass("wnd_desktop")) var par = div;
	else par = $("body");
	div.addClass("photos")
		.selectable(
 		{
		filter:".bigPhotoBox",
		start:photos_sel_start,
		selecting:photos_sel_selecting,
		selected:photos_sel_selected,
		stop:photos_sel_stop,
		appendTo:par
		})
		.attr({itm:obj.itmID, iconsize: iconsize, subklass: itm.subklass});
	itm.dblclick = dblclick;

	var bounds = new Rect().getElem(div);
	var total = 0;
	
	if (recs) 
		{
		var dir = obj.dir;
		total = recs.length;
		div.attr("total", total);
		for (var idx = 0; idx < total; ++idx) 
			{
			var rec = recs[idx];
			var box = wnditm_photos_newPhoto(wnd, div, itm, idx, rec).appendTo(div);

			if (itm.boxtype) box.attr("boxtype", itm.boxtype);
			if (itm.setPhotoAttr != undefined) itm.setPhotoAttr(box, rec);
			if (idx == 0) wnditm_photos_beginGetPos(box, bounds, itm.layout, iconsize, itm.subklass);
			wnditm_photos_setPos(box, iconsize, itm.subklass);

			box.draggable({
				stack: {group: ".bigPhotoBox",min: 50},
				appendTo: $("body"),
				helper: "clone",
				zIndex: 30000,
				start:dragPhoto_start,
				drag: dragPhoto_drag,
				stop:dragPhoto_stop,
				distance: 6
				});
			}
		}
	if (itm.popup)
		{
		div.bind("contextmenu", menu_contextPopup)
		.attr("popup", gMenuPopups.add(itm.popup));
		}
	if (itm.childpopup)
		{
		div.attr("childpopup", gMenuPopups.add(itm.childpopup));
		}
	if (itm.layout) div.attr("layout", itm.layout);
	div.bind("mousedown", wnditm_photosBkgnd_mousedown);
	if (itm.itmName)
		{
		var s;
		if (total == 0) s = "No "+itm.itmName+"s";
		else if (total == 1) s = "1 "+itm.itmName;
		else s = total+" "+itm.itmName+"s";
		wnd.children(".wnd_botbar").html(s);
		}
	wnd_endWait(wnd);
	delete recs,obj, bounds;
	wnd.trigger("done");
	}

//------------------------------------------------------------
function wnditm_photosBkgnd_mousedown(e)
	{
	menu_closepopup();
	$(this).children(".slct").removeClass("slct");
	wnditm_lasso_start(e, $(this));
	}
//------------------------------------------------------------
function wnditm_photos_mousedown(e)
	{
	if (!$(this).hasClass("slct")) 
		{
		if (!e.shiftKey) {
			$(this).parent().children(".slct").removeClass("slct");
		}
	}
	$(this).children(".photoBox").children(".photo")
	.removeClass("photoOver")
	.removeClass("photoNormal")
	.addClass("photoSelect");
	if (e.shiftKey) $(this).toggleClass("slct");
	else $(this).addClass("slct");
	e.stopPropagation();
	}
//------------------------------------------------------------
function dragPhoto_start(event, ui)
	{
	box = $(this).attr("anchor", true);
	startPos = box.position();
	boxes = $(this).wnd().find(".photos").children(".slct")
	dboxes = boxes.clone();
	dboxes.each(function()
		{
		dbox = $(this);
		var top = parseInt(dbox.css("top"));
		var left = parseInt(dbox.css("left"));
		dbox.attr({top1:top,left1:left});
		}).appendTo($("body"))
		.css("zIndex",32700);
	boxes.css({opacity:.25});
	}

//------------------------------------------------------------
function dragPhoto_drag(event, ui)
	{
	var pos = ui.offset;
	var offtop = pos.top - startPos.top;
	var offleft = pos.left - startPos.left;
	
	dboxes.each(function(idx){
		var dbox = $(this);
		var origtop = parseInt(dbox.attr("top1"));
		var origleft = parseInt(dbox.attr("left1"));
		dbox.css({
			top: origtop + offtop,
			left:origleft + offleft
		});
	});
	}
//------------------------------------------------------------
function dragPhoto_stop(event, ui)
	{
	boxes.css("opacity", 1);
	drop_End();
	$("body").children(".slct").remove();
	}

//------------------------------------------------------------
function wnditm_photos_hover_over()
	{
	$(this).children(".photoBox").children(".photo")
	.removeClass("photoSelect")
	.removeClass("photoNormal")
	.addClass("photoOver");
	}
//------------------------------------------------------------
function wnditm_photos_hover_out()
	{
	$(this).children(".photoBox").children(".photo")
	.removeClass("photoOver")
	.removeClass("photoSelect")
	.addClass("photoNormal");
	}

//------------------------------------------------------------
function wnditm_photos_mouseup()
	{
	}
//------------------------------------------------------------
function wnditm_photos_click()
	{
	}
//------------------------------------------------------------
function wnditm_photos_newPhoto(wnd, div, itm, idx, rec)
	{
	var box = wnditm_div_add({klass:"bigPhotoBox"}, wnd).attr("idx",idx)
	.css("zIndex",10);
	var imgBox = wnditm_div_add({klass:"photoBox"}, box);

	if (rec.isdir) {
		var imgDim = {
			width: 64,
			height: 64
		};
		if (rec.childCnt == 0) 
			path = "plooper_data/desktop/icons/folder_empty.jpg";
		else 
			path = "plooper_data/desktop/icons/folder_full.jpg";
		box.attr("isdir", true);
	}
	else {
		var imgDim = {
			width: rec["imgW"],
			height: rec["imgH"]
		};
		if (itm.dir) 
			path = itm.dir + "/" + rec["file"];
		else 
			path = rec["src"];
	}	

	var img = wnditm_img_add({
		klass: "photo",
		src: path,
		imgDim: imgDim},
		imgBox
	).addClass("photoNormal");	//.attr("orig", path);

	wnditm_div_add({klass:"photocap"}, box).html(itm.getCaption(wnd,idx,rec));
	if (div.attr("childpopup")) box.bind("contextmenu", menu_contextPopupPar);
	if (itm.dblclick) box.bind("dblclick", itm.dblclick);
	box.hover(wnditm_photos_hover_over, wnditm_photos_hover_out)
	.bind("mousedown", wnditm_photos_mousedown)
	.bind("mouseup", wnditm_photos_mouseup)
	.bind("click", wnditm_photos_click);

	return box;
	}

//----------------------------------------------------------------------
function wnditm_photos_redraw(photosBox)
	{
	var bounds = new Rect().getElem(photosBox);
	var idx = 0;
	var layout = "horiz";
	var iconsize = photosBox.attr("iconsize");
	var subklass = photosBox.attr("subklass");
	if (photosBox.attr("layout")) layout = photosBox.attr("layout");
	$(photosBox).children().each(function()
		{
		if (idx == 0) wnditm_photos_beginGetPos($(this), bounds, layout, iconsize, subklass);
		wnditm_photos_setPos($(this), iconsize, subklass);
		++idx;
		});
	delete bounds;
	}
//----------------------------------------------------------------------
function wnditm_photos_beginGetPos(box, bounds, layout, iconsize, subklass)
	{
	wnditm_photos_sizeBox(box, iconsize, subklass);
	gPhotos_hOffset = 2+box.width();
	gPhotos_vOffset = box.height();
	gPhotos_lastTop = 0;
	gPhotos_lastLeft = 0;
	
	switch(gPhotos_layout = layout)
		{
		case "vert":
			gPhotos_row = 0;
			gPhotos_rows = Math.floor(bounds.height/gPhotos_vOffset);
			break;

		case "horiz":
		default:
			gPhotos_col = 0;
			gPhotos_cols = Math.floor(bounds.width/gPhotos_hOffset);
			break;
		}
	}
//----------------------------------------------------------------------
function wnditm_photos_setPos(box, size, subklass)
	{
	wnditm_photos_sizeBox(box, size, subklass);
	box.css({top:gPhotos_lastTop, left:gPhotos_lastLeft});
	switch(gPhotos_layout)
		{
		case "vert":
			if (gPhotos_row < (gPhotos_rows-1))
				{
				gPhotos_lastTop += gPhotos_vOffset;
				++gPhotos_row;
				}
			else	
				{
				gPhotos_lastTop = 0;
				gPhotos_row = 0;
				gPhotos_lastLeft += gPhotos_hOffset;
				}
			break;

		case "horiz":
		default:
			if (gPhotos_col < (gPhotos_cols-1))
				{
				gPhotos_lastLeft += gPhotos_hOffset;
				++gPhotos_col;
				}
			else	
				{
				gPhotos_lastLeft = 0;
				gPhotos_col = 0;
				gPhotos_lastTop += gPhotos_vOffset;
				}
			break;
		}
	}
//----------------------------------------------------------------------
function photos_getFileList(photos)
	{
	var files = [];
	var idx = 0;
	photos.children(".bigPhotoBox").each(function() 
		{
		var img = $(this).children(".photoBox").children(".photo");
		files[idx] = [basename(img.attr("src")), img.attr("w"), img.attr("h")];
		var photo_id = $(this).attr("photo_id");
		if (photo_id) files[idx].photo_id = photo_id;
		++idx;
		}
 		);
	return files;
	}
//----------------------------------------------------------------------
function photos_getFileListFromOne(bigPhotoBox)
	{
	var img = bigPhotoBox.children(".photoBox").children(".photo");
	var files = [[basename(img.attr("src")), img.attr("w"), img.attr("h")]];
	return files;
	}

//----------------------------------------------------------------------
function wnditm_photos_setIconSize(wnd, size)
	{
	var photosBox = wnd.find(".photos").attr("iconsize", size);
	wnditm_photos_redraw(photosBox);
	}

//----------------------------------------------------------------------
function wnditm_photos_sizeBox(box, size, subklass)
	{
	var w;
	switch(subklass)
		{
		case "user":
		case "deskPat":
			switch(size)
				{
				case "small":	w=h=32;		break;
				case "medium":	w=h=64;		break;
				case "large":	w=h=128;	break;
				}
			break;
		case "desk":
				case "small":	w=64;	h=64;	break;
				case "medium":	w=128;  h=128;	break;
				case "large":	w=256;	h=256;	break;
			break;
			
		case "photo":
		case "album":
			switch(size)
				{
				case "small":	w=64;	h=48;	break;
				case "medium":	w=128;  h=96;	break;
				case "large":	w=256;	h=192;	break;
				}
		break;
		}
	
	var photoBox = box.children(".photoBox");
	photoBox.css("width", w).css("height", w);
	box.css("width",w+4).css("height", w+16);
	var img = photoBox.children(".photo");
	var w = parseInt(img.css("width")) * 100;
	var h = parseInt(img.css("height")) * 100;
	image_fix({img: img, flushBot:true, imgDim:{width:w,height:h}, parElem: img.parent(), subklass:subklass});
	}
//----------------------------------------------------------------------
function wnditm_photos_GetImg(bigBox)
	{
	return bigBox.children(".photoBox").children();
	}
//----------------------------------------------------------------------
function wnditm_photos_refresh(wnd)
	{
	var wnditm = wnd.find(".photos");
	if (wnditm.length>0)
		{
		var itm = gPhotosObjQueue.get(wnditm.attr("itm"));
		var par = wnditm.parent();
		wnditm.remove();
		wnditm_photos_add(itm, par);
		}
	}

//----------------------------------------------------------------------
function wnditm_photos_reset(boxes)
	{
	var box = $(boxes[0]);
	var itm = box.parent().attr("itm");
	itm = gPhotosObjQueue.get(itm);
	
	boxes
	.draggable("destroy")
	.draggable({
		stack: {
			group: ".bigPhotoBox",
			min: 50
		},
		appendTo: $("body"),
		helper: "clone",
		zIndex: 30000,
		start: dragPhoto_start,
		drag: dragPhoto_drag,
		stop: dragPhoto_stop,
		distance: 20
	});
	boxes.bind("contextmenu", menu_contextPopupPar);
	if (itm.dblclick) boxes.bind("dblclick", itm.dblclick);
	boxes.hover(wnditm_photos_hover_over, wnditm_photos_hover_out)
	.bind("mousedown", wnditm_photos_mousedown)
	.bind("mouseup", wnditm_photos_mouseup)
	.bind("click", wnditm_photos_click);
	return boxes;
	}

//----------------------------------------------------------------------
function wnditm_photos_dropMove(wnd, ui)
	{
	var box = $(ui.draggable);
	var parWnd = box.wnd();
	if (wnd.attr("id") == parWnd.attr("id")) {
		var boxes = wnd.find(".photos").children(".slct");
		wnditm_photos_setBoxPos(boxes, ui);
	}
/*
	var box = $(ui.draggable);
	var boxes = 
	
	var parWnd = box.wnd();
	if (wnd.attr("id") == parWnd.attr("id")) 
		{
		var pos = wnd.find(".photos").offset();
		var left = ui.offset.left - pos.left;
		var top = ui.offset.top - pos.top;
		delete pos;
		box.css({left: left,top: top});
		return true;
		}
*/
	return false;
	}
//----------------------------------------------------------------------
function wnditm_photos_addPhoto(wnd, ui)
	{
	var photo = $(ui.draggable);
	var photos = wnd.find(".photos");
	var parWnd = photo.wnd();
	var pos = photos.offset();
	var left = ui.offset.left - pos.left;
	var top = ui.offset.top - pos.top;
	delete pos;
	photo.css({left: left,top: top})
	.appendTo(photos);
	}
//----------------------------------------------------------------------
function wnditm_photos_setBoxPos(boxes, ui)
	{
	var box = $(ui.draggable);
	var pos = box.offset();
	var dTop = ui.offset.top - pos.top;
	var dLeft = ui.offset.left - pos.left;
	boxes.each(function(){
		var bx = $(this);
		var ps = bx.position();
		bx.css({
			top: ps.top + dTop,
			left: ps.left + dLeft
		});
	});
	return boxes;
	}

//----------------------------------------------------------------------
function wnditm_photos_getSlctIDs(par)
	{
	var ids = [];
	par.children(".slct").each(function(i){
		ids[i]=$(this).attr("photo_id");
	});
	return ids;
	}
	
function photos_sel_start(){
}
function photos_sel_selecting(){
//	var x=$(".ui-selectable-helper").css("zIndex")
	}
function photos_sel_selected()
	{
	$(this).wnd().find(".ui-selected").addClass("slct");
	$(this).wnd().find(".ui-selecting").addClass("slct");
	}
//----------------------------------------------------------------------
function photos_sel_stop()
	{}
//----------------------------------------------------------------------
function wnditm_photos_getTitle(box)
	{
	return box.children(".photocap").html();
	}