/*
  FOR Photo's Edit(title/desc...)
*/





var undefined;

var global_photos = {};
var global_sets = {};

var global_reg_photo_print_ids = [1,2,3];
var global_dig_photo_print_ids = [29,30,31];
var global_huge_photo_print_ids = [18];
var global_square_photo_print_ids = [23];
var global_wallet_photo_print_ids = [32];
//var global_dig_photo_print_ids = [29,30,31,32];


var writeDebug = function() {}
var writeAPIDebug = function() {}

var _trim = function(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}



// OK CHECKS

var _ok_for_swf = function() {
	if (actualVersion<6) return false;
	return true;
	var ua = navigator.userAgent;
	if (navigator.appVersion.toLowerCase().indexOf("mac") > 0) { // mac
		if (ua.indexOf('Gecko') == -1) return false;
	} else { // windows or unix
		if (ua.indexOf("MSIE 5.6") > 0 || ua.indexOf("MSIE 5.5") > 0 || ua.indexOf("MSIE 6") > 0) {
			// IE 5.5+
		} else {
			if (ua.indexOf('Gecko') == -1) return false;
		}
	}
	return true;
}

var _ok_for_scrumjax_xml = function() {
	if (global_fakeOperaXMLHttpRequestSupport) return false; // this is set in xmlhtttprequest.js
	return _ok_for_scrumjax();
}

var _ok_for_scrumjax = function() {
	if (!window.XMLHttpRequest) return false;
	if (navigator.appVersion.toLowerCase().indexOf("mac") > 0 && navigator.userAgent.indexOf('MSIE') > 0 && navigator.userAgent.indexOf('Opera') == -1) return false;
	return true;
}

// Date stuff

var global_today = new Date();
var global_tm = global_today.getMonth()+1;
var global_td = global_today.getDate();
var global_ty = global_today.getFullYear();
var global_days_in_months = [31,0,31,30,31,30,31,31,30,31,30,31];
var global_mn = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];

var _get_month_index = function(m) {
	var cm = m.substr(0,3).toUpperCase();
	for (var i=0; i<global_mn.length; i++) {
		if (cm == global_mn[i]) return i;
	}
	return -1;
}


var _in_array = function(needle, haystack) {
	for (var i=haystack.length-1;i>-1;i--) {
		if (haystack[i] == needle) return true;
	}
	
	return false;
}

var _disable_select = function() {
	document.onselectstart = new Function ('return false');
}

var _enable_select = function() {
	document.onselectstart = new Function ('return true');
}

var _limit_textarea = function (textarea, max) {
	//textarea.value = textarea.value.replace('  ', ' ')
	if (textarea.value.length > max) textarea.value = textarea.value.substring(0, max);
}

var _el_is_in_a_link = function(el) {
	if (el.href) return 1;
	if (el.parentNode.href) return 1;
	if (el.parentNode.parentNode.href) return 1;
	if (el.parentNode.parentNode.parentNode.href) return 1;
	return 0;
}

var _get_event_src = function(e) { 
	if (e){ return e.target; }
	if (window.event){ return window.event.srcElement; }
	return null;
}

// _find_x & _find_y courtesy PPK
// http://www.quirksmode.org/js/findpos.html
var _find_x = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

var _find_y = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

var _decorate = function() {
	var el = arguments[0];
	if (!el) return;
	arguments[0] = el;
	if (window['decorate_'+el.className]) return window['decorate_'+el.className](arguments);
}

var _pretty_list = function(list) {
    if (list.length == 1) {
		return list[0];
    }  else if (list.length==2) {
		return list.join(" and ");
    } else {
		var last = list.pop();
		return list.join(", ") + " and " + last;
    }
}

var _hyperlink = function(txt) {
	if (txt.indexOf('http://') > -1) {
		var txtA = txt.split('\n');
		for (var y=0; y<txtA.length; y++) {
			var subtxtA = txtA[y].split(' ');
			for (var t=0; t<subtxtA.length; t++) {
				var openParen = '';
				if (subtxtA[t].substr(0,1) == '(') {
					// remove open Paren
					subtxtA[t] = subtxtA[t].replace('(', '')
					openParen = '(';
				}
				if (subtxtA[t].substr(0,7) == 'http://') {
					var url = subtxtA[t];
					var paren = '';
					if (url.substr(url.length-1, 1) == ')') {
						url = url.substr(0,url.length-1);
						paren = ')';
					}
					var period = '';
					if (url.substr(url.length-1, 1) == '.') {
						url = url.substr(0,url.length-1);
						period = '.';
					}
					subtxtA[t] = '<a href="'+url+'" onclick="">' + url + '<\/a>'+paren+period;
					
				}
				// add open Paren back in
				subtxtA[t] = openParen+subtxtA[t]
			}
			txtA[y] = subtxtA.join(' ');
			//txtA[y] = txtA[y].replace('> <', '>&nbsp;<');
		}
		txt = txtA.join('<br \/>');
		
	}

	return txt;
}

var global_js_tip_count = 0;
var _get_tip_html = function(txt, tip_class) {
	if (txt.length == 0) return '';
	global_js_tip_count++;
	tip_class = (tip_class == undefined) ? 'Tip' : tip_class;
	var tip_html = '<span style=\"display: none;\" id=\"js_tooltip_'+global_js_tip_count+'\">'+txt+'</span>'
		+ '<span class=\"'+tip_class+'\">[<a href=\"#\" onclick=\"this.blur(); show_tooltip(this, \'js_tooltip_'+global_js_tip_count+'\', '+txt.length+'); return false;\">?</a>]</span>'
	
	return tip_html; 
}


var _set_cookie = function(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = '; expires='+date.toGMTString();
	}
	document.cookie = name+'='+value+expires+'; path=/';
}

var _get_cookie = function(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

var _delete_cookie = function(name) {
	_set_cookie(name, '', -1);
}


var _rebuild_tags_for_photos = function(photo_id, tags) {
	if (global_photos[photo_id] == undefined) return;
	global_photos[photo_id].tagsA = [];
	global_photos[photo_id].tags_rawA = [];
	global_photos[photo_id].tags_idA = [];
	global_photos[photo_id].tags_canDeleteA = [];
	for (var t=0;t<tags.length;t++) {
		var node = tags[t];
		
		var tag = node.firstChild.nodeValue; // this is clean
		var tag_raw = node.getAttribute('raw');
		var tagId = node.getAttribute('id');
		var adderId = node.getAttribute('author');
		var canDelete = (global_photos[photo_id].isOwner || global_nsid == adderId) ? true : false;
		
		global_photos[photo_id].tagsA.push(tag);
		global_photos[photo_id].tags_rawA.push(tag_raw);
		global_photos[photo_id].tags_idA.push(tagId);
		global_photos[photo_id].tags_canDeleteA.push(canDelete);
	}
}

var global_single_shadow_div;
var _make_single_shadow = function(div, height_plus, width_plus, x_plus, y_plus) {
	if (div == undefined) {
		div = document.createElement('div');
		div.style.position = 'absolute';
		div.style.top = '0px';
		div.style.left = '0px';
		document.body.appendChild(div);
	}
	
	// these defaults are for photo.js
	window.global_single_shadow_height_plus = (height_plus==undefined) ? (12-22) : height_plus;
	window.global_single_shadow_width_plus = (width_plus==undefined) ? (12-22) : width_plus;
	window.global_single_shadow_x_plus = (x_plus==undefined) ? (-5) : x_plus;
	window.global_single_shadow_y_plus = (y_plus==undefined) ? (-5) : y_plus;
	
	var html = '';
	//html+= '<img src="/images/drop_box.png" style="border:0">';
	html+= '<table id="shadow_table" cellpadding="0" cellspacing="0" border="0" style="padding:0px;">';
	html+= '<tr><td width="11"><img class="trans_png" width="11" height="11" src="/images/tc_white_shadow_tl.png"><\/td><td id="shadow_width_controller"><img class="trans_png" width="100%" height="11" src="/images/tc_white_shadow_t.png"><\/td><td width="11"><img class="trans_png" width="11" height="11" src="/images/tc_white_shadow_tr.png"><\/td><\/tr>';
	html+= '<tr><td height="30" id="shadow_height_controller"><img class="trans_png" width="11" height="100%" src="/images/tc_white_shadow_l.png"><\/td><td><\/td><td><img class="trans_png" width="11" height="100%" src="/images/tc_white_shadow_r.png" /><\/td><\/tr>';
	html+= '<tr><td><img class="trans_png" width="11" height="11" src="/images/tc_white_shadow_bl.png"><\/td><td><img class="trans_png" width="100%" height="11" src="/images/tc_white_shadow_b.png"><\/td><td><img class="trans_png" width="11" height="11" src="/images/tc_white_shadow_br.png"><\/td><\/tr><\/table>';
	div.innerHTML = html;
	div.style.zIndex = '999';
	div.style.display = 'none';
	div.id = 'shadow_div';
	
	window._set_single_shadow_x = function(x_of_shadowed_item) {
		div.style.marginLeft = (x_of_shadowed_item+global_single_shadow_x_plus)+'px';
	}
	
	window._set_single_shadow_y = function(y_of_shadowed_item) {
		div.style.marginTop = (y_of_shadowed_item+global_single_shadow_y_plus)+'px';
	}
	
	window._set_single_shadow_height = function(shadowed_item) {
		var el = _ge('shadow_height_controller');
		if(! el) return;
		var d = ((shadowed_item.offsetHeight)+global_single_shadow_height_plus);
		if (d < 0) d = 0;
		el.style.height = d +'px';
	}
	
	window._set_single_shadow_width = function(shadowed_item) {
		var el = _ge('shadow_width_controller');
		if(! el) return;
		var d = ((shadowed_item.offsetWidth)+global_single_shadow_width_plus);
		if (d < 0) d = 0;
		el.style.width = d +'px';
	}
	
	
	window.global_single_shadow_div = div;
}

var global_comm_div;
var _make_comm_div = function(zIndex) {
	var div = _ge('comm_div');
	if (!div) {
		div = document.createElement('DIV');
		div.id = 'comm_div';
		div.style.left = 0;
		div.style.top = 0;
		document.body.appendChild(div);
	}
	var html = '';
	html+= '<table id="comm_table" cellpadding="0" cellspacing="0" border="0" style="width:200px; padding:0px;">';
	html+= '<tr><td width="3"><img width="3" height="3" src="/images/tc_white_tl.gif"><\/td><td class="td_note_white"><\/td><td class="td_note_white"><\/td><td width="3"><img width="3" height="3" src="/images/tc_white_tr.gif"><\/td><\/tr>';
	html+= '<tr><td class="td_note_white"><\/td>';
	html+= '<td class="td_note_white" style="padding:3px 0px 0px 0px" width="1" valign="top"><img id="comm_pulser_img" src="/images/spinner.gif"'+''/*pulser2.gif" width="32" height="15"*/+' hspace="10" width="16" height="16" border="0"><\/td><td class="td_note_white" style="padding:3px; font-family:arial; font-size:12px;" id="comm_td">';
	html+= '<\/td><td class="td_note_white"><\/td><\/tr>';
	html+= '<tr id="comm_button_tr"><td class="td_note_white"><\/td>';
	html+= '<td class="td_note_white"><\/td><td class="td_note_white" style="padding:3px"><form>';
	html+= '<input id="comm_button_ok" type="button" class="Butt" value="OK" onclick="this.onclick_func();" style="margin:5px 5px 0 0;">';
	html+= '<input id="comm_button_cancel" type="button" class="CancelButt" value="CANCEL" onclick="this.onclick_func();" style="margin:5px 5px 0 0;">';
	html+= '</form><\/td><td class="td_note_white"><\/td><\/tr>';
	html+= '<tr><td><img width="3" height="3" src="/images/tc_white_bl.gif"><\/td><td class="td_note_white"><\/td><td class="td_note_white"><\/td><td><img width="3" height="3" src="/images/tc_white_br.gif"><\/td><\/tr><\/table>';
	div.innerHTML = html;

	div.button_tr = _ge('comm_button_tr');
	div.pulser_img = _ge('comm_pulser_img');

	div.button_ok = _ge('comm_button_ok');
	div.button_cancel = _ge('comm_button_cancel');
	
	div.button_cancel.onclick_func = div.button_ok.onclick_func = function() {
		div.stop_comming(); this.blur();
		if (typeof this.onclick_plus == 'function') this.onclick_plus();
		if (typeof this.onclick_do == 'function') this.onclick_do();
	}

	div.table = _ge('comm_table');
	div.td = _ge('comm_td');
	div.style.zIndex = zIndex;
	div.style.display = 'none';
	
	div.stop_comming = function() {
		this.style.display = 'none';
		global_single_shadow_div.style.display = 'none';
		this.comming = 0;
	}
	
	div.change_comming_msg = function(msg) {
		if (!msg) return;
		this.td.innerHTML = msg;
		_set_single_shadow_height(this.table);
		_set_single_shadow_width(this.table);
	}
	
	div.start_comming = function(msg, x_or_func, y_or_func, hide_anim, show_ok, ok_txt, ok_func, show_cancel, cancel_txt, cancel_func) {
		var txt = (!msg) ? 'working...' : msg;
		this.td.innerHTML = txt;
		
		this.pulser_img.style.display = (hide_anim) ? 'none' : 'inline';
		
		if (show_ok) {
			this.button_tr.style.display = (document.all && !window.opera) ? 'block' : 'table-row';
			this.button_ok.value = (ok_txt) ? ok_txt : 'OK';
			this.button_tr.style.display = (document.all && !window.opera) ? 'block' : 'table-row';
			this.button_ok.onclick_do = (typeof ok_func=='function') ? ok_func : null;
	
			this.button_cancel.value = (cancel_txt) ? cancel_txt : 'CANCEL';
			this.button_cancel.onclick_do = (typeof cancel_func=='function') ? cancel_func : null;
			this.button_cancel.style.visibility = (show_cancel) ? 'visible' : 'hidden';
		} else {
			this.button_tr.style.display = 'none';
		}
		
		this.style.display = 'block';
		
		if (show_ok && navigator.userAgent.indexOf("KHTML")==-1) this.button_ok.focus(); // crashes Safari! (when you click the focused button)
		
		if (window.innerWidth) {
			var ww = window.innerWidth;
			var wh = window.innerHeight;
			var bgX = window.pageXOffset;
			var bgY = window.pageYOffset;
		} else {
			var ww = document.body.clientWidth;
			var wh = document.body.clientHeight;
			var bgX = document.body.scrollLeft;
			var bgY = document.body.scrollTop;
		}
		
		var default_x_func = function() {
			var x = bgX + _pi((ww-_pi(global_comm_div.table.offsetWidth))/2);
			return x;
		}
		
		var default_y_func = function() {
			var y = bgY + _pi((wh-global_comm_div.table.offsetHeight)/2);
			return y;
		}
		
		var x = (typeof x_or_func == 'function') ? x_or_func() : (typeof x_or_func == 'number') ? x_or_func : default_x_func();
		var y = (typeof y_or_func == 'function') ? y_or_func() : (typeof y_or_func == 'number') ? y_or_func : default_y_func();
		
		this.style.marginLeft = x+'px';
		this.style.marginTop = y+'px';
		
		_set_single_shadow_x(x);
		_set_single_shadow_y(y);
		_set_single_shadow_height(this.table);
		_set_single_shadow_width(this.table);
		global_single_shadow_div.style.display = 'block';
		
		this.comming = 1;
	}
	
	window.global_comm_div = div;
	
	if (!global_single_shadow_div) _make_single_shadow(_ge('shadow_div'));
	
	return global_comm_div;
}









///////////////////////////////////////////////////
////////////////////////////////////////////////////
var global_photos = [];

var photos_meta_edit_url = '/photos/meta_edit_xml.ue';

//upload photos.

function PopupPhotoUploadWindow(url)
{	
	return Popup(window, url, 'PhotoUploadWindow_'+(new Date()).getTime(), 600, GetAvailScreenHeight(window)-40);
}



	
/*
var insitu_init_page_set_description_div = function(set_id) {
	if (!_ok_for_scrumjax()) return false; 
	var div = insitu_init_generic_description_div(set_id, global_sets);
	div.getInput = function() {
		return '<textarea name="content" style="font-family:arial; font-size:12px; padding:3px; margin-top:14px; width:100%; height:75px;border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;">'+this.form_content+'<\/textarea>';
	}
	return true;
}

var insitu_init_page_set_title_div = function(set_id) {
	if (!_ok_for_scrumjax()) return false; 
	var div = insitu_init_generic_title_div(set_id, global_sets);
	div.getInput = function() {
		return '<input name="content" value="'+QuoteEscape(this.form_content)+'" style="font-size:32px; font-family:arial; padding:3px; width:100%; border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;">';
	}
	div.getExtra = function() {
		return '<br>';
	}
	return true;
}
*/
// FOR PHOTO.GNE

var init_photo_edit_about_div = function(photo_id,hook_onload) {
try{
	var div = insitu_init_generic_description_div(photo_id, global_photos);
	div.getInput = function() {
		return '<textarea name="content" style="font-family:Verdana,arial; font-size:12px; padding:3px; margin-top:0px; width:100%; height:100px; border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;">'+HtmlEscape(this.form_content)+'<\/textarea>';
	}
	div.getExtra = function() {
		return '<br>';
	}
	div.hook_onload = hook_onload;
	return true;
}catch(e){}
}

///START HERE
var init_photo_edit_title_div = function(photo_id,hook_onload) {
try{
	var div = insitu_init_generic_title_div(photo_id, global_photos);
	div.getInput = function() {
		return '<input name="content" value="'+QuoteEscape(this.form_content)+'" style="font-size:22px; font-weight: bold; font-family: Arial; padding:4px; width:100%; border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;">';
	}
	div.getExtra = function() {
		return '<br>';
	}
	div.hook_onload = hook_onload;
	return true;
}catch(e){}
}

// FOR PHOTOS/USER PAGES
/*
var insitu_init_page_photos_user_description_div = function(photo_id) {
	if (!_ok_for_scrumjax()) return false; 
	var div = insitu_init_generic_description_div(photo_id, global_photos);
	div.getInput = function() {
		return '<textarea name="content" style="font-family:arial; font-size:12px; padding:3px; margin-top:0px; width:240px; height:75px;border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;">'+this.form_content+'<\/textarea>';
	}
	return true;
}

var insitu_init_page_photos_user_title_div = function(photo_id) {
	if (!_ok_for_scrumjax()) return false; 
	var div = insitu_init_generic_title_div(photo_id, global_photos);
	div.getInput = function() {
		return '<input name="content" value="'+this.form_content.replace('"', '&#34;')+'" style="font-size:14px; font-weight:bold; font-family:arial; padding:3px; width:240px; border:1px inset #e9e9ae; background-color:#ffffd3; margin-bottom:5px;" \/>';
	}
	return true;
}
*/

// FOR ALL PAGES

var insitu_init_generic_description_div = function(hash_id, hash) {

	var div = _ge('photo_about_div_'+hash_id);
	div.title = '点击鼠标进行编辑';
	div.hash_id = hash_id;
	if (!hash[div.hash_id]) {
		alert('ERROR: no hash ob exists for '+div.hash_id);
		return false;
	}
	div.form_content = hash[div.hash_id].description;
	div.emptyText = '<i>要添加图片说明吗?</i>&nbsp;'; // the nbsp is to ensure the correct lineheight in windows
	
	div.getExtra = function() {
		return '';
	}
	/*
	div.flickr_photos_getInfo_onLoad = div.flickr_photosets_getInfo_onLoad = function(success, responseXML) {
		if (success) {
			if (responseXML.documentElement.getElementsByTagName('description')[0].firstChild) {
				var description_raw = responseXML.documentElement.getElementsByTagName('description')[0].firstChild.nodeValue;
			} else {
				var description_raw = '';
			}
			hash[div.hash_id].description = div.form_content = description_raw;
			var new_description = (hash[div.hash_id].description=='') ? '&nbsp;' : hash[div.hash_id].description.chop().nl2br();
			if (new_description != div.innerHTML) div.innerHTML = new_description;
		} else {
			// just go ahead and use the exact text we entered
			div.form_content = hash[div.hash_id].description;
			div.innerHTML = (hash[div.hash_id].description=='') ? '&nbsp;' : hash[div.hash_id].description.chop().nl2br();
		}
	}*/

	div.photos_setMeta_onLoad = div.flickr_photosets_editMeta_onLoad = function(rsXML,argsObj) {
		var err = XmlHttpGetError(rsXML);
		if(err && err.code==0){
			//alert(err.desc);
				div.form_content = hash[div.hash_id].description;
				div.innerHTML = (hash[div.hash_id].description=='') ? '&nbsp;' : htmlencode(err.desc);//htmlencode(hash[div.hash_id].description);
		
				if(div.hook_onload){
					div.hook_onload(hash[div.hash_id].description);
				}
		} else {
			hash[div.hash_id].description = div.form_content;
			div.innerHTML = (hash[div.hash_id].description=='') ? '&nbsp;' : htmlencode(hash[div.hash_id].description);
			alert('Error: your description was NOT saved.');
		}
	}

	
	div.saveChanges = function(form) {
		//deja_view_uh_huh();
		
		hash[this.hash_id].description = form.content.value;
		this.innerHTML = '<i>保存中...</i>&nbsp;'; // the nbsp is to ensure the correct lineheight in windows
		this.endEditing();

		if (!hash[this.hash_id]) {
			this.innerHTML = 'ERROR: no hash ob exists for '+this.hash_id;
			return false;
		}
		/*if (hash == global_sets) {
			flickrAPI.callMethod('flickr.photosets.editMeta', {photoset_id:this.hash_id, title:hash[this.hash_id].title, description:hash[this.hash_id].description}, div);
		*/if (hash == global_photos)  {
			//alert('To be done.');
			var data= 'action=editabout'
					+'&id='+this.hash_id
					+'&about='+UrlEncode(hash[this.hash_id].description)
					+'';
			SendReq4XML(photos_meta_edit_url,data,div.photos_setMeta_onLoad,{});
		} else {
			alert('unknown hash')
		}
	}
	if (window.deja_view_should_refresh) {
		div.innerHTML = '...';
	}
	return insitu_init_editable_div(div);

}

var insitu_init_generic_title_div = function(hash_id, hash) {
	var div = _ge('photo_title_div_'+hash_id);
	div.title = '点击鼠标进行编辑';
	div.hash_id = hash_id;
	if (!hash[div.hash_id]) {
		alert('ERROR: no hash ob exists for '+div.hash_id);
		return false;
	}
	div.form_content = hash[div.hash_id].title;
	div.emptyText = '<i>要添加一个标题吗?</i>&nbsp;'; // the nbsp is to ensure the correct lineheight in windows
	
	div.getExtra = function() {
		return '';
	}
	/*
	div.flickr_photos_getInfo_onLoad = div.flickr_photosets_getInfo_onLoad = function(success, responseXML) {
		if (success) {
			if (responseXML.documentElement.getElementsByTagName('title')[0].firstChild) {
				var title_raw = responseXML.documentElement.getElementsByTagName('title')[0].firstChild.nodeValue;
			} else {
				var title_raw = '';
			}
			hash[div.hash_id].title = div.form_content = title_raw;
			var new_title = (hash[div.hash_id].title=='') ? '&nbsp;' : hash[div.hash_id].title.escapeForDisplay();
			if (new_title != div.innerHTML) div.innerHTML = new_title;
		} else {
			// just go ahead and use the exact text we entered
			div.form_content = hash[div.hash_id].title;
			div.innerHTML = (hash[div.hash_id].title=='') ? '&nbsp;' : hash[div.hash_id].title.escapeForDisplay();
		}
	}
	*/
	div.photos_setMeta_onLoad = div.photosets_editMeta_onLoad = function(rsXML, argsObj) {
		var err = XmlHttpGetError(rsXML);
		if(err && err.code==0){
			//alert(err.desc);
				div.form_content = hash[div.hash_id].title;
				div.innerHTML = (hash[div.hash_id].title=='') ? '&nbsp;' : HtmlEscape(hash[div.hash_id].title);
				
				if(div.hook_onload){
					div.hook_onload(hash[div.hash_id].title);
				}
		} else {
			hash[div.hash_id].title = div.form_content;
			div.innerHTML = (hash[div.hash_id].title=='') ? '&nbsp;' : HtmlEscape(hash[div.hash_id].title);
			alert('Error: your title was NOT saved.'+ (err?err.desc:""));
		}
	}

	div.saveChanges = function(form) {
		//deja_view_uh_huh();
		
		hash[this.hash_id].title = form.content.value;
		this.innerHTML = '<i>保存中...</i>&nbsp;'; // the nbsp is to ensure the correct lineheight in windows
		this.endEditing();

		if (!hash[this.hash_id]) {
			this.innerHTML = 'ERROR: no hash ob exists for '+this.hash_id;
			return false;
		}
		
		/*if (hash == global_sets) {
			flickrAPI.callMethod('flickr.photosets.editMeta', {photoset_id:this.hash_id, title:hash[this.hash_id].title, description:hash[this.hash_id].description}, div);
		} else */
		if (hash == global_photos)  {
			//alert('To be done.');
			var data= 'action=edittitle'
					+'&id='+this.hash_id
					+'&title='+UrlEncode(hash[this.hash_id].title)
					+'';
			SendReq4XML(photos_meta_edit_url,data,div.photos_setMeta_onLoad,{});
		//flickrAPI.callMethod('flickr.photos.setMeta', {photo_id:this.hash_id, title:hash[this.hash_id].title, description:hash[this.hash_id].description}, div);
		} else {
			alert('unknown hash')
		}
	}
	if (window.deja_view_should_refresh) {
		div.innerHTML = '...';
	}
	
	return insitu_init_editable_div(div);
}

var insitu_init_editable_div = function(div) {
	div.startEditing = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_button_action) return;
		window.should_I_ignore_stuff_because_of_editable_div_action = 1;
		var photo_notes = _ge('photo_notes');
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.take_her_away();
		this.isEditing = true;
		this.unhighlight();
		this.style.display = 'none';
		var form_div = this.getForm_div();
		form_div.style.display = 'block';
		var form = form_div.firstChild;
		form.content.focus();
		form.content.select();
	}
	div.endEditing = function() {
		window.should_I_ignore_stuff_because_of_editable_div_action = 0;
		this.isEditing = false;
		var form_div = this.getForm_div();
		form_div.innerHTML = '';
		form_div.style.display = 'none';
		this.style.display = 'block';
	}
	div.onclick = div.startEditing;
	
	div.getForm_div = function() {
		if (!this.form_div) {
			this.form_div = document.createElement('div');
			this.parentNode.insertBefore(this.form_div, this);
			this.form_div.display_div = this;
		}
		
		var formHTML = '<form onsubmit="this.parentNode.display_div.saveChanges(this); return false;" style="margin-left:'+this.style.marginLeft+'" style="margin-right:'+this.style.marginRight+'">';
		formHTML+= this.getInput();
		formHTML+= '<br \/><input type="submit" class="Butt" value="&nbsp;保&nbsp;存&nbsp;" \/>&nbsp;&nbsp;<span style="font-family:arial; font-size:12px;">OR</span>&nbsp;&nbsp;<input type="button" class="DeleteButt" value="&nbsp;取&nbsp;消&nbsp;" onclick="this.form.parentNode.display_div.endEditing();" \/><\/form>';
		formHTML+= this.getExtra();
		this.form_div.innerHTML = formHTML
		return this.form_div;
	}
	
	div.onmouseover = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_button_action) return;
		this.highlight();
	}
	
	div.onmouseout = function() {
		if (this.hideTimer) clearTimeout(this.hideTimer);
		this.hideTimer = setTimeout('var el = document.getElementById("'+this.id+'"); if (el) el.unhighlight()', 1000)
	}
	
	div.highlight = function() {
		if (this.hideTimer) clearTimeout(this.hideTimer);
		div.style.backgroundColor = '#ffffd3';
		if (this.emptyText && (div.innerHTML=='&nbsp;' || div.innerHTML==' ' || div.innerHTML.charCodeAt(0) == 160)) {
			div.style.color = '#888';	
			div.innerHTML = this.emptyText;
		}
	}
	
	div.unhighlight = function() {
		if (this.hideTimer) clearTimeout(this.hideTimer);
		div.style.backgroundColor = '';
		if (this.emptyText && div.innerHTML.toUpperCase()==this.emptyText.toUpperCase()) {
			div.innerHTML = '&nbsp;';
			div.style.color = '#000';
		}
	}
	return div;
}









////////////////////////////////////////////////////////













//////////////////////////////////////////////////////




var sendToBlogDialogDiv;

function show_sendToBlogDialog(photo_id) {
	if (!_ok_for_scrumjax_xml()) {
		document.location = '/blog.gne?photo='+photo_id;
		return;
	}

	var button_source = _ge('photo_gne_button_blog_this');
	button_source.EXT.disable_others();

	if (sendToBlogDialogDiv==undefined) {
		make_send_to_div('sendToBlog', photo_id);
	}
	sendToBlogDialogDiv.style.left = (_find_x(button_source)+0)+'px';
	sendToBlogDialogDiv.style.top = (_find_y(button_source)+24)+'px';
	sendToBlogDialogDiv.style.display = 'block';

	if (navigator.userAgent.indexOf("Mozilla/5.0") != -1 && navigator.userAgent.indexOf("KHTML")==-1) { // fix for stupid moz bug w/scrollbars and maxHeight
		var osh = _ge('sendToBlogDialogListDiv').offsetHeight;
		if (osh >= 220)_ge('sendToBlogDialogListDiv').style.overflow = '-moz-scrollbars-vertical';
	}
	hidePhotoSwf();
}



var sendToGroupDialogDiv;

function show_sendToGroupDialog(photo_id) {
	if (!_ok_for_scrumjax_xml()) {
		document.location = '/photo_sendto_group.gne?id='+photo_id;
		return;
	}

	var button_source = _ge('photo_gne_button_send_to_group');
	button_source.EXT.disable_others();

	if (sendToGroupDialogDiv==undefined) {
		make_send_to_div('sendToGroup', photo_id);
	}
	sendToGroupDialogDiv.style.left = (_find_x(button_source)+0)+'px';
	sendToGroupDialogDiv.style.top = (_find_y(button_source)+24)+'px';
	sendToGroupDialogDiv.style.display = 'block';

	if (navigator.userAgent.indexOf("Mozilla/5.0") != -1 && navigator.userAgent.indexOf("KHTML")==-1) { // fix for stupid moz bug w/scrollbars and maxHeight
		var osh = _ge('sendToGroupDialogListDiv').offsetHeight;
		if (osh >= 220)_ge('sendToGroupDialogListDiv').style.overflow = '-moz-scrollbars-vertical';
	}
	hidePhotoSwf();
}



var sendToSetDialogDiv;

function show_sendToSetDialog(photo_id) {
	if (!_ok_for_scrumjax_xml()) {
		document.location = '/photo_sendto_set.gne?id='+photo_id;
		return;
	}

	var button_source = _ge('photo_gne_button_send_to_set');
	button_source.EXT.disable_others();

	if (sendToSetDialogDiv==undefined) {
		make_send_to_div('sendToSet', photo_id);
	}
	sendToSetDialogDiv.style.left = (_find_x(button_source)+0)+'px';
	sendToSetDialogDiv.style.top = (_find_y(button_source)+24)+'px';
	sendToSetDialogDiv.style.display = 'block';

	if (navigator.userAgent.indexOf("Mozilla/5.0") != -1 && navigator.userAgent.indexOf("KHTML")==-1) { // fix for stupid moz bug w/scrollbars and maxHeight
		var osh = _ge('sendToSetDialogListDiv').offsetHeight;
		if (osh >= 220) _ge('sendToSetDialogListDiv').style.overflow = '-moz-scrollbars-vertical';
	}

	hidePhotoSwf();
}

var printDialogDiv;

function show_printDialog(photo_id) {
	if (!_ok_for_scrumjax_xml()) {
		document.location = '/photo_print.gne?id='+photo_id;
		return;
	}

	var button_source = _ge('photo_gne_button_print');
	button_source.EXT.disable_others();

	if (printDialogDiv==undefined) {
		make_send_to_div('print', photo_id);
	}
	printDialogDiv.style.left = (_find_x(button_source)+0)+'px';
	printDialogDiv.style.top = (_find_y(button_source)+24)+'px';
	printDialogDiv.style.display = 'block';

	/*
	if (navigator.userAgent.indexOf("Mozilla/5.0") != -1 && navigator.userAgent.indexOf("KHTML")==-1) { // fix for stupid moz bug w/scrollbars and maxHeight
		var osh = _ge('printDialogListDiv').offsetHeight;
		if (osh >= 220) _ge('printDialogListDiv').style.overflow = '-moz-scrollbars-vertical';
	}
	*/

	hidePhotoSwf();
}


function hide_printDialog() {
	hide_send_to_div(printDialogDiv, _ge('photo_gne_button_print'));
	
	//reset forms
	var print_form = _ge('print_form');
	if (print_form && print_form.clear) print_form.clear();
}

function hide_sendToSetDialog() {
	hide_send_to_div(sendToSetDialogDiv, _ge('photo_gne_button_send_to_set'), _ge('sendToSetDialogFormDiv'));
	var form = _ge('new_set_form');
	form.set_title.value = '';
	form.set_description.value = '';
}

function hide_sendToGroupDialog() {
	hide_send_to_div(sendToGroupDialogDiv, _ge('photo_gne_button_send_to_group'));
}

function hide_sendToBlogDialog() {
	hide_send_to_div(sendToBlogDialogDiv, _ge('photo_gne_button_blog_this'));
}

hide_send_to_div = function(div, button, form_div) {
	button.EXT.enable_others();
	button.EXT.lock_down = 0;
	button.onmouseout();
	div.style.display = 'none';
	window.should_I_ignore_stuff_because_of_button_action = 0;
	
	_ge(div.id.replace('Div', '')+'HeaderDiv').innerHTML = div.get_click_a_text();
			
	var list_div = _ge(div.id.replace('Div', '')+'ListDiv');
	
	if (form_div && form_div.style.display=='block') {
		list_div.style.display = 'block';
		form_div.style.display = 'none';
	}
}

make_send_to_div = function(which, photo_id) {

	var ref_str_base = which+'Dialog';
	var ref_str = ref_str_base+'Div';
	
	if (which=='sendToSet') {
		var what = 'set';
		var button_w = 49;
		var list_method = 'flickr.photosets.getList';
		// none_msg for sets is no longer used
		var none_msg = 'You haven\'t created any<br>photo sets yet. You can<br><a href="" onclick="_ge(\''+ref_str+'\').show_new_set_form();return false;">create one now<\/a> if you like.';
	} else if (which=='sendToGroup') {
		var what = 'group';
		var button_w = 54;
		var list_method = 'flickr.groups.pools.getGroups';
		var none_msg = 'You don\'t belong to any<br>groups yet. You can find<br>and create groups <a href="/groups/">here<\/a>.';
	} else if (which=='sendToBlog') {
		var what = 'blog';
		var button_w = 48;
		var list_method = 'flickr.blogs.getList';
		var none_msg = 'You haven\'t configured<br>a blog to post to yet.<br>Why not <a href="/blogs_add.gne?extra=photo'+photo_id+'">do that now<\/a>?';
	} else if (which=='print') {
		var what = 'print';
		var button_w = 58;
		var list_method = '';
		var none_msg = '';
	} else {
		return;
	}

	var div = window[ref_str] = document.createElement('div');
	div.id = ref_str;
	div.style.position = 'absolute';
	div.style.padding = '0 0px 0px 0px';
	div.style.border = '1px solid #999';
	div.style.borderWidth = '0 1px 1px 1px';
	div.style.fontFamily = 'arial';
	div.style.fontSize = '12px';
	div.style.textAlign = 'left';
	div.style.backgroundColor = '#fff';
	div.style.width = '250px';
	div.style.overflow = 'visible';

	 if (which=='print') {
		var start_html = '<div style="border:1px solid #999; border-width:1px 0 0 0;height:1px; margin-left:'+button_w+'px; font-size:1px;"><\/div>';
		start_html+= '<div id="'+ref_str_base+'HeaderDiv" style="text-align:center; padding:3px 0 3px 0;"><br>Loading print options...<br><br><\/div>';
		start_html+= '<div style="display:none; background-color:#fff; padding:4px 2px 4px 2px; border-bottom: 1px solid #ddd; border-top: 1px solid #ddd;">';
		start_html+= '<div id="'+ref_str_base+'ListDiv" style="padding-right:2px; max-height:400px; overflow:auto;"><\/div>';
		start_html+= '<\/div>';
		start_html+= '<div id="'+ref_str_base+'BottomDiv" style="margin:0px; display:none; background-color:#f7f7f7; font-family:arial; font-size:11px; padding:13px 13px 8px 13px;">';
		start_html+= '<!--Flickr Printing services are provided by EZPrints. When you\'ve finished collecting up the photos you want to print, click on the cart icon to finalize you order.';
		start_html+= '<br><img src="/images/cart.gif" width="19" height="16" border="0" style="border:0; float:right; display:block;"><br clear="all">';
		start_html+= '--><\/div>';
	} else { 
		var start_html = '<div style="border:1px solid #999; border-width:1px 0 0 0;height:1px; margin-left:'+button_w+'px; font-size:1px;"><\/div>';
		start_html+= '<div id="'+ref_str_base+'HeaderDiv" style="text-align:center; padding:3px 0 3px 0;"><br>loading '+what+'s...<br><br><\/div>';
		start_html+= '<div style="display:none; background-color:#fff; padding:4px 2px 4px 2px; border-bottom: 1px solid #ddd; border-top: 1px solid #ddd;">';
		start_html+= '<div id="'+ref_str_base+'ListDiv" style="padding-right:2px; max-height:220px; overflow:auto;"><\/div>';
		start_html+= '	<div id="'+ref_str_base+'FormDiv" style="margin:10px; display:none; height:200px">';
		if (which=='sendToSet') {
			start_html+= '<form id="new_set_form">';
			start_html+= 'Title:<br><input type="text" name="set_title" style="width:220px;"><br><br>';
			start_html+= 'Description:<br><textarea type="text" name="set_description" style="width:220px; height:105px;"><\/textarea><br>';
			start_html+= '<input type="button" class="Butt" value="CREATE NEW SET" onclick="_ge(\''+ref_str+'\').create_new_set(); this.blur();" style="margin:5px 0 0 0;">';
			start_html+= '<\/form>';
		}
		start_html+= '<\/div>';
		start_html+= '<\/div>';
		start_html+= '<div id="'+ref_str_base+'BottomDiv" style="margin:3px; display:none; text-align:center;">';
		start_html+= '<a href="" onclick="hide_'+ref_str_base+'(); return false;">Cancel<\/a>';
		start_html+= '<\/div>';
	}

	div.innerHTML = start_html;

	document.body.appendChild(div);

	div.get_click_a_text = function() {
		var txt = '';
		if (which=='sendToSet') {
			txt+= 'Choose a set below';
			txt+= (_ok_for_scrumjax_xml() && user_can_create_sets) ? ' (or <a href="" onclick="_ge(\''+ref_str+'\').show_new_set_form();return false;">create a new set<\/a>).' : ':';
		} else if (which=='print') {
			txt = 'Choose a print size &amp; quantity:';
		} else {
			txt = 'Choose a '+what+' below:';
		}
		
		return txt;
	}

	div.show_new_set_form = function() {
		_ge(ref_str_base+'ListDiv').parentNode.style.display = 'block';
		_ge(ref_str_base+'HeaderDiv').innerHTML = 'Create a new set:';
		_ge(ref_str_base+'ListDiv').style.display = 'none';
		_ge(ref_str_base+'FormDiv').style.display = 'block';
		_ge('new_set_form').set_title.focus();
	}

	div.create_new_set = function() {
		var form = _ge('new_set_form');
		var title = form.set_title.value;
		var desc = form.set_description.value;
		if (title=='') {
			alert('You must enter a title for the new set. Otherwsie, what would it be titled?');
			return;
		}
		this.send_to_action(null, title, 'create', desc)
	}

	div.send_to_action = function(id, title, action, set_desc, confirm_ok) {
		var photo_notes = _ge('photo_notes');
		//writeDebug(which);
		//writeDebug(!global_photos[photo_id].isPublic);
		//writeDebug(!confirm_ok);
		if (which=='sendToGroup' && action=='add' && !global_photos[photo_id].isPublic && !confirm_ok) {
			window['hide_'+ref_str_base]();
			
			var ok_func = function() {
				div.send_to_action(id, title, action, set_desc, 1)
			}
			
			var conf_txt = 'You are about to add a private photo to a group. This means that members of <b>'+title+'<\/b> will be able to view, comment on, tag and add notes to this photo. Is this OK with you?';
			photo_notes.EXT.start_comming(conf_txt, 1, 1, 'YES', ok_func, 1, 'NO', null);
			return;
		}
		
		deja_view_uh_huh();
		
		if (which=='print') {
			var action_str = 'Adding prints to cart...'
		} else if (action=='remove') {
			var action_str = 'Removing photo from '+what+'...';
		} else if (action=='create') {
			var action_str = 'Creating new '+what+'...';
		} else {
			var action_str = 'Adding photo to '+what+'...'
		}

		window['hide_'+ref_str_base]();
		this.id_dealing_with = id;
		this.title_dealing_with = title;

		if (which=='sendToSet') {
			if (action=='create') {
				flickrAPI.callMethod('flickr.photosets.create', {title:title, description:set_desc, primary_photo_id:photo_id}, div);
			} else if (action=='remove') {
				flickrAPI.callMethod('flickr.photosets.removePhoto', {photoset_id:id, photo_id:photo_id}, div);
			} else {
				flickrAPI.callMethod('flickr.photosets.addPhoto', {photoset_id:id, photo_id:photo_id}, div);
			}
		} else if (which=='sendToGroup') {
			if (action=='remove') {
				flickrAPI.callMethod('flickr.groups.pools.remove', {group_id:id, photo_id:photo_id}, div);
			} else {
				flickrAPI.callMethod('flickr.groups.pools.add', {group_id:id, photo_id:photo_id}, div);
			}
		} else if (which=='sendToBlog') {
			var blog_form = _ge('blog_form');
			blog_form.blog.value = id;
			blog_form.submit();
			return;
		} else if (which=='print') {
			div.addToCart_responses_expected = page_print_orderA.length;
			div.addToCart_responses = 0;
			div.addToCart_error_count = 0;
			div.addToCart_problem_product_ids = [];
			div.addToCart_cart_url = '';
			div.addToCart_last_error = '';
			
			div.send_another_printing_product();
			
		} else {
			return;
		}
		
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.start_comming(action_str);
	}
	
	div.send_another_printing_product = function() {
		var A = page_print_orderA[div.addToCart_responses].split(':');
		var params = {provider_id:page_printing_provider, photo_ids:photo_id, product_id:A[0].replace('prod',''), quantity:A[1]};
		//if (div.addToCart_responses==2) params.photo_id = photo_id+57777777; //fake an error
		flickrAPI.callMethod('flickr.printing.addToCart', params, div);
	}
	
	div.flickr_printing_addToCart_onLoad = function(success, responseXML, responseText, params) {
		div.addToCart_responses++;
		
		if (success) {
			div.addToCart_cart_url = responseXML.documentElement.getElementsByTagName('cart')[0].firstChild.nodeValue;
		} else {
			div.addToCart_error_count++;
			div.addToCart_problem_product_ids.push(params.product_id);
			if (responseText) {
				div.addToCart_last_error = responseText;
			} else {
				div.addToCart_last_error = 'unknown error';
			}
		}
		
		if (div.addToCart_responses != div.addToCart_responses_expected) {
			div.send_another_printing_product();
			return;
		}
		var photo_notes = _ge('photo_notes');

		writeDebug('div.addToCart_responses '+div.addToCart_responses+' '+div.addToCart_error_count);
		
		if (div.addToCart_responses > div.addToCart_error_count) { // at least one photo was added
			if (div.addToCart_error_count > 0) {
				var confirm_html = 'We\'ve added some prints of <b>'+global_photos[photo_id].title+'<\/b> to your cart'
				+ ', but there were errors adding '+div.addToCart_error_count+' of the prints. The last error was: '
				+ '<b>'+div.addToCart_last_error.truncate_with_ellipses(400).replace('&', '[wbr]&').replace('/', '/[wbr]').replace('=', '=[wbr]').escapeForDisplayAndWrap().replace('[wbr]', '<wbr>')+'<\/b><br><br>';
			} else {
				var confirm_html = 'We\'ve added these prints of <b>'+global_photos[photo_id].title+'<\/b> to <b><a href="'+div.addToCart_cart_url+'">your cart<\/a><\/b>:';
			}

			confirm_html+= '<ul style="padding-left:20px;">';
			for (var i=0; i<page_print_orderA.length; i++) {
				var A = page_print_orderA[i].split(':');
				if (_in_array(div.productsO[A[0]].id, div.addToCart_problem_product_ids)) {
					confirm_html+= '<li style="color:red;"><b>'+0+'<\/b> @ '+div.productsO[A[0]].id+' '+div.productsO[A[0]].name+'<\/li>';
				} else {
					confirm_html+= '<li><b>'+A[1]+'<\/b> @ '+div.productsO[A[0]].name+' <nobr>('+format_price(A[1]*(div.productsO[A[0]].price.replace('.','')))+')<\/nobr><\/li>';
				}
			}
			confirm_html+= '<\/ul>';

			confirm_html+= '<form id="print_cart_form"><input type="button" id="print_ok_button" class="Butt" value="CONTINUE BROWSING" style="margin:5px 0 10px 0;"><br>'
			+ 'Or are you ready to place your order?<br><input type="button" id="print_cart_button" class="Butt" value="PROCEED TO CART" style="margin:5px 0 10px 0;"><\/form>'
			+ '<div id="print_cart_prompt"><img src="/images/cart.gif" width="15" height="12" border="0" style="margin:0 6px 20px 0; border:0; float:left; display:block;">'
			+ '<div>You can get to your cart anytime by clicking on the cart icon at the top of each page.<\/div><\/div>';

			photo_notes.EXT.start_comming(confirm_html, 1, 0, 'GO TO CART', 1, 'CLOSE', null);

			var print_ok_button = _ge('print_ok_button');
			if (navigator.userAgent.indexOf("KHTML")==-1) print_ok_button.focus(); // crashes Safari! (when you click the focused button)
			print_ok_button.onclick = function() {
				photo_notes.EXT.stop_comming(); this.blur();
			}

			var print_cart_button = _ge('print_cart_button');
			print_cart_button.onclick = function() {
				photo_notes.EXT.stop_comming(); this.blur();
				document.location = div.addToCart_cart_url;
			}

		} else { // no prints were added
			alert('There were errors adding the prints to your cart. The last error was: '+div.addToCart_last_error);
			if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
		}
		
	}

	div.flickr_photosets_removePhoto_onLoad = div.flickr_groups_pools_remove_onLoad = function(success, responseXML, responseText) {
		var photo_notes = _ge('photo_notes');
		var add_to_div = _ge('add_to_'+what+'_'+this.id_dealing_with+'_div');
		var add_to_td = _ge('add_to_'+what+'_'+this.id_dealing_with+'_td');

		if (success || responseText.indexOf('not in pool') != -1 || responseText.indexOf('not in set') != -1) {
			if (add_to_div) add_to_div.make_me_for_adding();

			if (photo_notes && photo_notes.EXT && which=='sendToSet') {
				var confirm_html = 'This photo has been removed from your <b><a href="'+global_photos[photo_id].ownersUrl+'sets/'+this.id_dealing_with+'/">'+this.title_dealing_with+'<\/a><\/b> '+what+'!';
				//var context_link_id = 'contextLink_set'+this.id_dealing_with;
				remove_context_widget('set', this.id_dealing_with);
				flickrAPI.callMethod(list_method, {}, div);
			} else if (photo_notes && photo_notes.EXT && which=='sendToGroup') {
				var confirm_html = 'This photo has been removed from the <b><a href="/groups/'+this.id_dealing_with+'/pool/">'+this.title_dealing_with+'<\/a><\/b> '+what+'.';
				//var context_link_id = 'contextLink_pool'+this.id_dealing_with;
				remove_context_widget('pool', this.id_dealing_with);
			}

			photo_notes.EXT.start_comming(confirm_html, 1, 1, 'OK');
		} else {
			alert('There was an error: '+responseText);
			if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
		}
	}

	div.flickr_photosets_addPhoto_onLoad = div.flickr_groups_pools_add_onLoad = function(success, responseXML, responseText) {
		var photo_notes = _ge('photo_notes');
		var add_to_div = _ge('add_to_'+what+'_'+this.id_dealing_with+'_div');
		var add_to_td = _ge('add_to_'+what+'_'+this.id_dealing_with+'_td');

		if (success || responseText.indexOf('already in pool') != -1 || responseText.indexOf('already in set') != -1) {
			if (add_to_div) add_to_div.make_me_for_removing();

			if (photo_notes && photo_notes.EXT && which=='sendToSet') {
				var confirm_html = 'This photo has been added to your <b><a href="'+global_photos[photo_id].ownersUrl+'sets/'+this.id_dealing_with+'/">'+this.title_dealing_with+'<\/a><\/b> '+what+'.';
				add_context_widget('set', this.id_dealing_with, photo_id);
			} else if (photo_notes && photo_notes.EXT && which=='sendToGroup') {
				var confirm_html = 'This photo has been added to the <b><a href="/groups/'+this.id_dealing_with+'/pool/">'+this.title_dealing_with+'<\/a><\/b> '+what+'.';
				add_context_widget('pool', this.id_dealing_with, photo_id);
			}

			var remove_func = function() {
				add_to_div.confirm_remove();
			}

			photo_notes.EXT.start_comming(confirm_html, 1, 1, 'OK', null, 1, 'UNDO', remove_func);
		} else {
			alert('There was an error: '+responseText);
			if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
		}
	}

	div.flickr_photosets_create_onLoad = function(success, responseXML, responseText) {
		var photo_notes = _ge('photo_notes');

		if (success) {
			var new_set = responseXML.documentElement.getElementsByTagName('photoset')[0];

			if (photo_notes && photo_notes.EXT && which=='sendToSet') {
				var confirm_html = 'This photo has been added to a new set titled <b><a href="'+new_set.getAttribute('url')+'">'+this.title_dealing_with+'<\/a><\/b>.';
				add_context_widget('set', new_set.getAttribute('id'), photo_id);
			}
			photo_notes.EXT.start_comming(confirm_html, 1, 1, 'OK', null);
			flickrAPI.callMethod(list_method,{new_set_id_passthrough:new_set.getAttribute('id')}, div);
		} else {
			alert('There was an error: '+responseText);
			if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
		}
	}

	div.flickr_photosets_getList_onLoad = div.flickr_blogs_getList_onLoad = div.flickr_groups_pools_getGroups_onLoad = function(success, responseXML, responseText, params) {
		if (!success) {
			alert('there was an error: '+ responseText)
		}
		if (which=='sendToSet') {
			var collection = responseXML.documentElement.getElementsByTagName('photoset');
			user_can_create_sets = (responseXML.documentElement.getElementsByTagName('photosets')[0].getAttribute('cancreate')=='1') ? 1 : 0;
			_ge(ref_str_base+'HeaderDiv').innerHTML = this.get_click_a_text();
		} else if (which=='sendToGroup') {
			var collection = responseXML.documentElement.getElementsByTagName('group');
		} else if (which=='sendToBlog') {
			var collection = responseXML.documentElement.getElementsByTagName('blog');
		} else {
			return;
		}

		if (collection.length==0) {
			if (which=='sendToSet') {
				div.show_new_set_form();
			} else {
				_ge(ref_str_base+'BottomDiv').innerHTML = '<br>'+none_msg+'<br><br>' + _ge(ref_str_base+'BottomDiv').innerHTML +'';
				_ge(ref_str_base+'HeaderDiv').style.display = 'none';
			}
		} else {
			//var cursor = (document.all && !window.opera) ? 'hand' : 'pointer';

			_ge(ref_str_base+'ListDiv').innerHTML = '';

			for (var g=0;g<collection.length;g++) {
				var what_id = collection[g].getAttribute('id');
				if (which=='sendToSet') {
					var context_div_id = 'contextDiv_set'+what_id;
					var context_div_what = 'set';
					var what_title = responseXML.documentElement.getElementsByTagName('title')[g].firstChild.nodeValue;
					var what_icon = 'http://photos'+collection[g].getAttribute('server')+'.flickr.com/'+collection[g].getAttribute('primary')+'_'+collection[g].getAttribute('secret')+'_s.jpg';
					var icon_h = 25;
					var icon_w = 25;
				} else if (which=='sendToGroup') {
					var context_div_id = 'contextDiv_pool'+what_id;
					var context_div_what = 'pool';
					var what_title = collection[g].getAttribute('name');
					var what_icon = '/buddyicons/'+collection[g].getAttribute('id')+'.jpg';
					var icon_h = 24;
					var icon_w = 24;
				} else if (which=='sendToBlog') {
					var context_div_id = '';
					var context_div_what = '';
					var what_title = collection[g].getAttribute('name');
					var what_icon = '/images/spaceball.gif';
					var icon_h = 20;
					var icon_w = 2;
				}

				var marginTop = (g==0 || (document.all && !window.opera)) ? '0px' : '-2px';
				var menu_item = make_menu_item(what, what_id, what_title, marginTop, what_icon, icon_w, icon_h, ref_str_base);
				_ge(ref_str_base+'ListDiv').appendChild(menu_item);

				if (which != 'sendToBlog' && (_ge(context_div_id) && _ge(context_div_id).style.display=='block' || 
																		nextprev_currentContextID==context_div_what+what_id) ||
																		params.new_set_id_passthrough==what_id) {
					menu_item.make_me_for_removing();
				} else {
					menu_item.make_me_for_adding();
				}
			}

			_ge(ref_str_base+'ListDiv').parentNode.style.display = 'block';
			if (document.all && !window.opera) { // since IE does not do max-height
				_ge(ref_str_base+'ListDiv').style.height = 'auto';
				var osh = _ge(ref_str_base+'ListDiv').offsetHeight;
				if (osh && osh > 220) _ge(ref_str_base+'ListDiv').style.height = '220px';
			}
			_ge(ref_str_base+'HeaderDiv').innerHTML = div.get_click_a_text();
		}

		_ge(ref_str_base+'BottomDiv').style.display = 'block';

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	div.flickr_printing_getProducts_onLoad = function(success, responseXML, responseText, params) {
		if (!success) {
			alert('there was an error: '+ responseText);
			//should give an alert and return false here and hide the menu
		}
		
		calculate_photo_props(photo_id);
		var p = global_photos[photo_id];
		
		var collection = responseXML.documentElement.getElementsByTagName('product');

		if (collection.length==0) {
			alert('no products');
			//should give an alert and return false here and hide the menu
		} else {
			
			window.page_print_prices = {};
			div.productsO = {};
			for (var g=0;g<collection.length;g++) {
				var product_id = collection[g].getAttribute('id');
				var product_price = collection[g].getAttribute('price');
				var product_name = collection[g].getAttribute('name');
				var product_description = collection[g].getAttribute('description');
				var product_minresx = collection[g].getAttribute('minresx')*1;
				var product_minresy = collection[g].getAttribute('minresy')*1;
				var product_aspect = collection[g].getAttribute('aspect')*1;
				var product_panorama = collection[g].getAttribute('panorama')*1;
				
				var paul = 0;
				
				if (p.huge) {
					if (_in_array(product_id, global_huge_photo_print_ids)) paul = 1;
				} else {
					if (_in_array(product_id, global_wallet_photo_print_ids)) paul = 1;
				}
				
				if (p.square) {
					if (_in_array(product_id, global_square_photo_print_ids)) paul = 1;
				}
				
				 if (p.dig_dims) {
					if (_in_array(product_id, global_dig_photo_print_ids)) paul = 1;
				} else {
					if (_in_array(product_id, global_reg_photo_print_ids)) paul = 1;
				}
			
				writeDebug('min rec size:'+product_minresx+'x'+product_minresy+ ' size:'+p.w_o+'x'+p.h_o);
				
				if (product_aspect == 1) { // just check the smallest side for square/pano sizes
					var product_rec = (Math.min(p.w_o, p.h_o) >= product_minresx) ? 1 : 0;
				} else { // check both sides
					var product_rec = (
						Math.max(p.w_o, p.h_o) >= Math.max(product_minresx, product_minresy)
						&&
						Math.min(p.w_o, p.h_o) >= Math.min(product_minresx, product_minresy)
						) ? 1 : 0;
				}
				
				writeDebug('recommended:'+product_rec);
				
				div.productsO['prod'+product_id] = {
					id:product_id,
					name:product_name.replace('Digital Aspect', ''),
					description:product_description,
					price:product_price,
					recommended:product_rec,
					show:paul,
					minresx:product_minresx,
					minresy:product_minresy
					}
				
				page_print_prices['prod'+product_id] = product_price.replace('.', '');
			}
		}
		div.show_printing_products();
	}
	
	
	div.flickr_printing_setPrefs_onLoad = function(success, responseXML, responseText, params) {
		if (success) {
			flickrAPI.callMethod('flickr.printing.getProviders', {country_code:page_printing_country_code}, div);
		} else {
			alert('there was an error: '+responseText);
		}
	}
	
	div.flickr_printing_getProviders_onLoad = function(success, responseXML, responseText, params) {
		if (success) {
			var collection = responseXML.documentElement.getElementsByTagName('provider');
			
			if (collection.length==0) {
				page_printing_provider = 0;
			} else { // this only grabs the first one.
				page_printing_provider = collection[0].getAttribute('id');
			}
			
			if (page_printing_provider==0) {
				div.show_no_providers();
			} else {
				div.show_printing_products();
			}
			
		} else {
			alert('there was an error: '+responseText);
		}
	}
	
	div.show_print_screen_changing = function(txt) {
		_ge(ref_str_base+'HeaderDiv').innerHTML = '<br>'+txt+'<br><br>';
		_ge(ref_str_base+'ListDiv').parentNode.style.display = 'none';
	}
	
	div.set_country_select = function(val) {
		if (!val) val = 0;
		var country_select = _ge('country');
		for (var i=0;i<country_select.options.length;i++) {
			var o = country_select.options[i];
			if (val == o.value) {
				country_select.selectedIndex = i;
				return;
			}
		}
	}
	
	div.show_no_providers = function() {
		_ge(ref_str_base+'HeaderDiv').innerHTML = 'We do apologize...';
		_ge('choose_a_country_div').style.display = 'none';
		if (div.no_providers_text == undefined) div.no_providers_text = _ge('no_providers_div').innerHTML;
		var rep = function(txt) {
			txt = txt.replace('REP_COUNTRY', page_printing_country);
			return txt;
		}
		_ge('no_providers_div').innerHTML = rep(div.no_providers_text);
		
		_ge('change_country_link').onclick = function() {
			div.show_country_form();
			return false;
		}	
		_ge('no_providers_div').style.display = 'block';
		_ge(ref_str_base+'ListDiv').parentNode.style.display = 'block';
	}
	
	div.show_country_form = function() {
		_ge(ref_str_base+'HeaderDiv').innerHTML = 'Where are you located?';
		_ge('no_providers_div').style.display = 'none';
		_ge('print_form').style.display = 'none';
		div.set_country_select(page_printing_country_code);
		var country_submit = _ge('country_submit');
		var country_form = _ge('country_form');
		country_form.onsubmit = country_submit.onclick = function() {
			var i = country_form.country.selectedIndex;
			if (country_form.country.options[i].value == 0) return false;
			div.show_print_screen_changing('setting your country...');
			page_printing_country_code = country_form.country.options[i].value;
			page_printing_country = country_form.country.options[i].text;
			flickrAPI.callMethod('flickr.printing.setPrefs', {country_code:page_printing_country_code}, div);			
		}
		
		var country_select = _ge('country');
		country_select.onchange = function() {
			if (navigator.userAgent.indexOf("KHTML")==-1) country_submit.focus(); // crashes Safari! (when you click the focused button);
		}
		
		_ge('choose_a_country_div').style.display = 'block';
		_ge(ref_str_base+'ListDiv').parentNode.style.display = 'block';
		
		if (navigator.userAgent.indexOf("KHTML")==-1) country_select.focus(); // crashes Safari! (when you click the focused button);
	}

	div.show_all_printing_products = function() {
		printDialogDiv.style.display = 'none';
		for (p in div.productsO) {
			var prod = div.productsO[p];
			if (prod.show) continue;
			_ge('prod_'+prod.id+'_tr').style.display = (document.all && !window.opera) ? 'block' : 'table-row';
			if (!prod.recommended) _ge('print_not_rec_tr').style.display = (document.all && !window.opera) ? 'block' : 'table-row';
		}
		//_ge('other_printers_tr').style.display = (document.all && !window.opera) ? 'block' : 'table-row';
		show_printDialog(photo_id);
	}

	div.show_printing_products = function() {
		if (div.productsO == undefined) {
			flickrAPI.callMethod('flickr.printing.getProducts', {provider_id:page_printing_provider}, div);
			return;
		}
	
		_ge(ref_str_base+'HeaderDiv').innerHTML = div.get_click_a_text();
		_ge(ref_str_base+'HeaderDiv').style.display = 'block';
		_ge('choose_a_country_div').style.display = 'none';
		_ge('no_providers_div').style.display = 'none';
		
		var template_tr = _ge('print_product_tr');
		var tbody = template_tr.parentNode;
		
		var rep = function(txt, prod) {
			var tip_txt = prod.description+'. (We recommend a minimum photo resolution of '+prod.minresx+' x '+prod.minresy+' pixels.)';
			var tip_html = _get_tip_html(tip_txt, 'tip_print_menu');

			txt = txt.replace('REP_TIP', tip_html);
			txt = txt.replace('REP_NAME', prod.name);
			txt = txt.replace('REP_PRODUCT', 'prod'+prod.id);
			txt = txt.replace('REP_PRICE', prod.price);
			return txt;
		}

		for (p in div.productsO) {
			var prod = div.productsO[p];
			
			var tr = document.createElement('tr');
			tr.id = 'prod_'+prod.id+'_tr';
			
			if (!prod.show) tr.style.display = 'none';
			
			var td1 = document.createElement('td');
			td1.className = 'print_dropdown_td';
			td1.innerHTML = rep(template_tr.childNodes[0].innerHTML, prod);
			tr.appendChild(td1);
			
			var td2 = document.createElement('td');
			td2.className = (prod.recommended) ? 'print_item_td' : 'print_item_notrec_td';
			td2.innerHTML = rep(template_tr.childNodes[1].innerHTML, prod);
			tr.appendChild(td2);
			
			var td3 = document.createElement('td');
			td3.className = (prod.recommended) ? 'print_price_td' : 'print_price_notrec_td';
			td3.innerHTML = rep(template_tr.childNodes[2].innerHTML, prod);
			tr.appendChild(td3);
			
			if (prod.recommended) {
				tbody.insertBefore(tr, _ge('print_not_rec_tr'));
			} else {
				if (prod.show) _ge('print_not_rec_tr').style.display = (document.all && !window.opera) ? 'block' : 'table-row';
				tbody.appendChild(tr);
			}
		}
		
		//_ge(ref_str_base+'BottomDiv').style.display = 'block';

		var print_cancel = _ge('print_cancel');
		var print_add = _ge('print_add');
		var print_form = _ge('print_form');
		
		print_form.clear = function() {
			for (var i=0;i<print_form.elements.length;i++) {
				//writeDebug(print_form.elements[i].type)
				if (print_form.elements[i].type=='select-one') {
					print_form.elements[i].selectedIndex = 0;
				}
			}
			update_print_prices();
		}
		
		page_print_orderA = [];
		
		print_cancel.onclick = hide_printDialog;
		print_add.onclick = function() {
			page_print_orderA = [];
			for (var i=0;i<print_form.elements.length;i++) {
				//writeDebug(print_form.elements[i].type)
				if (print_form.elements[i].type=='select-one') {
					if (print_form.elements[i].value*1 > 0) {
						writeDebug('print_form.elements[i].value '+print_form.elements[i].value);
						page_print_orderA.push(print_form.elements[i].name+':'+print_form.elements[i].value);
					}
				}
			}

			if (page_print_orderA.length > 0) div.send_to_action(photo_id, 'cart', 'add');
			return false;
		}
		//temp:
		if (_ge('change_country_link2')) {
			_ge('change_country_link2').onclick = function() {
				div.show_country_form();
				return false;
			}
		}
		if (_ge('more_print_options')) {
			_ge('more_print_options').onclick = function() {
				//div.show_country_form();
				this.style.display = 'none';
				div.show_all_printing_products();
				return false;
			}
		}
		
		
		_ge('print_form').style.display = 'block';
		_ge(ref_str_base+'ListDiv').parentNode.style.display = 'block';
	}
	
	div.print_fragment_onLoad = function(success, responseText, params) {
		_ge(ref_str_base+'ListDiv').innerHTML = responseText;
		
		if (page_printing_country_code == 'NONE') {
			div.show_country_form();
		} else if (page_printing_provider == '0') {
			div.show_no_providers();
		} else {
			div.show_printing_products();
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	if (which=='print') { // get the fragment right away, there are no products to get
		var params = {photo_id:photo_id}
		//if (page_printing_provider == '0') params['get_countries'] = 1;
		//params['get_countries'] = 0; //temporary
		
		if (page_zazzle_url != undefined) params['zazzle_url'] = page_zazzle_url;
		
		flickr_fragment_getter.get('/photo_print_fragment.gne', params, div, 'print_fragment_onLoad');
	} else {
		flickrAPI.callMethod(list_method, {}, div);
	}
}


make_menu_item = function(what, what_id, what_title, marginTop, what_icon, icon_w, icon_h, ref_str_base) {
	//var ref_str_base = 'sendTo'+((what=='set') ?'Set' : 'Group')+'Dialog';
	//writeDebug(ref_str_base)
	var table_w = (document.all && !window.opera) ? 217 : 220;

	var item_div = document.createElement('DIV');
	item_div.className = 'contextDiv';
	item_div.id = 'add_to_'+what+'_'+what_id+'_div';
	item_div.style.marginTop = marginTop;

	item_div.highlight = function() {
		this.style.backgroundColor='#0063DC';
		_ge('add_to_'+what+'_'+what_id+'_td').style.color='#fff';
	}
	item_div.unhighlight = function() {
		this.style.backgroundColor='transparent';
		_ge('add_to_'+what+'_'+what_id+'_td').style.color='#0063DC';
	}
	item_div.add_action = function() {
		window[ref_str_base+'Div'].send_to_action(what_id, what_title, 'add');
	}
	item_div.remove_action = function() {
		window[ref_str_base+'Div'].send_to_action(what_id, what_title, 'remove');
	}
	item_div.confirm_remove = function() {
		window['hide_'+ref_str_base]();

		var item_div = this;
		var remove_func = function() {
			item_div.remove_action();
		}

		var photo_notes = _ge('photo_notes');
		photo_notes.EXT.start_comming('Are you sure you want to remove this photo from <b>'+what_title+'<\/b>?', 1, 1, 'YES', remove_func, 1, 'CANCEL', null);
	}

	item_div.make_me_for_removing = function() {
		this.unhighlight();
		this.style.cursor = 'default';
		_ge('add_to_'+what+'_'+what_id+'_td').style.color='#999';
		
		if (what=='set') {
			remove_link = '<a title="Remove this photo from your '+what_title.replace('<', '&lt;').replace('"', '&quot;')+' set" class="remove_thing" href="" onclick="_ge(\''+item_div.id+'\').confirm_remove();return false;">[X]<\/a>';
		} else if (what=='group') {
			remove_link = '<a title="Remove this photo from the '+what_title.replace('<', '&lt;').replace('"', '&quot;')+' pool" class="remove_thing" href="" onclick="_ge(\''+item_div.id+'\').confirm_remove();return false;">[X]<\/a>';
		}

		_ge('add_to_'+what+'_'+what_id+'_td').innerHTML = what_title.truncate_with_ellipses(25) +' '+remove_link;

		this.onmouseout = null;
		this.onmouseover = null;
		this.onclick = null;
	}
	item_div.make_me_for_adding = function() {
		this.unhighlight();
		this.style.cursor = (document.all && !window.opera) ? 'hand' : 'pointer';
		_ge('add_to_'+what+'_'+what_id+'_td').style.color='#0063DC';
		_ge('add_to_'+what+'_'+what_id+'_td').innerHTML = '<span>'+what_title.truncate_with_ellipses(31)+'<\/span>'; // the span is so that &'s in the title are not treated as entity starts unless they are
		this.onmouseout = this.unhighlight;
		this.onmouseover = this.highlight;
		this.onclick = this.add_action;
	}

	var html = '';
	//html+= '<div id="add_to_'+what+'_'+what_id+'_div" style="cursor:'+cursor+'; margin-top:'+marginTop+'" onmouseover=""';
	//html+= 'onmouseout="" onclick="'+">';
	html+= '<table border="0" cellspacing="0" cellpadding="0" width="'+table_w+'"><tr>';

	html+= '<td valign="top" style="padding:0; width:'+(icon_w+7)+'px;">';
	html+= '<img src="'+what_icon+'" width="'+icon_w+'" height="'+icon_h+'" style="border:0; display:block; margin: 2px 0 2px 2px;"><\/td>';

	html+= '<td id="add_to_'+what+'_'+what_id+'_td">';
	html+= '<\/td><\/tr><\/table>';

	item_div.innerHTML = html;

	return item_div;
}



remove_context_widget = function(context_type, context_id) {
	var context_div = _ge('contextDiv_'+context_type+context_id);

	if (context_div) {
		context_div.style.display = 'none';

		if (nextprev_currentContextID==context_type+context_id) { // this is the current context
			 var stream_button = _ge('nextprev_button_stream'+global_nsid); // using global_nsid is only safe here because only owners can add/remove from sets/groups via the menus. 
			 if (stream_button && !stream_button.EXT.isOpen) {
			 	stream_button.onmouseup();
			 	stream_button.onmouseout();
			 }
		}

		var other_contexts_p = _ge('other_contexts_p');
		//var exisiting_context_divs = other_contexts_p.parentNode.getElementsByTagName('DIV');
		var exisiting_context_divs = document.getElementsByClass('contextDiv', 'DIV');

		var c = 0;
		for (var i=0; i<exisiting_context_divs.length; i++) {
			if (exisiting_context_divs[i].style.display=='block' && exisiting_context_divs[i].parentNode==other_contexts_p.parentNode) {
				c++;
				break;
			}
		}
		if (!c) other_contexts_p.style.display = 'none';
	}
}

add_context_widget = function(context_type, context_id, photo_id) {
	var context_div = _ge('contextDiv_'+context_type+context_id);
	var other_contexts_p = _ge('other_contexts_p');
	other_contexts_p.style.display = 'block';

	if (context_div) { // this shoudl be rare, only if you are adding after removing to the same set/group without reloading the page.
		context_div.style.display = 'block';

		if (nextprev_currentContextID==context_type+context_id) { // this is the current context
			 var stream_button = _ge('nextprev_button_stream'+global_nsid); // using global_nsid is only safe here because only owners can add/remove from sets/groups via the menus. 
			 if (stream_button && stream_button.EXT.isOpen) {
			 	stream_button.onmouseup();
			 	stream_button.onmouseout();
			 }
		}
	} else { // we must contruct the widget from scratch
		other_contexts_p.add_context_widget = function(success, responseText, params) {
			if (responseText=='') return; // unable to load it for some reason... bail

			var temp = document.createElement('div');
			temp.id = 'contextDiv_'+context_type+context_id;
			temp.style.display = 'block';
			temp.innerHTML = responseText;

			var exisiting_context_divs = this.parentNode.getElementsByTagName('DIV');

			if (exisiting_context_divs.length > 0) {
				this.parentNode.insertBefore(temp, exisiting_context_divs[0]);
			} else {
				this.parentNode.appendChild(temp);
			}

			_decorate(_ge('nextprev_button_'+context_type+context_id), context_type, context_id, photo_id);
		}
		flickr_fragment_getter.get('/photo_context_fragment.gne', {photo_id:photo_id, context_type:context_type, context_id:context_id}, other_contexts_p, 'add_context_widget');

	}
}

increment_print_dropdown = function(which) {
	var w = _ge(which);
	//writeDebug(w.options.length);
	var si = w.selectedIndex;
	si++;
	if (si>=w.options.length) {
		for (var i=0; i<10; i++) {
			w.options.length++;
			w.options[w.options.length-1].value = w.options[w.options.length-1].text = w.options.length-1;
		}
	}
	w.selectedIndex = si;
	update_print_prices(which);
}

decrement_print_dropdown = function(which) {
	var w = _ge(which);
	//writeDebug(w.options.length);
	var si = w.selectedIndex;
	si--;
	if (si < 0) return;
	w.selectedIndex = si;
	update_print_prices(which);
}


format_price = function(p) {
	var dp = p/100;
	if (dp==0) {
		dp = '0.00';
	} else if (dp < 1) {
		dp = dp.toString();
	}
	
	dpA = dp.toString().split('.');
	if (dpA.length==1) {
		dp = dp.toString()+'.00';
	} else if (dpA[1].length==1) {
		dp = dp.toString()+'0';
	}
	
	dp = '$ '+dp;
	return dp
}


update_print_prices = function(which) {
	//alert(which)
	var print_form = _ge('print_form');
	var ppt_td = _ge('price_print_total_td');
	//var w = _ge(which); not using this
	
	var t = 0;
	
	for (var i=0;i<print_form.elements.length;i++) {
		if (print_form.elements[i].type=='select-one') {
			if (print_form.elements[i].value*1 > 0) {
				var p = page_print_prices[print_form.elements[i].name];
			//	alert(print_form.elements[i].name+' '+p)
				t = t+(p*print_form.elements[i].value);
			}
		}
	}
	
	var ft = format_price(t);
	if (t > 0) {
		ft = '<nobr><b>'+ft+'<\/b><\/nobr>';
		
		if (print_form.last_total != t){
			
			anim_animate.call(ppt_td, [
				[1, 1, null, [], anim_set_color_rgb, {p:'r',b:0,e:255,eq:'easeOutQuad'}, {p:'g',b:0,e:0,eq:'easeOutQuad'}, {p:'b',b:0,e:132,eq:'easeOutQuad'}],
				[4, 50, null, [], anim_set_color_rgb, {p:'r',b:255,e:0,eq:'easeOutQuad'}, {p:'g',b:0,e:0,eq:'easeOutQuad'}, {p:'b',b:132,e:0,eq:'easeOutQuad'}],
				[1, 1, null, [], anim_set_color_rgb, {p:'r',b:0,e:255,eq:'easeOutQuad'}, {p:'g',b:0,e:0,eq:'easeOutQuad'}, {p:'b',b:0,e:132,eq:'easeOutQuad'}],
				[4, 50, function(t) {}, [], anim_set_color_rgb, {p:'r',b:255,e:0,eq:'easeOutQuad'}, {p:'g',b:0,e:0,eq:'easeOutQuad'}, {p:'b',b:132,e:0,eq:'easeOutQuad'}]
			]);
			
			
			
		}	
	} else {
		ppt_td.style.color = 'rgb(0, 0, 0)';
	}
	
	ppt_td.innerHTML = ft;
	print_form.last_total = t;
}

var place_notes_text_div = function(t) {
	if (!document.all || window.opera) return;
	var photo_notes = _ge('photo_notes');
	if (photo_notes) {
		photo_notes.EXT.place_notes_text_div();
		_set_single_shadow_height(photo_notes.EXT.notes_text_table);
	}
	return;
}

var adjust_textarea_height = function(t) {
	return;
	temp_div.innerHTML = t.value.replace('<', '&lt;').replace('\n', '<br>')+'g'; //.replace(' ', '&nbsp;'); but then it won't wrap.
	//temp_div.offsetHeight
	t.style.height = (temp_div.offsetHeight)+'px';

	return;
	if (t.scrollHeight > t.clientHeight) {
		t.style.height = t.scrollHeight;

	}
	//writeDebug('scrollHeight '+t.scrollHeight)
	//writeDebug('clientHeight '+t.clientHeight)
}

//called as the photo's onload
function show_notes_initially() {
	if (window.should_I_ignore_stuff_because_of_button_action || window.should_I_ignore_stuff_because_of_editable_div_action) return;
	var photo_notes = _ge('photo_notes');
	if (photo_notes && photo_notes.EXT) {
		photo_notes.EXT.shown_initially = 1;
		photo_notes.style.visibility = 'visible';
		if (!photo_notes.EXT.tried_to_show_before_shown_initially) photo_notes.EXT.go_away_in_a_bit();
		
		photo_notes.EXT.refresh_notes();
	}
}

function calculate_photo_props(photo_id) {
	var p = global_photos[photo_id];
	if (!p) return;
	
	var longer_side = Math.max(p.w_o, p.h_o);
	var shorter_side = Math.min(p.w_o, p.h_o);
	
	p.aspect = longer_side/shorter_side;
	p.panorama = (p.aspect >= 2) ? 1 : 0;
	p.dig_dims = (p.aspect < 1.36) ? 1 : 0;
	p.square = (p.aspect > .96 && p.aspect < 1.04) ? 1 : 0;
	p.small = (longer_side <= 1024) ? 1 : 0;
	p.huge = (shorter_side >= 3000 && longer_side >= 4500) ? 1 : 0;
	
	if (writeDebug) writeDebug('aspect '+p.aspect);
	if (writeDebug) writeDebug('panorama '+p.panorama);
	if (writeDebug) writeDebug('dig_dims '+p.dig_dims);
	if (writeDebug) writeDebug('square '+p.square);
	if (writeDebug) writeDebug('small '+p.small);
	if (writeDebug) writeDebug('huge '+p.huge);
}




function deja_view_check(deja_view_item) {
	if (!deja_view_item //|| !_ok_for_scrumjax_xml()
			) return;
	
	window.deja_view_item = deja_view_item;
	window.deja_view_should_refresh = 0;
	
	window.deja_view_type = deja_view_item.substring(0,1); // photo or set?
	window.deja_view_id = deja_view_item.replace(deja_view_type, '');
	
	var george = _get_cookie('deja_view');
	if (george) {
		georgeA = george.split(' ');
		for (var i=0; i<georgeA.length; i++) {
			var item = georgeA[i].split('^');
			if (item[0] == deja_view_item) {
				if (global_time_stamp == item[1]) {
					deja_view_should_refresh = 1; // because a change was made, man!
					document.write('<style>#otherContexts_div {display:none} <\/style>');
				}
				break;
			}
		}
	}
}

function deja_view_uh_huh() {
	if (!window.deja_view_item) return;
	var new_value = deja_view_item+'^'+global_time_stamp;
	
	var tempA = [new_value];
	var george = _get_cookie('deja_view');
	
	if (george) {
		georgeA = george.split(' ');
		for (var i=0; i<georgeA.length; i++) {
			var item = georgeA[i].split('^');
			if (item[0] != deja_view_item) tempA.push(georgeA[i]);
			if (tempA.length == 30) break;
		}
	}
	
	_set_cookie('deja_view', tempA.join(' '), 1);
	//rdb(_get_cookie('deja_view'))
}

function deja_view_refresh() {
	if (!window.deja_view_should_refresh || !window.deja_view_item 
		//|| !_ok_for_scrumjax_xml()
		) return;
	
	var caterina = {};
	caterina.flickr_photos_getInfo_onLoad = function(success, responseXML, responseText) {
		if (success) {
		
			var title_div = _ge('title_div'+deja_view_id);
			if (title_div) title_div.flickr_photos_getInfo_onLoad(success, responseXML, responseText);
			
			var photo_notes = _ge('photo_notes');
			if (photo_notes && photo_notes.EXT) photo_notes.EXT.flickr_photos_getInfo_onLoad(success, responseXML, responseText);
			
			var description_div = _ge('description_div'+deja_view_id);
			if (description_div) description_div.flickr_photos_getInfo_onLoad(success, responseXML, responseText);
			
			var tagadder = _ge('tagadder');
			if (tagadder) tagadder.flickr_photos_getInfo_onLoad(success, responseXML, responseText);
			
		} else {
			//alert('deja_view_refresh failed to load info');
		}
	}
	
	caterina.flickr_photos_getAllContexts_onLoad = function(success, responseXML, responseText) {
		var otherContexts_div = _ge('otherContexts_div');
		if (success) {
			var actual = {};
			var sets = responseXML.documentElement.getElementsByTagName('set');
			for (var t=0;t<sets.length;t++) {
				var id = sets[t].getAttribute('id');
				actual['contextDiv_set'+id] = 1;
				add_context_widget('set', id, deja_view_id);
			}
			
			var pools = responseXML.documentElement.getElementsByTagName('pool');
			for (var t=0;t<pools.length;t++) {
				var id = pools[t].getAttribute('id');
				actual['contextDiv_pool'+id] = 1;
				add_context_widget('pool', id, deja_view_id);
			}
			
			var exisiting_context_divs = document.getElementsByClass('contextDiv', 'DIV');
			for (var t=0;t<exisiting_context_divs.length;t++) {
				var div_id = exisiting_context_divs[t].id;
				if (actual[div_id] != 1) {
					
					//assume it is a set, just for fun
					var type = 'set';
					var temp = div_id.split(type);
					if (temp.length==1) { // then it must not be a set!
						type = 'pool';
						temp = div_id.split(type);
					}
					var id = temp[1];

					remove_context_widget(type, id);
				}
			}
			
			
		} else {
			//alert('deja_view_refresh failed to load contexts');
		}
		otherContexts_div.style.display = 'block';
	}
	if (deja_view_type == 'p') {
		flickrAPI.callMethod('flickr.photos.getInfo', {photo_id:deja_view_id}, caterina);
		flickrAPI.callMethod('flickr.photos.getAllContexts', {photo_id:deja_view_id}, caterina);
	} else {
		// not doing this for sets yet
	}
}
