function auctions()
{
	this.colors = new Array();
	this.boxes = null;
	this.box = null;
	this.overlay = null;
	this.ids = new Array();
	this.xhrr = null;
	
	this.authAlert = function()
	{
		this.overlay.show();
		
		var div = new Element( 'div',
		{
			'styles': 
			{
				'width': 300,
				'text-align': 'center'
			},
			'text': 'Aby wziąć udział w licytacji musisz się zalogować'
		});
		
		this.overlay.insert('html',div);
		return false;
	};

	this.showAlert = function( text )
	{
		this.overlay.show();
		
		var div = new Element( 'div',
		{
			'styles': 
			{
				'width': 200,
				'height': 20,
				'text-align': 'center'
			},
			'text': text
		});
		
		this.overlay.insert('html',div);
		return false;
	};
	
	this.makeBid = function(auction_id)
	{
		var myRequest = new Request({
			url: '/frontend.php/xhr/makebid/'+auction_id,
			onSuccess: function(result) {
				if ( result == 'first_auth' )
				{
					this.authAlert();
				}
				else if ( result == 'not_enough_credits' )
				{
					auctions.showAlert('Nie masz już klików. Aby licytować w aukcji musisz doładować kliki');
				}
			}
		}).send();
	};
	
	this.showGallery = function()
	{
		if( milkbox )
		{
			milkbox.showGallery(
			{
				gallery: 'auction'
			} );
		}

		return false;
	};
	
	this.setPhoto = function( container, thumb, photo )
	{
		img = $( container.parentNode.parentNode.parentNode.parentNode ).getElement( '.auctionImage' );
		img.src = thumb;
		img.parentNode.href = photo;
		
		milkbox.reloadGalleries();
	};
	
	this.checkCounter = function()
	{
		setTimeout( 'auctions.checkCounter()', 1000 );
		for ( i = 0; i < this.boxes.length; i++ )
		{
			var id = this.boxes[i].get( 'class' ).split( ' ' )[1].replace( 'auction_', '' );
			
			var element = this.boxes[i].getElement('.time').getElement('span');
			if ( !element.firstChild )
			{
				continue;
			}
			var time = element.firstChild.nodeValue.split(':');
			if( time.length > 1 )
			{
				if ( time[0] == 0 && time[1] == 0 && time[2] < 31 )
				{
					!element.getParent( 'div.col-special-price' ) ? element.setStyle('color','#aa0000') : element.setStyle('color','#ff6666');
				}
				else
				{
					element.setStyle('color', null );
				}

				var unixtime = new Date();
				if ( unixtime.getHours() < 8) { continue; }

				
				if ( time[2] > 0 || time[1] > 0 || time[0] > 0 )
				{
					time = new Date( 0, 0, 0, time[0], time[1], time[2]-1 );
					hours = time.getHours();
					time.getDate() != 31 ? hours += time.getDate()*24 : null;
					minutes = time.getMinutes();
					seconds = time.getSeconds();
						
					if( hours || minutes || seconds )
					{
						hours < 10 ? hours = '0'+hours : null;
						minutes < 10 ? minutes = '0'+minutes : null;
						seconds < 10 ? seconds = '0'+seconds : null;
						
						element.removeChild( element.firstChild );
						element.set('text', hours+":"+minutes+":"+seconds );
					}
				}
			}
		}
	};
	
	this.reloadAuctions = function()
	{
		var boxes = $$('.auctionBox');
		if ( boxes.length > 0 )
		{
			var unixtime = new Date();
				unixtime = unixtime.getTime();
				unixtime = parseInt( unixtime / 1000 );
				
			if ( this.xhrr )
			{
				this.xhrr.cancel();
			}
			
			this.xhrr = new Request.JSON(
			{
				url: "/getAuctions.php",
				noCache: true,
				onSuccess: function( list )
				{
					setTimeout( 'auctions.reloadAuctions()', 1000 );
					for ( i = 0; i < list.length; i++ )
					{
						var box = $$( '.auction_'+list[i].id );
						var n = 0;
						for ( n = 0; n < box.length; n++ )
						{
							time = box[n].getElement( '.time' ).getElement( 'span' );

							if ( !auctions.colors[list[i].id] && !time.getParent( 'div.col-special-price' ) )
							{
								auctions.colors[list[i].id] = time.getStyle('color')
							}
							if ( list[i].pause == 1 )
							{ 
								time.get('text') == 'PAUZA' ? time.set( 'text', list[i].endtime ) : time.set( 'text', 'PAUZA' );
							}
							else
							{
								time.set('text', list[i].endtime );
							}
							price = box[n].getElement( '.price' ).getElement( 'span' );
							if (price.firstChild.nodeValue != list[i].price)
							{
								color1 = price.getStyle('color');
								color2 = time.getStyle('color'); 
								
								if( time.getParent( 'div.col-special-price' ) )
								{
									price.setStyle('color','#000000')
									var myFx1 = new Fx.Morph(price, { duration: 700, transition: Fx.Transitions.Sine.easeOut });
									myFx1.start({ 'color': color1 });
									time.setStyle('color','#000000')
									var myFx2 = new Fx.Morph(time, { duration: 700, transition: Fx.Transitions.Sine.easeOut });
									myFx2.start({ 'color': color2 });
								}
								else
								{
									price.setStyle('color','#ffffff')
									var myFx1 = new Fx.Morph(price, { duration: 700, transition: Fx.Transitions.Sine.easeOut });
									myFx1.start({ 'color': color1 });
									time.setStyle('color','#ffffff')
									var myFx2 = new Fx.Morph(time, { duration: 700, transition: Fx.Transitions.Sine.easeOut });
									myFx2.start({ 'color': color2 });
								}
							}
							
							while ( price.firstChild ) { price.removeChild ( price.firstChild ); }
							price.appendText ( list[i].price );

							span = box[n].getElement( '.user' ).getElement( 'span' )
							while( span.firstChild ) { span.removeChild( span.firstChild ); }
							span.appendText( list[i].username ? list[i].username : ' - brak - ' );
						}
					}
				}
			}).get({'time': unixtime,  'currency': $( 'currencyCode' ).get( 'value' ), 'auctions': auctions.ids });
		}
	};
	
	this.reloadAuction = function()
	{
		var clicks = $('auctionClicksHistory');
		if ( clicks )
		{
			setTimeout( 'auctions.reloadAuction()', 1000 );
			var unixtime = new Date();
				unixtime = unixtime.getTime();
				unixtime = parseInt( unixtime / 1000 );
			
			var xhr = new Request.JSON(
			{
				url: "/getAuction.php",
				noCache: true,
				onSuccess: function( list )
				{
					while ( clicks.firstChild )
					{
						clicks.removeChild ( clicks.firstChild );
					}
					for ( i = 0; i < list.length; i++ )
					{
						var tr = new Element( 'tr' );
							clicks.appendChild( tr );
						var td = new Element( 'td' );
							td.appendText( list[i].username );
							tr.appendChild( td );
						var td = new Element( 'td' );
							td.appendText( list[i].price );
							tr.appendChild( td );
						var td = new Element( 'td' );
							td.appendText( list[i].time );
							tr.appendChild( td );
						var td = new Element( 'td' );
							img = new Element ( 'img' );
							if ( list[i].automat == 1 )
							{
								img.setAttribute("src","/views/default/images/icons/computer.png");
								img.setAttribute("alt","autoklik");
								img.setAttribute("title","autoklik");
							}
							else
							{
								img.setAttribute("src","/views/default/images/icons/person.png");
								img.setAttribute("alt","klik");
								img.setAttribute("title","klik");
							}
							td.appendChild( img );
							tr.appendChild( td );
					}
				}
			}).get({'id': clicks.getParent('div.auction-history').getElement('.auctionId').get('text'),
					'currency': $( 'currencyCode' ).get( 'value' )});
		}
	};

	this.setAutomat = function( id )
	{
		auctions.overlay.show();
		var request = new Request( 
		{ 
			method: 'get', 
			url: '/auction/automat/'+id+'.html', 
			onRequest: function() 
			{ 
				auctions.overlay.insert( 'text', '<img src="/views/default/images/ajax-loader.gif" alt="loading" />' ); 
			}, 
			onSuccess: function( responseText ) 
			{ 
				auctions.editSuccess( responseText ); 
			} 
		}); 
		request.send(); 
		
		return false;
	};
	
	this.editSuccess = function( response )
	{
		var div = new Element( 'div',
		{
			'html': response
		} );

		document.body.appendChild( div );

		var divs = div.getElements( '.globalModule' )[0];
		var forms = divs.getElements( 'form' );
		var i = 0;                
			while( forms[i] )
			{
				forms[i].addEvent( 'submit', function( e )
				{
					new Event( e ).stop();

					this.set( 'send',
					{
						url: this.action,
						method: 'post',
						onRequest: function()
						{
							auctions.overlay.insert( 'text', '<img src="/icon/ajax-loader.gif" alt="loading" />' );
						},
						onSuccess: function(responseText, responseXML)
						{
							auctions.editSuccess( responseText );
						}
					} ).send();

					return false;
				} );

				i++;
			}

		var a = divs.getElements( 'a' );
			i = 0;
			while( a[i] )
			{
				a[i].addEvent( 'click', function( e )
				{
					new Event( e ).stop();
					
					if ( this.hasClass('exit') )
					{
						auctions.overlay.hidden();
					}
					else
					{
						var request = new Request(
						{
							method: 'get',
							url: this.get( 'href' ),
							onRequest: function()
							{
								auctions.overlay.insert( 'text', '<img src="/icon/ajax-loader.gif" alt="loading" />' );
							},
							onSuccess: function( responseText )
							{
								auctions.editSuccess( responseText );
							}
						} );
						request.send();
					}
					return false;
				} );

				i++;
			}

		this.overlay.insert( 'html', divs );

		document.body.removeChild( div );
	};
   
}

var auctions = new auctions;

addEvent('domready', function() {
	auctions.boxes = $$('.auctionBox');
	for ( i = 0; i < auctions.boxes.length; i++ )
	{
		auctions.ids[ auctions.ids.length ] = auctions.boxes[ i ].getElement('.auctionId').value;
	}
	auctions.reloadAuctions();
	auctions.checkCounter();
	
	clicks = $('auctionClicksHistory');
	if ( clicks )
	{
		auctions.reloadAuction();
	}
	
	auctions.overlay = new mooOverlay();
});