var AuctionFinder = Class.create();

AuctionFinder.prototype =
{
	initialize: function()
	{
		this.content = 'auctionFinder';
		this.width = 630;
		this.height = 200;
		this.movement = 150;
		this.open = false;
		this.category = '';
		
		this.drawer = $$('#'+this.content+' .drawer')[0];		
		this.loading = $$('#'+this.content+' .loading')[0];
		
		var obj = this;

		this.input = $$('#'+this.content+' input')[0];
		this.input.onclick = function()
		{
			if (this.input.value == this.input.defaultValue) this.input.value = '';
			else this.input.select();
		}.bindAsEventListener(this);
		
		this.input.onkeyup = function(e)
		{
			var keycode = e.charCode || e.keyCode;
			var keypress = String.fromCharCode(keycode);
			if (keycode == 13 || keycode == 10 || keycode == 3)
			{
				this.category = '';
				this.query();				
			}				
		}.bindAsEventListener(this);
		
		this.close = $$('#'+this.content+' a.closelink')[0];
		this.close.onclick = function(e) 
		{ 
			Event.stop(e);
			window.lightbox.hide(); 
		}.bindAsEventListener(this);
		
		this.search = $$('#'+this.content+' a.browserlink')[0];
		this.search.onclick = function(e) 
		{ 
			Event.stop(e);
			this.category = '';
			this.query();
		}.bindAsEventListener(this);		

		window.lightbox = new app.lightbox(this.content, this.width, this.height,
			function()
			{
				obj.item = false;
				window.widget.swf.restore();
				if (window.widget.config.seller) 
				{					
					this.height = obj.height + 2 * obj.movement;
					obj.drawer.show();
					obj.open = true;
					
					if (!obj.saved) obj.saved = obj.drawer.cloneNode(true);
					
					if (obj.savedseller != window.widget.config.seller)
					{
						if (obj.savedseller) obj.replace(obj.saved);						
						obj.savedseller = window.widget.config.seller;						
						obj.query();
					}					
				}				
			},
			function()
			{
				window.widget.swf.draw();
				obj.drawer.hide();
				obj.open = false;
				obj.input.value = obj.input.defaultValue;

				if (!window.widget.config.seller) 
					obj.replace(obj.saved);

				if (window.widget.config.seller)
				{
					this.height = obj.height;
				}
				
				if (obj.y > 0)
					window.lightbox.top(obj.y);
			}
		);			
		
	},
	
	getitem: function(id)
	{
		if (!id) return;
				
		var obj = this;
		this.backpage = this.drawer.cloneNode(true);
		
		var results = $$('#'+this.content+' .results')[0];
		if (results) this.savescroll = results.scrollTop;

		var args = $H({
			item: id,
			TZ: TZ
		});

		app.blockLoad('item', this.drawer, '/app/auctionfinder.php?'+args.toQueryString(),
			function(json)
			{
				if (json && json.error)
				{
					obj.showerror();
					return;
				}

				if (!json.img)
				{
					var imageFrame = $$('#'+obj.content+' .viewItem .imageFrame')[0];
					imageFrame.style.top = 0;
				}
				else
				{
					var image = document.createElement('img');
					image.src = json.img;
					var imageContainer = $$('#'+obj.content+' .viewItem .theImage')[0];
					var imageFrame = $$('#'+obj.content+' .viewItem .imageFrame')[0];
					
					var savetop = parseInt(imageFrame.getStyle('top'), 10);
					imageFrame.style.top = '0px';

					image.onload = function()
					{
						if (!image || !imageContainer || !imageFrame) return;

						if (!image.style || !imageContainer.style || !imageFrame.style) return;

						var cached = new Image();
						cached.src = json.img;

						var width = parseInt(imageContainer.getStyle('width'), 10);
						var height = parseInt(imageContainer.getStyle('height'), 10);

						var imageWidth = image.width || cached.width;
						var imageHeight = image.height || cached.height

						if (imageWidth && imageHeight && width && height)
						{								
							if (image.width > image.height)
							{
								image.height = (imageHeight * (width / imageWidth));									
								image.width = width;
							}
							else
							{
								image.width = (imageWidth * (height / imageHeight));
								image.height = height;							
							}

							imageFrame.style.top = (savetop + (height - image.height)) + 'px';
							image.style.position = 'relative';															
							image.style.left = Math.floor((width - image.width)/2) + 'px';
							image.style.top = Math.floor((height - image.height)/2) + 'px';
						}
						else
						{
							image.width = width;
							image.height = height;
							imageFrame.style.top = savetop + 'px';
						}
						if (imageContainer && imageContainer.insertBefore && imageContainer.firstChild)
							imageContainer.insertBefore(image, imageContainer.firstChild);
						
						imageFrame.style.top = (parseInt(imageFrame.style.top, 10) - imageFrame.offsetTop) + 'px';
					}
				}

				var title = $$('#'+obj.content+' .viewItem h1')[0];

				title.onclick = function(e)
				{
					if (e.shiftKey)
						window.open(json.url, 'toGoAuction');
				}.bindAsEventListener(this);

				var goBack = $$('#'+obj.content+' .goBack a')[0];

				goBack.onclick = function(e) 
				{ 
					Event.stop(e);
					obj.replace(obj.backpage);					
					obj.queryEvents();
					
					window.scrollBy(0,0); // SAFARI FIX
					
					var results = $$('#'+obj.content+' .results')[0];
					if (results) results.scrollTop = obj.savescroll;					
					
				}.bindAsEventListener(this);				

				var selectAuction = $$('#'+obj.content+' .selectAuction a')[0];

				selectAuction.onclick = function(e) 
				{ 
					Event.stop(e);
					obj.item = json;
					window.lightbox.hide();						
				}.bindAsEventListener(this);

				var breadcrumbs = $$('#'+obj.content+' .breadcrumbs a');

				breadcrumbs.each(function(x) {
					x.onclick = function(e)
					{
						Event.stop(e);
						obj.category = x.rel;
						obj.query();
					}.bindAsEventListener(this);
				});				
			}, false, false, this.loading
		);
	},
	
	query: function(page)
	{
		if (this.category == -1) this.input.value = this.input.defaultValue;
		
		var query = (this.input.value == this.input.defaultValue) ? '' : this.input.value.__trim();
		if (!query && !window.widget.config.seller) return;

		var obj = this;

		var args = $H({
			query: query,
			page: page,
			seller: window.widget.config.seller,
			category: this.category,
			TZ: TZ
		});

		var callback = function()
		{
			obj.open = true;
						
			app.blockLoad('list', obj.drawer, '/app/auctionfinder.php?'+args.toQueryString(),
				function(json)
				{					
					if (json && json.error)
					{
						obj.showerror();
						return;
					}					
					obj.queryEvents();
				}, false, false, obj.loading
			);
		}

		if (this.open)
		{
			callback();
		}
		else
		{
			this.saved = this.drawer.cloneNode(true);			
			this.y = window.lightbox.top() > this.movement ? this.movement : window.lightbox.top();

			if (this.y > 0)
			{
				new Effect.Parallel([
					new Effect.MoveBy(window.lightbox.lightbox, -this.y, 0, {sync:true}),
					new Effect.BlindDown(this.drawer, {scaleFromCenter: true, sync:true})],
					{ afterFinish: callback });
			}
			else
			{
				new Effect.BlindDown(this.drawer, {scaleFromCenter: true, afterFinish: callback});
			}
		}
	},

	queryEvents: function(json)
	{
		var obj = this;

		var paging = $$('#'+this.content+' .paging a');
		paging.each(function(x) {
			if (Prototype.Browser.WebKit) // SAFARI BUG WITH OVERFLOW AUTO
				x.setStyle({position: 'relative', zIndex: '20001'});			
			x.onclick = function(e) 
			{ 
				Event.stop(e);
				obj.query(x.rel);
			}.bindAsEventListener(this);
		});

		var breadcrumbs = $$('#'+this.content+' .breadcrumbs a');
		breadcrumbs.each(function(x) {
			x.onclick = function(e)
			{
				Event.stop(e);
				obj.category = x.rel;
				obj.query();
			}.bindAsEventListener(this);
		});
		
		var categories = $$('#'+this.content+' .categories a');
		categories.each(function(x) {
			x.onclick = function(e)
			{
				Event.stop(e);
				obj.category = x.rel;
				obj.query();
			}.bindAsEventListener(this);
		});

		var items = $$('#'+this.content+' .item');
		items.each(function(x) {
			if (x.className != 'item') x.className = 'item';
			if (Prototype.Browser.WebKit) // SAFARI BUG WITH OVERFLOW AUTO
				x.setStyle({position: 'relative', zIndex: '20000'});

			x.onclick = function()
			{
				obj.getitem(x.id.substr(1));
			};
		});	
	},
			
	replace: function(node)
	{
		if (!node) return;
		this.drawer.up().replaceChild(node, this.drawer);
		this.drawer = node;		
	},
	
	showerror: function()
	{
		var obj = this;
		
		var error = $$('#'+this.content+' .errorContainer')[0];
		Effect.Appear(error, {from: 0.0, to: 0.999999, duration: 0.3});
		error.onclick = function() { Effect.Fade(error, {duration: 0.3}); }
		this.input.onclick = function() 
		{ 
			obj.input.select(); 
			Effect.Fade(error, {duration: 0.3, afterFinish: function() {
				obj.input.onclick = function() { obj.input.select() };
			}}); 
		};
	}
}

window.windowOnload.push(function()
{
	window.auctionFinder = new AuctionFinder();
});
