
var LGSGoReaderPopup;
var LGSGoReaderPopupWindowAttributes  =
    'top=' + 100 + ', ' +
    'left='+ 100 + ', ' +
    'width=' + 423 + ', ' +
    'height='+ 525  ;


function popupGoReader(preload) {
	popupLGSGoReader('comment.php?pre='+preload);
}
function popupGoReader_noexport(preload) {
	popupLGSGoReader('read.php?pre='+preload);
}

function popupLGSGoReader(s) {
try {
var pageTracker = _gat._getTracker("UA-5477436-4");
pageTracker._trackPageview();
} catch(err) {}

  LGSGoReaderPopup =
	  window.open("/LGS/editor2/"+s, "popup", LGSGoReaderPopupWindowAttributes);

  if( typeof LGSGoReaderPopup !='undefined' && LGSGoReaderPopup )
    LGSGoReaderPopup.parentWindow = window;
  else
    alert('Popup is missing');

  if(window.receiveCommentQipu)
    LGSGoReaderPopup.callback = window.receiveCommentQipu;
  else
    alert('receiveCommentQipu not found.');

//if(typeof LGSGoReaderPopup.callback !='function')
//  alert('callback insertion failed;');

}

function popupLGSGoReader2(s) {
	try {
		var pageTracker = _gat._getTracker("UA-5477436-4");
		pageTracker._trackPageview();
	} catch(err) {}

	if( typeof LGSGoReaderPopup != 'undefined' &&
			LGSGoReaderPopup.parentWindow == window ) {

		
		//switch
		var newpop =
	    window.open("/LGS/editor2/"+s, "popup",
	    LGSGoReaderPopupWindowAttributes);
		newpop.parentWindow = window;
		newpop.callback = window.receiveCommentQipu;		

		LGSGoReaderPopup = newpop;
	}
	else {
	  LGSGoReaderPopup =
  	  window.open("/LGS/editor2/"+s, "popup",
			LGSGoReaderPopupWindowAttributes);

		if( typeof LGSGoReaderPopup !='undefined' && LGSGoReaderPopup )
			LGSGoReaderPopup.parentWindow = window;
		else
			alert('Popup is missing');

		if(window.receiveCommentQipu)
			LGSGoReaderPopup.callback = window.receiveCommentQipu;
		else
			alert('receiveCommentQipu not found.');
	}	

}



function receiveCommentQipu( str ) {
	if( typeof doInsertCommentQipu == 'function' ) {
		doInsertCommentQipu( str );		
	}
}


function initCommentQipu() {
	if( typeof jQuery != 'undefined' ) {

    jQuery("#commentform").submit(
      function() {
        
				//alert( jQuery("#comment").val() + getCommentQipuCode() );
				jQuery('#comment').disabled = true;
				
				jQuery("#comment").val( 
					jQuery("#comment").val() + getCommentQipuCode() );
        return true;

      });
  }

}


function doInsertCommentQipu( str ) {
//alert('doInsertCommentQipu:' + str);

	if(typeof jQuery=='undefined') return;

	var dir1 = str.charAt(0);
	var dir2 = str.charAt(1);
	
	window.last_comment_qipu = str;
	jQuery('#LGS_id_comment_qipuPreviewImage').attr( 'src', '/LGS/sgfimg/output/'+ dir1 + '/' + dir2 + '/' + str + '.png' );	

	jQuery('#LGS_id_comment_qipuHint').css('display', 'none');
	jQuery('#LGS_id_comment_qipuPreview').css('display', 'inherit');

	//window.location = window.location + '#LGS_id_comment_qipuBox';
	//window.location.hash = '#LGS_id_comment_qipuBox';
	window.location.hash = '#respond';

}

function getCommentQipuCode() {
	if( typeof window.last_comment_qipu != 'undefined' )
		return '[qipu:'+window.last_comment_qipu+']';
	else
		return '';
}

function clearCommentQipu() {

	if( typeof jQuery == 'undefined') return;

  jQuery('#LGS_id_comment_qipuPreview').css('display', 'none');
	window.last_comment_qipu = '';

	jQuery('#LGS_id_comment_qipuPreviewImage').attr('src', '');

}

function editCommentQipu() {
	if( typeof window.last_comment_qipu != 'undefined' )
		popupGoReader(window.last_comment_qipu);	
}

