/******************************************************************************
 * Datei:				functions.js
 * Autor:				Thorsten Rinne <thorsten@phpmyfaq.de>
 * Datum:				2003-11-13
 * Letzte �nderung:		2004-07-07
 * Copyright:           (c) 2001-2004 phpMyFAQ Team
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 ******************************************************************************/

function printarticle(id, lang)
{
	Fenster = window.open(AMHERST.ats.ap.SITENAME + "/printing.php?id=" + id, "Fenster", "width=720, height=450, resizable=yes, menubar=yes, scrollbars=yes");
	Fenster.moveTo(50,50);
	Fenster.focus();
}

function Picture(pic,title,width,height)
{
    popup = window.open(pic, title, 'width='+width+', height='+height+', toolbar=no, directories=no, status=no, scrollbars=no, resizable=yes, menubar=no');
    popup.focus();
}

function checkAll(checkBox)
{
    var v = checkBox.checked;
    var f = checkBox.form;
    for (var i = 0; i < f.elements.length; i++) {
        if (f.elements[i].type == "checkbox") {
            f.elements[i].checked = v;
            }
        }
}

//previewAnswer written by npd
//creates a popup window with the height of half the other window and the width of the content column
//it then submits the previewForm on the original page which opens the parser in the new window
function previewAnswer()
{
	var el = document.getElementById('content');
	var theWidth = el.offsetWidth;
	//alert(theWidth);
	var theHeight;
	
	//height-finding code (cross-browser) courtesy of quirksmode.org
	if (self.innerHeight) // all except Explorer
	{
			x = self.innerWidth;
			theHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
	{
			x = document.documentElement.clientWidth;
			theHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
			x = document.body.clientWidth;
			theHeight = document.body.clientHeight;
	}    
	theHeight = theHeight / 2;
	//alert(theHeight);
	var previewWin = window.open('', 'previewWin','location=no,status=no,scrollbars=yes,resizable=yes,width='+theWidth+',height='+theHeight);
	var pForm = document.getElementById('previewForm');
	pForm.submit();
	previewWin.focus();
}

// makes the hidden field in the preview form have the same content as the main comment textarea
// not needed anymore
function updateComment(smallForm)
{
	var theComment = document.getElementById('commentArea');
	var commentP = document.getElementById('commentP');
	commentP.value = theComment.value;
}

function bnr(dec)		// this function returns largest n such that 2^n is smaller than dec
{
		i=dec;
		var hit="";
		 while(i >= 1)
		 {
		   var m=(i*10)/4;
		   while(m > 1){m=m-5;};
		   if(m < 0){i=(i-1)/2;hit+="1";}else{i=i/2;hit+="0";};
		 };
		if(hit.length>4)
		{
			foo=4;
		}
		else foo=hit.length - 1;
		return foo;
}

// add some animation to show/hide. useless but cool
function expandComments(id)
{
	var cDiv = document.getElementById('comments'+id);
	var b = document.getElementById('insidebox'+id);
  if(cDiv.style.display != 'none')
  {
   	var h = b.offsetHeight;
   	var n = bnr(h);
   	var i=Math.pow(2,n);
		q=setInterval(function()
		{
			cDiv.style.height=i+'px';
			i=i/2;
			if(i<=1) {		
				clearInterval(q);
				cDiv.style.display='none';
				q=0;
			}
		}
		, 1);
  } 
  else
  {
  	cDiv.style.height='1px';
  	cDiv.style.display='block';
   	var h = b.offsetHeight;
   	var n = bnr(h);
   	var i=0;
		q=setInterval(function()
		{
			cDiv.style.height=Math.pow(2,i)+'px';
			i++;
			if(i>=n) {		
				clearInterval(q);
				cDiv.style.height=h+20+'px'; // a little bottom margin
				q=0;
			}
		}
		, 1);
  }
	
	var cIcon = document.getElementById('cIcon'+id);
	cIcon.src = (cIcon.src.indexOf('images/expand.gif') == -1) ? AMHERST.ats.ap.SITENAME + '/images/expand.gif' : AMHERST.ats.ap.SITENAME + '/images/collapse.gif';
	var cTip = document.getElementById('cTip'+id);
	cTip.title = (cTip.title == 'Click to expand') ? 'Click to collapse' : 'Click to expand';
}

function expandcmt(id)
{
	cDiv = document.getElementById('comments'+id);
	cDiv.className = (cDiv.className == "cmtBlock") ? 'cmt' : 'cmtBlock';
}

function sort()
{
	box = document.sortby.navi;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function newComment() // qingsi
{
	// browser detection fork
	// if IE or Gecko, use HTMLArea, or now, xinha
	if(navigator.appName.indexOf("Internet Explorer") != -1 || navigator.userAgent.indexOf("KHTML") == -1)  // safari is KHTML
	{
		if(document.getElementById('getanid'))  // test if editor is already initiated; prevent multiple editors
		{
			editor.setHTML(editor.inwardHtml(''));   // start anew
			editor.updateToolbar();
			editor.initIframe();  // initialize iframe to get the blinking cursor back
			editor.focusEditor();
		}
		else 
		{
			cf=document.getElementById('commentForm'); 
			cf.style.display='block';  //show the form
			xinha_init();
		}
		document.commentForm.id_comment.value="new";  // telling savecomment.php that this is a new commen
	}
	else  // play safe. everything else uses Textile.
	{
		cf=document.getElementById('commentForm'); 
		cf.style.display='block';  //show the form
		var ta = document.getElementById('commentArea'); 
		ta.value = "";
		ta.focus();
		pb=document.getElementById('previewButton');  // turn on preview button for textile
		pb.style.display='inline';
		tf=document.getElementById('previewForm');
		tf.style.display='block';
		document.commentForm.id_comment.value="new";  // telling savecomment.php that this is a new comment
	}   
}

function editComment(cid, comment)  //qingsi
{
	document.commentForm.id_comment.value=cid;  //telling savecomment.php to update this comment
	cf=document.getElementById('commentForm'); 
	var ta = document.getElementById('commentArea'); 
	ta.value = comment;
	cf.style.display = 'block';  //show the form

	//browser fork
	//gecko or IE, use HTMLArea
	if(navigator.appName.indexOf("Internet Explorer") != -1 || navigator.userAgent.indexOf("KHTML") == -1)  // safari is KHTML
	{
		if(document.getElementById('getanid'))  // test if editor is already initiated; prevent multiple editors
		{
			editor.setHTML(editor.inwardHtml(comment));   // load original text into editor
			editor.updateToolbar();
			editor.initIframe();  // initialize iframe to get the blinking cursor back
			editor.focusEditor();
		}
		else 
		{
			cf=document.getElementById('commentForm'); 
			cf.style.display='block';  //show the form
			cf.value=comment;
			xinha_init();
		}
	}
	else
	{
		ta.focus();
		pb=document.getElementById('previewButton');   // turn on preview button for textile
		pb.style.display='inline';
		tf=document.getElementById('previewForm');
		tf.style.display='block';
	}
}

		// a debugging function, maybe? not sure where it came from. we'll leave it in case future coders on the project wish to debug.
function inspect(elm){
			var str = "";
			for (var i in elm) {
				//str += eval('elm.'+i);
				str += i + ": " + elm.getAttribute(i) + "__";
			} 
			alert('inspect');
			alert(str);
}

function showBio(i) // display biographical info about a panelist qingsi
{
//	var y=findY(obj);
	var bc=document.getElementById('bioCell');
//	var yy=findY(bc);
//	var relY=y-yy-4;  // last number is fine-tuing
	bc.innerHTML= bios[i];
//	bc.innerHTML= "<span style=\"position:relative;top:"+relY+";\">"+bios[i]+"</span>";
//	window.scrollTo(0,relY-20);
}

// I stole this from facebook.com -qingsi

// Get Absolute X Position of HTML Element
function findX(obj)
{
  var curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

// Get Absolute Y Position of HTML Element
function findY(obj)
{
  var curtop = 0;
  if(obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}


// check email address validity. taken from quirkmode

