///////////////////////////
//  ajax functions for tags
///////////////////////////
//url to post to
//c_id = content_id
//b_id = blog_id
//par_ele = parameters to pass...


function update_content_tags(url,val,c_id,b_id,par_ele,content_tp,content_id_tag_field)
{	
	var tagsToAttempt = Array();
			
	//get the array of new tags we want to run using ajax
	tagsToAttempt = screen_new_tags(content_id_tag_field, c_id);
		

	//no tags to try and add
	if(tagsToAttempt.count == 0) {
		return false;
	}	

	ele = par_ele;			
	
	YAHOO.util.Connect.asyncRequest(
		'GET',
		url + '?txtTags' + c_id + '=' + tagsToAttempt + '&content_id=' + c_id + '&blog_id=' + b_id + '&content_type=' + content_tp
	);

	//create the comma separated string
	tagStringToConcat = tagsToAttempt.join(',');
	
	//update the field with the new string piece
	update_tag_field('add', content_id_tag_field, tagStringToConcat);	

	
	update_content_tags_response(c_id,b_id,content_tp,content_id_tag_field);
	//update the mgt form		
	return true;
 }
 
 function strip_trailing_comma(the_string) {

 	len_the_string = (the_string.length-1);
 	 	
 	if(the_string.indexOf(' ')==0)  {
 		the_string =  the_string.substr(1,(the_string.length)); 	
 	} 	
 	
 	len_the_string = (the_string.length-1);
 	
	if(the_string.lastIndexOf(' ')==len_the_string)  {
 		the_string =  the_string.substr(0,(the_string.lastIndexOf(' '))); 	
 	} 	

 	len_the_string = (the_string.length-1);
 	
 	if(the_string.lastIndexOf(',')==len_the_string)  {
 		return the_string.substr(0,(the_string.lastIndexOf(',')));	
 	} else {
 		return the_string;
 	} 	
 }
 
 
function delete_content_tag(url,val, c_id, b_id,par_ele,content_tp,content_id_tag_field)  {

	//the array
	var tagArray = Array();
	
	//get a tag array
	tagArray = string_to_tag_array(YAHOO.util.Dom.get(content_id_tag_field).value);
	

	
	ele = par_ele;		
	
	YAHOO.util.Connect.asyncRequest(
		'GET',
		url + '?txtTags' + c_id + '=' + val + '&content_id=' + c_id + '&blog_id=' + b_id + '&content_type=' + content_tp
	);
	
	tagArray = tagArray.without(val);		
	
	
	//return the comma separated string
	tagStringToConcat = tagArray.join(',');
	
	

	
	//update the field with the new string piece	
	update_tag_field('delete', content_id_tag_field, val);
	

	
	update_content_tags_response(c_id,b_id,content_tp,content_id_tag_field);
	
	
	
	document.getElementById('tag_div'+ele).style.display='none';
	
	
	return true;
	
}

function update_content_tags_response(c_id,b_id,content_tp,content_id_tag_field) {
	var arrTagList = new Array();
	var del_string = '';
	var display_string = '';
	var x=1;
	var val='';
		
	
	tag_response = YAHOO.util.Dom.get(content_id_tag_field).value;
	
	if(tag_response == '') {			
			document.getElementById('content_tag_list_display' + ele).innerHTML = '';
			return false;
	}
	

	//split the response into an array
	arrTagList = tag_response.split(',');	
			
	
	
	del_string = '<div class="content_tag_delete_hdr">Delete Tags</div><table style="margin:5px 5px 5px 5px;" id="content_tag_form_table">';
	//alert('tag_delete_span' + ele);		
	
	for (var i=0; i<arrTagList.length;i++) {
		if(x==1)  {
			del_string = del_string.concat('<tr>');
		}		

		//if the value is longer than 17 chars, chop it off
		if(arrTagList[i].length>17)  {
			val = arrTagList[i].substring(0,17)+'...';	
		} else {
			val = arrTagList[i];
		}
		if(val.length>0) {			
			del_string = del_string.concat('<td align="right">'+val+ '</td><td><span class="trashcan10"><img style="cursor:hand;" onClick="delete_content_tag(\'/admin/content_tag_ajax.one?a=del\',\''+arrTagList[i]+'\', \''+c_id+'\', \''+b_id+'\', \''+c_id+'\',\''+content_tp+'\',document.getElementById(\'cur_tag_vals'+c_id+'\'));document.getElementById(\'tag_div'+c_id+'\').style.display=\'none\'; return false;" alt="Delete Tag" src="/resources/images/icons/icn_trash_10.gif" /></span></td>');			
			
			display_string = display_string.concat(val+', ');								
			
			if(x % 3 == 0)  {
				del_string = del_string.concat('</tr><tr>');	
			}
		}	
		x++;
		
		
		if(typeof document.getElementById('tag_delete_span' + ele)=='object' && document.getElementById('tag_delete_span' + ele))  {	
			document.getElementById('tag_delete_span' + ele).innerHTML = '';
			document.getElementById('tag_delete_span' + ele).innerHTML = del_string;		
		}					
		
		//write the response
		document.getElementById('content_tag_list_display' + ele).innerHTML = strip_trailing_comma(display_string);
		document.getElementById('txtTags' + ele).value = '';							
		
	}
	
	document.getElementById('tag_div'+ele).style.display='none';
	
	return false;
}


/******************************************************************************
* 	Turn a string into an array of valid tags
*
*	@param	tag_str		a string of tags
*  						ex:  tag1,tag2,tag3  => (tag1, tag2, tag3)
* 						tag1 tag2, tag3 => (tag1 tag2, tag3)
*						tagging and spaces? => (tagging, and, spaces)
*
*	@return Array()		array of valid tags
*******************************************************************************/

function string_to_tag_array(tag_str) {

	var tagArray = Array();
	
	//strip everything but alphanumeric, space and comma
	tag_str = tag_str.replace(/[^a-zA-Z ,0-9]+/g,'');
	tag_str = strip_trailing_comma(tag_str);	

	var commaPresent = tag_str.indexOf(',');

	//if a comma was present
	if(commaPresent != -1) {

		//strip all :
		tag_str = tag_str.gsub(':','');
		tag_str = tag_str.gsub('_','');

		//replace all spaces with _
		tag_str = tag_str.gsub(' ','_');

		//replace all commas with spaces
		tag_str = tag_str.gsub(',',' ');

		tagArray = tag_str.split(' ');
		//tagArray = tag_str.split();

		//loop through and turn _ back into spaces and trim whitespace
		tagArray.each(function(s, index) {
		  tagArray[index] = tagArray[index].gsub('_',' ').strip();
		}
		
		);		
	}
	

	//no comma present
	else {

		//split string by spaces and explode into an array
		tagArray = tag_str.split(' ');

		//loop through and turn _ back into spaces and trim whitespace
		tagArray.each(function(s, index) {
		  tagArray[index] = tagArray[index].strip();
		}
		);

	}

	//return the array
	return tagArray;

} //string to tag array

function screen_new_tags(content_id_tag_field, c_id) {

	newTagStr = String();
	
	newTagStr = YAHOO.util.Dom.get('txtTags' + c_id).value;

	

	//nothing in the form field, so return false right away
	if(newTagStr.length == 0) {
		return false;
	}

	//turn the form field into an Array
	var newTagArray = string_to_tag_array(newTagStr);

	//no new tags or no valid tags, so return false
	if(newTagArray.count < 1) {
		return false;
	}

	var existingTagStr = String();
	existingTagStr = YAHOO.util.Dom.get(content_id_tag_field).value;

	//turn the existing tag field into an array of tags
	var existingTagArray = string_to_tag_array(existingTagStr);


	var tagsToAdd = Array();

	//loop through new tags and see if we have them yet
	newTagArray.each(function(s, index) {

		var already_exists = existingTagArray.indexOf(s);

		//does not exist yet
		if(already_exists == -1) {
			

			tagsToAdd.push(s);
		}

		//already exists
		else {
			
		}
	}
	);

	return tagsToAdd;

}

/******************************************************************************
*	Add or delete tags from the hidden form field
*
*	@param	act					'delete' or 'add'
*	@param	content_id_div		the ID of the hidden field with existing tags
*	@param	my_var				string of a tag to delete or CSV tag list
*
******************************************************************************/
function update_tag_field(act, content_id_tag_field, my_var) {

	var existingTagStr = String();
	
	
	//the string value of the existing tag field
	existingTagStr = YAHOO.util.Dom.get(content_id_tag_field).value;

	//delete a single tag from the form field
	if(act == 'delete') {
		YAHOO.util.Dom.get(content_id_tag_field).value = strip_trailing_comma(remove_tag_from_tag_string(strip_trailing_comma(my_var), strip_trailing_comma(existingTagStr)));
	}
	//add new tags to hidden field
	else if(act == 'add') {

		//nothing exists in the field currently
		if(existingTagStr.length == 0) {
			//content_id_tag_field.value = my_var;
			content_id_tag_field.value = strip_trailing_comma(my_var);
		}		
		//we already have tag data
		else {
			my_var = my_var.strip(' ');			
		strip_trailing_comma(my_var);
			if(my_var.length!=0) {
				
				YAHOO.util.Dom.get(content_id_tag_field).value = strip_trailing_comma(existingTagStr) + ',' + strip_trailing_comma(my_var);
				
			} else {
				
				YAHOO.util.Dom.get(content_id_tag_field).value = strip_trailing_comma(existingTagStr);
			}	
		}
	}
}


function remove_tag_from_tag_string(tag_to_remove, tag_string) {

	//string was already empty, so nothing left to do
	if(tag_string.length == 0) {
		return '';
	}	
	
	//the array
	var tagArray = Array();

	//get a tag array
	tagArray = string_to_tag_array(strip_trailing_comma(tag_string));
	

	//remove the one tag
	tagArray = tagArray.without(strip_trailing_comma(tag_to_remove));	


	//return the comma separated string
	return tagArray.join(',');

}
