/*** 2010-11-27 ***/
var oXH, eTitle, eFirst, eTweetInput, eTweetCount, eTweetChk, iTweetMax;

function ajEditTitle( sCap, iTopic, sPostTarget, eCaller ) {
	var a= document.getElementsByTagName( 'h2' );
	eTitle= a[0].firstChild.firstChild;
	a= document.getElementsByTagName( 'h3' );
	eFirst= a[0].childNodes[( a[0].childNodes[0].tagName== 'IMG'? 2: 0 )].firstChild;
	var sTitle= prompt( sCap, eTitle.nodeValue );
	if( sTitle ) if( sTitle!= eTitle.nodeValue ) {
		if( !oXH ) oXH= ajXhCreate();
		if( !oXH ) {
			alert( 'No XmlHttp available' );
			return;
		}

		eTitle.nodeValue= eFirst.nodeValue= '';
		ajXhSend( 'topic_id='+ iTopic+ '&topic_title='+ encodeURIComponent( sTitle ), sPostTarget );
	}
}

function ajXhSend( sData, sPostTarget ) {
	if( oXH.readyState== 4|| oXH.readyState== 0 ) {
		ajActivity( true );

		oXH.open( 'POST', sPostTarget, true );
		oXH.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
		oXH.onreadystatechange= ajXhResponse;
		oXH.send( sData );
	} else window.setTimeout( 'ajXhSend( \''+ sData+ '\', \''+ sPostTarget+ '\' )', 500 );
}

function ajXhResponse() {
	if( oXH.readyState!= 4 ) return;
	eTitle.nodeValue= eFirst.nodeValue= oXH.responseText;
	ajActivity( false );
}

function ajActivity( bShow ) {
	var oImg= document.getElementById( 'act_indicator' );
	if( !oImg ) return;
	if( oImg.style.visibility== 'hidden'&& bShow ) oImg.style.visibility= 'visible'; else
	if( !bShow ) oImg.style.visibility= 'hidden';
}

function ajXhCreate() {
	if( window.XMLHttpRequest ) return new XMLHttpRequest(); else
	if( window.ActiveXObject ) return new ActiveXObject( 'Microsoft.XMLHTTP' ); else
	return null;
}

function ajTwInit() {
	eTweetInput= document.getElementById( 'tweettext' );
	eTweetCount= document.getElementById( 'tweetchars' ).firstChild;
	eTweetChk= document.getElementById( 'tweet' );

	iTweetMax= eTweetCount.nodeValue.match( /[0-9]+/ );
	iTweetMax= iTweetMax? iTweetMax[0]: 0;
	eTweetInput.onkeyup= eTweetInput.onmouseup= eTweetInput.onclick= eTweetInput.onchange= ajTwCount;
	eTweetChk.onkeydown= eTweetChk.onclick= eTweetChk.onchange= ajTwSubject;

	ajTwCount();

	document.getElementById( 'postform' ).onsubmit= ajTwSubject;  // For initially ticked checkbox
}

function ajTwCount() {
	var iLen= iLenReal= eTweetInput.value.length, rAmp;
	/*rAmp= eTweetInput.value.match( /&/g );
  if( rAmp ) iLen+= rAmp.length* 4;*/

	if( iLen> iTweetMax ) {
		eTweetInput.value= eTweetInput.value.substr( 0, iTweetMax- ( iLen- iLenReal ) );
		iLen= iTweetMax;
	}
	eTweetCount.nodeValue= eTweetCount.nodeValue.replace( /[0-9]+/, (iTweetMax- iLen) );
}

function ajTwSubject() {
	if( !eTweetChk.checked|| eTweetInput.value.length> 0 ) return;

	var s= document.getElementById( 'subject' ).value.replace( /\([^)]+\)|^Re: /g, '' );
	var eD= document.getElementById( 'desc' );
	if( eD ) eD= eD.value; else {
		eD= document.getElementById( 'descplain' );
		if( eD ) eD= eD.firstChild.nodeValue; else eD= '';
	}
	if( eD.length> 0 ) s+= ' '+ eD.replace( /(^ +| +$)/g, '' );
	eTweetInput.value= s.substr( 0, iTweetMax );
	ajTwCount();
}

/*** 2010-12-21 ***/
function updateText( e, s ) {  // Modify textnode
	if( typeof e== 'string' ) e= document.getElementById( e );
	e.firstChild.replaceData( 0, 12, s );
}
function setDisplay( e, b ) {  // Hide/show element
	e.style.display= b? '': 'none';
}

var aI, aSort, iActive, i, eTodo, eTodoCap, ePics, iTime, eTimeCap;
function getLink() {  // Find links and thumbnails in BBCode
	iTime= new Date();

	aI= new Array();
	aSort= new Array( new Array(), new Array() );  // Horz, Vert
	iActive= 0;

	var eIn= document.getElementById( 'in' );
	var aMatch, re= /\[url=["']?(http[^"'\]]+)["']?\]\[img\](http[^\[]+)\[\/img\]\[\/url\]/gi;
	var eA, eImg;
	while( aMatch= re.exec( eIn.value ) ) {
		eA= document.createElement( 'a' );
		eA.href= aMatch[1];

		eImg= document.createElement( 'img' );
		aI.push( new Array( eA, eImg, 0 ) );
		eImg.onerror= new Function( 'setStatus( '+ (iActive++)+ ', -1 );' );
		eImg.src= aMatch[2];

		eA.appendChild( eImg );
	}

	eTodo= document.getElementById( 'iTodo' );
	eTodoCap= document.getElementById( 'sTodo' );
	updateText( eTodo, iActive );
	setDisplay( eTodoCap, true );
	ePics= document.getElementById( 'pics' );
	while( ePics.hasChildNodes() ) ePics.removeChild( ePics.firstChild );
	updateText( 'iPic', aI.length );
	setDisplay( document.getElementById( 'info' ), true );
	eTimeCap= document.getElementById( 'sTime' );
	setDisplay( eTimeCap );

	window.setTimeout( 'getComplete()', 250 );
}
function setStatus( iElem, iStat ) {  // Image loaded
	aI[iElem][2]= iStat;
	iActive--;
}
function getComplete() {  // Check image download
	for( i= 0; i< aI.length; i++ ) if( aI[i][2]== 0&& aI[i][1].complete ) {
		setStatus( i, 1 );
		aSort[aI[i][1].width< aI[i][1].height? 1: 0].push( i );
	}
	updateText( eTodo, iActive );
	if( iActive> 0 ) window.setTimeout( 'getComplete()', 250 ); else setList();
}
function getArray( iIndex ) {  // Render BBCode and preview
	var sText= '';
	for( i= 0; i< aSort[iIndex].length; i++ ) {
		ePics.appendChild( aI[aSort[iIndex][i]][0] );
		sText+= ' [url='+ aI[aSort[iIndex][i]][0].href+ '][img]'+ aI[aSort[iIndex][i]][1].src+ '[/img][/url]';
	}
	return sText.substr( 1 );
}
function setList() {  // Finalize
	var iBigger= aSort[0].length< aSort[1].length? 1: 0;
	var sText= getArray( 1- iBigger ), iErr= 0;
	if( aSort[0].length> 0&& aSort[1].length> 0 ) {
		sText+= '\n';
		ePics.appendChild( document.createElement( 'br' ) );
	}
	sText+= getArray( iBigger );
	for( i= 0; i< aI.length; i++ ) if( aI[i][2]== -1 ) iErr++;

	document.getElementById( 'out' ).value= sText;
	updateText( 'iHorz', aSort[0].length );
	updateText( 'iVert', aSort[1].length );
	updateText( 'iErr', iErr );
	setDisplay( eTodoCap );
	updateText( 'iTime', Math.ceil( ((new Date()).getTime()- iTime.getTime())/ 1000 )+ 's' );
	setDisplay( eTimeCap, true );
}

var aPom= new Array();
function ajPomColor( e ) {
	var aPos= e.id.match( /^o[0-9]+-([1-9]+)$/ );
	if( aPos ) {
		var iPos= aPos[1];
		if( !aPom[iPos] ) {
			aPom[iPos]= 1;

			var a= document.getElementsByTagName( 'label' );
			var iCnt= a.length, i1;
			for( i1= 0; i1< iCnt; i1++ ) {
				if( a[i1].id.match( new RegExp( '^o[0-9]+-'+ iPos+ '$', '' ) ) ) {
					a[i1].style.backgroundColor= '#BFB';
				}
			}
		}
	}
}
function ajPomClick() {
	ajPomColor( this );
}
function ajPomInit() {
	var a= document.getElementsByTagName( 'label' ), aPos;
	var iCnt= a.length, i1;
	for( i1= 0; i1< iCnt; i1++ ) {
		aPos= a[i1].id.match( /^o[0-9]+-([1-9]+)$/ );
		if( aPos ) {
			if( a[i1].firstChild.checked ) ajPomColor( a[i1] );
			a[i1].onclick= ajPomClick;
		}
	}
}

/*** 2011-05-25 ***/
var eTHi;
function ajThumbHoverInit() {
	var a= document.getElementsByTagName( 'img' );
	var iCnt= a.length, i1, iT= 0;
	for( i1= 0; i1< iCnt; i1++ ) if( a[i1].className== 'thumb' ) {
		iT++;
		a[i1].onmouseover= ajThumbShow;
		a[i1].onmouseout= ajThumbHide;
		a[i1].onmousemove= ajThumbMove;
	}
	if( iT ) {
		eTHi= document.createElement( 'img' );
		with( eTHi.style ) {
			position= 'fixed';
			zIndex= '10';
			visibility= 'hidden';
			border= '1px solid #665';
			padding= '1px';
			backgroundColor= '#fff';
		}
		document.getElementById( 'page-body' ).appendChild( eTHi );
	}
	return iT;
}
function ajThumbShow( e ) {
	eTHi.src= this.src;
	eTHi.style.visibility= 'visible';
	ajThumbMove( e );
}
function ajThumbMove( e ) {
	if( !e ) e= window.event;
	var iL= e.clientX+ 50, iT= e.clientY- eTHi.offsetHeight+ 20;
	if( iL+ eTHi.offsetWidth> window.innerWidth ) iL= window.innerWidth- eTHi.offsetWidth;
	if( iT< 0 ) iT= 0; else
	if( iT+ eTHi.offsetHeight> window.innerHeight ) iT= window.innerHeight- eTHi.offsetHeight;
	eTHi.style.left= iL+ 'px';
	eTHi.style.top= iT+ 'px';
}
function ajThumbHide() {
	eTHi.style.visibility= 'hidden';
}

/*** 2011-07-25 ***/
function colorPassword( e ) {
	var sT= e.value, iS= 0, aC= new Array( 'f5a9a9', 'f5bca9', 'f5d0a9', 'f4e395', 'f3f781', 'a9f5a9', '30ff30' );

	if( sT.length== 0 ) {
		e.style.backgroundColor= '';
		return;
	}
	if( sT.length> 5 ) iS++;
	if( sT.length> 12 ) iS++;
	if( sT.length> 20 ) iS++;
	if( sT.search( /[a-z]/ )!= -1&& sT.search( /[A-Z]/ )!= -1 ) iS++;
	if( sT.search( /\d/ )!= -1&& sT.search( /\D/ )!= -1 ) iS++;
	if( sT.search( /[\x21-\x2f\x3a-\x40\x5b-\x5f\x7b-\x7e]/ )!= -1&& sT.search( /\w/ )!= -1 ) iS++;

	e.style.backgroundColor= '#'+ aC[iS];
}
function colorPasswordBind( n ) {
	var e= document.getElementById( n );
	e.onkeyup= e.onclick= e.onchange= function() { colorPassword( e ); };
}
