var comment_manager = new function(){
		
	//this.req = null;
	
	//this.url = null;

    this.init = function(){
		
		if( document.getElementById('comment_section' ) ) {
		
			var $p = document.getElementById( 'comment_section' ).getElementsByTagName('p');
			
			for( $x = 0; $x < $p.length; $x++ ){
				
				if( $p[$x].getAttribute( 'class' ) == 'comment_anchors' ) {
					
				    $p[$x].onclick = function(){ 
					
						comment_manager.toggleCommentVisibility( this );			
					
					}
					
				}

			}

			var $divs = document.getElementsByTagName('div');
            
		    for( $x = 0; $x < $divs.length; $x++ ){
			
				if( $divs[$x].getAttribute( 'class' ) == 'comment_smilie_box' ) {
				
					comment_manager.cloneMainSmilieDiv( $divs[$x] );
					
				}
			
			}
			
			if( document.getElementById('main_smilie_comment_box') ) {
			
				comment_manager.hookImgs( document.getElementById('main_smilie_comment_box') );
			
			}
			
		}
	
	}
	
	this.cloneMainSmilieDiv = function( $div ){
		
		if( document.getElementById('main_smilie_comment_box') ){
            
			var $smilie_box = document.getElementById('main_smilie_comment_box') ;
		    
			var $clone = $smilie_box.cloneNode( true );
			
			$clone.removeAttribute( 'id' );
			
			if( $clone ) {
				
				comment_manager.hookImgs( $clone );
			
				var $t = $div.parentNode.replaceChild( $clone, $div );
			
			}
	  
		}
	
	}
	
	this.hookImgs = function( $div ) {
		
		var $imgs = $div.getElementsByTagName( 'img' );
		
		for( $x = 0; $x < $imgs.length; $x++ ) {
			
			$imgs[$x].onclick = function(){
				
				// alert( this.parentNode.nodeName );
				
				var $form = comment_manager.getForm(  this );
				
				var $textarea = $form.getElementsByTagName('textarea');
				
				if( $textarea[0] ){
				
					$textarea[0].value +=  ' ' + this.getAttribute( 'alt' ) + ' ';
					
					//PUTS INSERTION POINTER AT END OF TEXT IN TEXTAREA					
					if( $textarea[0].setSelectionRange ) {
						
						$textarea[0].setSelectionRange( $textarea[0].value.length, $textarea[0].value.length );
					
					}
					
					$textarea[0].focus();
		
				}
		
			}
		
		}
	
	}

    this.getForm = function( $img ){
		
		var $parent = $img.parentNode;
		
		while( $parent && $parent.nodeName != 'FORM' ){
			
			var $parent = $parent.parentNode;			
			
		}
		
		return $parent;
		
	}

	this.toggleCommentVisibility = function( $parent_tag ){
        
        var $span_tag = $parent_tag.getElementsByTagName('span');
        
		var elem, vis;
		
		if( document.getElementById ) { // this is the way the standards work
		
			elem = document.getElementById( $span_tag[0].getAttribute('title')  );
		
		} else if( document.all ) {// this is the way old msie versions work
		
			elem = document.all[$span_tag[0].getAttribute('title') ];
		
		} else if( document.layers ) { // this is the way nn4 works
		
			elem = document.layers[$span_tag[0].getAttribute('title') ];
		
		}

        if( elem ) {
			
			vis = elem.style;
		
			// if the style.display value is blank we try to figure it out here
			if( vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined ){
		
				vis.display = ( elem.offsetWidth != 0 && elem.offsetHeight != 0 ) ? 'block' : 'none';
		
			}
		
			vis.display = ( vis.display=='' || vis.display=='block' ) ? 'none' : 'block';
		
		}
		
	}

	this.smilie = function( $thesmilie,$thetxtbox ) {

         // inserts smilie text
		var oField = document.getElementById($thetxtbox); //get reference to the textarea
		
		var thesmilie = " " + thesmilie + " ";  //make the first half of the string, which includes the substring of the textarea starting at the first character and ending where the insertion point is at
		
		oField.value += thesmilie;
		
		oField.focus();
	
	}


}


