function initComments(){
	var botonopina = document.getElementById('botonopina');

	if(botonopina){
		var span = document.createElement('span');
		span.setAttribute('id', 'enlaceopina');
		span.className = 'buttonBg';

		var a = document.createElement('a');
		a.setAttribute('href', '#');
		a.className = 'button';
		a.style.color = '#6E6C6D';
		a.innerHTML = botonopina.innerHTML;

		span.appendChild(a);

		botonopina.parentNode.insertBefore(span, botonopina);
		botonopina.parentNode.removeChild(botonopina);

		if(document.getElementById('loginform')){
			document.getElementById('opina').style.display = 'none';
		}

		$(a).bind('click', showCommentsForm);
	}

	var comments = $('#commentslist li');

	if(comments.length == 0){
		return;
	}

	var pager = document.createElement('div');

	pager.setAttribute('id', 'commentsPager');


	var enlacePrevious = document.createElement('a');
	enlacePrevious.href="";
	var leftPage = document.createElement('img');
	leftPage.setAttribute('src', '/media/img/common/utils/comments_leftarrow.gif');
	enlacePrevious.appendChild(leftPage);	
	$(enlacePrevious).bind('click', function(e) {
		e.preventDefault();

		var comments = document.getElementById('commentslist').getElementsByTagName('li');
		var index;
	
		for (var i = 0; i < comments.length; i++){
			if(comments[i].style.display != 'none'){
				index = i;
				break;
			}
		}
	
		if(index > 0){
			index--;
			setComment(index);
		}		
	});

	var numbers = document.createElement('span');
	
	
	var enlaceNext = document.createElement('a');
	enlaceNext.href="";
	var rightPage = document.createElement('img');
	rightPage.setAttribute('src', '/media/img/common/utils/comments_rightarrow.gif');
	enlaceNext.appendChild(rightPage);	
	$(enlaceNext).bind('click', function(e) {

		e.preventDefault();
		var comments = document.getElementById('commentslist').getElementsByTagName('li');
		var index;
	
		for (var i = 0; i < comments.length; i++){
			if(comments[i].style.display != 'none'){
				index = i;
				break;
			}
		}
	
		if(index < comments.length - 1){
			index++;
			setComment(index);
		}		
		
	});

	pager.appendChild(enlacePrevious);
	pager.appendChild(numbers);
	pager.appendChild(enlaceNext);

	var header = document.getElementById('comentarios').getElementsByTagName('h4')[0];
	header.appendChild(pager);
	document.getElementById('relatedHolder').style.position = 'relative';
	setComment(0);
}

function setComment(index){
	var comments = document.getElementById('commentslist').getElementsByTagName('li');

	for (var i = 0; i < comments.length; i++){
		comments[i].style.display = 'none';
	}

	comments[index].style.display = 'block';

	var pager = document.getElementById('commentsPager');

	var imgs = pager.getElementsByTagName('img');

	if(index == 0){
		imgs[0].style.visibility = 'hidden';
	}
	else{
		imgs[0].style.visibility = 'visible';
	}

	if(index == comments.length - 1){
		imgs[1].style.visibility = 'hidden';
	}
	else{
		imgs[1].style.visibility = 'visible';
	}

	var numbers = pager.getElementsByTagName('span')[0];
	numbers.innerHTML = (index + 1)+'/'+comments.length;
	
	document.getElementById('relatedHolder').style.position = 'relative';
}

function setPreviousComment(){
	var comments = document.getElementById('commentslist').getElementsByTagName('li');
	var index;

	for (var i = 0; i < comments.length; i++){
		if(comments[i].style.display != 'none'){
			index = i;
			break;
		}
	}

	if(index > 0){
		index--;
		setComment(index);
	}
}

function setNextComment(){

	var comments = document.getElementById('commentslist').getElementsByTagName('li');
	var index;

	for (var i = 0; i < comments.length; i++){
		if(comments[i].style.display != 'none'){
			index = i;
			break;
		}
	}

	if(index < comments.length - 1){
		index++;
		setComment(index);
	}
}

function showCommentsForm(){
  document.getElementById('enlaceopina').style.display = 'none';
  document.getElementById('logFormHolder').style.display = 'block';

  if(document.getElementById('loginform')){
      var options = {
          beforeSubmit:  	checkForm,  // pre-submit callback
          success:       	catchLoginFormResponse  // post-submit callback
      };

    $('#loginform').ajaxForm(options);
  }
  else{
  	setCommentsForm();
  }
  
  if(PA_IE6){
	document.getElementById('relatedHolder').style.display = 'none';
	document.getElementById('relatedHolder').style.position = 'relative';
	setTimeout('refreshRelated()', 25);
  }
  return false;
}

function refreshRelated(){
	document.getElementById('relatedHolder').style.display = 'block';
	document.getElementById('relatedHolder').style.position = 'relative';
}

function setCommentsForm(){
	var options = {
		beforeSubmit:  	checkForm,  // pre-submit callback
		success:       	catchCommentsForm  // post-submit callback
	};

	$('#opinaform').ajaxForm(options);
}

function catchLoginFormResponse(xml){
  var p = xml.getElementsByTagName('p');

  var errorParagraph = null;
  var _className;

  for (var i = 0; i < p.length; i++){
    _className = p[i].getAttribute('class');
    if(_className == 'error'){
      errorParagraph = p[i];
    }
  }

  if(errorParagraph == null){
	document.getElementById('nologged').style.display = 'none';
    document.getElementById('opina').style.display = 'block';
	setCommentsForm();
	
	  if(PA_IE6){
		document.getElementById('relatedHolder').style.display = 'none';
		document.getElementById('relatedHolder').style.position = 'relative';
		setTimeout('refreshRelated()', 25);
	  }	
	
  }
  else{
	var currentform = document.getElementById('loginform');
	hideFormLoading(currentform);
	setError(currentform, null, errorText);
  }

  enableInputs();
}

function catchCommentsForm(){
	var form = document.getElementById('opinaform');
    p = form.getElementsByTagName('p')[0];
	p.innerHTML = document.getElementById('thankyoutext').value;
}

$().ready(initComments);