// wnddb.js

var _wnddb_list = new List();
var wnddb_okToSave = true;
var gWndDB_loading = false;
var gWndDBRecIdx;
var gWndDBRec;
var gWndDBWnd;
var gWndDBFrontWnd;
var gWndDBHighZIndex;
var gWndDBProgress;
var gWndDBDone;

//----------------------------------------------------------------------
function wnddb_save()
	{
	if (!wnddb_okToSave || gJSPage!="desktop") return;
	wnddb_addIdxs();
	var data = new Object();
	data.command = "save";
	data.user_id = getuserid();
	data.wnds = [];
	$(".wnd").each(function(widx)
		{
		var wnd = $(this);
		var wbox = wnd.parent();
		var info = gWndInfos.get(wnd.attr("info"));
		var pos = wbox.positionTL();
		var prev_wnd = 0; 
		var prev_obj = 0;
		var next_wnd = 0;
		var parElem = false;
		var iconsize = "";
		var vis = wnd_vis(wnd);
		if (wnd.attr("prev")) 
			{
			parElem = gWndLinks.get(wnd.attr("prev"));
			prev_wnd = parElem.wnd().attr("widx");
			prev_obj = parElem.attr("oidx");
			}
		if (wnd.attr("next"))
			{
			var w = $("#"+wnd.attr("next"));
			if (w) next_wnd = w.attr("widx");
			if (!next_wnd) next_wnd = 0;
			}
		var photos = wnd.find(".photos");
		if (photos) {
			if (photos.attr("iconsize")) iconsize = photos.attr("iconsize");
			}
		data.wnds[widx] = 
			{
			klass: info.klass,
			owner_user_id: 	wnd.attrZero("user_id"),
			top: 			pos.top,
			left: 			pos.left,
			height: 		parseInt(wbox.css("height")),
			width: 			parseInt(wbox.css("width")),
			folder_id: 		wnd.attrZero("folder_id"),
			photo_id: 		wnd.attrZero("photo_id"),
			blog_id: 		wnd.attrZero("blog_id"),
			prev_obj: 		prev_obj,
			prev_wnd: 		prev_wnd,
			next_wnd: 		next_wnd,
			visible:  		vis,
			zindex:			wbox.css("zIndex"),
			iconsize: 		iconsize,
			hbar:			wnd_gethbar(wnd),
			idx:			wnd.attr("widx")
			};
		if (!vis) wnd_show(wnd);
		data.wnds[widx].objs = [];

		if (photos)
			{
			photos.children().each(function(oidx)
				{
				var obj = $(this);
				var opos = obj.positionTL();
				var media_id = wnddb_getMediaID(obj);
				data.wnds[widx].objs[oidx] =
					{
					boxtype: obj.attr("boxtype"),
					top: opos.top,
					left: opos.left,
					media_id: media_id,
					title: obj.children(".photocap").html(),
					idx: obj.attr("oidx")
					}
				});
			}
		if (!vis) wnd_hide(wnd);
		});
	
	wnddb_removeIdxs();
	var dataString = $.toJSON(data);
	delete data;
	$.post("wnddb.php", {data: dataString}, done_wnddb_save);
	}

//--------------------------------------
function done_wnddb_save(res)
	{	
	}

//--------------------------------------
function wnddb_load(progress, done)
	{
	if (gJSPage!="desktop") return;
	wnd_zoomOff();
	var data = new Object();
	data.command = "load";
	data.progress = _wnddb_list.add(progress);
	data.done = _wnddb_list.add(done);
	var dataString = $.toJSON(data);
	delete data;
	$.post("wnddb.php", {data: dataString}, done_wnddb_load);
	}
//--------------------------------------
function done_wnddb_load(res)
	{
	var obj = $.evalJSON(res);
	gWndDBRecs = obj.recs;
	if (gWndDBRecs) {
		gWndDB_loading = true;
		gWndDBRecIdx = 0;
		gWndDBRec = gWndDBRecs[0];
		gWndDBWnd = wnddb_initDesktop(gWndDBRec);
		gWndDBFrontWnd = false;
		gWndDBHighZIndex = 0;
		gWndDBProgress = obj.progress;
		gWndDBDone = obj.done;
		wnddb_loadIfReady();
	}
	else 
		{
		wnd_zoomOn();
		gWndDB_loading = false;
		if (obj.done) _wnddb_list.dofunc(obj.done);
		}
	delete obj;
	}
//--------------------------------------
function wnddb_loadIfReady()
	{
	if (gWndDBWnd)
		{
		if (gWndDBWnd.wndDone())
			{
			wnddb_setBoxes(gWndDBWnd, gWndDBRec.objs);
			if (gWndDBRecIdx < (gWndDBRecs.length - 1)) 
				{
				++gWndDBRecIdx;
				_wnddb_list.get(gWndDBProgress)(gWndDBRecs.length, gWndDBRecIdx);
				gWndDBRec = gWndDBRecs[gWndDBRecIdx];
				gWndDBWnd = wnddb_openWnd(gWndDBRec);
				}
			else
				{
				//----- all done ----------------
				if (gWndDBProgress)
					{
					var prog = _wnddb_list.get(gWndDBProgress);
					prog(gWndDBRecs.length,gWndDBRecs.length);
					_wnddb_list.remove(gWndDBProgress);
					} 
				if (gWndDBDone) _wnddb_list.dofunc(gWndDBDone);
				
				wnd_zoomOn();
				gWndDB_loading = false;
				if (gWndDBFrontWnd)
					{
					wnd_makeFront(gWndDBFrontWnd);
					} 
				return;
				}
			}
		setTimeout(wnddb_loadIfReady, 100);
		}
	}
//--------------------------------------
function wnddb_addIdxs()
	{
	$(".wnd").each(function(widx)
		{
		$(this).attr("widx", widx+1);
		var photos = $(this).find(".photos");
		if (photos)
			{
			photos.children().each(function(oidx)
				{
				$(this).attr("oidx", oidx+1);
				});
			}
		});
	}
//--------------------------------------
function wnddb_removeIdxs()
	{
	$(".wnd").each(function()
		{
		$(this).removeAttr("widx");
		$(this).find(".photos").children().removeAttr("oidx");
		});
	}
//------------------------------------------
function wnddb_getMediaAttrname(boxtype)
	{
	switch (boxtype) 
		{
		case "user":	return "user_id";
		case "album":	return "folder_id";
		case "photo":	return "photo_id";
		case "peeps":
		case "profile":
		case "settings":
		case "recblogs":
		case "recphotos":
		case "upload":
		case "email":
		case "logoff":
			break;
		}
	return false;
	}
//------------------------------------------
function wnddb_getMediaID(box)
	{
	var attr = wnddb_getMediaAttrname(box.attr("boxtype"));
	if (attr) return box.attr(attr);
	return 0;
	}
//------------------------------------------
function wnddb_setMediaID(box, mediaID)
	{
	var attr = wnddb_getMediaAttrname(box.attr("boxtype"));
	if (attr) box.attr(attr, mediaID);
	}

//------------------------------------------
function wnddb_getWndInfo(klass)
	{
	switch(klass) 
		{
		case "wnd_users":			return _wnd_users;
		case "wnd_album":			return _wnd_album;
		case "wnd_albums":			return _wnd_albums;
		case "wnd_bloglist":		return _wnd_bloglist;
		case "wnd_buddy":			return _wnd_buddy;
		case "wnd_chat":			return _wnd_chat;
		case "wnd_desktop":			return _wnd_desktop;
		case "wnd_desktops":		return _wnd_desktops;
		case "wnd_email":			return _wnd_email;
		case "wnd_media":			return _wnd_media;
		case "wnd_mugedit":			return _wnd_mugedit;
		case "wnd_profile":			return _wnd_profile;
		case "wnd_profile_edit":	return _wnd_profile_edit;
		case "wnd_upload":			return _wnd_upload;
		case "wnd_settings":		return _wnd_settings;
		}
	}

//------------------------------------------
function wnddb_initDesktop(rec)
	{
	var wnd = $(".wnd_desktop");
	wnddb_setBoxes(wnd, rec.objs);
	wnd.attr("widx",1);
	return wnd;
	}
//------------------------------------------
function wnddb_openWnd(rec)
	{
	var info = wnddb_getWndInfo(rec.klass);
	info.rect.top = rec.top;
	info.rect.left = rec.left;
	info.rect.height = rec.height;
	info.rect.width = rec.width;
	info.hide = (rec.visible==0);
	info.hbar = rec.hbar;
	if (rec.iconsize) info.iconsize = rec.iconsize;
	var wnd;
	info.zindex = rec.zindex;
	if (rec.prev_wnd>0 && rec.prev_obj>0)
		{
		var parWnd = $(".wnd[widx=" + rec.prev_wnd + "]");
		if (parWnd)
			{
			if (parWnd.length>0)
				{
				parElem = parWnd.find(".photos").children(".bigPhotoBox[oidx=" + rec.prev_obj + "]");
				if (parElem.length>0) wnd = wnd_open(info, parElem, true);
				}
			}
		}
	if (!wnd) 
		{
		info.more = 
			{
			user_id: rec.owner_user_id,
			folder_id: rec.folder_id,
			photo_id: rec.photo_id,
			blog_id: rec.blog_id
			};
		wnd = wnd_open(info);
		info.more = null;
		}
		
	if (parseInt(rec.zindex)>=gWndDBHighZIndex)
		{
		gWndDBHighZIndex = rec.zindex;
		gWndDBFrontWnd = wnd;
		}
	info.zIndex = undefined;
	wnd.attr("widx", rec.idx);
	return wnd;
	}
//------------------------------------------
function wnddb_getbox(wnd, obj)
	{
	var attr = wnddb_getMediaAttrname(obj.boxtype);
	var photos = wnd.find(".photos");
	if (attr)
		{
		var box = photos.children("["+attr+"="+obj.media_id+"]");
		if (box.length>0) return box;
		}
	else
		{
		var box = photos.children("[boxtype="+obj.boxtype+"]");
		if (box.length>0) return box;
		}
	return false;
	}
//------------------------------------------
function wnddb_setBoxes(wnd, objs)
	{
	if (objs)
		{
		for (var i = 0; i < objs.length; ++i)
			{
			var obj = objs[i];
			var box = wnddb_getbox(wnd, obj);
			if (box) 
				{
				box.css({top:parseInt(obj.top), left:parseInt(obj.left)})
				.attr("oidx",obj.idx)
				.children(".photocap").html(obj.title);
				}
			}
		}
	 }
//------------------------------------------
function wnddb_clear()
	{
	var data = new Object();
	data.command = "clear";
	var dataString = $.toJSON(data);
	delete data;
	$.post("wnddb.php", {data: dataString}, done_wnddb_clear);
	}
//--------------------------------------
function done_wnddb_clear(res)
	{
	wnddb_okToSave = false;
	goto_desktop();
	}