// button actions for photo

var decorate_photo_gne_button = function(args) {
	var EL = args[0];
	EL.EXT = new Photo_Gne_button_EXT(args[0], args[1], args[2], args[3]);
	return EL;
}

Photo_Gne_button_EXT = function(el, p_id, isOff, base_name_off) {
	var EL = this.EL = el; var EXT = this;
	var base_name = EXT.base_name = el.id.split(el.className+'_').join(''); // get the base for building URLS from the id minus the className
	EXT.lock_down = 0;
	EXT.p_id = p_id;

	if (base_name=='add_note' && 0 ) {
		EL.style.display = 'none';
		return;
	}

	EXT.on = new Image(); EXT.on.src = '/images/'+base_name+'_grey.gif';
	EXT.on_over = new Image(); EXT.on_over.src = '/images/'+base_name+'_color.gif';
	EXT.on_down = new Image(); EXT.on_down.src = '/images/'+base_name+'_color_down.gif';
	if (base_name=='send_to_group' || base_name=='send_to_set' || base_name=='print' || base_name=='blog_this') {
		EXT.on_locked = new Image(); EXT.on_locked.src = '/images/'+base_name+'_color_locked.gif';
	}

	base_name_off = (base_name_off) ? base_name_off : base_name;

	EXT.off = new Image(); EXT.off.src = '/images/'+base_name_off+'_grey.gif';
	EXT.off_over = new Image(); EXT.off_over.src = '/images/'+base_name_off+'_color.gif';
	EXT.off_down = new Image(); EXT.off_down.src = '/images/'+base_name_off+'_color_down.gif';

	EXT.isOver = EXT.isDown = 0;
	EXT.isOff = (isOff) ? 1 : 0;

	EL.style.cursor = (document.all) ? 'hand' : 'pointer';

	EL.onmouseover = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_editable_div_action) return;
		this.EXT.isOver = 1;
		this.EXT.change_src();
	}
	EL.onmouseout = function() {
		this.EXT.isOver = 0;
		if (!this.EXT.lock_down) this.EXT.isDown = 0;
		this.EXT.change_src();
	}
	EL.onmousedown = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_editable_div_action) return;
		if (this.EXT.disabled) {
			this.EXT.enable_others();
			this.EXT.enable_self();
		}
		this.EXT.isDown = 1;
		this.EXT.isOver = 1;
		this.EXT.change_src();
		var photo_notes = _ge('photo_notes');
		if (this.EXT.base_name != 'rotate' && photo_notes && photo_notes.EXT) photo_notes.EXT.hide_rotate_div();
		if (this.EXT.base_name != 'print' && printDialogDiv) hide_printDialog();
		if (this.EXT.base_name != 'send_to_set' && sendToSetDialogDiv) hide_sendToSetDialog();
		if (this.EXT.base_name != 'send_to_group' && sendToGroupDialogDiv) hide_sendToGroupDialog();
		if (this.EXT.base_name != 'blog_this' && sendToBlogDialogDiv) hide_sendToBlogDialog();
	}
	EL.onmouseup = function() {
		if (window.should_I_ignore_stuff_because_note_editing || window.should_I_ignore_stuff_because_of_editable_div_action) return;
	
		if (this.EXT.disabled) return;
		var what_to_do = this.EXT[this.EXT.base_name+'_action']();
	
		this.EXT.isOver = 1;
		if (what_to_do==1) { // lock it down
			this.EXT.isDown = 1;
			this.EXT.lock_down = 1;
		} else {
			this.EXT.isDown = 0;
		}
		this.EXT.change_src();
	}
	
	this.disable_others = function() {
		var all = document.getElementsByClass('photo_gne_button', 'IMG');
		for (var i=0; i<all.length; i++) {
			if (all[i].EXT.base_name != EXT.base_name) all[i].EXT.disable_self();
		}
		var photo_notes = _ge('photo_notes');
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.take_her_away('disable_others');
		window.should_I_ignore_stuff_because_of_button_action = 1;
	}
	this.enable_others = function() {
		var all = document.getElementsByClass('photo_gne_button', 'IMG');
		for (var i=0; i<all.length; i++) {
			if (all[i].EXT.base_name != EXT.base_name) all[i].EXT.enable_self();
		}
		window.should_I_ignore_stuff_because_of_button_action = 0;
	}
	this.disable_self = function() {
		EXT.disabled = 1;
		EL.style.cursor = 'default';
	}
	this.enable_self = function() {
		EXT.disabled = 0;
		EL.style.cursor = (document.all) ? 'hand' : 'pointer';
	}
	this.change_src = function() {
		if (EXT.disabled) return;
		if (EXT.lock_down) {
			EL.src = EXT.on_locked.src;
		} else if (EXT.isOff) {
			if (EXT.isDown) {
				EL.src = EXT.off_down.src;
			} else if (EXT.isOver) {
				EL.src = EXT.off_over.src;
			} else {
				EL.src = EXT.off.src;
			}
		} else {
			if (EXT.isDown) {
				EL.src = EXT.on_down.src;
			} else if (EXT.isOver) {
				EL.src = EXT.on_over.src;
			} else {
				EL.src = EXT.on.src;
			}
		}
	}
	// DELETE
	
	this.delete_action = function() {
		setTimeout("if (confirm('Are you sure you want to delete this photo? (This cannot be undone.)'))  document.location = global_photos['"+EXT.p_id+"'].deleteUrl;",50);
		return 0;
	}
	
	// ROTATE
	
	this.rotate_action = function() {
		var photo_notes = _ge('photo_notes');
		if (photo_notes && photo_notes.EXT) {
			EXT.disable_others();
			photo_notes.EXT.show_rotate_div(EL);
		}
		return 0;
	}
	
	// SEND TO SET
	
	this.send_to_set_action = function() {
		if (_ok_for_scrumjax_xml()) {
			if (EXT.lock_down) {
				hide_sendToSetDialog();
				return 0;
			} else {
				show_sendToSetDialog(EXT.p_id);
				return 1;
			}
		} else {
			document.location = '/photo_sendto_set.gne?id='+EXT.p_id;
		}
		return 0;
	}
	
	// PRINT
	
	this.print_action = function() {
		if (_ok_for_scrumjax_xml()) {
			if (EXT.lock_down) {
				hide_printDialog();
				return 0;
			} else {
				show_printDialog(EXT.p_id);
				return 1;
			}
		} else {
			document.location = '/photo_print.gne?id='+EXT.p_id;
		}
		return 0;
	}
	
	// SEND TO GROUP
	
	this.send_to_group_action = function() {
		if (_ok_for_scrumjax_xml()) {
			if (EXT.lock_down) {
				hide_sendToGroupDialog();
				return 0;
			} else {
				show_sendToGroupDialog(EXT.p_id);
				return 1;
			}
		} else {
			document.location = '/photo_sendto_group.gne?id='+EXT.p_id;
		}
		return 0;
	}
	
	// BLOG THIS NOTE
	
	this.blog_this_action = function() {
		if (_ok_for_scrumjax_xml()) {
			if (EXT.lock_down) {
				hide_sendToBlogDialog();
				return 0;
			} else {
				show_sendToBlogDialog(EXT.p_id);
				return 1;
			}
		} else {
			document.location = '/blog.gne?photo='+EXT.p_id;
		}
		return 0;
	}
	
	// ADD NOTE
	
	this.add_note_action = function() {
		var photo_notes = _ge('photo_notes');
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.start_adding_note();
		return 0;
	}
	
	// View Original Photo
	
	this.original_action = function(){
		var p = global_photos[EXT.p_id];
		var r= window.open("/viewatt?attid="+p.id+"&disp=inline&attkey="+p.attkey,"_blank");
		if(!r)
			alert('弹出窗口被拦截，请关闭浏览器的弹出窗口拦截功能。');
		return 0;
	}
	
	// Download the Original Photo
	
	this.download_action = function(){
		var p = global_photos[EXT.p_id];
		var r= window.open("/viewatt?attid="+p.id+"&disp=att&attkey="+p.attkey,"_self");
		if(!r)
			alert('弹出窗口被拦截，请关闭浏览器的弹出窗口拦截功能。');
		return 0;
	};

	this.photo_manage_action = function(){
	    var p = global_photos[EXT.p_id];
	    new photo_admin(p.id);
	    return 0;
	};
	
	// ZOOM
	
	this.zoom_action = function() {
		document.location = global_photos[EXT.p_id].zoomUrl;
		return 0;
	}
	
	// ADD/REMOVE FROM FAVES
	
	this.add_to_faves_action = function() {
		var photo_notes = _ge('photo_notes');
		var fave_form = _ge('fave_form');
		if (EXT.isOff) {
			if (_ok_for_scrumjax()) {
				MyAPI.callMethod('My.favorites.remove', {photo_id:EXT.p_id}, EXT);
				if (photo_notes && photo_notes.EXT) photo_notes.EXT.start_comming('Removing from favorites');
			} else {
				if (fave_form) {
					fave_form.action = page_current_url;
					fave_form.faveremove.value = 1;
					fave_form.submit();
				}
			}
		} else {
			if (_ok_for_scrumjax()) {
				MyAPI.callMethod('My.favorites.add', {photo_id:EXT.p_id}, EXT);
				if (photo_notes && photo_notes.EXT) photo_notes.EXT.start_comming('Adding to favorites');
			} else {
				if (fave_form) {
					fave_form.action = page_current_url;
					fave_form.faveadd.value = 1;
					fave_form.submit();
				}
			}
		}
		return 0;
	}
	
	this.My_favorites_add_onLoad = function(success, responseXML, responseText) {
		var photo_notes = _ge('photo_notes');
		EXT.isOff = !EXT.isOff;
		EXT.change_src();
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
	}
	this.My_favorites_remove_onLoad = function(success, responseXML, responseText) {
		var photo_notes = _ge('photo_notes');
		EXT.isOff = !EXT.isOff;
		EXT.change_src();
		if (photo_notes && photo_notes.EXT) photo_notes.EXT.stop_comming(1);
	}

	if (EXT.isOff) EXT.change_src();
}
