


////
////
////	Panels.

var Panel = new Class
({

	onOpen: null,
	onClose: null,

	initialize: function ( name )
	{
		this.$name	= name;
		this.$open	= false;
		this.$tab	= $( name + '_tab' );
		this.$body	= $( name + '_body' );

		if ( !Panel.$panels )
			Panel.$panels = {};
		Panel.$panels [ name ] = this;

		if ( this.$tab )
		{
			var self = this;
			this.$tab.addEvent ( 'click', function (e) { e.stop (); self.toggle (); } );
		}
	},

	toggle: function ()
	{
		if ( this.$open )
			this.close ();
		else
			this.open ();
	},

	open: function ()
	{
		if ( Panel.$open )
			Panel.$open.close ();

		Panel.$open = this;
		this.$open = true;

		//	Animate.

		this.$body
			.set ( 'tween', { duration: 200 } )
				.fade ( 'hide' )
					.fade ( 'in' );

		//	Show.

		if ( this.$tab )
			this.$tab.addClass ( 'toolbar-tab-active' );
		this.$body.addClass ( 'panel-container-visible' );

		if ( this.onOpen )
			this.onOpen.delay ( 250 );
	},

	close: function ()
	{
		if ( Panel.$open == this )
			Panel.$open = null;
		this.$open = false;

		if ( this.$tab )
			this.$tab.removeClass ( 'toolbar-tab-active' );
		this.$body.removeClass ( 'panel-container-visible' );

		if ( this.onClose )
			this.onClose ();
	}

});

Panel.getPanel = function ( name )
{
	if ( !Panel.$panels )
		return null;
	return Panel.$panels [ name ];
};

Panel.registerAll = function ()
{
	var containers = $$( '.panel-container' );
	if ( containers )
		for ( i = 0; i < containers.length; i ++ )
		{
			var element = containers [ i ];
			var id = element.get ( 'id' );
			var name = id.split ( '_body' ) [ 0 ];
			if ( !Panel.$panels || !Panel.$panels [ name ] )
				new Panel ( name );
		}
};



////
////
////	Search.

var SearchSystem = new Class
({

	initialize: function ( name )
	{
		var self = this;

		this.$default = '';

		this.checkInput = this.checkInput.bind ( this );
		this.sendQuery = this.sendQuery.bind ( this );
		this.trackQuery = this.trackQuery.bind ( this );

		this.$input = $( name + '_input' );
		this.$holder = $( name + '_results' );
		this.$queryTimeout = 0;
		this.$active = false;
		this.$lastQuery = '';

		this.$searchClient = new Request ({
			url: "search.php",
			method: "get",
			link: "chain",
			onRequest: function ()
			{
				self.$input.addClass ( 'search-input-loading' );
			},
			onComplete: function ()
			{
				self.$input.removeClass ( 'search-input-loading' );
			},
			onSuccess: function ( text, xml )
			{
				self.$holder.getElements ( 'a' )
					.removeEvent ( 'click' );

				self.$holder.innerHTML = text;

				self.$holder.getElements ( 'a' )
					.addEvent ( 'click', self.trackQuery );
			}
		});

		this.$input.addEvent ( 'focus', function ()
		{
			if ( self.$input.value == self.$default )
				self.$input.value = '';

			self.$active = true;
			self.checkInput.delay ( 250 );
		});

		this.$input.addEvent ( 'blur', function ()
		{
			if ( self.$input.value == '' )
				self.$input.value = self.$default;

			self.$active = false;
			self.$queryTimeout = false;
		});

		var panel = Panel.getPanel ( name );
		if ( panel )
		{			
			panel.onOpen = function ()
			{
				self.$input.focus ();
				self.$input.select ();
			};
			panel.onClose = function ()
			{
				self.$input.blur ();
				self.$active = false;
			};
		}
		else if ( this.$input.value == '' )
		{
			this.$default = 'търсене в каталога';
			this.$input.value = this.$default;
		}
	},

	checkInput: function ()
	{
		if ( !this.$active )
			return;

		var query = this.$input.get ( 'value' );

		if ( query != this.$default && query != this.$lastQuery )
		{
			this.$lastQuery = query;
			if ( this.$queryTimeout )
				this.$queryTimeout = $clear ( this.$queryTimeout );
			if ( query.length > 0 )
				this.$queryTimeout = this.sendQuery.delay ( 250 );
		}

		if ( this.$active )
			this.checkInput.delay ( 50 );
	},

	sendQuery: function ()
	{
		var query = this.$input.value;
		if ( query == this.$default || query.length == 0 )
			return;

		this.$queryTimeout = null;
		this.$lastQuery = query;
		this.$searchClient.send ( 'q=' + encodeURIComponent ( query ) )
	},

	trackQuery: function ()
	{
		var query = this.$holder.getElements ( '.effective-query' ).get ( 'text' );
		if ( !query.length )
			query = this.$lastQuery;
		else
			query = query [ 0 ];

		query = encodeURIComponent ( query );
		try
		{
			pageTracker._trackPageview ( "/search.php?q=" + query );
		}
		catch ( e )
		{
			//	...
		}
	}

});

SearchSystem.registerAll = function ()
{
	var containers = $$( '.search-input' );
	if ( containers )
		for ( i = 0; i < containers.length; i ++ )
		{
			var element = containers [ i ];
			var id = element.get ( 'id' );
			var name = id.split ( '_input' ) [ 0 ];

			new SearchSystem ( name );
		}

	delete SearchSystem.registerAll;
};



////
////
////	Run everything.

window.addEvent ( 'domready', function ()
{
	Panel.registerAll ();
	SearchSystem.registerAll ();
});















