	function CommentAttachment(objectID)
	{
	  this.uploaderMode = 'commentAttachment';
	  this.objectID 	= objectID;
      this.txtYouTube  	= '#txtYouTube_item'+objectID;
      this.txtLink   	= '#txtLink_item'+objectID;
      this.txtImage   	= '#txtFile_item'+objectID;
      this.selectedModeObject = '#selectedMode_item'+objectID;
      this.storedValueObject  = '#storedValue_item'+objectID;

      this.uploadContainerObject  = '#uploadContainer_item'+objectID;
      this.answerContainerObject  = '#answerContainer_item'+objectID;


      this.modeIconObject 			= '#modeIcon_item'+objectID;
      this.attachmentContainerObject = '#attachmentContainer_item'+objectID;
      this.attachedMediaTitleObject  = '#attachedMediaTitle_item'+objectID;
	}	

    CommentAttachment.prototype.initContainer = function()
	{
	   // hideUploader(this.objectID);
		$(this.txtLink).val('');
		$(this.txtYouTube).val('');
		$(this.txtImage).val('');
		$(this.selectedModeObject).val(0);
		$(this.storedValueObject).val('');
		this.switchContainerTabs('tab1');
		this.changeIcons('none');
	}
    
    CommentAttachment.prototype.switchContainerTabs = function(tabID)
    {
      var tab1 = '#tab1_item'+this.objectID;
      var tab2 = '#tab2_item'+this.objectID;
      var tab3 = '#tab3_item'+this.objectID;
      var selectedTab = '#'+tabID+'_item'+this.objectID;
      var containerMode = $(this.storedValueObject).val();

      switch(tabID)
      {
        case 'tab1': 
        {
          if($(tab2).css('display') == 'block') $(tab2).hide();
          if ($('#'+'tab2Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab2Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab2Switcher_item'+this.objectID).addClass("medItem");
          }
          
          if($(tab3).css('display') == 'block') $(tab3).hide();
          if ($('#'+'tab3Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab3Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab3Switcher_item'+this.objectID).addClass("medItem");
          }
          
          $('#selectedMode_item'+this.objectID).val('1');

          break;
        } 

        case 'tab2': 
        {
          if($(tab1).css('display') == 'block') $(tab1).hide();
          if ($('#'+'tab1Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab1Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab1Switcher_item'+this.objectID).addClass("medItem");
          }
          
          if($(tab3).css('display') == 'block') $(tab3).hide();
          if ($('#'+'tab3Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab3Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab3Switcher_item'+this.objectID).addClass("medItem");
          }

          $('#selectedMode_item'+this.objectID).val('2');
          
          break;
        } 

        case 'tab3': 
        {
          if($(tab1).css('display') == 'block') $(tab1).hide();
          if ($('#'+'tab1Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab1Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab1Switcher_item'+this.objectID).addClass("medItem");
          }
          if($(tab2).css('display') == 'block') $(tab2).hide();
          if ($('#'+'tab2Switcher_item'+this.objectID).is (".medItemOn"))
          {
            $('#'+'tab2Switcher_item'+this.objectID).removeClass("medItemOn");
              $('#'+'tab2Switcher_item'+this.objectID).addClass("medItem");
          }
          
          $('#selectedMode_item'+this.objectID).val('3');
          
          break;
        } 
        
      }
      $('#'+tabID+'Switcher_item'+this.objectID).removeClass("medItem");
      $('#'+tabID+'Switcher_item'+this.objectID).addClass("medItemOn");
      if($(selectedTab).css('display') == 'none') $(selectedTab).show();
  
    }
	
	CommentAttachment.prototype.changeIcons = function (mediaType)
	{
      switch (mediaType) {
	  	case 'image':{
	  		if (!$(this.modeIconObject).is(".link_image")) {
	  			$(this.modeIconObject).removeClass("link_none");
	  			$(this.modeIconObject).removeClass("link_tubem");
	  			$(this.modeIconObject).removeClass("link_see");
	  			$(this.modeIconObject).addClass("link_image");
	  		}
	  		break;
	  	}
	  	case 'youtube':{
	  	
	  		if (!$(this.modeIconObject).is(".link_tubem")) {
	  			$(this.modeIconObject).removeClass("link_none");
	  			$(this.modeIconObject).removeClass("link_image");
	  			$(this.modeIconObject).removeClass("link_see");
	  			$(this.modeIconObject).addClass("link_tubem");
	  		}
	  		break;
	  	}
	  	case 'link':{
	  	
	  		if (!$(this.modeIconObject).is(".link_see")) {
	  			$(this.modeIconObject).removeClass("link_none");
	  			$(this.modeIconObject).removeClass("link_image");
	  			$(this.modeIconObject).removeClass("link_tubem");
	  			$(this.modeIconObject).addClass("link_see");
	  		}
	  		break;
	  	}
	  	case 'none':{
	  	
	  		if (!$(this.modeIconObject).is(".link_none")) {
	  			$(this.modeIconObject).removeClass("link_see");
	  			$(this.modeIconObject).removeClass("link_image");
	  			$(this.modeIconObject).removeClass("link_tubem");
	  			$(this.modeIconObject).addClass("link_none");
	  		}
	  		break;
	  	}
	  }
		if($(this.storedValueObject).val() == '')
		{
			$(this.attachmentContainerObject).css("display","none");
			$(this.attachedMediaTitleObject).html('');
		}else
		{
			$(this.attachmentContainerObject).css("display","block");
			$(this.attachedMediaTitleObject).html($(this.storedValueObject).val());
			
		}


	}
    
    CommentAttachment.prototype.addMedia = function(mediaType)
    {
      var containerMode = $(this.storedValueObject).val();
      switch(mediaType)
      {
        case 'image' : 
        {
		  if(this.validateFileName()) this.ajaxFileUpload();
          break;
        }       
        case 'youtube' : 
        {
          var seValueText = $(this.txtYouTube).val();
          if(seValueText != '')
          {
            $(this.storedValueObject).val(seValueText);
			$(this.txtLink).val('');
            this.switchContainerTabs(2);
			this.changeIcons(mediaType);
			$(this.selectedModeObject).val('2');
          }
          break;
        }       
        case 'link' : 
        {
          var seValueText = $(this.txtLink).val();
          if(seValueText != '')
          {
		  	$(this.txtYouTube).val('')
            $(this.storedValueObject).val(seValueText);
            this.switchContainerTabs(3);
			this.changeIcons(mediaType);
			$(this.selectedModeObject).val('3');

          }
          break;
        } 
        
      }
      
    }
	
    CommentAttachment.prototype.deleteMedia = function()
    {
	  switch(this.getMode())
      {
        case '1' : 
        {
		  this.deleteImageAJAX();
		  this.initContainer();
          break;
        }       
        case '2' : 
        {
		  this.initContainer();
          break;
        }       
        case '3' : 
        {
		  this.initContainer();
          break;
        } 
      }
    }
	
    CommentAttachment.prototype.hideUploader = function()
    {
      $(this.uploadContainerObject).hide();
      $(this.answerContainerObject).show();
    }

    CommentAttachment.prototype.showUploader = function()
    {
      $(this.uploadContainerObject).show();
      $(this.answerContainerObject).hide();
    }
   		
	CommentAttachment.prototype.validateFileName = function()
	{
		var uploadControl = "txtFile_item"+this.objectID;
		var  fileName = document.getElementById(uploadControl).value.toLowerCase();
	    if (fileName.length < 1)
	    {
	        showErrorMessage('Please choose an image from your computer first.');
	    }
	    else
	    {
	        fileTypes = ['jpeg', 'gif', 'jpg', 'png'];
	        dots = fileName.split(".");
	        fileType = '.' + dots[dots.length - 1];
	        if (fileTypes.join('.').indexOf(fileType) == -1)
	        {
		        showErrorMessage('You can only upload images of type gif, jpg, or png.');
	            var ctrUpl = '#'+uploadControl;
				$(ctrUpl).val('');
	        }
			return true;
	    }
	}
	
	CommentAttachment.prototype.getAttachment = function()
	{
		return $(this.storedValueObject).val();
	}	

	CommentAttachment.prototype.getMode = function()
	{
		return $(this.selectedModeObject).val();
	}	



	CommentAttachment.prototype.fileUploadedSuccesfull = function()
	{
		$(this.txtImage).val('');
	    $(this.selectedModeObject).val(1);
		this.switchContainerTabs(1);
		this.changeIcons('image');
	}


	CommentAttachment.prototype.ajaxFileUpload = function()
	{
		$("#loading_message").center();
		$("#loading_message")
		.ajaxStart(function(){ $(this).show();})
		.ajaxComplete(function(){ $(this).hide(); });

		var uploadControl = 'txtFile_item'+this.objectID;
		var storedValueObject = this.storedValueObject;
		$.ajaxFileUpload
		(
			{
				url:this.rootUrl+'wsdl/ajaxfileupload.php?action=doUpload&mode='+this.uploaderMode+'&control='+uploadControl,
				secureuri:false,
				fileElementId:uploadControl,
				dataType: 'json',
				object: this,
				
				
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							showErrorMessage(data.error);
							return false;
						}else
						{

							if(data.fileName != '')
							{
								$(storedValueObject).val(data.fileName);
								this.object.fileUploadedSuccesfull();
								return true;
							}else
							{
								showErrorMessage('File could not be uploaded.');
								return false;
							}
						}
					}
					
				},

				error: function (data, status, e)
				{
					showErrorMessage('File could not be uploaded.');
//					showErrorMessage('File could not be uploaded.');

					return false;
				}
			}
		)
		return false;
	}
	
	CommentAttachment.prototype.deleteImageAJAX = function()
	{
		var uplFileName = this.getAttachment();
		if(uplFileName == '')
		{
			return false;
		}
		$.post(this.rootUrl+'wsdl/ajaxfileupload.php?action=doDelete&mode='+this.uploaderMode+'&control=none&fileName='+uplFileName, 
				{ action: 'doDelete', fileName: uplFileName }, 
		function(response)
		{ 
			if (response.error == '')
			{
			} 
		}, "json");

	}

	function showErrorMessage(errorText)
	{
      	document.getElementById('error_message_text').innerHTML = errorText;
		$('#error_message').center();
		$('#error_message').show();
		$('#loading_message').hide();
	}
	
    $(document).ready(function(){
		$('#closeLoadingWindow').click(function(){
			$('#loading_message').hide();
		});      
		$('#closeErrorWindow').click(function(){
			$('#error_message').hide();
		});      

    });

