function showGID( gid, value )
{
	$( gid ).style.display = value;
}

function findPosX(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;
}

function findPosY(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;
}

function gid( name )
{
	return document.getElementById( name );
}

var cmtBox = {
	CreateBox : function () {
//		alert( document.getElementById( 'cmtOut' ) );
//		alert( $( 'cmtOut' ) );
//		cmtOut = ( gid( "aaaaaaaaaacmtOut" ) );
//		return;
		if( $( 'cmtMid' ) != null )
			return;
		cmtMidDiv = document.createElement( 'div' );
		cmtMidDiv.setAttribute( 'id', 'cmtMid' );
			cmtInDiv = document.createElement( 'div' );
			cmtInDiv.setAttribute( 'id', 'cmtIn' );
				cmtClo = document.createElement( 'div' );
				cmtClo.setAttribute( 'id', 'cmtClo' );
				cmtClo.innerHTML = '<a href="javascript:cmtBox.Hide();">Anuluj [ X ]</a>';
				cmtRes = document.createElement( 'div' );
				cmtRes.setAttribute( 'id', 'cmtResponse' );
				cmtRes.style.display = 'none';
				
				cmtForm = document.createElement( 'form' );
				cmtForm.setAttribute( 'method', 'post' );
				cmtForm.setAttribute( 'action', '/advajax/comment.php' );
				cmtForm.setAttribute( 'id', 'cmtForm' );
				cmtForm.style.margin = 0;
				cmtForm.style.padding = 0;
				
				cmtInput1 = document.createElement( 'input' );
				cmtInput1.setAttribute( 'type', 'hidden' );
				cmtInput1.setAttribute( 'name', 'cmtNewsId' );
				cmtInput1.setAttribute( 'id', 'cmtNewsId' );

				cmtInput2 = document.createElement( 'input' );
				cmtInput2.setAttribute( 'type', 'hidden' );
				cmtInput2.setAttribute( 'name', 'ref' );
				cmtInput2.setAttribute( 'id', 'cmtRefId' );

				h4 = document.createElement( 'h4' );
				h4.innerHTML = 'Dodaj komentarz';

				tt = document.createElement( 'div' );
				tt.setAttribute( 'class', 'cmtLewyDiv' );
				tt.innerHTML = 'Tytuł';
				tt2 = document.createElement( 'div' );
				tt2.setAttribute( 'class', 'cmtPrawDiv' );
				
				ttInput = document.createElement( 'input' );
				ttInput.setAttribute( 'id', 'cmtTitle' );
				ttInput.setAttribute( 'type', 'text' );
				ttInput.setAttribute( 'name', 'title' );
				ttInput.setAttribute( 'size', '60' );
				tt2.appendChild( ttInput );
				
				cmtForm.appendChild( h4 );
				cmtBr2 = document.createElement( 'br' );
				cmtBr2.setAttribute( 'clear', 'all' );

				cmtForm.appendChild( cmtBr2 );
				cmtForm.appendChild( tt );
				cmtForm.appendChild( tt2 );

				cmtBr = document.createElement( 'br' );
				cmtBr.setAttribute( 'clear', 'all' );
				cmtForm.appendChild( cmtBr );


				t2t = document.createElement( 'div' );
				t2t.setAttribute( 'class', 'cmtLewyDiv' );
				t2t.setAttribute( 'className', 'cmtLewyDiv' );
				t2t.innerHTML = 'Treść';
				t2t2 = document.createElement( 'div' );
				t2t2.setAttribute( 'class', 'cmtPrawDiv' );
				t2t2.setAttribute( 'className', 'cmtPrawDiv' );
				
				t2tInput = document.createElement( 'textarea' );
				t2tInput.setAttribute( 'id', 'cmtText' );
				t2tInput.setAttribute( 'style', 'height: 80px;' );
				t2tInput.setAttribute( 'name', 'body' );
				t2tInput.setAttribute( 'cols', '50' );
				t2t2.appendChild( t2tInput );
				
				cmtForm.appendChild( t2t );
				cmtForm.appendChild( t2t2 );

				t2t = document.createElement( 'div' );
				t2t.setAttribute( 'class', 'cmtLewyDiv' );
				t2t.setAttribute( 'className', 'cmtLewyDiv' );
				t2t.innerHTML = 'Treść';
				t2t2 = document.createElement( 'div' );
				t2t2.setAttribute( 'class', 'cmtPrawDiv' );
				t2t2.setAttribute( 'className', 'cmtPrawDiv' );


				btn1 = document.createElement( 'input' );
				btn1.setAttribute( 'type', 'button' );
				btn1.setAttribute( 'class', 'small' );
				btn1.value = 'Wyślij';

				btn2 = document.createElement( 'input' );
				btn2.setAttribute( 'type', 'button' );
				try {
//					btn1.setAttribute( 'onClick', 'cmtBox.Send()' );
					addEvent( btn1, 'click', cmtBox.Send );
					addEvent( btn2, 'click', cmtBox.Hide );
				} catch( e ) { };
				btn2.value = 'Anuluj';
				btn2.setAttribute( 'class', 'small' );

				cmtBr = document.createElement( 'br' );
				cmtBr.setAttribute( 'clear', 'all' );
				cmtForm.appendChild( cmtBr );
				
		cmtForm.appendChild( cmtInput1 );
		cmtForm.appendChild( cmtInput2 );
		cmtForm.appendChild( cmtBr );
		cmtForm.appendChild( btn1 );
		cmtForm.appendChild( btn2 );
		cmtInDiv.appendChild( cmtClo );
		cmtInDiv.appendChild( cmtRes );
		cmtInDiv.appendChild( cmtForm );
		cmtMidDiv.appendChild( cmtInDiv );
		commDiv = $( 'cmtOut' );
		commDiv.appendChild( cmtMidDiv );
	},
	Show : function( newsid ) {
		cmtBox.CreateBox();
		$( 'cmtResponse' ).style.display = 'none';
		$( 'cmtResponse' ).innerHTML = ''; 
		$( 'cmtRefId' ).value = window.location.href;
		if ( $( 'cmtNewsId' ).value != newsid )
			cmtBox.Clear();
		comment = $('cmtOut');
		news = $( 'newsid' + newsid );
		var top = findPosY( $( 'newsid' + newsid ) );
		var top = findPosY( $( 'tenodnosnik' + newsid ) );
		$( 'cmtNewsId' ).value = newsid;
		comment.style.top =  top + 'px';
		comment.style.zIndex = 2;
		comment.style.left = ( findPosX( $( 'newsid' + newsid ) ) + 50 ) + 'px';
		comment.style.display = 'block';
		var height = ( findPosY( $( 'tenodnosnik' + newsid ) ) - top ) + 10;
		if ( height < 170 )
			height = 170;
		comment.style.height = height + 'px';
		$( 'cmtForm' ).style.display = 'block';
	},
	Clear : function() {
		$( 'cmtTitle' ).value = '';
		$( 'cmtText' ).value = '';
	},
	Hide : function()  {
		$('cmtOut').style.display = 'none';
	},
	MoveTo : function( aX, aY ) {
		//alert( $( 'msgIn' ).style.x );
		//( $( 'msgIn' ).style.x = 0 );
	},
	Resize : function( aWidth, aHeight ) {
		if ( aWidth <= 0 || aHeight <= 0 )
			cmtBox.Hide();
		else {
			$( 'cmtIn' ).style.width  = aWidth;
			$( 'cmtIn' ).style.height = aHeight;
		}
	},
	UseCmt : function( aMsg )	{
		$( 'cmtBody' ).innerHTML = aMsg;
	},
	Send : function() {
		advAJAX.submit( $( 'cmtForm' ), {
			onSuccess : function( obj ) { 
				if ( obj.responseText == '<nologin />' ) {
					window.location.href = '/user-login/';
					return;
				}
				cmtBox.Clear();
				$( 'cmtResponse' ).innerHTML = obj.responseText; 
				$( 'cmtForm' ).style.display = 'none';
				$( 'cmtResponse' ).style.display = 'block';
			},
			onError : function( obj ) { alert( 'error' ); }
		});
	}
}
