
function tag_item(item_id, tag_id, type, rate_prompt) {
	if( setSelectedTag(item_id, tag_id) ) {
		//send message to server to record results, on callback
		//update item_id_tag_sum_display
		var message = new Object();
		message.item_id = item_id;
		message.tag_id = tag_id;
		message.type = type;

		var callback = { 
			success: function(o) {
				var result = JSON.parse(o);
				updateTagDisplay(item_id, result, rate_prompt);
			}, 
			failure: function(o) {
				alert(o); 
			}
		};

		ds_remote_api_jq('tag_item', message, callback);
	}
}

function setSelectedTag(item_id, tag_id) {
	var $tag_selector = $('#' + item_id + '_tag_selector');

	if ($tag_selector.length > 0) {
		var $tags = $tag_selector.find('div.selected_tag,div.available_tag');
		$tags.each(function() {
			var id = $(this).attr('id');
			if (id == (item_id+'_'+tag_id+'_tagid')) {
				$(this).removeClass('available_tag').addClass('selected_tag');
			} else {
				$(this).removeClass('selected_tag').addClass('available_tag');
			}
		});
		return true;
	} 
	return false;
}

function updateTagDisplay(item_id, result_array, rate_prompt) {
	var tag_selector = document.getElementById(item_id + '_tag_sum_display');
	var ral = result_array.length;
	if ( ral > 0 ) {
		var dsp_string = "";
		var help_total = 0;
		for(var q=0; q<ral; q++) {
			dsp_string += "<span class='tag_count'>";
			dsp_string += result_array[q].tag;
			help_total += parseInt(result_array[q].count);
			dsp_string += " (" + result_array[q].count + ")";
			dsp_string += "</span>";
			if(q == ral-2) {
				dsp_string += ", and ";
			} else if(q == ral-1) {
				dsp_string += ""; //was period
			} else {
				dsp_string += ", ";
			}
		}
		//help_total + ( (help_total>1) ? " Members " : " Member " ) + 
		//+ rate_prompt +
		dsp_string = "Member Ratings:" +  " " + dsp_string;
		
		tag_selector.innerHTML = dsp_string;
		
		//if (tag_selector.className = 'whitebar_count_e') tag_selector.className = 'whitebar_count';
			tag_selector.style.display = 'block';
	}
}

function toggleShareThis(bookmarkid, imageid) {
	toggleView(bookmarkid);
	var imgid = document.getElementById(imageid);
	//this is not using the static server
	if ( imgid.className == 'share_this_closed' ) {
//		imgid.className = 'share_this_open'; //remove for now, but its used to change the down arrow image
	} else {
//		imgid.className = 'share_this_closed'; //remove for now, but its used to change the down arrow image
	}
}

function showSocialLink(url) {
	var message = new Object();
	message.log = 'social_click_log';
	message.url = url;
	ds_remote_api_jq('log_click', message, null);
	
	window.open(url,'social_link','width=800,height=600,resizable=yes');
}

function callback_exclude_instruction(id, name, value) {
	if ( value == 'Enter a comment here' || value == '' ) {
		return false;
	} else {
		return true;
	}
}

/* handles corners and other presentation on tag selector box */
$('document').ready(function() {
	$('div.tagging_tag_selector').each(function() {;
		var $this = $(this);
		$this.
			parents('div.tagging_tag_bar').
			css({background:'#f4f6e9'}); 
		$this.
			find('.whitebar_rate').
				css({background:'transparent'});
		$this.
			find('.tag').
			hover(
				function() { $(this).css({textDecoration:'underline'}); },
				function() { $(this).css({textDecoration:'none'}); }
			);
				
		/* $this.
			css({border:'0',background:'transparent'}).
			wrap('<div class="t"><div class="b"><div class="l"><div class="r"><div class="bl"><div class="br"><div class="tl"><div class="tr"></div></div></div></div></div></div></div></div>').
			after('<div style="clear:both; height:0;"></div>'); */

	});


});
