$(window).addEvent('domready', function() 
{

	var FxState = new Class( {

						'speed' : {
									'adjustHeight'   : 50,
									'shiftColor'     : 300,
									'moveWatermarks' : 0
									},
						'watermark'  : [ 0, 0 ],
						'colorShift' : 0,

						'save' : function()
						{
							document.cookie = 'toxABGFXstate=' + [ this.watermark[0], this.watermark[1], this.colorShift ].join( '|' );
						},

						'initialize' : function()
						{
							var cookies = document.cookie.split( ';' );
							var pattern = /^\s*([^\s=]+)\s*=(.+)$/;

							for ( var i = 0; i < cookies.length; i++ )
								if ( pattern.exec( cookies[i] ) )
									if ( RegExp.$1 == 'toxABGFXstate' )
									{
										var parts = RegExp.$2.split( '|' );

										this.watermark  = [ parseFloat( parts[0] ), parseFloat( parts[1] ) ];
										this.colorShift = parseFloat( parts[2] );

										if ( !isNaN( this.watermark[0] ) && !isNaN( this.watermark[1] ) && !isNaN( this.colorShift ) )
											return;
									}

							this.watermark  = [0,0];//[ Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI ];
							this.colorShift = 0;
						}

					} );

	var state = new FxState();

	var animations = {
		'heightAdjustor' : [
								function( initial, state )
								{
									var currentViewHeight = Math.max(
																$(window).getScroll().y + $(window).getSize().y,
																initial.headerHeight + initial.contentHeight + initial.footerHeight
																);

									$$('body','#page','#secwm').setStyle( 'height', currentViewHeight );
									$$('#content .blocks .center > div.panels').setStyle( 'height', currentViewHeight - initial.footerHeight - initial.headerHeight );

									if ( state.speed.adjustHeight > 0 )
										window.setTimeout( this.bind( this, [ initial, state ] ), state.speed.adjustHeight );
								},
								[ {
									'headerHeight'  : $('header').getSize().y + $('banner').getSize().y,
									'contentHeight' : $$('#content .blocks .center > div.panels').getSize().shift().y,
									'footerHeight'  : $('footer').getSize().y
								}, state ]
							],

		'colorShifter' : [
								function( state, palette )
								{
									var cl = ( state.colorShift % ( Math.PI / 2 ) );
									var cs = Math.floor( state.colorShift / ( Math.PI / 2 ) ) % ( palette.length - 1 );
									var cf = 1 - ( Math.cos( cl ) * Math.cos( cl ) );

									state.colorShift += 0.012;

									var r = Math.floor( palette[cs][0] + cf * ( palette[cs+1][0] - palette[cs][0] ) );
									var g = Math.floor( palette[cs][1] + cf * ( palette[cs+1][1] - palette[cs][1] ) );
									var b = Math.floor( palette[cs][2] + cf * ( palette[cs+1][2] - palette[cs][2] ) );

									$$('body').setStyle( 'backgroundColor', 'rgb(' + r + ',' + g + ',' + b + ')' );

									state.save();

									if ( state.speed.shiftColor > 0 )
										window.setTimeout( this.bind( this, [ state, palette ] ), state.speed.shiftColor );
								},
								[ state, [
									[0,0x36,0x63],
									[0,0,0],
									[48,0,0],
									[0,52,0],
									[128,64,0],
									[0,0x36,0x63]
									] ]
							],

		'watermarksMover' : [
								function( state, ellipses )
								{
									$$('body','#secwm').each( function( item, index ) {
										var x = Math.round( ellipses[index].x * Math.sin( state.watermark[index] ) );
										var y = Math.round( ellipses[index].y * Math.cos( state.watermark[index] ) );

										$(item).setStyle( 'backgroundPosition', x + 'px ' + y + 'px' );

										state.watermark[index] = ( state.watermark[0] + 0.001 ) % ( 2 * Math.PI );
									} );

									state.save();

									if ( state.speed.moveWatermarks > 0 )
										window.setTimeout( this.bind( this, [ state, ellipses ] ), state.speed.moveWatermarks );
								},
								[ state, [ { x: 60, y: 120 }, { x:140, y:70 } ] ]
							]
	};

	for ( var fn in animations ) animations[fn][0].bind( animations[fn][0], animations[fn][1] )();


	$$('.mod_cataloglist .item').each( function( item )
	{
		item = $(item);

		var isLeft  = item.hasClass( 'even' );
		var sibling = isLeft ? item.getNext() : item.getPrevious();

		var itemPos = item.getCoordinates( item.getParents('.mod_cataloglist')[0] );

		var teaser  = item.getElement('.teaser');

		teaser.fade('hide');

		item.getChildren('h3').inject(teaser,'top');

		var img = item.getElement('img');
		if ( img )
			img.addEvents({
				mouseenter : function()
				{
					item.addClass('framed');
					if ( sibling ) sibling.fade('hide');
					teaser.fade('show');
				},
				mouseleave : function()
				{
					item.removeClass('framed');
					if ( sibling ) sibling.fade('show');
					teaser.fade('hide');
				},
				click : function()
				{
					var href = item.getElement('.more a');
					if ( href )
					{
						if ( item.getParents('.partners-list,.customers-list').length > 0 )
							window.open(href);
						else
							location.href = href;
						return false;
					}
				}
			});
	});
} );

