var hider = {
	add:	function ( action ) {
			if ( hider.el ) return false
			hider.el = document.createElement( 'div' )
			hider.el.id = 'loveHider'
			hider.el.style.height = document.documentElement.scrollHeight + 'px'
			hider.el.onclick = function () { if ( action ) action(); if ( hider.el ) hider.remove() }
			document.body.appendChild( hider.el )
		},
	remove:	function () {
			if ( hider.el )
				document.body.removeChild( hider.el )
			hider.el = null
		}
}

function MagicField ( element, text ) {
	obj = this
	obj.field = element
	obj.text = text
	obj.cls = ( obj.field.className ) ? obj.field.className : ''
	if ( location.href.indexOf( 'search' ) == -1 ) obj.field.value = ''

	if ( obj.field.value == 0 ) obj.field.value = text

	if ( obj.field.value != obj.text ) obj.field.className = 'focus'

	obj.field.onfocus =	function () {
					if ( this.value == obj.text ) this.value = ''
					if ( this.value != obj.text ) this.className = obj.cls + ' focus'
				}

	obj.field.onblur =	function () {
					if ( this.value != '' ) return
					this.className = obj.cls
					this.value = obj.text
				}

	obj.changeText =	function ( txt ) {
		if ( obj.field.value == obj.text ) obj.field.value = txt
		obj.text = txt
		obj.field.onblur()
	}

}

function id ( id ) { return document.getElementById( id ) }

var tools = {

	init:	function ( e ) {

		tools.o = id( 'searchOptsList' )


		tools.click = function ( e ) {

			var e = e || event; var t = e.target || e.srcElement

			var $curPlace = $( '#curPlace' )
			var $searchOpts = $( '#searchOpts' )

			var makeClone = false

			if (
				t.tagName == 'A' && (
					!( !t.getAttribute( 'rel' ) && !id( 'searchPlace' ).value )
					&& t.getAttribute( 'rel' ) !== id( 'searchPlace' ).value
				)
			) {

				makeClone = true

				if ( t.getAttribute( 'rel' ) ) {

					id( 'searchPlace' ).value = t.getAttribute( 'rel' )

					$curPlace.hide( 0, function(){
						$curPlace
							.text( t.firstChild.nodeValue )
							.show( 0 )
					} )

					if ( $searchOpts.text() != 'Изменить' )
						$searchOpts.hide( 0, function(){
							$searchOpts
								.text( 'Изменить' )
								.show( 0 )
						} )


				} else {

					id( 'searchPlace' ).value = ''
					$searchOpts.hide( 0, function(){
						$searchOpts
							.text( 'Искать в определенном издании' )
							.show( 0 )
					} )
					$curPlace
						.hide( 0, function(){ $curPlace.text( '' ) } )

				}

				tools.hide( t, makeClone )

			} else if ( t != tools.o )
				tools.hide( t, makeClone )

		}

		tools.o.onclick = function ( e ) { return !! tools.click( e ) }
		tools.o.onselectstart = function () { return false }

		return ( tools.o.className == 'hidden' )
			? tools.show( e )
			: tools.hide()

	},

	show:	function ( e ) {
		var e = e || event
		var html = document.documentElement
		var x = e.clientX + html.scrollLeft - 20
		var y = e.clientY + html.scrollTop  + 10
		tools.o.style.left = (
			( ( $( tools.o ).width() + x + 20 ) > html.scrollWidth )
				? ( html.scrollWidth - $( tools.o ).width() - 20 )
				: ( x )
		) + 'px'
		tools.o.style.top  = y + 'px'

		$( tools.o ).show( 0 ).removeClass( 'hidden' )
		hider.add( tools.hide )
	},

	hide:	function ( t, makeClone ) {

		if ( t && makeClone ) {

			var clone = t.cloneNode( true )
			clone.className = 'searchOptsList'
			var $clone = $( clone )
			$clone.css( {
				position: 'absolute',
				left: $( t ).offset().left,
				top: $( t ).offset().top,
				width: $( t ).width() + 'px',
				textAlign: 'left'
			} )
			document.body.appendChild( clone )

			setTimeout( function(){
				$clone.fadeOut( 0, function () {
					$clone.remove()
				} )
			}, 20 )

		}

		$( tools.o )
			.fadeOut( 0, function () {
				$( tools.o ).addClass( 'hidden' )
			} )

		hider.remove()
	}

}

window.onload = function () {

	if ( id( 'searchField' ) )
		window.searchField = new MagicField( id( 'searchField' ), 'поиск' )

	if ( id( 'searchOpts' ) )
		id( 'searchOpts' ).onclick = tools.init

}

