/**
* UGC Tag Handler object
*
*/
var OneUGCTagCollection = {
 	_modules : new Array(),
 	//the current content ID
 	_currentID : 0,
 	//get the id of the current user
 	_currentUID : 0,
 	
 	_nodeID: 0,
 
    /**
    * check for an instance of the object; if no instance create new one
    */
    toggleUGCTagFormView : function(id) {     
        if(id in this._modules) {              
   			this._modules[id].toggleUGCTagFormView();         
        }  else  {        
         	// id not found ~ instantiate new object
         	this._modules[id] = new OneUGCTag(id);         
   			this._modules[id].toggleUGCTagFormView();         
        }
    },
    toggleUGCTagViewAll : function(id) {     
        if(id in this._modules) {              
   			this._modules[id].toggleUGCTagViewAll();         
        }  else  {        
         	// id not found ~ instantiate new object
         	this._modules[id] = new OneUGCTag(id);         
   			this._modules[id].toggleUGCTagViewAll();         
        }
    },
 	//reset the form field value for the add new tag form
    clearFieldValue :  function(id)     {
     	if(id in this._modules) {       
         	this._modules[id].clearFieldValue();       
        }  else  {        
         	// id not found ~ instantiate new object
         	this._modules[id] = new OneUGCTag(id);
         	this._modules[id].clearFieldValue();
        }
    },
    //remove a tag from a piece of content
    deleteTag :  function(id,tid,uid)     {
     
     	//pass content ID to class var
     	this._currentID = id;
     	//pass user ID to class var
     	this._currentUID = uid;
     
     	if(id in this._modules) {       
         	this._modules[id].deleteTag(uid,tid);       
        }  else  {        
         	// id not found ~ instantiate new object
         	this._modules[id] = new OneUGCTag(id);
         	this._modules[id].deleteTag(uid,tid);
        }
    },
    //add a tag to a piece of content
    addTag : function(id,tag,uid, node_id)     {
     	//pass content ID to class var
     	this._currentID = id;
     	//pass user ID to class var
     	this._currentUID = uid;
     	
     	this._nodeID = node_id;
     	
     	tag = tag.replace("&", "%26");
     	
     	if(id in this._modules) {       
         	this._modules[id].addTag(uid,tag);
       
        }  else  {        
         	// id not found ~ instantiate new object
         	this._modules[id] = new OneUGCTag(id);
         	this._modules[id].addTag(uid,tag);
        }
    },
    /**
 	* Load the tag listing
 	*/
 	loadTagListing : function(svcResponse)  {    		
 		
  		//get the content id
  		id = svcResponse[0]['content_id'];  
  		
  		//hide the tag form after the submission
  		//must re-reference the main OneUGCTagCollection
  		//because object state has been reset
  		if(id in OneUGCTagCollection._modules) {       
			OneUGCTagCollection._modules[id].hideTagForm();       
        }  else  {                 
         	// id not found ~ instantiate new object
         	OneUGCTagCollection._modules[id] = new OneUGCTag(id);
         	OneUGCTagCollection._modules[id].hideTagForm();
        }  
  		//check the response code to ensure that errors did not occur 
  		if(svcResponse['faultcode'] == undefined)  {   
   			//if the total is more than 5 add a view all link   
   			if(svcResponse['total'] > 5)  {
    			document.getElementById('more_tags_link'+id).innerHTML = '<a href="javascript:OneUGCTagCollection.toggleUGCTagViewAll('+id+');">View More</a>';
   			} else {
    			document.getElementById('more_tags_link'+id).innerHTML = '';
   			}
   
			//reset the tag listing div
   			document.getElementById(id).innerHTML = '';  
   			//initialize response string
   			response_string = '';
   
   			//initialize view all hidden variable to zero ...   
   			view_all_hidden = 0;
   
   			//loop through the response
   			for (var i = 0; i < svcResponse.length; i++)  {      
    			//make sure the tag is actually defined before building display
    			//if all tags were deleted, tag will be undefined
    			if(svcResponse[i]['tag'])  {     
     				if(i > svcResponse[0]['default_view'] && view_all_hidden == 0)  {
      					response_string += '<span id="view_all_tags_span'+id+'" class="view_all_tags_span">';
      					view_all_hidden = 1;
     				}          
     	
     				response_string += ' <span class="normal_tag_style"><a href="' + svcResponse[i]['tag_link'] + '">' + svcResponse[i]['tag'] + '</a></span>';   
     				//check if current user is owner ... if so, give delete links
     				if(OneUGCTagCollection._currentUID==svcResponse[i]['owner_id'])  {
      					response_string += ' <span class="tag_span_style">[<a href="javascript:OneUGCTagCollection.deleteTag('+id+','+svcResponse[i]['tag_id']+','+OneUGCTagCollection._currentUID+');">x</a>]</span> ';
     				} else {
     					//not the owner ~ add commas
     					if((i+1) < svcResponse.length)  {
     						response_string += ',';
     					}	
     				}
    			}
   			}
     
   			//check to see if the view all hidden flag was set ...   
   			if(view_all_hidden==1)  {
    			//if so, close the view_all_tags_span
    			response_string += '</span>';
    
    			document.getElementById(id).innerHTML = response_string;    
    			//hide the tags in the view all tag span
    			OneUGCTagCollection._modules[id].showViewAll();
   			} else {
    			document.getElementById(id).innerHTML = response_string;     
   			}
  		} else {
   			alert("Errors occurred loading the updated tag listing.");
  		}
 	}
}

/**
* Instance version � used with new operator
* This function will be used when  "new OneUGCTag " is called
* and creates an instance
*
*/
function OneUGCTag(id) {
 	this._id = id;
 	this._formState = 'collapsed';
 	this._viewAllState = 'collapsed';
 
 	this._formDiv = document.getElementById('manage_tags_div'+this._id);
 	this._formField = document.getElementById('txtTag'+this._id);
}

/**
* toggle the hide/show on the tag form field
*/
OneUGCTag.prototype.toggleUGCTagFormView = function() {
	switch(this._formState) { 
		case 'expanded':
			this.hideTagForm();
			break;
		case 'collapsed':
			this.showTagForm();
			break;
	}
}

/**
* show the tag form field
*/
OneUGCTag.prototype.showTagForm = function() { 
 
	//set the display on the div
	this._formDiv.style.display = 'block'; 

	//set the state of the div
	this._formState = 'expanded';
}

/**
* hide the tag form field
*/
OneUGCTag.prototype.hideTagForm = function() { 
	//set the form field values
	this._formField.value = 'Use commas to separate tags';
	this._formField.style.textAlign = 'center';
	this._formField.style.color = '#696969';  

	//set the display on the div
	this._formDiv.style.display = 'none'; 

	//set the state of the div
	this._formState = 'collapsed';
}

/**
* toggle the ability to view all of the tags; or just 5
*/
OneUGCTag.prototype.toggleUGCTagViewAll = function()  { 
	switch(this._viewAllState) { 
		case 'expanded':
			this.hideViewAll();
			break;
		case 'collapsed':      
			this.showViewAll();
			break;
	}
}

/**
* show all of the tags
*/
OneUGCTag.prototype.showViewAll = function() { 
	//set the display on the div
	document.getElementById('view_all_tags_span'+this._id).style.display = 'inline'; 
	document.getElementById('more_tags_link'+this._id).innerHTML = '<a href="javascript:OneUGCTagCollection.toggleUGCTagViewAll('+this._id+');">Hide More</a>';
	//set the state of the div
	this._viewAllState = 'expanded';
}

/**
* hide all but 5 tags
*/
OneUGCTag.prototype.hideViewAll = function() { 
	//set the display on the div 
	document.getElementById('view_all_tags_span'+this._id).style.display = 'none'; 
	document.getElementById('more_tags_link'+this._id).innerHTML = '<a href="javascript:OneUGCTagCollection.toggleUGCTagViewAll('+this._id+');">View More</a>';
	//set the state of the div
	this._viewAllState = 'collapsed';
}

/**
* on field focus, reset the values of hte field
*/
OneUGCTag.prototype.clearFieldValue = function()  {
	if (this._formField.value != 'Use commas to separate tags') {
		return;
	}
	
 	//set the form field values
 	this._formField.value = '';
 	this._formField.style.textAlign = 'left';
 	this._formField.style.color = '#000000'; 
}

/**
* remove a tag from a piece of content
*/
OneUGCTag.prototype.deleteTag = function(uid,tid)  {
	//Initialize the array
	var params = new Array();

	//This section lists all the required params
	params['responseFunction'] = this.tagServiceResponseHandler;  
	params['action'] = 'removeTagFromContent';
	params['responseFormat'] = 'json';
	params['responseType'] = 'JSON';        
	params['handlerName'] = 'svcTag';
	params['requestType'] = 'service';    

	//These are all the user defined params
	params['contentID'] = this._id;    
	params['tagID'] = tid; 
	params['userID'] = uid;

	//The actual call
	OneAjax.request(params);
}

/**
* add a tag from a piece of content
*/
OneUGCTag.prototype.addTag = function(uid, tag)  {
	//Initialize the array
	var params = new Array();
	
	//This section lists all the required params
	params['responseFunction'] = this.tagServiceResponseHandler;  
	params['action'] = 'addTag';
	params['responseFormat'] = 'json';
	params['responseType'] = 'JSON';    
	params['handlerName'] = 'svcTag';
	params['requestType'] = 'service';
	
	//These are all the user defined params
	params['contentID'] = this._id;  
	params['tag'] = tag;     
	params['userID'] = uid;
	params['nodeID'] = OneUGCTagCollection._nodeID;
	
	//The actual call
	OneAjax.request(params);
}

/**
* Handle response from tag service;I.E.: reload tag list
*   svcResponse ~ response from service call
*/
OneUGCTag.prototype.tagServiceResponseHandler = function(svcResponse) 
{		
	if(svcResponse['faultcode'] == undefined)  {  
		//Initialize the array
		var params = new Array();

		//This section lists all the required params
		params['responseFunction'] = OneUGCTagCollection.loadTagListing;  
		params['action'] = 'getTagsForContent';     
		params['handlerName'] = 'svcTag';
		params['requestType'] = 'service';
		
		//These are all the user defined params
		params['contentID'] = OneUGCTagCollection._currentID;   
		params['perPage'] = 100;     
		params['responseFormat'] = 'json';
		
		//The actual call
		OneAjax.request(params);
	} else {
		alert("Errors occurred processing your request.  Please try again.");
	} 
}

/**
* Allows tags to be submitted when enter key is pressed.
*
*/
function enterPressed(e, content_id, tag, uid, node_id)
{
	var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
	
    if (keycode == 13) {
		OneUGCTagCollection.addTag(content_id, tag, uid, node_id);
		return false;
	}
}