var menuStatus =
{
	isAnim: false,
	sectionId: 0,
	menuId: 0,
	submenuId: 0,
	flashHeight: 0
}

var myChain = new Chain();

var contentLoader = null;

var formUploader = null;

var iconLoader = null;

var newsPage = 1;

var coreMenus = function()
{

	this.process = function( sectionId, menuId, submenuId, user_func )
	{
		if ( menuStatus.isAnim )
		{
			return false;
		}
		
//		alert( $( 'table-main' ).getSize().size.x );
		
		myChain.clearChain();
		
		if ( !$defined( submenuId ) )
		{
			if ( $defined( contentLoader ) ) contentLoader.cancel();
			
			menuStatus.isAnim = true;
			
			if ( menuStatus.sectionId > 0 )
			{
				myChain.chain( function()
				{
					if ( $defined( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ) ) )
					{
						$( 'submenu-cont-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'background-color': 'transparent' } );
						$( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'color': '#009BD7' } );
					}
					
					$( 'close-' + menuStatus.sectionId + '-' + menuStatus.menuId ).setStyles( { display: 'none' } );
					$( 'submenu-'  + menuStatus.sectionId + '-' + menuStatus.menuId ).setStyles( { display: 'none' } );
					
					$( 'content-'  + menuStatus.sectionId + '-' + menuStatus.menuId ).setText( '' );
					
					new Fx.Elements(
						$$(
							$( 'menu-' + menuStatus.sectionId + '-' + menuStatus.menuId ),
							$( ( menuStatus.sectionId == 1 || menuStatus.sectionId == 3 ? 'td-left' : 'td-right' ) ),						   							
							$( 'menu-title-' + menuStatus.sectionId + '-' + menuStatus.menuId ),
							$( 'line-' + menuStatus.sectionId ),
							$( 'table-main' ),
							$( 'td-middle' )
						), { duration: 500 } ).start( {
							'0':
							{
								'width':			[ '200px' ],
								'margin-top':		[ '0px' ],
								'margin-bottom':	[ '0px' ],
								'padding-top':		[ '0px' ],
								'padding-left':		[ '0px' ],
								'height':			[ '20px' ],
								'border-color':		[ '#FFFFFF' ],
								'background-color': [ '#FFFFFF' ]
							},
							'1':
							{
								'width': [ ( !( menuStatus.sectionId + sectionId == 4 || menuStatus.sectionId + sectionId == 6 ) &&
												menuStatus.sectionId != sectionId ||
												( menuStatus.sectionId == sectionId && menuStatus.menuId == menuId ) ) ? ( ( menuStatus.sectionId == 2 || menuStatus.sectionId == 4 ) ? '250px' : '250px' ) : '590px' ] 
							},
							'2':
							{
								'font-size':		[ '17px' ],
								'color':			[ '#009BD7' ]
							},
							'3':
							{
								'width': 			[ ( menuStatus.sectionId != sectionId ||
													  ( menuStatus.sectionId == sectionId && menuStatus.menuId == menuId ) ) ? '200px' : '580px' ]
							},
							'4':
							{
								'width': [ '520px' ]
							}
							}
							).addEvent( 'onComplete', function()
								{
									$( 'menu-title-' + menuStatus.sectionId + '-' + menuStatus.menuId ).setStyles( { textDecoration: 'underline' } );

									if ( menuStatus.sectionId == sectionId && menuStatus.menuId == menuId )
									{
										menuStatus.sectionId = 0;
										menuStatus.menuId = 0;
										menuStatus.submenuId = 0;
										menuStatus.isAnim = false;
										myChain.clearChain();
									}
									else
									{
										myChain.callChain();
									}
								} );
				}
				);
			}

			myChain.chain( function()
				{
					$( 'menu-title-' + sectionId + '-' + menuId ).setStyles( { textDecoration: 'none' } );
					
					new Fx.Styles( $( 'menu-' + sectionId + '-' + menuId ) ).set( { 'border-color': '#ffffff' } );
			
					$( 'div-tester' ).empty().adopt( $( 'submenu-' + sectionId + '-' + menuId ).clone().getChildren() );

					new Fx.Elements(
						$$(
							$( 'menu-' + sectionId + '-' + menuId ),
							$( ( sectionId == 1 || sectionId == 3 ? 'td-left' : 'td-right' ) ),
							$( 'menu-title-' + sectionId + '-' + menuId ),
							$( 'line-' + sectionId ),
							$( 'table-main' )
						), { duration: 300 } ).start( {
							'0':
								{
									'width':			[ '560px' ],
									'margin-bottom':	[ menuId != 1 ? '0px' : '10px' ],
									'margin-top':		[ menuId != 1 ? '10px' : '0px' ],
									'padding-top':		[ '10px' ],
									'padding-left':		[ '18px' ],
									'height':			[ $( 'div-tester' ).getSize().size.y + 60 ],
									'border-color':		[ '#d3d6d6' ],
									'background-color':	[ '#f8fcfb' ]
								},
							'1':
								{
									'width': [ '590px' ]
								},
							'2':
								{
									'font-size': [ '24px' ],
									'color': [ '#000000' ]
								},
							'3':
								{
									'width': [ '580px' ]
								},
							'4':
								{
									'width': [ '860px' ]
								}
					} ).addEvent( 'onComplete', function() { myChain.callChain(); } );
				} );

			myChain.chain( function()
				{
					$( 'close-' + sectionId + '-' + menuId ).setStyles( { display: 'block' } );
					$( 'submenu-' + sectionId + '-' + menuId ).setStyles( { display: 'block' } );					

					$( 'menu-title-' + sectionId + '-' + menuId ).setStyles( { textDecoration: 'none' } );

					menuStatus.isAnim = false;

					myChain.callChain();
				} );

			myChain.chain( function()
				{
					menuStatus.sectionId = sectionId;
					menuStatus.menuId = menuId;
					menuStatus.submenuId = 0;

					menus.loadContent( sectionId, menuId, 1, user_func );
				} );

			myChain.callChain();


		}
		else
		{
			menus.loadContent( sectionId, menuId, submenuId, user_func );
		}

		return false;
	},


	this.loadContent = function( sectionId, menuId, submenuId, user_func )
	{
		if ( menuStatus.sectionId == sectionId && menuStatus.menuId == menuId && menuStatus.submenuId == submenuId  || menuStatus.isAnim == true ) return;

		if ( $defined( contentLoader ) ) contentLoader.cancel();		

		if ( $defined( $( 'icon-loader' ) ) )
		{
			$( 'icon-loader' ).remove();
		}

		contentLoader = new XHR(
			{
				onRequest:
					function()
					{
						iconLoader = new Element( 'div' ).setProperty( 'id', 'icon-loader' );
						
						ajaxLoadingIcon.injectInside( iconLoader );

						if ( $defined( $( 'submenu-cont-' + sectionId + '-' + menuId + '-' + submenuId ) ) )
						{
							submenuEl = $( 'submenu-cont-' + sectionId + '-' + menuId + '-' + submenuId );
							iconLoader.setStyles( { 'position' : 'absolute', 'top' : submenuEl.getTop() + 7 , 'left' : submenuEl.getLeft() + submenuEl.getSize().size.x } );
						}
						else
						{
							submenuEl = $( 'menu-title-' + sectionId + '-' + menuId );
							iconLoader.setStyles( { 'position' : 'absolute', 'top' : submenuEl.getTop() + 7 , 'left' : submenuEl.getLeft() + submenuEl.getSize().size.x + 5 } );
						}
						
						iconLoader.injectInside( document.body );
					},
				onFailure:
					function()
					{
						alert( 'Произошла ошибка при загрузке данных с сервера.' );
						contentLoader = null;
						iconLoader.remove();
					},

				onCancel:
					function()
					{
						contentLoader = null;
						iconLoader.remove();
					},

				onSuccess:
					function()
					{
						menuStatus.isAnim = true;						

						iconLoader.remove();
						
						if ( $defined( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ) ) )
						{
							$( 'submenu-cont-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'background-color': 'transparent' } );
							$( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'color': '#009BD7' } );
						}

						menuStatus.submenuId = submenuId;

						content = $( 'content-' + menuStatus.sectionId + '-' + menuStatus.menuId );

						content.setHTML( this.response.text );

						contentLoader = null;

						new Fx.Style( 'menu-' + menuStatus.sectionId + '-' + menuStatus.menuId, 'height' ).
							start( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId ).getSize().size.y + content.getSize().size.y + 50 ).
								addEvent( 'onComplete', function(){ menuStatus.isAnim = false; } )
						
						if ( $defined( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ) ) )
						{
							$( 'submenu-cont-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'background-color': '#009BD7' } );
							$( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).setStyles( { 'color': 'white' } );
						}
						
						if ( 
								$defined( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ) ) && 
								$defined( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).rel ) && 
								$( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).rel != '' )
						{
							user_func = $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId + '-' + menuStatus.submenuId ).rel;
						}
						
						
						if ( $defined( user_func ) )
						{
							window[ user_func ]();
						}
					},

				method: 'post'
			} ).send( 'get_data.php', 'id=' + sectionId + '_' + menuId + '_' + submenuId );

	}
}


var PearBrowser = new Class({
	initialize: function(browser, slider, options) {
		this.browser = browser;
		this.slider = slider;
		this.options = options;
		
		var pearKnob = slider.getChildren()[0];
		this.pearKnob = pearKnob;

		this.setKnobWidth();

		this.engagePearBrowser();
	},
	// Calculates the knob size.
	setKnobWidth: function() {
		// Defines the knob.
		var pearKnobBg 			= this.pearKnob.getChildren()[1];
		
		// Calculates the total knob left and right width. 
		var pearKnobLeftSize 	= this.pearKnob.getChildren()[0].getCoordinates().width;
		var pearKnobRightSize 	= this.pearKnob.getChildren()[2].getCoordinates().width;
		var knobSidesWidth = pearKnobLeftSize + pearKnobRightSize;
		
		// How wide is the browser?
		var pearBrowserSize = this.browser.getCoordinates().width;
		// How wide is the container inside the browser?
		var pearContainerSize = this.browser.getChildren()[0].getCoordinates().width;
		// Calculates times wider the container is.
		var timesSize = pearContainerSize/pearBrowserSize;
		// Calculates how wide the knob should be.
		var pearKnobSize = this.pearKnob.getCoordinates().width / timesSize;
		
		// Sets the knob width.
		this.pearKnob.setStyle('width',pearKnobSize);
		pearKnobBg.setStyle('width',pearKnobSize-knobSidesWidth);
		
	},
	// Creates the PearBrowser.
	engagePearBrowser: function() {
		var browser = this.browser;
		// Calculates the amount of steps.
		var nrSteps = browser.getChildren()[0].getCoordinates().width - browser.getCoordinates().width;
		// Creates the slider
		
//		alert( this.nrSteps );

		var pearSlide = new Slider( this.slider, this.pearKnob, {
			steps: nrSteps,
			onChange: function(step) {
				// Scrolls the browser
				browser.getChildren()[0].setStyle('margin-left',step-(step*2));
			}
		}).set( 0 );
	}
	
});


function smoothbox_rescan()
{
	$$("a.smoothbox").each(function(el){el.onclick=TB_bind});
}

function pear_init()
{
	new PearBrowser( $('pearBrowser'), $('pearSlider') );
}

function send_data( sectionId, menuId, submenuId )
{
	$( 'form-' + sectionId + '-' + menuId + '-' + submenuId ).send().
		addEvent( 'onSuccess', 
			function() 
			{ 
				if ( this.response.text != 'ok' )
				{
					alert( this.response.json );
				}
				else
				{

					$( 'form_btn_' + sectionId + '_' + menuId + '_' + submenuId ).remove();

					fMsg = new Element('div').setProperty( 'id', 'form_message' );

					fMsg.setStyles( { 'display': 'none' } );

					fMsg.setHTML( '<div style="padding: 25px 0px 0px 0px; text-align: center;">Ваши данные были успешно отправлены.<br/><br/>Спасибо.<div style="padding: 20px 0px 0px 0px;"><form onSubmit="TB_remove(); return false;"><input type="submit" value="Окей" style="border: none; background-color: #009BD7; width: 80px; color: white;"/></div></form></div>' );

					fMsg.injectInside( document.body );

					TB_show( '', '#TB_inline?inlineId=form_message&height=125&width=300', '' );

					fMsg.remove();

					$( 'form-' + sectionId + '-' + menuId + '-' + submenuId ).getFormElements().each( function( el )
						{
							el.setProperty( 'disabled', 'disabled' );
						} );
				}
			} 
		);

	return false;
}

var Soot = 1663 / 370;

var ajaxLoadingIcon = null;

window.addEvent( 'domready',
	function() 
	{
		ajaxLoadingIcon = new Asset.image( 'img/ajax-loader.gif', { id : 'ajax-loading-icon', 'title' : 'Загрузка', 'border' : '0' } );
	
		menus = new	coreMenus();
		
		var so = new SWFObject( 'main.swf', 'flash-bottom', '100%', '485', '8', '#FFFFFF', 'high' );
		so.addParam( "scale", "noorder" );
		so.addParam( "wmode", "transparent" );
		so.addParam( "menu", "false" );		
		so.addParam("salign", "");
		so.addParam("base", "");		
		so.write( 'flash-container' );
		
		$( 'flash-container' ).setStyles( { 'height':  window.getWidth() / Soot } );		
		
		new Element( 'div' ).setProperty( 'id', 'div-tester' ).setStyles( { 'position' : 'absolute', 'overflow' : 'hidden', 'top' : '-20em', 'left' : '-20em' } ).injectInside( document.body );
		
		window.addEvent( 'resize', 
			function()
			{
				$( 'flash-container' ).setStyles( { 'height':  window.getWidth() / Soot } );
			} ); 
			
//		new SmoothScroll();			
	}
);


function showNews( id )
{
	if ( $defined( $( 'news-content-' + id ) ) )
	{
//		menuStatus.isAnim = true;
		
		var menu = $( 'menu-3-2' )
		var content = $( 'content-3-2' );
		var newsContent = $( 'news-content-' + id );
		
		var nCH = newsContent.getSize().size.y;
		
		if ( newsContent.getStyle( 'display' ) == 'none' )
		{
			newsContent.setStyle( 'display', 'block' ); 			
			menu.setStyle( 'height', menu.getSize().size.y + newsContent.getSize().size.y - 8 );
		}
		else
		{
			menu.setStyle( 'height', menu.getSize().size.y - newsContent.getSize().size.y - 16 );
			newsContent.setStyle( 'display', 'none' ); 			
		}
	}
}
// end of showNews() function



function showNewsPage( d )
{
	
	if ( $defined( contentLoader ) ) contentLoader.cancel();		

	if ( $defined( $( 'icon-loader' ) ) )
	{
		$( 'icon-loader' ).remove();
	}
	
	if ( d == 'next' )
	{
		reqPage = --newsPage;
	}
	else
	{
		reqPage = ++newsPage;		
	}

	contentLoader = new XHR(
		{
			onRequest:
				function()
				{
					iconLoader = new Element( 'div' ).setProperty( 'id', 'icon-loader' );
					
					ajaxLoadingIcon.injectInside( iconLoader );

					submenuEl = $( 'menu-title-3-2' );
					iconLoader.setStyles( { 'position' : 'absolute', 'top' : submenuEl.getTop() + 7 , 'left' : submenuEl.getLeft() + submenuEl.getSize().size.x + 5 } );
		
					iconLoader.injectInside( document.body );
				},

			onFailure:
				function()
				{
					alert( 'Произошла ошибка при загрузке данных с сервера.' );
					contentLoader = null;
					iconLoader.remove();
				},

			onCancel:
				function()
				{
					contentLoader = null;
					iconLoader.remove();
				},

			onSuccess:
				function()
				{
					menuStatus.isAnim = true;						

					iconLoader.remove();

					content = $( 'content-3-2' );

					content.setHTML( this.response.text );

					contentLoader = null;

					new Fx.Style( 'menu-' + menuStatus.sectionId + '-' + menuStatus.menuId, 'height' ).
						start( $( 'submenu-' + menuStatus.sectionId + '-' + menuStatus.menuId ).getSize().size.y + content.getSize().size.y + 50 ).
							addEvent( 'onComplete', function(){ menuStatus.isAnim = false; } )
				},

			method: 'post'
		} ).send( 'get_data.php', 'id=3_2_1&page=' + reqPage );


}