var T = {
	submitButton:function( par ){
		par.defaultClassName = '';
		switch( par.color ){
			case 'link':
				par.defaultClassName = 'newButtonLink';
				break;
			case 'buy':
				par.defaultClassName = 'newButtonBuy';
				break;
		}
		var el, tdm;
		var re = mkE( {
			tag:'table',
			className:'newButton ' + par.defaultClassName + ( par.className ? ' ' + par.className : '' ),
			prop:{
				id:par.id || '',
				cellSpacing:'0'
			},
			els:[
				mkE( {
					tag:'tbody',
					els:[
						mkE( {
							tag:'tr',
							className:par.size || '',
							els:[
								mkE( {
									tag:'td',
									attr:{
										width:'7'
									},
									els:[
										mkE( {
											tag:'div',
											className:'el l'
										} )
									]
								} ),
								tdm = mkE( {
									tag:'td',
									className:'el m'
								} ),
								mkE( {
									tag:'td',
									attr:{
										width:'7'
									},
									els:[
										mkE( {
											tag:'div',
											className:'el r'
										} )
									]
								} )
							]
						} ) // tr
					]
				} ) // tbody
			]
		} ); // table
		if( ! par.type || par.type == 'submit' ){
			el = mkE( {
				tag:'input',
				className:( par.icon ? par.icon + 'Icon icon' : '' ),
				prop:{
					type:'submit',
					value:par.caption || 'Submit'
				}
			} ).append( tdm );
		} else if( par.type == 'link' ) {
			el = mkE( {
				tag:'a',
				className:( par.icon ? par.icon + 'Icon icon' : '' ),
				text:par.caption || 'Link',
				prop:{
					href:par.href || ''
				}
			} ).append( tdm );
			if( par.isHTML ){
				el.innerHTML = par.caption || '';
			}
		}
		if( par.onclick ){
			el.onclick = par.onclick;
		}
		return re;
	},
	
	rateStarsMMove:function( e ){
		if( this._drDisabled ){
			return;
		}
		this.style.cursor = 'hand';
		this.style.cursor = 'pointer';
		this.onmouseover = this.onmousemove;
		var node = this.getElementsByTagName( 'div' )[ 0 ];
		if( ! node._drWidth ){
			node._drWidth = getStyle( node, 'width' );
		}
		var oneStarWidth = this.offsetWidth / Number( this.getAttribute( 'starcount' ) );
		node.style.width = Math.ceil( Math.max( 1, ( D.mouseX - findPosX( this ) ) / oneStarWidth ) ) * oneStarWidth + 'px';
	},
	
	rateStarsMOut:function( e ){
		if( this._drDisabled ){
			return;
		}		
		var node = this.getElementsByTagName( 'div' )[ 0 ];
		node.style.width = node._drWidth;
	},
	
	rateStarsClick:function( e, fn ){
		if( this._drDisabled ){
			return;
		}
		var startCount = Number( this.getAttribute( 'starcount' ) );
		var node = this.getElementsByTagName( 'div' )[ 0 ];
		var oneStarWidth = this.offsetWidth / startCount;
		var v = Math.round( node.offsetWidth / oneStarWidth );
		v = Math.min( v, startCount );
		v = Math.max( 1, v );
		var hidden = $( 'input', this )[ 0 ];
		if( ! hidden || hidden.name == '' ){
			this.style.cursor = 'default';
			this._drDisabled = true;
		} else {
			node._drWidth = v * oneStarWidth + 'px';
			hidden.value = v;
		}
		fn( v, node, this );
	},
	
	cbx:function( el, href, id, onclick ){
		if( existsClassName( el.parentNode.parentNode, 'open' ) ){
			removeClassName( el.parentNode.parentNode, 'open' );
			if( onclick ){
				onclick( false );
			}
			if( id ){
				$( '#' + id ).hide();
			}
		} else {
			addClassName( el.parentNode.parentNode, 'open' );
			if( ! empty( href ) ){
				if( empty( id ) ){
					rq( href, $( '.cbxContent', el.parentNode.parentNode )[ 0 ] );
				} else {
					$( '#' + id ).show();
					rq( href, $( '#' + id )[ 0 ] );
				}
			}
			if( onclick ){
				onclick( true );
			}
		}
	},
	
	userListTop:function( el, v ){		
		var tbody = el.parentNode.parentNode;
		var trs =  D.getChildByTagName( tbody, 'tr' );		
		var trIndex = Number( array_search( el.parentNode, trs ) );
		var i = array_search( el, D.getChildByTagName( el.parentNode, 'td' ) );
		var node = D.getChildByTagName( trs[ trIndex + 1 ], 'td' )[ i ];
		
		if( v ){
			addClassName( el, 'hover' );
			addClassName( node, 'hover' );
			//clearTimeout( el._drHoverTimeout );
		} else {
			//el._drHoverTimeout = setTimeout( function(){
				removeClassName( el, 'hover' );
				removeClassName( node, 'hover' );
			//}, 100 );
		}
	},
	
	userListBottom:function( el, v ){
		var tbody = el.parentNode.parentNode;
		var trs =  D.getChildByTagName( tbody, 'tr' );				
		var trIndex = Number( array_search( el.parentNode, trs ) );
		var i = array_search( el, D.getChildByTagName( el.parentNode, 'td' ) );		
		var node = D.getChildByTagName( trs[ trIndex - 1 ], 'td' )[ i ];
		if( v ){
			addClassName( el, 'hover' );
			addClassName( node, 'hover' );
			//clearTimeout( el._drHoverTimeout );
		} else {
			//el._drHoverTimeout = setTimeout( function(){
				removeClassName( el, 'hover' );
				removeClassName( node, 'hover' );
			//}, 100 );
		}
	},
	
	searchFieldOnfocus:function(){
		if (this.value == this.getAttribute('defaultText')) {
			this.value='';
			removeClassName(this,'color2');
		} else {
			removeClassName(this,'color2');
		}
	},
	
	searchFieldOnblur:function(){
		if( empty( this.value ) ){
			this.value = this.getAttribute('defaultText');
			addClassName(this,'color2');
		}
	},
		
	radiobuttonSet:function( el ){
		$( el ).parents('ul').find('li, span').removeClass('active');
		$( el ).parent().addClass('active').parent().addClass('active');
	},
	
	vTabsSelect:function( el, par ){
		var parent = el.parentNode.parentNode.parentNode;
		$( '.vTabsLeftC', parent ).removeClass( 'vTabsActive' );
		addClassName( el, 'vTabsActive' );
		$( '.vTabsContentItem', parent ).css( { display:'none' } );
		var nodes = $( '.vTabsContentItem' + par.i, parent );
		nodes.css( { display:'' } );
		if( par.url ){
			rq( par.url, nodes[0] );
		}
		return false;
	},
	
	permisionsAdvanced:function(){
		$( '#permisionSimple_' ).slideUp();
		$( '#permisionAdvanced_' ).slideDown();
		$( '#permisionSimple' )[ 0 ].value = 0;
		return false;
	},
	
	permisionsSimple:function(){
		$( '#permisionSimple_' ).slideDown();
		$( '#permisionAdvanced_' ).slideUp();
		$( '#permisionSimple' )[ 0 ].value = 1;
		return false;
	},
	
	permisionRadio:function( el ){
		var checkboxs = {
			'0':document.getElementById( 'prmision0' ),
			'all':document.getElementById( 'permisionAll' ),
			'fr':document.getElementById( 'permisionFr' ),
			'frfr':document.getElementById( 'permisionOther' ),
			'groups':document.getElementById( 'permisionGroups' ),
			'public':document.getElementById( 'permisionPublic' )
		};
		switch( el.value ){
			case '0':
				if( el.checked ){
					checkboxs.all.checked = false;
					checkboxs.fr.checked = false;
					checkboxs.frfr.checked = false;
					checkboxs.groups.checked = false;
					checkboxs.public.checked = false;
					$( '#permisionGroups_ input' ).each( function(){
						this.disabled = true;
						this.checked = false;
					} );
				} else {
					el.checked = true;
				}
				break;
			case 'all':
				if( el.checked ){
					checkboxs.fr.checked = true;
					checkboxs.frfr.checked = true;
					checkboxs.groups.checked = false;
					checkboxs.public.checked = true;
					$( '#permisionGroups_ input' ).each( function(){
						this.disabled = true;
						this.checked = true;
					} );
				}
				break;
			case 'fr':
				checkboxs.groups.checked = false;
				$( '#permisionGroups_ input' ).each( function(){
					this.disabled = true;
					this.checked = el.checked;
				} );
				break;
			case 'groups':
				checkboxs.fr.checked = false;
				$( '#permisionGroups_ input' ).each( function(){
					this.disabled = ! el.checked;
				} );
				break;			
		}
		if( el.value != '0' ){
			checkboxs[ 0 ].checked = false;
		}
		if( el.value != 'all' ){
			checkboxs.all.checked = checkboxs.fr.checked && checkboxs.frfr.checked && checkboxs.public.checked;
		}
	},
	
	smallButton:function( par ){
		if( ! par ){
			par = {};
		}
		par.className = par.className || '';
		par.href = par.href || '';
		par.onclick = par.onclick || function(){};
		par.className += ' small-button roundBorders';
		switch( par.color ){
			case 'submit':
				par.className += ' small-button-submit';
				break;
			case 'link':
				par.className += ' small-button-link';
				break;
		}
		return mkE( {
			tag:'a',
			className:par.className,
			prop:{
				href:par.href,
				onclick:par.onclick
			},
			els:[
				mkE( {
					tag:'span',
					text:par.caption || '',
					className:( par.icon ? par.icon + 'Icon small-button-icon' : '' )
				} )
			]
		} );
	}
}; // var T


Draugiem.shpicResize = function( el ){
	var parent = el.parentNode;
	if( el.width == 0 || el.height == 0 || el.width < parent.offsetWidth || el.height < parent.offsetHeight ){
		return;
	}
	var k = el.width / el.height;
	if( el.width / el.height < parent.offsetWidth / parent.offsetHeight ){
		el.width = parent.offsetWidth;
		el.height = parent.offsetWidth / k;
	}	else {
		el.height = parent.offsetHeight;
		el.width = parent.offsetHeight * k;
	}
};

Draugiem.shpicResize2 = function( el ){
	var parent = el.parentNode;
	if( el.width == 0 || el.height == 0 ){
		return;
	}
	var k = el.width / el.height;	
	el.width = parent.offsetWidth;
	el.height = parent.offsetWidth / k;
};

function toggleTreeList (obj, callback) {
	var sublist = $(obj).parent('li').children('ul');
	var button = $(obj).parent('li').children('span.togbtn');
	
	var closed = $(button).hasClass('closed');
	var new_class = closed ? 'open' : 'closed';
	var new_state = closed ? 'block' : 'none';
		
	$(button).removeClass().addClass('togbtn ' + new_class);
	$(sublist).css('display', new_state);
	
	if (typeof(callback) == 'function') {
		callback();
	}
};

// +++  T.comments +++
T.comments = {
	replyLink:false,
	
	reply:function( el, cid ){
		var addComment = document.getElementById( 'commentsAddComment' );
		if( this.commentsReplyLink === el ){
			this.commentsReplyLink.innerHTML = xGlobal[ 'reply' ] || 'Reply';
			this.commentsReplyLink = false;
			removeClassName( addComment, 'answer' );
			addComment.parentNode.appendChild( addComment );
			document.getElementById( 'commantsAddParent' ).value = '0';
		} else {
			if( this.commentsReplyLink ){
				this.commentsReplyLink.innerHTML = xGlobal[ 'reply' ] || 'Reply';
			}
			this.commentsReplyLink = el;
			this.commentsReplyLink.innerHTML = xGlobal[ 'Cancel' ] || 'Cancel reply';
			addClassName( addComment, 'answer' );
			D.insertAfter( addComment, document.getElementById( 'commentsItem' + cid ) );
			document.getElementById( 'commantsAddParent' ).value = cid;
		}
		return false;
	},
	
	order:function( par ){
		var url = '/rq/comments.php?comId=' + par.id + '&comType=' + par.type  + '&commentsOrder=' + par.order;
		rq( url, 'comments' + par.type + '_' + par.id, {overlay:true,replace:true} );
		return false;
	},
	
	more:function( par ){
		var url = '/rq/comments.php?comId=' + par.id + '&comType=' + par.type + '&comPg=' + par.pg + '&comItems&comLastCid=' + par.lastCid;
		rq( url, 'comments' + par.type + '_' + par.id + 'More', {overlay:true,replace:true} );
		return false;
	},
	
	onsubmit:function( el, par ){
		var url = '/rq/comments.php?comId=' + par.id + '&comType=' + par.type;
		D.ajaxPost( el, url, 'comments' + par.type + '_' + par.id, {overlay:true,replace:true} );
		return false;
	},
	
	'delete':function( par ){
		if( ! D.confirmDelete() ){
			return false
		};
		var url = '/rq/comments.php?comId=' + par.id +
			'&comType=' + par.type +
			'&' + par.p.name + '=' + par.p.value +
			'&commentsDel=' + par.cid + '&comPg=' + par.pg;
		rq( url, 'comments' + par.type + '_' + par.id, {overlay:true,replace:true} );
		return false;
	},
	
	onkeyup:function( el, event ){		
		if( D.checkCtrlEnter(event) && el.form ){
			if( el.form.onsubmit && el.form.onsubmit() === false ){
				return false;
			};
			if( BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 8 ){
				return;
			}
			el.form.submit();
		}
	},
	
	insertSmiley:function( el ){
		var ta = el.parentNode.parentNode.parentNode.getElementsByTagName( 'textarea' )[0];
		if( D.insertSmiley.close() ){
			return false;
		}
		var s = D.insertSmiley( {
			onclick:function( v ){
				insertAtCursor( ta, v );
			}
		} );
		var pos = D.position( el );
		s.node.style.top = pos.top + 'px';
		s.node.style.left = pos.left + 40 + 'px';
		s.append( document.getElementById( 'outermost' ) );
		return false;
	}
};
// ---  T.comments ---

// +++ T.customValue +++
T.customValue = {
	
	onkeyup:function( el, par, onChange ){
		var inp = this.input( el );
		var inpVal = inp.value.replace( ',', '.' );
		var v = Number( inpVal );
		if( par.max && v > par.max ){
			v = par.max;
			inp.value = this.valid( v, par );
		}		
		onChange( v );
		/*
		if( inpVal == '' || inpVal == '0' || inpVal == '0.' || inpVal == '.' ){
			return true;
		}
		var v = Number( inpVal );
		if( ! v ){
			v = 0;
		}
		if( par.min !== false ){
			if( v < par.min ){
				v = par.min;
			}
		}
		if( par.max !== false ){
			if( v > par.max ){
				v = par.max;
			}
		}
		v = this.valid( v, par );
		if( v != Number( inpVal ) ){
			this.input( el ).value = v;
			onChange( v );
		}*/		
	},
	
	onblur:function( el, par, onChange ){
		var inp = this.input( el );
		var inpVal = inp.value.replace( ',', '.' );
		var v = Number( inpVal );
		if( ! v ){
			v = 0;
		}
		inp.value = this.valid( v, par );
		onChange( inp.value );
	},
	
	onmousedown:function( el ){
		el.onmouseout = el.onmouseup;
		if( el._drInterval ){
			clearInterval( el._drInterval );
		}
		el._drInterval = setInterval( function(){
			el.onclick();
		}, 200 );
	},
	
	onmouseup:function( el ){
		clearInterval( el._drInterval );
	},
	
	inc:function( el, par, onChange ){
		var inp = this.input( el );
		var v = Number( inp.value.replace( ',', '.' ) );
		if( ! v ){
			v = 0;
		}		
		v += par.step;
		this.input( el ).value = this.valid( v, par );
		onChange( inp.value );
		return false;
	},
	
	dec:function( el, par, onChange ){
		var inp = this.input( el );
		var v = Number( inp.value.replace( ',', '.' ) );
		if( ! v ){
			v = 0;
		}		
		v -= par.step;
		this.input( el ).value = this.valid( v, par );
		onChange( inp.value );
		return false;
	},
	
	input:function( el ){
		return el.parentNode.getElementsByTagName( 'input' )[ 0 ];
	},
	
	valid:function( v, par ){
		if( par.min !== false ){
			if( v < par.min ){
				v = par.min;
			}
		}
		if( par.max !== false ){
			if( v > par.max ){
				v = par.max;
			}
		}		
		v = D.numberFormat( v, par.round );
		return v;
	}	
};
// --- T.customValue ---