
INDEX_COLORS = new Array();
INDEX_COLORS[0] = "#ddddff";
INDEX_COLORS[1] = "#ccddff";
INDEX_COLORS[2] = "#ffffdd";
INDEX_COLORS[3] = "#ffEEdd";
INDEX_COLORS[4] = "#ccffee";
INDEX_COLORS[5] = "#ffeeed";
INDEX_COLORS[6] = "#aaefdd";
INDEX_COLORS[7] = "#cc99ff";
INDEX_COLORS[8] = "#ccddff";

String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var host_proj_obj;// = proj_objs[subproj_id];
var proj_objs = null;	//[0]=host_proj_obj, [1]...[N-1]=>subprojects

var subproj_id = 1;
var cookies = document.cookie.split(';');
for(var i=0; i < cookies.length; i++) {
	var cookie = cookies[i].split('=');
	if (cookie[0].trim()=="subproj_id") {//
		subproj_id = cookie[1].trim(); 
	}
}

String.prototype.summary_proc = function () {
	return this.replace(/[\s](http:\/\/\S+)/g, " <a href='$1' target='#'>$1</a>").replace(/\t/g, "    ").replace(/\n{2,}/g, "<br><br>").replace(/\n/g, "<br>");
}

//history.go(0);
//alert(document.cookie);

var mini_adder = new mini_adder(); //Mini adder for vertical tab slider
function mini_adder () {

	this.THUMB_COUNT	= 20;
	this.introLoad		= true;
	
	this.build = function () {

		var content_str = "<div style='color:#333333;text-align:left;'>";

		content_str += "<form name='mini_adder_form'>";
		content_str += "<h4>Project Title: </h4>";
		content_str += "<input name='title' onClick='event.cancelBubble=true;' type='text' class=entry_input />";
		
		content_str += "<p/>";
		
		content_str += "<h4>Project Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  onClick='event.cancelBubble=true;' rows=16></textarea>";
		content_str += "</form>";
		
		content_str += "<p/>";
	
		content_str += "<button class=submit_button onClick='javascript:mini_adder.create_proj();' title='Create Project''>";
		content_str += "Create Project";
		content_str += "</button>";

		content_str += "</div>";

		return content_str;
	}

	this.create_proj = function () {

		var title	= document.mini_adder_form.title.value;
		var summary = document.mini_adder_form.summary.value;
		document.mini_adder_form.reset();

		add_proj.add(mini_adder.callback, title, summary);

	}

	this.callback = function (message) {
		alert(message);
	}
}

var subproj_adder = new subproj_adder(); 
function subproj_adder () {

	this.host_id = 0;
	
	this.build = function () {

		var content_str = "";//"<div style='color:#555555;'>";style='float:left; margin:4 8 0 0;'

		content_str += "<h2 style='color:#888888;border-bottom:1px solid gray;margin-top:24px;'>Create subproject under \""+host_proj_obj.title+"\"</h2>";

		content_str += "<form name='subproj_adder_form' style='color:#666666;'>";

		content_str += "<h4 >Title (up to 20 characters): </h4>";
		content_str += "<input name='title' type='text' class=entry_input maxlength=20 style='width:240px;'/>";
		
		content_str += "<p/>";
		
		content_str += "<h4 >Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=16></textarea>";
		content_str += "</form>";
		
		content_str += "<p/>";
	
		content_str += "<button class=submit_button onClick='javascript:subproj_adder.create_proj();hideEditor();' title='Click to create subproject''>";
		content_str += "Submit";
		content_str += "</button>";

		//content_str += "</div>";

		return content_str;
	}

	this.show_adder = function (host_id) {

//alert(host_id);
		
		this.host_id = host_id;

		document.getElementById("editor_mask").style.visibility = "visible";
		document.getElementById("overlay_editor_box").style.top = window.pageYOffset+20;
		document.getElementById("overlay_editor_box").style.visibility = "visible";
		document.getElementById("overlay_editor").innerHTML = this.build();

	}

	this.create_proj = function () {

		var title	= document.subproj_adder_form.title.value;
		var summary = document.subproj_adder_form.summary.value;
		document.subproj_adder_form.reset();

		//***20100303: CAUTION: Changed this.host_id to global db_id which may be post set after server data overide.
		//add_proj.add_subproj(subproj_adder.callback, this.host_id, title, summary);
		add_proj.add_subproj(subproj_adder.callback, db_id, title, summary);

	}

	this.callback = function (message) {
		history.go(0);
		alert(message);
	}
}







var attachment_adder = new attachment_adder(); 
function attachment_adder () {

	this.host_id = 0;
		
	this.build = function () {

		var content_str = "";//"<div style='color:#555555;'>";style='float:left; margin:4 8 0 0;'

		content_str += "<p style='margin-top:32px'/>";
		
		//content_str += "<h2 style='color:#888888;border-bottom:1px solid gray;'>Add Attachment to \""+host_proj_obj.title+"\"</h2>";
		content_str += "<h2 style='color:#888888;border-bottom:1px solid gray;'>Add Attachment</h2>";

//<form action="./service.php" method="POST" id='entry_form' enctype="multipart/form-data" onsubmit="showMask('1');" onReset="togglePanel('entry_panel');togglePanel('entry_form');showPanel('twitButtons','1');"  style='margin-top:8px;'>
		content_str += "<form action='./service_proj.php' name='attachment_adder_form' style='color:#666666;' ";
		content_str += "method='POST' enctype='multipart/form-data'  accept='image/jpg' ";
		content_str += "onReset='javascript:hideEditor();return false;' ";
		content_str += "onSubmit='javascript:history.go(0);'>";//project_core.save_editor();
		content_str += "<h4>Add as Folder <input type='checkbox' name='folder' onClick='attachment_adder.toggle_aux(this);' value='folder'></h4>";

		content_str += "<input type=hidden name='action' value='add_attachment'>";
		//content_str += "<input type=hidden name='user_id' value='"+user_id+"'>";
		//content_str += "<input type=hidden name='host_id' value='"+host_proj_obj.db_id+"'>";
		content_str += "<input type=hidden name='host_id' value='"+this.host_id+"'>";
							
		content_str += "<p/>";
		//content_str += "<p/>";

		content_str += "<h4 >Title: </h4>";
		content_str += "<input name='title' type='text' class=entry_input />";//style='width:240px;'
		
		content_str += "<p/>";
		
		content_str += "<h4 >Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=16></textarea>";

		content_str += "<p/>";

		content_str += "<div style='' id='attachment_adder_aux'";

		content_str += "<h4 >URL (Optional): </h4>";
		content_str += "<input name='url' type='text' class=entry_input />";//style='width:240px;'
		
		content_str += "<p/>";
		content_str += "<span style='font-size:14;font-weight:bold;'>Links to online content </span>(Seperate multiple with commas)";
		content_str += "<input name='links' type='text' class=entry_input />";//style='width:240px;'

		content_str += "<span style='font-size:14;font-weight:bold;' >Local files upload </span> (To upload multiple files, hold Shift or Ctrl key when clicking on file names.)";
		content_str += "<input type='file' multiple name='local_files[]' class='entry_input' accept='image/jpg'>";
		
		content_str += "<p/>";
		content_str += "</div>";

		content_str += "<input type=submit class=submit_button style='float:right;' title='Submit' value='Save'>";
		content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel' value='Cancel'>";
		content_str += "</form>";
		content_str += "<p/>";
	
		//content_str += "<button class=submit_button onClick='javascript:attachment_adder.create_attatchment();hideEditor();' title='Click to create subproject''>";
		//content_str += "Submit";
		//content_str += "</button>";

		//content_str += "</div>";

		return content_str;
	}

	this.show_adder = function (host_id) {

//alert("attachment add host_id="+host_id);
		this.host_id = host_id;

		document.getElementById("editor_mask").style.visibility = "visible";
		document.getElementById("overlay_editor_box").style.top = window.pageYOffset+20;
		document.getElementById("overlay_editor_box").style.visibility = "visible";
		document.getElementById("overlay_editor").innerHTML = this.build();
	}

	this.toggle_aux = function(sw) {
		document.getElementById("attachment_adder_aux").style.display = sw.checked?"none":"block";
	}

	//NOT USED!! Use form post
	this.create_attatchment = function () {

		var title	= document.attachment_adder_form.title.value;
		var summary = document.attachment_adder_form.summary.value;
		document.attachment_adder_form.reset();

		//not used. Use form post add
		add_proj.add_attachment(attachment_adder.callback, title, summary);

	}

	this.callback = function (message) {
		//history.go(0);
		alert(message);
	}
}


var inlay_adder = new inlay_adder(); 
function inlay_adder () {
	
	this.adder_visible = false;
	this.host_obj = null;

	this.build = function (host_obj) {

		this.host_obj = host_obj;

		var content_str = "<a style='color:#333;float:right;margin-bottom:8px;' href='javascript:inlay_adder.toggle_view();'>Add more content under \""+host_obj.title+"\"</a>";

		content_str += "<div id='inlay_adder_"+host_obj.db_id+"' class=rounded_bg style='display:none;'>";
		content_str += "<form action='./service_proj.php' name='inlay_adder_form' style='color:#666666;' ";
		content_str += "method='POST' enctype='multipart/form-data'  accept='image/jpg' ";
		content_str += "onSubmit='javascript:history.go(0);' ";//project_core.save_editor();
		content_str += "onReset='javascript:inlay_adder.toggle_view();return false;'> ";

		content_str += "<input type=hidden name='action' value='add_inlay'>";
		//content_str += "<input type=hidden name='user_id' value='"+user_id+"'>";
		content_str += "<input type=hidden name='host_id' value='"+host_obj.db_id+"'>";
							
		content_str += "<span style='font-size:14;font-weight:bold;' >Online Links </span>(Use commas to seperate multiple links.)";
		content_str += "<input name='links' type='text' class=entry_input />";//style='width:240px;'
		
		content_str += "<p/>";

		content_str += "<span style='font-size:14;font-weight:bold;' >Local files upload </span> (For multiple files, hold Shift or Ctrl key when clicking on file names.)";
		content_str += "<input type='file' multiple name='local_files[]' class='entry_input' accept='image/jpg'>";
		
		content_str += "<p/>";

		content_str += "<input type=submit class=submit_button style='float:right;' title='Submit' value='Submit'>";
		content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel' value='Cancel'>";
		content_str += "</form>";
		content_str += "<p/>";
		content_str += "</div>";
		return content_str;
	}

	this.toggle_view = function () {
		document.getElementById("inlay_adder_"+this.host_obj.db_id).style.display=this.adder_visible?"none":"block";
		this.adder_visible ^= true;
	}
}

var file_viewer = new file_viewer(); 
function file_viewer () {

	this.file_objs		= null;
	this.file_obj		= null;
	this.index_current	= 0;
	
	this.build = function (file_obj) {

		this.file_obj = file_obj;

		var content_str = "";
		content_str += "<p style='clear:both;margin-bottom:18px;' />";
		
		content_str += "<a style='margin-right:24px;font-size:18px;color:#888;' href='javascript:file_viewer.load_pre();' title='Previous'>Previous</a>";
		content_str += parseInt(this.index_current+1) + "/"+this.file_objs.length;
		content_str += "<a style='margin-left:24px;font-size:18px;color:#888;' href='javascript:file_viewer.load_next();' title='Next'>Next</a>";
		
		content_str += "<p style='clear:both;margin-bottom:48px;' />";
		content_str += "<div id='file_viewer_header' style='margin-top:16px;'>";//"<div style='color:#555555;'>";style='float:left; margin:4 8 0 0;'
		
		//if(signed_in>0) {
		if(is_owner) {
			content_str += "<a style='float:right;margin-top:4px;' href='javascript:file_viewer.load_editor();' title='Edit title and summary'>Edit</a>";
			content_str += "<a style='float:right;margin:4 8 0 0;padding-right:8px;border-right:1px solid gray;' href='javascript:object_relocator.load();' title='Move to other folders'>Move</a>";
		}
		
		//content_str += "<div style='font-size:18;font-weight:bold;margin-bottom:12px;border-bottom:1px solid gray;width;100%;'><a ";
		content_str += "<h2 style='border-bottom:1px solid gray;width;100%;'><a ";
				
				
				
				
		if(unescape(this.file_obj.link).match(/http[s]?:\/\/docs\.google\.com.+/)!=null) {

			var google_doc_key = unescape(this.file_obj.link).replace(/http[s]?:\/\/docs\.google\.com.+?=(\S+?)((I=en)|&).+/,"$1");
			//https://docs.google.com/drawings/edit?id=1O0PpK0lRa7NB7fY1VplC_vln8KTNOZzLqWJxIIb6GwM&hl=en			
			var google_doc_link = "https://docs.google.com/drawings/edit?id="+google_doc_key+"&hl=en";
			content_str += "href='javascript:newWindow(\""+google_doc_link+"\");'";

		} else if(file_obj.link.length>5) content_str += "href='javascript:newWindow(\""+file_obj.link+"\");'";
		
		var title = file_obj.title.length>1?file_obj.title:file_obj.type.toUpperCase();
		//content_str += "id=file_viewer_title>"+title+"</a></div>";
		content_str += "id=file_viewer_title>"+title+"</a></h2>";
		
		if (file_obj.summary.length>1) {
			if(file_obj.type=="note"&&file_obj.filenames.length>4) {
				content_str += "<a href='javascript:newWindow(document.note_image.src);' target='#' title='View Image "+file_obj.filenames+"'>";
				content_str += "<img src='"+file_obj.filenames+"' name='note_image' style='width:100%;border:1px solid white;'>";
				content_str += "</a>";
			}

			content_str += "<p/><div  id=file_viewer_summary class=text_body>"+file_obj.summary.summary_proc()+"</div>";
		}
		content_str += "</div><div style='margin:8px;'></div>";

		content_str += this.build_editor();
		content_str +=object_relocator.build(this.host_id, this.file_obj.db_id, "file_viewer_summary");
		content_str += "<p style='margin-bottom:8px;'/>";

		var link = unescape(this.file_obj.link);
		switch(true) {

			//Google Presentation
			case unescape(this.file_obj.link).match(/http[s]?:\/\/docs\.google\.com\/present\/.+/)!=null:

				var WIDTH=640;
				var HEIGHT=WIDTH/1.27;
			
				var present_url = unescape(this.file_obj.link);		

				content_str += "<iframe frameborder=0 src='"+present_url+"' width="+WIDTH+" height="+HEIGHT+"></iframe>";
				//content_str += inlay_adder.build(file_obj);
				//content_str += "<p style='margin-bottom:16px;'/>";				
				break;
			
			//Google Drawing
			case unescape(this.file_obj.link).match(/http[s]?:\/\/docs\.google\.com\/drawings\/.+/)!=null:

				var WIDTH=640;
				var HEIGHT=480;

				var google_doc_key = unescape(this.file_obj.link).replace(/http[s]?:\/\/docs\.google\.com.+?=(\S+?)((I=en)|&).+/,"$1");
				var photo_link = "http://docs.google.com/drawings/pub?id="+google_doc_key+"&w="+WIDTH+"&h="+HEIGHT;//this.proj_objs[slide_index].url);		

				content_str += "<a href='javascript:newWindow(document.image_0.src);' target='#' title='View Image "+photo_link+"'>";
				content_str += "<img class='core_photo' src='"+photo_link+"' name='image_0' border=0>";//width:100%;
				content_str += "</a>";

				content_str += "<p style='margin-bottom:16px;'/>";

				//content_str += inlay_adder.build(file_obj);
				//content_str += "<p style='margin-bottom:16px;'/>";				
				break;
			
			case file_obj.type=="folder":

				content_str += note_attachments.build(file_obj.db_id);

				content_str += "<p/>&nbsp<p/>";

				content_str += "<div style='float:right;'>";
				content_str += "<a href='http://xeeku.com/slides/"+file_obj.db_id+"' target='#'>View Slides</a>";
				content_str += "</div>";

				content_str += "<div style='float:right; margin-right:24px;'>";
				content_str += "<a href='http://xeeku.com/index_cards/"+file_obj.db_id+"' target='#'>Print Index Cards</a>";//print_index_card.php?id
				content_str += "</div>";
				content_str += "<p/> <p/>";
				break;

			case file_obj.type=="photo"||file_obj.type=="audio":
				
				content_str += "<a href='javascript:newWindow(document.image_0.src);' target='#' title='View Image "+file_obj.link+"'>";
				content_str += "<img class=core_photo src='"+file_obj.link+"' name='image_0' border=0>";//width:100%;
				content_str += "</a>";

				content_str += "<p style='margin-bottom:8px;'/>";
/*
				content_str += "<div class=audio_thumb>";
				content_str += '<object CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="256" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">';

				content_str += '<param name="src" value="'+file_obj.link+'">';
				content_str += '<param name="autoplay" value="true">';
				content_str += '<param name="loop" value="false">';
				content_str += '<param name="controller" value="false">';
				content_str += '<embed src="'+file_obj.link+'" qtsrc="rtsp://realmedia.uic.edu/itl/ecampb5/demo_broad.mov" width="320" height="256" autoplay="true" loop="false" controller="false" pluginspage="http://www.apple.com/quicktime/"></embed>';
				content_str += '</object>';

				content_str += '<audio src="'+file_obj.link.replace(/http/,"rtsp")+'" controls="controls">Your browser does not support the audio element.</audio>';

				//QT_WriteOBJECT(file_obj.link , '320', '240', '');


	content_str += "
		<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width="320"
        height="255" codebase='http://www.apple.com/qtactivex/qtplugin.cab'>
        <param name='src' value="http://servername/path/to/media.file">
        <param name='autoplay' value="false">
        <param name='controller' value="true">
        <param name='loop' value="false">
        <EMBED src="http://servername/path/to/media.file" width="320" height="255" autoplay="false" 
        controller="true" loop="false" pluginspage='http://www.apple.com/quicktime/download/'>
        </EMBED>
        </OBJECT>
		
				content_str += '</div>';	*/	


/*
				if(file_obj.latitude>0) {

					var width=640;
					var height=100;
					var zoom = 14;

					var static_map	= "http://maps.google.com/staticmap?";
					static_map += "zoom="+zoom+"&size="+width+"x"+height+"&sensor=false";
					
					//static_map += "&key=ABQIAAAA_KySUQnM7OdWB364sT4rZhSPgNhxjkuQ4z_exkn_3Ebrz7TAZRRjUKTBb1kifJEa5vqUtpFAyJspSA";
					static_map += "&key=ABQIAAAA_KySUQnM7OdWB364sT4rZhS4v7O42JX_rFASO9FVHOiNfIl1SxRI7JfgeEWz_9Nxre3hrWWUF6msYg";
					static_map += "&markers="+file_obj.latitude+","+file_obj.longitude+",tinyred";
					static_map += "&center="+file_obj.latitude+","+file_obj.longitude;

					content_str += "<img src="+static_map+" class=map_thumb style='width:"+width+"px;height:"+height+"px;'>";
				}
*/
				content_str += "<p style='margin-bottom:16px;'/>";

				content_str += inlay_adder.build(file_obj);
				content_str += "<p style='margin-bottom:16px;'/>";

				var filenames = file_obj.filenames.replace(/,$/,"").replace(/^,/,"");
				var files = filenames.split(",");
				if(files.length > 1) {

					content_str += "<p/>";

					for (var i=1; i<files.length; i++) {
						var file = files[i];//.replace(/\/photos/, "/photos/98x98");
						if(file.length<4) continue;
						content_str += "<img src='"+file+"'  class=photo_thumb name='image_"+i+"' onClick='file_viewer.switchImage("+i+");'>";

					}
				}

				break;

			case file_obj.type=="youtube":

				var width = 640;
				var height= 420;
				//var youtube_url = "http://img.youtube.com/vi/"+ tweet_obj.youtube_key+"/0.jpg";
				content_str += "<object width='"+width+"' height='"+height+"'><param name='movie' value='http://www.youtube.com/v/"+file_obj.youtube_key+"&hl=en&fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param>";
				content_str += "<embed src='http://www.youtube.com/v/"+file_obj.youtube_key+"&hl=en&fs=1&autoplay=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+width+"' height='"+height+"'></embed></object>";
				//this.youtube_emb = escape(this.youtube_emb);
				
				break;

			//case file_obj.link.length>10:
			case file_obj.type=="bookmark" || file_obj.type=='document':

				content_str += "<iframe src='"+file_obj.link.replace(/\s+/g,"+")+"' style='height:600px;width:100%;scrolling=auto;border:1px solid white;'/>";
			
				var files = file_obj.filenames.split(",");
				if(files.length > 0) {
		
					content_str += "<p/>";

					for (var i=0; i<files.length; i++) {
						var file = files[i].replace(/\/photos/, "/photos/98x98");
						content_str += "<img src='"+file+"' style='height:98px;border:1px solid white;margin:0 2 2 0;'>";

					}
				}
				break;

		}
	
		return content_str;
	}

	this.switchImage = function(index) {


		//if (this.photoContent.length>1) document.getElementById("content_media").innerHTML = this.photoContent;  
		//this.photoContent = "";

		eval("var currentImage = document.image_0.src");
		eval("var activeImage  = document.image_"+index+".src");
		document.image_0.src = activeImage;
		eval("document.image_"+index+".src = currentImage");
	}


	this.build_editor = function () {
		var content_str = "";
		content_str += "<div class=rounded_bg id='file_editor_form' style='color:#666666;display:none'>";

		content_str += "<form  name='file_editor_form' ";
		content_str += "onReset='javascript:file_viewer.hide_editor();return false;' ";
		content_str += "onSubmit='javascript:file_viewer.save_editor();return false;'>";

		content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel edit.' value='Cancel'>";
		content_str += "<input type=submit class=submit_button style='float:right;' title='Save edit (f).' value='Save'>";

		content_str += "<h4 >Title: </h4>";
		content_str += "<input name='title' type='text' class=entry_input style='width:400px;'/>";//maxlength=60 
		
		content_str += "<p/>";
		
		content_str += "<h4 >Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=16></textarea>";
		content_str += "</form>";
		content_str += "</div>";
		return content_str;
	}

	this.load_editor = function () {
		document.getElementById("file_editor_form").style.display = "block";
		document.getElementById("file_viewer_header").style.display = "none";

		//var proj_obj = proj_objs[subproj_id];
		document.file_editor_form.title.value = this.file_obj.title;
		document.file_editor_form.summary.value = this.file_obj.summary;
	}

	this.hide_editor = function () {
		document.getElementById("file_editor_form").style.display = "none";
		document.getElementById("file_viewer_header").style.display = "block";
		document.file_editor_form.reset();
	}
	
	this.save_editor = function () {
		this.hide_editor();

		var db_id   = this.file_obj.db_id;
		var title   = document.file_editor_form.title.value;
		var summary = document.file_editor_form.summary.value;

		this.file_obj.title = title;
		this.file_obj.summary = summary;
		
		
		document.getElementById("file_viewer_title").innerHTML = "Please wait...";
		//document.getElementById("file_viewer_summary").innerHTML = summary;
		
//alert(title);
//return;
		
		edit_proj.edit(this.edit_callback, db_id, title, summary);
	}
	
	this.edit_callback = function (message) {
alert(message);
		document.getElementById("file_viewer_title").innerHTML   = document.file_editor_form.title.value;//file_viewer.file_obj
		document.getElementById("file_viewer_summary").innerHTML = document.file_editor_form.summary.value.replace(/\n/g, "<p style='margin:4px;'> ");//file_viewer.file_obj.summary

//history.go(0);
	}

	//this.show = function(file_obj) {
	this.show = function(host_id, index, file_objs) {

		this.index_current	= index;
		this.file_objs		= file_objs;
		this.host_id		= host_id;


		var file_obj = this.file_objs[this.index_current];
		
		document.getElementById("overlay_mask").style.visibility = "visible";
		document.getElementById("overlay_content_box").style.top = window.pageYOffset+20;
		document.getElementById("overlay_content_box").style.visibility = "visible";
		document.getElementById("overlay_content").innerHTML = this.build(file_obj);

		//note_attachments.set_adder("javascript:note_attachments.show_adder();");
		load_attachments.load(note_attachments.callback, file_obj.db_id);
	}

	this.load_pre = function () {
		if (this.index_current > 0) {
			document.getElementById("overlay_content").innerHTML = this.build(this.file_objs[--this.index_current]);
		}
	}

 	this.load_next = function () {
		if (this.index_current < this.file_objs.length-1) {
			document.getElementById("overlay_content").innerHTML = this.build(this.file_objs[++this.index_current]);
		}
	}


	this.callback = function (message) {
		//history.go(0);
alert(message);
		//alert("abcd");
	}
}

var object_relocator = new object_relocator(); 
function object_relocator () {

	this.host_id = 0;
	this.obj_id = 0;
	this.content_divid = 0;

	this.cloud_divid = null;

	this.build = function (host_id, obj_id, content_divid) {

		this.host_id		= host_id;
		this.obj_id			= obj_id;
		this.content_divid	= content_divid;
		this.cloud_divid	= "relocator_folder_cloud"+obj_id;//"cloud_divid"+host_id;

//alert(host_id);

		var content_str = "";//class=rounded_bg 
		content_str += "<div id='relocator_form' class=rounded_bg style='color:#666666;display:none;'>";
		content_str += "<div style='font-size:16px;margin-bottom:8px;'>Click on folder name(s) to move note to.</div>";
		content_str += "<div style='clear:both;width:100%;line-height:24px;' id='"+this.cloud_divid+"'></div>";//relocator_folder_cloud
		
		content_str += "<form action='/service_proj.php' name='relocator_form' method='POST' ";
		content_str += "onReset='javascript:object_relocator.hide();return false;' ";
		//content_str += "onSubmit='javascript:object_relocator.save();return true;'>";//action='' 
		
		content_str += "<input name='action' value='move_to_folder' type='hidden'>";
		content_str += "<input name='host_id' value='"+this.host_id+"' type='hidden'>";
		content_str += "<input name='new_host_ids' type='hidden'>";
		content_str += "<input name='obj_id' value='"+this.obj_id+"' type='hidden'>";

		content_str += "<input type=reset class=submit_button style='float:right;margin-right:0px;' title='Cancel Move.' value='Cancel'>";
		content_str += "<input type=submit class=submit_button style='float:right;' title='Carry out Move' value='Move'>";
		content_str += "</form>";

		content_str += "<p/>";
		content_str += "</div>";

		//content_str += "<a href='javascript:object_relocator.save_relocator()'>Test</a>";

		return content_str;
	}

	this.load = function () {
		document.getElementById("relocator_form").style.display = "block";
		
		try {document.getElementById(this.content_divid).style.display = "none";}
		catch (err) {}

		this.load_folders();
	}

	this.hide = function () {
		document.getElementById("relocator_form").style.display = "none";
		try {document.getElementById(this.content_divid).style.display = "block";}
		catch (err) {}

		document.relocator_form.reset();
	}
	
	this.save = function () {
		
		//this.hide_relocator();

		var note_id   = this.note_obj.db_id;
		var host_ids  = document.relocator_form.new_host_ids.value;

		//alert("note_id od="+note_id+", host_ids="+host_ids);
		//edit_proj.edit(this.edit_callback, db_id, title, summary);
	}

	this.load_folders = function () {
		
		//var user_id="jzhang88888";
//alert("user_id="+user_id);

		var req = new XMLHttpRequest();

		req.onreadystatechange = getReadyStateHandler(req, this.load_callback);
		req.open('POST', '/chrome.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send('action=load_folders&user_id='+escape(user_id));
	}
	
	this.load_callback = function (xml_str) {

		var folderObjs = xeeku_parser(xml_str);
		if (folderObjs.length==0) return;

		var folders_str = "";
		
		for (var i = 0; i < folderObjs.length; i++) {

			var db_id = folderObjs[i].db_id;
			folderObjs[i].index = i;

			//tags_str += "<a class=tag_label style='color:#"+parseInt(randomColorDark()).toString(16)+";' "; class=tag_label 
			
			folders_str += "<a id='folder"+db_id+"' href='javascript:object_relocator.toggle_folder("+db_id+", \""+folderObjs[i].title+"\");' style='margin-right:24px;font-size:"+eval(Math.random()*8+12)+"px'>";

			folders_str += folderObjs[i].title;
			folders_str += "</a> ";
		}

		document.getElementById(object_relocator.cloud_divid).innerHTML = folders_str;//"relocator_folder_cloud"+
	}

	this.toggle_folder = function (id, label) {

		//alert(id);

		var folder_id = 'folder'+id;
		var font_weight = document.getElementById(folder_id).style.fontWeight;
		if(font_weight=='bold') {
			document.getElementById(folder_id).style.fontWeight = 'normal';
			document.getElementById(folder_id).style.textDecoration = 'none';
			document.getElementById(folder_id).style.color = '#dc8700';

			var tags_str = document.relocator_form.new_host_ids.value.replace(id+',','');
			document.relocator_form.new_host_ids.value = tags_str;

		} else {
			document.getElementById(folder_id).style.fontWeight = 'bold';
			document.getElementById(folder_id).style.textDecoration = 'underline';
			document.getElementById(folder_id).style.color = 'red';
			if (!document.relocator_form.new_host_ids.value.match(/,$/)) document.relocator_form.new_host_ids.value += ',';
			document.relocator_form.new_host_ids.value += id+',';
		}
	}
}

var note_viewer = new note_viewer(); 
function note_viewer () {
	
	this.note_obj = null;
	this.host_id  = 0;

	this.build = function () {

		//this.note_obj = note_obj;
		//alert(this.note_obj.title);

		var content_str = "";//"<div style='color:#555555;'>";style='float:left; margin:4 8 0 0;'
		var content_str = "<div id='note_viewer_header'>";//"<div style='color:#555555;'>";style='float:left; margin:4 8 0 0;'
		content_str += "<a href='"+this.note_obj.link+"' style='font-style:italic;font-size:11px;width:100%;' target='#'>"+this.note_obj.link.substr(0, 70)+"...</a>";

		content_str += "<a style='float:right;margin-top:20px;' href='javascript:note_viewer.load_editor();'  title='Edit note title and summary'>Edit</a>";
		content_str += "<a style='float:right;margin:20 8 0 0;padding-right:8px;border-right:1px solid gray;' href='javascript:object_relocator.load();' title='Move to other folders'>Move</a>";
		content_str += "<h3  style='margin-top:4px;border-bottom:1px solid gray;'>"+this.note_obj.title+"</h3>";
		content_str += "</div><p>";
		
		content_str += this.build_editor();
		//content_str += this.build_relocator();
		content_str += object_relocator.build(this.host_id, this.note_obj.db_id, "note_summary");
		content_str += "<p style='margin-bottom:16px;'/>";

		var summary = "";
		//assuming always 1 one attachment for index card (note/qcard)
		if (this.note_obj.link.match(/\.jpg/))			summary	+= "<img src='"+this.note_obj.link+"' class='index_card_photo'> ";
		else if (this.note_obj.youtube_key) {

			var width = 580;
			var height= 400;
			var youtube_url = "http://img.youtube.com/vi/"+ this.note_obj.youtube_key+"/0.jpg";

			var youtube_emb = "<object width='"+width+"' height='"+height+"'><param name='movie' value='http://www.youtube.com/v/"+this.note_obj.youtube_key+"&hl=en&fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param>";
			youtube_emb += "<embed src='http://www.youtube.com/v/"+this.note_obj.youtube_key+"&hl=en&fs=1&autoplay=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+width+"' height='"+height+"'></embed></object>";
			youtube_emb = escape(youtube_emb);

			summary += "<a href='javascript:showNoteOverlay(\""+youtube_emb+"\");' class='tweet_media' title='Watch Youtube \""+this.note_obj.title+"\"'>";//onClick='http://twitter.com/"+tweetObj.name+"'
			summary += "<img src='"+youtube_url+"' style='height:73px;' border=0>";
			summary += "</a>";

			//summary	+= "<img src='http://img.youtube.com/vi/"+this.note_obj.youtube_key+"/default.jpg'  class='index_card_photo'>";
		
		} else if (this.note_obj.filenames.length>5)		summary	+= "<img src='"+this.note_obj.filenames+"' class='index_card_photo'> ";
		
		if (this.note_obj.summary.length>1) {
			summary +=  this.note_obj.summary.summary_proc().replace(/\n/g, "<p style='margin:4px;'/>");
		}
		
		content_str += "<div id=note_summary class=text_body>"+summary+"</div>";
		content_str += "</p><p/>";
		
		//content_str += note_attachments.build();	
		return content_str;
	}

	this.show = function(host_id, note_obj) {

		this.host_id = host_id;
		this.note_obj = note_obj;
		
		//document.getElementById("note_mask").style.visibility = "visible";
		//document.getElementById("overlay_note_box").style.top = window.pageYOffset+120;
		//document.getElementById("overlay_note_box").style.left = 80;
		//document.getElementById("overlay_note_box").style.visibility = "visible";
		//document.getElementById("overlay_note").innerHTML = this.build(note_obj);

		showNoteOverlay (this.build(note_obj));
	}

/*
	this.build_relocator = function () {
		var content_str = "";//class=rounded_bg 
		content_str += "<div id='note_relocator_form' style='color:#666666;display:none;'>";
		content_str += "<div style='clear:both;width:100%;line-height:24px;' id=note_relocator_folder_cloud></div>";
		
		content_str += "<form action='/service_proj.php' name='note_relocator_form' method='POST' ";
		content_str += "onReset='javascript:note_viewer.hide_relocator();return false;' ";
		content_str += "onSubmit='javascript:note_viewer.save_relocator();return true;'>";//action='' 
		
		content_str += "<input name='action' value='move_to_folder' type='hidden'>";
		content_str += "<input name='host_id' value='"+this.host_id+"' type='hidden'>";
		content_str += "<input name='new_host_ids' type='hidden'>";
		content_str += "<input name='obj_id' value='"+this.note_obj.db_id+"' type='hidden'>";

		content_str += "<input type=reset class=submit_button style='float:right;margin-right:0px;' title='Cancel Move.' value='Cancel'>";
		content_str += "<input type=submit class=submit_button style='float:right;' title='Carry out Move' value='Move'>";
		content_str += "</form>";

		content_str += "</div>";

		content_str += "<a href='javascript:note_viewer.save_relocator()'>Test</a>";

		return content_str;
	}

	this.load_relocator = function () {
		document.getElementById("note_relocator_form").style.display = "block";
		document.getElementById("note_summary").style.display = "none";
		this.load_relocator_folders();
	}

	this.hide_relocator = function () {
		document.getElementById("note_relocator_form").style.display = "none";
		document.getElementById("note_summary").style.display = "block";
		document.note_relocator_form.reset();
	}
	
	this.save_relocator = function () {
		
		//this.hide_relocator();

		var note_id   = this.note_obj.db_id;
		var host_ids  = document.note_relocator_form.new_host_ids.value;

		//alert("note_id od="+note_id+", host_ids="+host_ids);
		//edit_proj.edit(this.edit_callback, db_id, title, summary);
	}
	


	this.load_relocator_folders = function () {
		
		var user_id="jzhang88888";
		var req = new XMLHttpRequest();

		req.onreadystatechange = getReadyStateHandler(req, this.load_folders_callback);
		req.open('POST', '/chrome.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send('action=load_folders&user_id='+user_id);
	}
	
	this.load_folders_callback = function (xml_str) {
			
		var folderObjs = xeeku_parser(xml_str);
		if (folderObjs.length==0) return;

		var folders_str = "";
		
		for (var i = 0; i < folderObjs.length; i++) {

			var db_id = folderObjs[i].db_id;
			folderObjs[i].index = i;

			//tags_str += "<a class=tag_label style='color:#"+parseInt(randomColorDark()).toString(16)+";' "; class=tag_label 
			
			folders_str += "<a id='folder"+db_id+"' href='javascript:note_viewer.toggle_folder("+db_id+", \""+folderObjs[i].title+"\");' style='margin-right:24px;font-size:"+eval(Math.random()*8+12)+"px'>";

			folders_str += folderObjs[i].title;
			folders_str += "</a> ";
		}
		document.getElementById("note_relocator_folder_cloud").innerHTML = folders_str;
	}

	this.toggle_folder = function (id, label) {

		//alert(id);

		var folder_id = 'folder'+id;
		var font_weight = document.getElementById(folder_id).style.fontWeight;
		if(font_weight=='bold') {
			document.getElementById(folder_id).style.fontWeight = 'normal';
			document.getElementById(folder_id).style.textDecoration = 'none';
			document.getElementById(folder_id).style.color = '#dc8700';

			var tags_str = document.note_relocator_form.new_host_ids.value.replace(id+',','');
			document.note_relocator_form.new_host_ids.value = tags_str;

		} else {
			document.getElementById(folder_id).style.fontWeight = 'bold';
			document.getElementById(folder_id).style.textDecoration = 'underline';
			document.getElementById(folder_id).style.color = 'red';
			if (!document.note_relocator_form.new_host_ids.value.match(/,$/)) document.note_relocator_form.new_host_ids.value += ',';
			document.note_relocator_form.new_host_ids.value += id+',';
		}
	}
*/


	this.build_editor = function () {
		var content_str = "";
		content_str += "<div class=rounded_bg id='note_editor_form' style='color:#666666;display:none'>";

		content_str += "<form name='note_editor_form' ";//action='' 
		content_str += "onReset='javascript:note_viewer.hide_editor();return false;' ";
		content_str += "onSubmit='javascript:note_viewer.save_editor(true);return false;'>";

		content_str += "<p/>";
		content_str += "<h4 >Title: </h4>";
		content_str += "<input name='title' type='text' class=entry_input style='width:400px;'/>";//maxlength=60 
		
		content_str += "<p/>";
		
		content_str += "<h4 >Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=16></textarea>";
		content_str += "<p/>";

		content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel edit.' value='Cancel'>";
		content_str += "<input type=submit class=submit_button style='float:right;' title='Save and exit.' value='Save and Exit'>";
		content_str += "<input type=button class=submit_button style='float:right;' onClick='javascript:note_viewer.save_editor(false)' title='Save and continue.' value='Save'>";

		content_str += "</form>";
		content_str += "<p/>";
		content_str += "</div>";
		return content_str;
	}

	this.load_editor = function () {
		document.getElementById("note_editor_form").style.display = "block";
		document.getElementById("note_viewer_header").style.display = "none";

		//var proj_obj = proj_objs[subproj_id];
		document.note_editor_form.title.value = this.note_obj.title;
		document.note_editor_form.summary.value = this.note_obj.summary;
	}

	this.hide_editor = function () {
		document.getElementById("note_editor_form").style.display = "none";
		document.getElementById("note_viewer_header").style.display = "block";
		document.note_editor_form.reset();
	}
	
	this.save_editor = function (exit) {


		var db_id   = this.note_obj.db_id;
		var title   = document.note_editor_form.title.value;
		var summary = document.note_editor_form.summary.value;
		edit_proj.edit(this.edit_callback, db_id, title, summary);

		if(exit) {
			this.note_obj.title		= title;
			this.note_obj.summary	= summary;
			this.show(this.host_id, this.note_obj);
			this.hide_editor();
		}
	}
	
	this.edit_callback = function (message) {
		alert("Edit has been saved.");
		//alert(message);
		//history.go(0);
	}
}

var note_attachments = new note_attachments(); //for now only note type are placed under a second tier folder????
function note_attachments () {

	this.attachment_objs = null;
	this.attachment_objs_hash = null;

	this.host_id = 0;
	
	this.build = function (host_id) {

		this.host_id = host_id; //db_id of the note which host the attachments here.

		var content_str = "";
		content_str += "<div style='border-bottom:1px solid gray;width:100%'>";
		content_str += "<a id=note_add href='javascript:attachment_adder.show_adder(note_attachments.host_id);' style='float:right;color:#333;margin-top:4px;'>Add Content</a>";
		content_str += "<h3 style=color:gray>Notes</h3>";
		content_str += "</div>";

		//content_str += "<p/>";
		content_str += "<div id=notes_body class=attachments_body></div>";

		return (content_str);
	}
	
	this.set_adder = function (adder) {
		document.getElementById("note_add").title = adder;
		document.getElementById("note_add").href = adder;
	}

/*
	this.load_body = function (body) {
		document.getElementById("notes_body").innerHTML = body;
	}

	this.show = function (attachment_obj) {
		alert(attachment_obj.title);
	}


	this.load = function(db_id) {

		alert(db_id);
	}

*/
	
	this.load_viewer  = function (index) {
		//file_viewer.show(this.attachment_objs[index]);
		note_viewer.show(this.host_id, this.attachment_objs[index]);
		
		//alert(index);
		//file_viewer.show();
	}
	this.callback = function (host_id, attachment_objs) {
	
		note_attachments.host_id = host_id;

		note_attachments.attachment_objs_hash = new Array();
		
		note_attachments.attachment_objs = attachment_objs;
		document.getElementById("notes_body").innerHTML = "";

		for (var i=0; i<attachment_objs.length; i++) {


			var attachment_obj = attachment_objs[i];
			attachment_obj.index = i;

			note_attachments.attachment_objs_hash[attachment_obj.db_id] = attachment_obj;

			//alert(attachment_obj.db_id);
			//if(i==0) alert(attachment_obj.db_id);
			//if(i==0) alert(note_attachments.attachment_objs_hash[parseInt("6878")].title);//attachment_obj.db_id

			var attachment_div = document.createElement("div");

			var title_header = document.createElement("div");
			title_header.setAttribute("class", "title_header");
			var title = attachment_objs[i].title;
			title = title.length>42?title.substr(0,38)+"...":title;
			title_header.innerHTML = title;

			attachment_div.appendChild(title_header);


			attachment_div.setAttribute("class", "cell");
			//attachment_div.setAttribute("style", style_rotate(9));//"-webkit-transform: rotate(9deg)");
			attachment_div.setAttribute("onClick", "javascript:note_attachments.load_viewer("+i+");");
			attachment_div.setAttribute("title", "Note ID: "+attachment_objs[i].db_id);

			var is_google_drawing = attachment_obj.link.unescape().match(/http[s]?:\/\/docs\.google\.com.+/);


		if (is_google_drawing) {

				//http://docs.google.com/drawings/pub?id=1O0PpK0lRa7NB7fY1VplC_vln8KTNOZzLqWJxIIb6GwM&w=960&h=720
				var google_doc_key = attachment_obj.link.replace(/http[s]?:\/\/docs\.google\.com.+?=(\S+?)((I=en)|&).+/,"$1");
				var photo_link = "http://docs.google.com/drawings/pub?id="+google_doc_key+"&w="+WIDTH+"&h="+HEIGHT;//this.proj_objs[slide_index].url);
				
				var photo = document.createElement("img");
				photo.setAttribute("src", photo_link);
				photo.setAttribute("style", "max-width:98;max-height:98");
				attachment_div.appendChild(photo);
					
				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;

				//attachment_div.appendChild(title_header);
				
		} else if (attachment_obj.type=="photo") {

				var photo_link = attachment_obj.link.replace(/photos\//, "photos/98x98/");

				var photo = document.createElement("img");
				photo.setAttribute("src", photo_link);
				photo.setAttribute("style", "max-width:98;max-height:98");
				attachment_div.appendChild(photo);
					
				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;

				//attachment_div.appendChild(title_header);

			} else if (attachment_obj.type=="youtube") {

				var photo_link = "http://img.youtube.com/vi/"+attachment_obj.youtube_key+"/0.jpg";

				var photo = document.createElement("img");
				photo.setAttribute("src", photo_link);
				photo.setAttribute("style", "max-width:98;max-height:98");
				attachment_div.appendChild(photo);
					
				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;

				//attachment_div.appendChild(title_header);


			} else if (attachment_obj.type=="bookmark") {

				var photo_link;
				if(attachment_obj.link.match(/(\.jpg$)|(\.png)/i)) {
					photo_link = attachment_obj.link;

				} else if (attachment_obj.link.match(/youtube\.com/i)) {
					var youtube_key = attachment_obj.link.replace(/.+v=([\d\w_\-]+).*/,"$1");
					photo_link = "http://img.youtube.com/vi/"+youtube_key+"/default.jpg";

				} else if (attachment_obj.filenames.split(/,/)[0].match(/(\.jpg$)|(\.png$)/i)) {
					photo_link=attachment_obj.filenames.split(/,/)[0];
				
				} else if (attachment_obj.link.length>4) {
					photo_link = "http://images.websnapr.com/?size=s&url="+attachment_obj.link;
				}

				if(photo_link.length>4) {
					var photo = document.createElement("img");
					photo.setAttribute("src", photo_link);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);
				}
					
				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;

				//attachment_div.appendChild(title_header);

			} else if (attachment_obj.type=="note") {

				if(attachment_obj.filenames.length>4)  {

					var photo = document.createElement("img");
					photo.setAttribute("src", attachment_obj.filenames);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);
				}
					
				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;

				//attachment_div.appendChild(title_header);

			} else {

				//var title_header = document.createElement("div");
				//title_header.setAttribute("class", "title_header");
				//title_header.innerHTML = attachment_objs[i].title;
				//attachment_div.appendChild(title_header);
				
				var summary_body = document.createElement("div");
				summary_body.setAttribute("class", "summary_body");

				var summary = attachment_objs[i].summary;
//summary = summary.length>200?summary.substr(0, 50)+"...":summary;
				summary_body.innerHTML = summary;
				attachment_div.appendChild(summary_body);
			}
			
			document.getElementById("notes_body").appendChild(attachment_div);
		}

		document.getElementById("notes_body").appendChild(document.createElement("p"));
		note_attachments.insert_attachment();
	
	}

	this.insert_attachment = function () {
		var content = document.getElementById("core_content").innerHTML;
		
		//content = content.replace(/`(\d+)/g, project_core.insert_gen("$1"));
		var keys = content.replace(/.*?`(\d+)+/g, "$1``").split(/``/);


		var set_left = "";
		for (var i in keys) {

			if (keys[i].match(/^\d+$/)) {

				var key = parseInt(keys[i]);
				var attach_obj = note_attachments.attachment_objs_hash[key];

				if (attach_obj==undefined) continue;

				if(set_left.length>5) set_left = "";
				else set_left = "style='float:left;margin:4 12 2 0;'";

				var page_num = parseInt(i)+1;
				var obj_str = "<div class='attachment_insert' "+set_left+" onClick='javascript:note_attachments.load_viewer("+attach_obj.index+");'>";
				
				switch(true) {
					
					case attach_obj.type=="photo":
						obj_str += "<img src='"+attach_obj.link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
						break;

 					case attach_obj.type=="youtube":
						var youtube_thumb_link = "http://img.youtube.com/vi/"+attach_obj.youtube_key+"/0.jpg";
						obj_str += "<img src='"+youtube_thumb_link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
						break;


					case attach_obj.type=="bookmark":
						
						var thumb_link;
						
						if (attach_obj.filenames.split(/,/)[0].match(/(\.jpg$)|(\.jpeg$)|(\.png$)/i)) 
								thumb_link = attach_obj.filenames.split(/,/)[0];
						else thumb_link= "http://images.websnapr.com/?size=s&url="+attach_obj.link;

						obj_str += "<img src='"+thumb_link+"' style='width:202;max-height:160;'>";
						break;
					
					case attach_obj.type=="document":
						obj_str += "<img src='./graphics/pdf_80.png' style=''>";//e='max-height:80px;float:right'>";
						break;
				}		
			
				//obj_str += "<img src='"+attach_obj.link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
				obj_str += "<div style='font-size:11px;'>Figure "+page_num+". "+attach_obj.title+"</div>";
				obj_str += "</div>";

				var reg_exp = new RegExp("`"+keys[i], "");
				content = content.replace(reg_exp, obj_str);
			}
		}

		document.getElementById("core_content").innerHTML = content+"<p/>";
	}
}










































 function getReadyStateHandler(req, responseXmlHandler) {

   return function () {
     if (req.readyState == 4) {//1:idle;2:start;3:inthe middle;4:complete
       
       if (req.status == 200) {
         responseXmlHandler(req.responseText);

       } else {
         alert("HTTP error "+req.status+": "+req.statusText);
       }
     }
   }
 }

var add_proj = new add_proj();
function add_proj () {

	this.callback		= '';

	this.add = function (callback, title, summary) {
		
		this.callback		= callback;

		var server_args = "action=add_project";
		server_args += "&title="+escape(title);
		server_args += "&summary="+escape(summary);
		server_args += "&owner="+user_id;

		this.ajax_req(server_args);
	}

	this.add_subproj = function (callback, host_id, title, summary) {
		
		this.callback		= callback;

		var server_args = "action=add_subproject";
		server_args += "&title="+escape(title);
		server_args += "&summary="+escape(summary);
		server_args += "&owner="+user_id;
		//server_args += "&host_id="+host_proj_obj.db_id;
		server_args += "&host_id="+host_id;

		this.ajax_req(server_args);
	}

/* Must use form post for file upload
	this.add_attachment = function (callback, title, summary) {
		
		this.callback		= callback;

		var server_args = "action=add_attachment";
		server_args += "&title="+escape(title);
		server_args += "&summary="+escape(summary);
		server_args += "&owner="+user_id;
		server_args += "&host_id="+host_proj_obj.db_id;

		this.ajax_req(server_args);
	}
*/

	this.ajax_req = function (server_args) {



		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(server_args);
	}

	this.ajax_rcv = function (message) {

		//alert(message);
		add_proj.callback(message);
	}
}

var edit_proj = new edit_proj();
function edit_proj () {

	this.callback		= '';

	this.edit = function (callback, db_id, title, summary) {
		
		this.callback		= callback;

		var server_args = "action=edit_project";
		server_args += "&db_id="+escape(db_id);
		server_args += "&title="+escape(title);
		server_args += "&summary="+escape(summary);
		server_args += "&owner="+user_id;

		this.ajax_req(server_args);
	}

	this.ajax_req = function (server_args) {

		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(server_args);
	}

	this.ajax_rcv = function (message) {
		edit_proj.callback(message);
	}
}

var load_projs_listing = new load_projs_listing(); //load projects index under vertical tab
function load_projs_listing () {

	this.callback		= null;
	this.panel_id		= null;

	this.load = function (callback, panel_id) {	

		this.callback			= callback;
		this.panel_id			= panel_id;

		var server_args = "action=load_projs_listing";
		server_args += "&owner="+user_id;

		//alert("load proj lisitng");

		this.ajax_req(server_args);
	}

	this.ajax_req = function (server_args) {

		var req = new XMLHttpRequest();

		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(server_args);
	}

	this.ajax_rcv = function (message) {

//alert(message);

		//proj_objs		= xeeku_parser(message);
		//host_proj_obj	= xeeku_parser(message)[subproj_id];

		//load_projs_listing.callback(xeeku_parser(message)[0]);
		load_projs_listing.callback(xeeku_parser(message), load_projs_listing.panel_id);
	}
}

var loadBookmarkTags = new loadBookmarkTags();
function loadBookmarkTags () {//load latest 40 photos

	this.t				= 0;

	this.callback		= null;
	this.panel_id		= null;

	this.auto_update	= true;

	this.load = function (callback, panel_id) {
		
		this.callback		= callback;
		this.panel_id		= panel_id;

		this.ajax_req();
		//this.update();
	}
	
	this.update = function () {

		if (this.auto_update) {
			this.ajax_req('');
			window.clearTimeout(this.t);
			this.t = window.setTimeout("loadBookmarkTags.update()", "600000");//every 10 minutes
		}
	}

	this.ajax_req = function () {

		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send('action=load_tags&user_id='+user_id);
	}

	this.ajax_rcv = function (xml_str) {

		//var items = ie_xml_parser(xml_str);
		var items = xml_tag_parser(xml_str);
		//var items = xeeku_parser(xml_str);
		loadBookmarkTags.callback(items, loadBookmarkTags.panel_id);
	}
}



//load_projs_listing.load();

var load_proj = new load_proj();
function load_proj () {

	this.callback		= '';

	this.load = function (callback, db_id) {
		
		this.callback		= callback;

		var server_args = "action=load_project";
		
		/*
		switch(true) {
			
			case signed_in>0 && user_id=="jzhang88888":
				server_args = "action=load_project";
				break;

			case signed_in>0:
				server_args = "action=load_latest_project";
				break;

			default:
				server_args = "action=load_project";
		}
*/

		server_args += "&db_id="+db_id;
		server_args += "&owner="+user_id;
		server_args += "&signed_in="+signed_in;

//alert(server_args);
		this.ajax_req(server_args);
	}

	this.ajax_req = function (server_args) {

		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(server_args);
	}

	this.ajax_rcv = function (message) {

//alert(message);

		//IMPORTANT NOTE: proj_objs[0] is the hosting project
		//proj_objs[1-N_1] are the "subproject" or folders under the hosting project (proj_objs[0]).
		proj_objs		= xeeku_parser(message);


//alert(proj_objs.length);
		
		if(subproj_id>proj_objs.length-1) {
			subproj_id=0;
			host_proj_obj = null;

		} else {
			host_proj_obj	= proj_objs[subproj_id];
		}

//alert(server_args);
//alert("msg"+message);
//alert(subproj_id);
//alert(xeeku_parser();
//
		load_proj.callback(xeeku_parser(message)[0]);
		
		//#### 20100303: Experimental: When the cookie's master db_id is not set, the server uses the latest proj's db_id
		// then the local db_id must set for "Create subproject"
		if (db_id < 4) db_id = xeeku_parser(message)[0].db_id;	
	}
}

var load_attachments = new load_attachments();
function load_attachments () {

	this.callback		= '';
	this.host_id		= 0;

	this.load = function (callback, db_id) {
		
		this.callback		= callback;
		this.host_id		= db_id;

		var server_args = "action=load_attachments";
		server_args += "&db_id="+db_id;
		server_args += "&owner="+user_id;

		this.ajax_req(server_args);
	}

	this.ajax_req = function (server_args) {

		var req = newXMLHttpRequest();
		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(server_args);
	}

	this.ajax_rcv = function (xml_string) {
//alert(xml_string);
		load_attachments.callback(load_attachments.host_id, xeeku_parser (xml_string));
	}
}



function xeeku_parser (xml_string) { //for xeeku host only (not for RSS feed as fields are prioperitory

//alert(xml_string);

	var xmlobject = (new DOMParser()).parseFromString(xml_string, "text/xml");

	// get a reference to the root-element "rss"
	var root = xmlobject.getElementsByTagName('rss')[0];
	// get reference to "channel" element

	var channels = root.getElementsByTagName("channel");		
	// now get all "item" tags in the channel
	var items = channels[0].getElementsByTagName("item");

	var siteTitles	= channels[0].getElementsByTagName("title");
	var siteTitle	= siteTitles[0].firstChild.nodeValue;
	
	var siteDescs = channels[0].getElementsByTagName("description");
	var siteDesc = siteDescs[0].firstChild.nodeValue;

	var rssItems = new Array();

	for (i=0; i<items.length; i++) {
		
		var item		= new Object;
		item.siteTitle	= siteTitle; 
		item.siteDesc	= siteDesc; 
		
		var title		= items[i].getElementsByTagName("title");
		var keyword		= items[i].getElementsByTagName("keyword");
		var owner		= items[i].getElementsByTagName("owner");
		var summaries	= items[i].getElementsByTagName("summary");
		var date		= items[i].getElementsByTagName("timestamp");
		var type		= items[i].getElementsByTagName("type");
		var filenames	= items[i].getElementsByTagName("filenames");
		var rss_host	= items[i].getElementsByTagName("rss_host");
		var rss_host_title	= items[i].getElementsByTagName("rss_host_title");
		var video_keys	= items[i].getElementsByTagName("video_keys");
		var link		= items[i].getElementsByTagName("link");
		var latitude	= items[i].getElementsByTagName("latitude");
		var longitude	= items[i].getElementsByTagName("longitude");
		var photo_calendar	= items[i].getElementsByTagName("photo_calendar");
		var photo_count	= items[i].getElementsByTagName("photo_count");
		var db_id		= items[i].getElementsByTagName("id");
		var rating		= items[i].getElementsByTagName("rating");
		var timestamp	= items[i].getElementsByTagName("timestamp");
		var tag			= items[i].getElementsByTagName("tag");

//alert(summaries);

		if (title[0])		item.title	= title[0].firstChild.nodeValue; 
		if (keyword[0])		item.keyword	= keyword[0].firstChild.nodeValue; 
		if (owner[0])		item.owner		= owner[0].firstChild.nodeValue; 
		if (summaries[0].firstChild)	item.summary	= summaries[0].firstChild.nodeValue; 
		if (date[0])		item.date		= date[0].firstChild.nodeValue; 
		if (type[0].firstChild)	item.type	= type[0].firstChild.nodeValue; 
		if (filenames[0])	item.filenames	= filenames[0].firstChild.nodeValue; 
		if (rss_host[0])	item.rss_host	= rss_host[0].firstChild.nodeValue; 
		if (rss_host_title[0]) item.host_title	= rss_host_title[0].firstChild.nodeValue; 
		if (video_keys[0])	item.video_keys	= video_keys[0].firstChild.nodeValue; 
		if (link[0])		item.link		= link[0].firstChild.nodeValue; 
		if (latitude[0])	item.latitude	= latitude[0].firstChild.nodeValue; 
		if (longitude[0])	item.longitude	= longitude[0].firstChild.nodeValue; 
		if (photo_calendar[0])	item.photo_calendar	= photo_calendar[0].firstChild.nodeValue; 
		if (photo_count[0])	item.photo_count	= photo_count[0].firstChild.nodeValue; 
		if (db_id[0])		item.db_id	= db_id[0].firstChild.nodeValue; 
		if (rating[0])		item.rating	= rating[0].firstChild.nodeValue; 
		if (timestamp[0])	item.timestamp	= timestamp[0].firstChild.nodeValue; 
		if (tag[0])			item.tag	= tag[0].firstChild.nodeValue; 

		//id,db_table,owner,permission,type,link,rss_host_title,rss_host,
		//filenames,video_keys,rating,logintude,latitue,last_updated,timestamp;

		if(item.link.match(/http:\/\/(www\.)?youtube\.com/)) {
			item.youtube_key = item.link.replace(/.*http:\/\/(www\.)?youtube\.com\/\S+?v=([\w\d\-_]+).*/, "$2");

		} else if(item.link.match(/.*http:\/\/youtu\.be.*/)) {
			item.youtube_key = item.link.replace(/.*http:\/\/youtu\.be\/([\w\d\-]+).*/, "$1");
		}

		item.summary = unescape(item.summary).replace(/\+/g, " ");
		rssItems[i] = item;
	}
	return rssItems;
}


var inline_editor = new inline_editor(); //support limited to title/summary only
function inline_editor () {

	this.content_str  = "";

	this.db_id = 0;
	this.host_id = null;

	this.content_type = null;
	this.title_div	  = null;
	this.summary_div  = null;
	this.form_name  = null;
	
	this.build = function (content_type, db_id, host_id) {//, title_div, summary_div) {
		this.db_id = db_id;
		this.host_id = host_id;
		this.content_type = content_type;
		//this.title_div	  = title_div;
		//this.summary_div  = summary_div;
		this.form_name = content_type+"_inline_editor_form";
		
		this.content_str = "";
		this.content_str += "<div class=rounded_bg id='"+this.content_type+"_inline_editor_form' style='color:#666666;display:none'>";

		this.content_str += "<form name='"+this.form_name+ "' ";
		this.content_str += "onReset='javascript:inline_editor.hide();return false;' ";
		this.content_str += "onSubmit='javascript:inline_editor.submit();return false;'>";

		this.content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel edit' value='Cancel'>";
		this.content_str += "<input type=submit class=submit_button style='float:right;' title='Save edit' value='Save'>";

		this.content_str += "<h4 >"+this.content_type+" Title (up to 20 characters): </h4>";
		this.content_str += "<input name='title' type='text' class=entry_input maxlength=20 style='width:240px;'/>";
		
		this.content_str += "<p/>";
		
		this.content_str += "<h4 >Summary: </h4>";
		this.content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=4></textarea>";
		this.content_str += "</form>";
		this.content_str += "</div>";	
		
	}
	
	this.render = function () {
		return this.content_str;
	}

	this.show = function (host_obj) {

		var form_entry =eval("document."+inline_editor.form_name);
		
		form_entry.title.value		= host_obj.title;
		form_entry.summary.value	= host_obj.summary;

		var show_hide = document.getElementById(this.form_name).style.display=="none"?"block":"none";
		document.getElementById(this.form_name).style.display=show_hide;
		
		var hide_show = document.getElementById(inline_editor.host_id).style.display=="none"?"block":"none";
		document.getElementById(inline_editor.host_id).style.display=hide_show;
	}

	this.submit = function () {
		
		var form_entry =eval("document."+this.form_name);
		
		var title	= form_entry.title.value;
		var summary = form_entry.summary.value;

		this.ajax_req(title, summary);
		this.hide();
	}

	this.hide = function () {
		document.getElementById(this.form_name).style.display="none";
		document.getElementById(inline_editor.host_id).style.display="block";
	}

	this.ajax_req = function (title, summary) {

		var req = new XMLHttpRequest();

		req.onreadystatechange = getReadyStateHandler(req, this.ajax_rcv);
		req.open('POST', '/service_proj.php', true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var server_args = "action=edit_project";
		server_args += "&title="+escape(title);
		server_args += "&summary="+escape(summary);
		server_args += "&db_id="+this.db_id;

		req.send(server_args);
	}

	this.ajax_rcv = function (message) {
		history.go(0);
		//alert(message);
	}
}

var content_layout = new content_layout();
function content_layout () {

	this.title = null;
	this.summary = null;
	
	this.setup = function (db_id) {

//alert(db_id);//####

		document.cookie = "project_id="+db_id+"; path=/";

		var content_str = "";
		//content_str += "<div id=header>header</div>";
		content_str += "<div id=project_index class=project_index></div>";

		content_str += "<div id=prj_title style='float:left;'>title block</div>";//font defined by H8		

		if(user_id=="jzhang88888" && signed_in>0)
			content_str += "<a style='margin-left:16px;font-size:14px;' href='javascript:inline_editor.show(content_layout);'>Edit Topic</a>";
		
		//content_str += "<div style='clear:both;margin-bottom:0px;'>aaa</div>";
		
		content_str += "<div class=project>";
		
		inline_editor.build("Project", db_id, "prj_title");
		content_str += inline_editor.render();

		//=========================== Bookmarker ================================
//content_str += "<a href=\"javascript:var d=document,z=d.createElement('scr'+'ipt'),b=d.body;alert(d.location.href);\">abcd</a>";
		//=======================================================================

		content_str += "<p style='margin-bottom:0px;'/>";

		content_str += "<div id=prj_core		class=core>project core</div>";
		content_str += "<div id=prj_attachments class=attachments>project attachments</div>";

		content_str += "<div id=subprojects class=subprojects style=''>subproj</div>";
		content_str += "</div>"; //end of class project

		document.getElementById("content").innerHTML = content_str;  
		
		//document.getElementById("prj_core").innerHTML = project_core.build();
		//document.getElementById("prj_attachments").innerHTML = project_attachments.build();
		//document.getElementById("subprojects").innerHTML = subprojects.build();
		//subprojects.set_adder("javascript:subproj_adder.show_adder("+db_id+");"); //###

		load_proj.load(this.load_callback, db_id); //db_id loaded from url $_GET
	}

	this.load_callback = function (obj) {


//alert("project: #1827"+obj.title);

		//GLOBAL SETTING
		is_owner = (obj.owner==user_id) && (signed_in>0);
		
		document.getElementById("prj_core").innerHTML = project_core.build();
		document.getElementById("prj_attachments").innerHTML = project_attachments.build();
		document.getElementById("subprojects").innerHTML = subprojects.build();
		subprojects.set_adder("javascript:subproj_adder.show_adder("+db_id+");"); //###

		//if(is_owner) {
			//alert(is_owner);
			//document.getElementById("core_edit").style.visible="visible";
			//document.getElementById("attachments_add").style.visible="visible";
			//document.getElementById("subproject_add").style.visible="visible";
		//}
	
		content_layout.title = obj.title;
		content_layout.summary = obj.summary;

		content_layout.add_project_title ("", obj.title, obj.summary);
		//content_layout.add_index(obj.title, INDEX_COLORS[0], "");

		for (var i=1; i<proj_objs.length; i++) {
			
			//vertical subprojects index
			content_layout.add_index(proj_objs[i].title, INDEX_COLORS[i], "javascript:content_layout.set_core("+i+")");

			//subprojects cells
			var index_div = document.createElement("div");


			var action = null;
			index_div.setAttribute("class", "cell");
			index_div.setAttribute("style", "background-color:"+INDEX_COLORS[i]);
			index_div.setAttribute("title", "Load subproject \""+proj_objs[i].title+"\" ("+proj_objs[i].db_id+")");
			index_div.setAttribute("onClick", "javascript:content_layout.set_core("+i+")");
			
/*			var summary = proj_objs[i].summary;
			summary = summary.length>200?summary.substr(0, 100)+"...":summary;
			index_div.innerHTML = "<b>"+proj_objs[i].title+"</b><br>"+summary;
			document.getElementById("subproject_body").appendChild(index_div);
*/



			var title_header = document.createElement("div");
			title_header.setAttribute("class", "title_header");
			title_header.innerHTML = proj_objs[i].title;
			index_div.appendChild(title_header);
			
			var summary_body = document.createElement("div");
			summary_body.setAttribute("class", "summary_body");

			var summary = proj_objs[i].summary;
			summary = summary.replace(/`\d+/g,"");
			summary = summary.length>40?summary.substr(0, 36)+"...":summary;
			summary = summary.length<2?"":summary;

			summary_body.innerHTML = summary;
			index_div.appendChild(summary_body);

			document.getElementById("subproject_body").appendChild(index_div);
		}

		document.getElementById("subproject_body").appendChild(document.createElement("p"));
		content_layout.set_core(subproj_id); //subproj_id is globally defined on load
		load_attachments.load(project_attachments.callback, host_proj_obj.db_id);
	}

	this.set_core = function (index) {


		if(index<1) index = 1; //make sure to select first sub-project

		document.cookie = "subproj_id="+index+"; path=/";
		subproj_id = index;

		host_proj_obj = proj_objs[subproj_id];
//alert(host_proj_obj); //all subproj must be made public to be shared!
//alert(host_proj_obj.db_id);
	
		//content_layout.add_header("Greetings!");

		project_core.set_title(host_proj_obj.title);//proj_objs[index].title
		project_core.set_content(host_proj_obj.summary.summary_proc()+"<p/><a href='http://xeeku.com/slides/"+host_proj_obj.db_id+"' target='#'>View Slides</a>");
		project_core.set_ts(host_proj_obj.timestamp);
				
		load_attachments.load(project_attachments.callback, host_proj_obj.db_id);
		project_attachments.set_adder("javascript:attachment_adder.show_adder("+host_proj_obj.db_id+");");

	}

	this.add_index = function (label, color, action) {
		
		var index_span = document.createElement("span");

		index_span.innerHTML = label;
		index_span.setAttribute("class", "item");
		index_span.setAttribute("style", "background-color:"+color);
		index_span.setAttribute("onClick", action);
		index_span.setAttribute("title", action);

		document.getElementById("project_index").appendChild(index_span);
	}

	/*
	this.add_header = function (header) {
		document.getElementById("header").innerHTML = header;
	}
	*/

	this.add_project_title = function (host_proj, proj, summary) {

		var str = "";
		//str += "<div class=heads >";
		//str += "<h4>"+host_proj+"</h4>";
		str += "<h8>"+proj+"</h8>";
		//str += "<div style='float:right;'>"+summary+"</div>";
		//str += "</div>";
		//str += "<p/>";

		document.getElementById("prj_title").innerHTML = str;
	}

	/*
	this.add_project_core = function (core) {
		document.getElementById("prj_core").innerHTML = core;
	}

	this.add_project_attachments = function (attachments) {
		document.getElementById("prj_attachments").innerHTML = attachments;
	}
	*/
}

var project_core = new project_core();
function project_core () {

	this.THUMB_COUNT	= 20;
	this.introLoad		= true;
	
	this.build = function () {

		var content_str = "";
		content_str += "<div style='border-bottom:1px solid gray;width:100%'>";
		
		if(is_owner)// & host_proj_obj.db_id>0) //if(signed_in>0)
			content_str += "<a id=core_edit style='float:right;margin-top:20px;' href='javascript:project_core.load_editor();'>Edit Title</a>";
		content_str += "<h1 id=core_title style=''/>";
		content_str += "</div>";
		content_str += "<div id=core_ts style='font-style:italic;font-size:12px;'></div>";

		content_str += "<p/>";

		content_str += "<div id=core_content></div>";

		content_str += "<div class=rounded_bg id='core_editor_form' style='color:#666666;display:none'>";

		content_str += "<form action='' name='core_editor_form' ";
		content_str += "onReset='javascript:project_core.hide_editor();return false;' ";
		content_str += "onSubmit='javascript:project_core.save_editor();return false;'>";

		content_str += "<input type=reset class=submit_button style='float:right;' title='Cancel edit.' value='Cancel'>";
		content_str += "<input type=submit class=submit_button style='float:right;' title='Save edit.' value='Save'>";

		content_str += "<h4 >Title (up to 20 characters): </h4>";
		content_str += "<input name='title' type='text' class=entry_input maxlength=20 style='width:240px;'/>";
		
		content_str += "<p/>";
		
		content_str += "<h4 >Summary: </h4>";
		content_str += "<textarea name='summary' type='text' class=entry_textarea  rows=16></textarea>";
		content_str += "</form>";
		content_str += "</div>";

		return (content_str);
	}

	this.load_editor = function () {

		if (document.getElementById("core_editor_form").style.display == "block")
		{
			this.hide_editor();
			return;
		}
		document.getElementById("core_editor_form").style.display = "block";
		document.getElementById("core_content").style.display = "none";

		//var proj_obj = proj_objs[subproj_id];
		document.core_editor_form.title.value = host_proj_obj.title;
		document.core_editor_form.summary.value = host_proj_obj.summary;
	}

	this.hide_editor = function () {
		document.getElementById("core_editor_form").style.display = "none";
		document.getElementById("core_content").style.display = "block";
		document.core_editor_form.reset();
	}
	
	this.save_editor = function () {
		
		this.hide_editor();

		//var db_id = proj_objs[subproj_id].db_id;
		var db_id = host_proj_obj.db_id;

		var title = document.core_editor_form.title.value;
		var summary = document.core_editor_form.summary.value;

		edit_proj.edit(this.edit_callback, db_id, title, summary);
		//alert("save editor");
	}
	
	this.edit_callback = function (message) {
		
		//alert(message);
		history.go(0);

	}

	this.set_title = function (title) {
		document.getElementById("core_title").innerHTML = unescape(title);
	}
	
	this.set_ts = function (timestamp) {
		document.getElementById("core_ts").innerHTML = timestamp;
	}


	this.set_edit = function (edit) {
		document.getElementById("core_edit").title = edit;
		document.getElementById("core_edit").href = edit;
	}

	this.set_content = function (content) {

		content = content.replace(/\n{2,}/g,"<br/><br/>");
		content = content.replace(/\n+/g,"<br/>");
		
		//content = content.replace(/`(\d+)/g, this.insert_gen("$1"));

		document.getElementById("core_content").innerHTML = content;
	}

/*
	this.insert_gen = function (id) {


		//id = stringOf(id);
		//alert (id.replace(/(\d+)/, "$1"));

		//var db_id = id+" ";
		//alert(db_id);
		return "============>"+id+"<===";

		var attachment_obj = project_attachments.attachment_objs_hash[parseInt(db_id)];
		return "==>"+attachment_obj.title+"<==";
	}

	this.strProc = function (inp) {
		return "--"+inp;
	}
*/
}

var project_attachments = new project_attachments();
function project_attachments () {

	this.attachment_objs = null;
	this.attachment_objs_hash = null;
	this.host_id = 0;
	
	this.build = function () {

		//this.host_id = host_id;

		var content_str = "";
		content_str += "<div style='border-bottom:1px solid gray;width:100%'>";
		if(is_owner) 
			content_str += "<a id=attachments_add style='float:right;'>Add Attachment</a>";
		content_str += "<h3>Attachments</h3>";
		content_str += "</div>";

		content_str += "<p/>";
		content_str += "<div id=attachments_body class=attachments_body></div>";

		return (content_str);
	}
	
	this.set_adder = function (adder) {
		document.getElementById("attachments_add").title = adder;
		document.getElementById("attachments_add").href = adder;
	}

	this.load_body = function (body) {
		document.getElementById("attachments_body").innerHTML = body;
	}

	this.show = function (attachment_obj) {
		alert(attachment_obj.title);
	}


	this.load = function(db_id) {

		alert(db_id);
	}

	this.load_viewer  = function (host_id, index) {
		this.host_id = host_id;
		file_viewer.show(host_id, index, this.attachment_objs);
	}

	this.thumb_stacks = function (thumb_links, attachment_div) {	
	
		//thumb_links = thumb_links.replace(/^,?/,"");
		thumb_links = thumb_links.replace(/,?$/,"");

		if (thumb_links.length>4) {

			var folder_thumbs = shuffle(thumb_links.split(/,/));
			for(var thumb_index=0; thumb_index < Math.min(3, folder_thumbs.length); thumb_index++) {

				var folder_thumb = folder_thumbs[thumb_index];
				if (folder_thumb.match(/(\.jpg$)|(\.png$)/)) {
					
					//if(folder_thumb.match(/\.pdf$/)) folder_thumb = "./graphics/pdf_80.png";
					//folder_thumb = folder_thumb.replace(/xeeku\.com\/photos\//, "xeeku.com/photos/98x98/");

					var photo = document.createElement("img");
					photo.setAttribute("src", folder_thumb);
					photo.setAttribute("class", "folder_thumb");

					//alert(folder_thumb);
					
					//var rotate = style_rotate(10);
					//var offset = ";top:"+parseInt((Math.random()-0.5)*20+12);
					//offset += ";left:"+parseInt((Math.random()-0.5)*20+10);
					//var style = rotate+offset;
					
					var rotate,off_x, off_y;
					switch(thumb_index) {

						//case Math.min(3, folder_thumbs.length): rotate = 0; break;

						case 0: rotate = parseInt((Math.random()-0.5)*24+12);	off_x=14; off_y=10; break;
						case 1: rotate = parseInt((Math.random()-0.5)*24+12);;	off_x=8; off_y=20; break;
						case 2: rotate = parseInt((Math.random()-0.5)*24+12);	off_x=10; off_y=12; break;
						case 3: rotate = parseInt((Math.random()-0.5)*24+12);	off_x=14; off_y=18; break;
						case 4: rotate = parseInt((Math.random()-0.5)*24+12);	off_x=6; off_y=20; break;
						default:rotate = parseInt((Math.random()-0.5)*24+12);	off_x=5; off_y=52; break;
					}
					
					var style = "";
					if(folder_thumbs.length>1) {
						style += "-webkit-transform: rotate("+rotate+"deg);";
						style += "-moz-transform: rotate("+rotate+"deg);";
					}
					
					style += "left:"+off_x+";";
					style += "top:"+off_y+";";
					
					photo.setAttribute("style", style);
					attachment_div.appendChild(photo);
				}
			}
		}
	}


	this.callback = function (host_id, attachment_objs) {

		project_attachments.host_id = host_id;
		project_attachments.attachment_objs_hash = new Array();
		
		project_attachments.attachment_objs = attachment_objs;
		document.getElementById("attachments_body").innerHTML = "";



		for (var i=0; i<attachment_objs.length; i++) {

			var attachment_obj = attachment_objs[i];
			attachment_obj.title = attachment_obj.title.length>32?attachment_obj.title.substr(0, 28)+"...":attachment_obj.title;
			attachment_obj.index = i;

			project_attachments.attachment_objs_hash[attachment_obj.db_id] = attachment_obj;

//alert(project_attachments.attachment_objs_hash[6906].title);

			//alert(attachment_obj.db_id);
			//if(i==0) alert(attachment_obj.db_id);
			//if(i==0) alert(project_attachments.attachment_objs_hash[parseInt("6878")].title);//attachment_obj.db_id

			var attachment_div = document.createElement("div");
					
			var title_header = document.createElement("div");
			title_header.setAttribute("class", "title_header");
			var title=attachment_objs[i].title;
			title = title.length>32?title.substr(0,28)+"...":title;
			title_header.innerHTML = title;

			attachment_div.appendChild(title_header);

		var is_google_drawing = unescape(attachment_obj.link).match(/http[s]?:\/\/docs\.google\.com.+/);//unescape().
		if (is_google_drawing) {

			var WIDTH=120;
			var HEIGHT=120;
			//http://docs.google.com/drawings/pub?id=1O0PpK0lRa7NB7fY1VplC_vln8KTNOZzLqWJxIIb6GwM&w=960&h=720
			var google_doc_key = unescape(attachment_obj.link).replace(/http[s]?:\/\/docs\.google\.com.+?=(\S+?)((I=en)|&).+/,"$1");
			var photo_link = "http://docs.google.com/drawings/pub?id="+google_doc_key+"&w="+WIDTH+"&h="+HEIGHT;//this.proj_objs[slide_index].url);		

			var photo = document.createElement("img");
			photo.setAttribute("src", photo_link);
			photo.setAttribute("style", "max-width:98;max-height:98");

			attachment_div.setAttribute("class", "cell");
			attachment_div.setAttribute("onClick", "javascript:project_attachments.load_viewer("+host_id+", "+i+");");
			attachment_div.setAttribute("title", "Attachment ID: "+attachment_objs[i].db_id);
			attachment_div.appendChild(photo);
					
		} else if (attachment_obj.type=="folder" || attachment_obj.type=="photo") {

				var thumb_links=null;
				switch (true) {
					case attachment_obj.type=="folder":
						thumb_links = attachment_obj.video_keys;
						attachment_div.setAttribute("class", "folder");
						break;

					case attachment_obj.type=="photo":
						thumb_links = attachment_obj.filenames;
						attachment_div.setAttribute("class", "photo");
						break;
				}
				
				//attachment_div.setAttribute("style", style_rotate(9));//"-webkit-transform: rotate(9deg)");
				attachment_div.setAttribute("onClick", "javascript:project_attachments.load_viewer("+host_id+", "+i+");");
				attachment_div.setAttribute("title", "Attachment ID: "+attachment_objs[i].db_id);

				/*
				var title_header = document.createElement("div");
				title_header.setAttribute("class", "title_header");
				title_header.setAttribute("style", "position:absolute;bottom:4px;right:4px;text-align:right;color:#333;margin-left:4px;");
				title_header.innerHTML = attachment_objs[i].title;
				attachment_div.appendChild(title_header);
				*/

				project_attachments.thumb_stacks(thumb_links, attachment_div);	

				/*
				if (thumb_links.length>4) {
	
					var folder_thumbs = shuffle(thumb_links.split(/,/));
					for(var thumb_index=0; thumb_index < Math.min(3, folder_thumbs.length); thumb_index++) {

						var folder_thumb = folder_thumbs[thumb_index];
						if (folder_thumb.match(/(\.jpg$)|(\.png$)/)) {
							
							//alert(folder_thumb);

							var photo = document.createElement("img");
							photo.setAttribute("src", folder_thumb);
							photo.setAttribute("class", "folder_thumb");
							
							//var rotate = style_rotate(10);
							//var offset = ";top:"+parseInt((Math.random()-0.5)*20+12);
							//offset += ";left:"+parseInt((Math.random()-0.5)*20+10);
							//var style = rotate+offset;
							
							var rotate,off_x, off_y;
							switch(thumb_index) {
								case 0: rotate = -9;	off_x=8; off_y=10; break;
								case 1: rotate = 4;		off_x=10; off_y=20; break;
								case 2: rotate = 12;	off_x=4; off_y=12; break;
								case 3: rotate = -15;	off_x=0; off_y=18; break;
								case 4: rotate = 14;	off_x=3; off_y=20; break;
								default:rotate = -8;	off_x=9; off_y=52; break;
							}
							
							var style = "-webkit-transform: rotate("+rotate+"deg);";
							style += "-moz-transform: rotate("+rotate+"deg);";
							style += "transform: rotate("+rotate+"deg);";
							
							style += "right:"+off_x+";";
							style += "top:"+off_y+";";
							
							photo.setAttribute("style", style);
							attachment_div.appendChild(photo);
						}
					}
				}
				*/

				if(attachment_obj.type=="folder") {
					var photo = document.createElement("img");
					photo.setAttribute("src", "../graphics/folder_edge.png");
					photo.setAttribute("style", "position:absolute;top:0;left:0;");
					attachment_div.appendChild(photo);
				}


			} else {//non-folder and non-photo types
				
				attachment_div.setAttribute("class", "cell");
				//attachment_div.setAttribute("style", style_rotate(9));//"-webkit-transform: rotate(9deg)");
				attachment_div.setAttribute("onClick", "javascript:project_attachments.load_viewer("+host_id+", "+i+");");
				attachment_div.setAttribute("title", "Attachment ID: "+attachment_objs[i].db_id);
			
				/*
				if (attachment_obj.type=="photo") {

					var photo_link = attachment_obj.link.replace(/photos\//, "photos/98x98/");


					var photo = document.createElement("img");
					photo.setAttribute("src", photo_link);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);
						
					//var title_header = document.createElement("div");
					//title_header.setAttribute("class", "title_header");
					//title_header.innerHTML = attachment_objs[i].title;
					//attachment_div.appendChild(title_header);

				} else */if (attachment_obj.type=="youtube") {

					var photo_link = "http://img.youtube.com/vi/"+attachment_obj.youtube_key+"/default.jpg";

					var photo = document.createElement("img");
					photo.setAttribute("src", photo_link);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);
						
					//var title_header = document.createElement("div");
					//title_header.setAttribute("class", "title_header");
					//title_header.innerHTML = attachment_objs[i].title;

					//attachment_div.appendChild(title_header);


				} else if (attachment_obj.type=="bookmark") {

					//var photo_link = "http://images.websnapr.com/?size=s&url="+attachment_obj.link;
					
					var thumb_link = null;
					var filename = attachment_obj.filenames.split(/,/)[0];
					
					if (filename.match(/(\.jpg$)|(\.jpeg$)|(\.png$)/i)) {
							thumb_link = filename;
					
					} else {
							thumb_link= "http://images.websnapr.com/?size=s&url="+attachment_obj.link;
					}

					var photo = document.createElement("img");
					photo.setAttribute("src", thumb_link);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);
						
					//var title_header = document.createElement("div");
					//title_header.setAttribute("class", "title_header");
					//title_header.innerHTML = attachment_objs[i].title;

					//attachment_div.appendChild(title_header);



				//} else if (attachment_obj.type=="note" && attachment_obj.filenames.length>4) {
				} else if (attachment_obj.filenames.length>4) {

					var photo_link = attachment_obj.filenames.split(/,/)[0];//attachment_obj.link.replace(/photos\//, "photos/98x98/");
					if(photo_link.match(/\.pdf$/)) photo_link = "./graphics/pdf_80.png";


					var photo = document.createElement("img");
					photo.setAttribute("src", photo_link);
					photo.setAttribute("style", "max-width:98;max-height:98");
					attachment_div.appendChild(photo);

					//var title_header = document.createElement("div");
					//title_header.setAttribute("class", "title_header");
					//title_header.innerHTML = attachment_objs[i].title;
					//attachment_div.appendChild(title_header);
					
				} else {

					//var title_header = document.createElement("div");
					//title_header.setAttribute("class", "title_header");
					//title_header.innerHTML = attachment_objs[i].title;
					//attachment_div.appendChild(title_header);
					
					var summary_body = document.createElement("div");
					summary_body.setAttribute("class", "summary_body");

					var summary = attachment_objs[i].summary;
					summary = summary.length>200?summary.substr(0, 50)+"...":summary;
					summary_body.innerHTML = summary;
					attachment_div.appendChild(summary_body);
				}
				
			}
			
			document.getElementById("attachments_body").appendChild(attachment_div);
		}

		document.getElementById("attachments_body").appendChild(document.createElement("p"));
		project_attachments.insert_attachment();
	
	}

	this.insert_attachment = function () {
		var content = document.getElementById("core_content").innerHTML;
		
		//content = content.replace(/`(\d+)/g, project_core.insert_gen("$1"));
		var keys = content.replace(/.*?`(\d+)+/g, "$1``").split(/``/);


		var set_left = "";
		for (var i in keys) {

			if (keys[i].match(/^\d+$/)) {

				var key = parseInt(keys[i]);
				var attach_obj = project_attachments.attachment_objs_hash[key];

				if (attach_obj==undefined) continue;

				if(set_left.length>5) set_left = "";
				else set_left = "style='float:left;margin:4 12 2 0;'";

				var page_num = parseInt(i)+1;
				var obj_str = "<div class='attachment_insert' "+set_left+" onClick='javascript:project_attachments.load_viewer("+this.host_id+","+attach_obj.index+");'>";

				switch(true) {
					
					case attach_obj.type=="photo":
						obj_str += "<img src='"+attach_obj.link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
						break;

					case attach_obj.type=="youtube":
						var youtube_thumb_link = "http://img.youtube.com/vi/"+attach_obj.youtube_key+"/0.jpg";
						obj_str += "<img src='"+youtube_thumb_link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
						break;

					case attach_obj.type=="bookmark":
						
						var thumb_link = null;
						if(attach_obj.filenames.length>4&&attach_obj.filenames.match(/(\.jpg$)|(\.jpeg)|(\.png)/i)) 
							thumb_link = attach_obj.filenames.split(/,/)[0];
						else thumb_link = "http://images.websnapr.com/?size=s&url="+attach_obj.link;

						obj_str += "<img src='"+thumb_link+"' style='width:202;max-height:160;'>";
						break;
					
					case attach_obj.type=="document":
						obj_str += "<img src='./graphics/pdf_80.png' style=''>";//e='max-height:80px;float:right'>";
						break;
				}		
			
				//obj_str += "<img src='"+attach_obj.link+"' style='max-width:240;max-height:160;'>";//e='max-height:80px;float:right'>";
				obj_str += "<div style='font-size:11px;'>Figure "+page_num+". "+attach_obj.title+"</div>";
				obj_str += "</div>";

				var reg_exp = new RegExp("`"+keys[i], "");
				content = content.replace(reg_exp, obj_str);
			}
		}

		document.getElementById("core_content").innerHTML = content+"<p/>";
	}
}

//http://snippets.dzone.com/posts/show/849
shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function style_rotate(degree) {

	var rotation = parseInt((Math.random()-0.5)*degree*2);
	var style = "-webkit-transform: rotate("+rotation+"deg);";
	style += "-moz-transform: rotate("+rotation+"deg);";
	style += "transform: rotate("+rotation+"deg);";
	return style;

}


var subprojects = new subprojects();
function subprojects () {
	
	this.build = function () {

		var content_str = "";
		content_str += "<div style='border-bottom:1px solid gray;width:100%'>";
		content_str += "<a id=subproject_add style='float:right'>Create Binder</a>";//if(signed_in>0) 
		content_str += "<h2>Binders</h2>";
		content_str += "</div>";

		content_str += "<p/>";
		content_str += "<div id=subproject_body class=body></div>";

		return (content_str);
	}
	
	this.set_adder = function (adder) {
		
		if(signed_in>0) {
			document.getElementById("subproject_add").title = adder;
			document.getElementById("subproject_add").href = adder;

		} else document.getElementById("subproject_add").style.visibility = "hidden";
	}

	//this.load_body = function (body) {
		//document.getElementById("subproject_body").innerHTML = body;
	//}
}


	



	function loadCore(title) {

		loadContent.load(coreCallback, title);//.replace(/\s+/g,""));
		document.getElementById("content").innerHTML = "Loading content under \""+title+"\"...";  

	}

	function coreCallback (photoObjs, keyword) {
		
		if (photoObjs.length==0) return;

		photoObjects = photoObjs;
		//var indexSel = Math.floor(Math.random()*(Math.min(photoObjs.length, 19)-1));
		
		//document.getElementById("content_panel").innerHTML = content_cell.load (photoObjs[0]);   
		//checkStar.load(content_cell.checkStarCallback, photoObjects[0].link);

		var summary_str = "<h3 style='border-bottom:1px solid gray'>Bookmarks</h3>";
		summary_str += "<div style='font-size:12px'>Featured Bookmarks Collection under <b>\""+keyword+"\"</b></div>";

		for (var i = 0; i < photoObjs.length; i++) {
			photoObjs[i].index = i;
			summary_str += summary_cell.load (photoObjs[i], summaryCallback);
		}
		
		document.getElementById("content").innerHTML = summary_str; //summary_cells_str[0];   
	}

	function summaryCallback(index) {

		showOverlay();
		document.getElementById("overlay_content").innerHTML = content_cell.load (photoObjects[index]);  
		//checkStar.load(content_cell.checkStarCallback, photoObjects[index].link);

		//loadUsers.load(usersCallback, photoObjects[index].db_id);
	}

