


var blackFader = {
	_element : null,
	visible : false,
	show : function ( opacity, speed, callback ) {
		if ( blackFader.visible ) {
			return false;
		}
		blackFader.visible = true;
		var vps = $.pageSize();
		blackFader._getElement().css( { 'opacity':0, 'z-index':( $.maxZIndex() + 1 ), 'display' : 'block', 'width' : vps.width + 'px', 'height' : vps.height + 'px' } ).animate( { opacity : opacity || "0.8" } , speed || 500, null, callback );
	},
	hide : function ( speed, callback ) {
		if ( !blackFader.visible ) {
			return false;
		}
		blackFader._getElement().animate( { opacity : "0.0" } , speed || 500, null, function() {
			blackFader._getElement().hide();
			blackFader.visible = false;
			if ( callback )
				callback();
		} );
	},
	_getElement : function() {
		return blackFader._element ? blackFader._element : ( blackFader._element = $("#black_fader") ); 
	}
};



var whiteFader = {
	_element : null,
	visible : false,
	show : function ( opacity, speed, callback ) {
		if ( whiteFader.visible ) {
			return false;
		}
		whiteFader.visible = true;
		var vps = $.pageSize();
		whiteFader._getElement().css( { 'opacity':0, 'z-index':( $.maxZIndex() + 1 ), 'display' : 'block', 'width' : vps.width + 'px', 'height' : vps.height + 'px' } ).animate( { opacity : opacity || "0.8" } , speed || 500, null, callback );
	},
	hide : function ( speed, callback ) {
		if ( !whiteFader.visible ) {
			return false;
		}
		whiteFader._getElement().animate( { opacity : "0.0" } , speed || 500, null, function() {
			whiteFader._getElement().hide();
			whiteFader.visible = false;
			if ( callback )
				callback();
		} );
	},
	_getElement : function() {
		return whiteFader._element ? whiteFader._element : ( whiteFader._element = $("#white_fader") ); 
	}
};



var portfolioCtrl = {
	inSlide : false,
	position : 0,
	maxPosition : null,
	_container : null,
	_leftControl : null,
	_rightControl : null,
	_slider : null,
	_step : null,
	fix : function () {
		portfolioCtrl._step = null;

		//fix IE redraw
		$("#portfolio_viewport > div.slider > a").css( {'border': '1px solid black' } );
		$("#portfolio_viewport > div.slider > a").css( {'border': 'none' } );

		portfolioCtrl._getSlider().css({'left': ( -portfolioCtrl.position*portfolioCtrl._getStepValue()  +'px' ) });
	},
	capture : function () {
		portfolioCtrl._getLeftControl().click( portfolioCtrl.next );
		portfolioCtrl._getRightControl().click( portfolioCtrl.prev );
		portfolioCtrl.maxPosition = $("#portfolio_viewport > div.slider > a" ).length - 1;
		portfolioCtrl._getSlider().css({'left':'0px'});
		portfolioCtrl.fixControl();
	},
	fixControl : function () {
		portfolioCtrl.position = Math.round( Math.abs( parseInt( portfolioCtrl._getSlider().css( 'left' ) ) ) / portfolioCtrl._getStepValue() );
		portfolioCtrl._getLeftControl().css( { 'visibility' : ( portfolioCtrl.position == portfolioCtrl.maxPosition ? 'hidden' : 'visible' ) } );
		portfolioCtrl._getRightControl().css( { 'visibility' : ( portfolioCtrl.position == 0 ? 'hidden' : 'visible' ) } );
	},
	next : function () {
		this.blur();
		if ( portfolioCtrl.inSlide )
			return false;
		portfolioCtrl.inSlide = true;
		portfolioCtrl.position++;
		portfolioCtrl._getSlider().animate( {'left': ( -portfolioCtrl.position*portfolioCtrl._getStepValue()  +'px' ) }, 500, null, function() {
			portfolioCtrl.inSlide = false;
			portfolioCtrl.fixControl();
		} );
		return false;
	},
	prev : function () {
		this.blur();
		if ( portfolioCtrl.inSlide )
			return false;
		portfolioCtrl.inSlide = true;
		portfolioCtrl.position--;
		portfolioCtrl._getSlider().animate( {'left': ( -portfolioCtrl.position*portfolioCtrl._getStepValue()  +'px' ) }, 500, null, function() {
			portfolioCtrl.inSlide = false;
			portfolioCtrl.fixControl();
		} );
		return false;
	},
	_getContainer : function () {
		return portfolioCtrl._container ? portfolioCtrl._container : ( portfolioCtrl._container = $("#portfolio_viewport") ); 
	},
	_getLeftControl : function () {
		return portfolioCtrl._leftControl ? portfolioCtrl._leftControl : ( portfolioCtrl._leftControl = $("#portfolio_control > div.left > a") ); 
	},
	_getRightControl : function () {
		return portfolioCtrl._rightControl ? portfolioCtrl._rightControl : ( portfolioCtrl._rightControl = $("#portfolio_control > div.right > a") ); 
	},
	_getSlider : function () {
		return portfolioCtrl._slider ? portfolioCtrl._slider : ( portfolioCtrl._slider = $("#portfolio_viewport > div.slider" ) ); 
	},
	_getStepValue : function () {
		if ( portfolioCtrl._step )
			return portfolioCtrl._step;
		var firstElem = $("#portfolio_viewport > div.slider > a:first" );
		return ( portfolioCtrl._step = ( firstElem.outerWidth() + parseInt( firstElem.css( 'margin-left' ) ) + parseInt( firstElem.css( 'margin-right' ) ) ) ); 
	}
};



var topNewsCtrl = {
	inSlide : false,
	position : 0,
	maxPosition : null,
	_container : null,
	_leftControl : null,
	_rightControl : null,
	_slider : null,
	_step : null,
	capture : function () {
		topNewsCtrl._getLeftControl().click( topNewsCtrl.prev );
		topNewsCtrl._getRightControl().click( topNewsCtrl.next );
		topNewsCtrl.maxPosition = $("#topnews_viewport > div.slider > div.item" ).length - 1;
		topNewsCtrl._getSlider().css({'left':'0px'});
		topNewsCtrl.fixControl();
	},
	fixControl : function () {
		topNewsCtrl.position = Math.round( Math.abs( parseInt( topNewsCtrl._getSlider().css( 'left' ) ) ) / topNewsCtrl._getStepValue() );
		var
			atEnd = topNewsCtrl.position == ( topNewsCtrl.maxPosition - 2 ),
			atBegin = topNewsCtrl.position == 0;
		topNewsCtrl._getRightControl().css( { 'opacity' : ( atEnd ? '0.3' : '1' ), 'cursor' : ( atEnd ? 'default' : 'pointer' ) } );
		topNewsCtrl._getLeftControl().css( { 'opacity' : ( atBegin ? '0.3' : '1' ), 'cursor' : ( atBegin ? 'default' : 'pointer' ) } );
	},
	next : function () {
		this.blur();
		if ( topNewsCtrl.inSlide || topNewsCtrl.position == ( topNewsCtrl.maxPosition - 2 ) )
			return false;
		topNewsCtrl.inSlide = true;
		topNewsCtrl.position++;
		topNewsCtrl._getSlider().animate( {'left': ( -topNewsCtrl.position*topNewsCtrl._getStepValue()  +'px' ) }, 400, null, function() {
			topNewsCtrl.inSlide = false;
			topNewsCtrl.fixControl();
		} );
		return false;
	},
	prev : function () {
		this.blur();
		if ( topNewsCtrl.inSlide || topNewsCtrl.position == 0 )
			return false;
		topNewsCtrl.inSlide = true;
		topNewsCtrl.position--;
		topNewsCtrl._getSlider().animate( {'left': ( -topNewsCtrl.position*topNewsCtrl._getStepValue()  +'px' ) }, 400, null, function() {
			topNewsCtrl.inSlide = false;
			topNewsCtrl.fixControl();
		} );
		return false;
	},
	_getContainer : function () {
		return topNewsCtrl._container ? topNewsCtrl._container : ( topNewsCtrl._container = $("#topnews_viewport") ); 
	},
	_getLeftControl : function () {
		return topNewsCtrl._leftControl ? topNewsCtrl._leftControl : ( topNewsCtrl._leftControl = $("#top_news a.left") ); 
	},
	_getRightControl : function () {
		return topNewsCtrl._rightControl ? topNewsCtrl._rightControl : ( topNewsCtrl._rightControl = $("#top_news a.right") ); 
	},
	_getSlider : function () {
		return topNewsCtrl._slider ? topNewsCtrl._slider : ( topNewsCtrl._slider = $("#topnews_viewport > div.slider" ) ); 
	},
	_getStepValue : function () {
		if ( topNewsCtrl._step )
			return topNewsCtrl._step;
		var firstElem = $("#topnews_viewport > div.slider > div.item:first" );
		return ( topNewsCtrl._step = ( firstElem.outerWidth() + parseInt( firstElem.css( 'margin-left' ) ) + parseInt( firstElem.css( 'margin-right' ) ) ) ); 
	}
};



function switcherDayNight( set ) {
	if ( $.cookie( 'night' ) == 'yes' ) {
		whiteFader.show( 1, 400, function() {
			$.cookie( 'night', 'no' );
			$( 'body' ).get( 0 ).className = 'day';
			if ( $("#main_rota_image").length )
				$("#main_rota_image").css( { 'background-image' : 'url(' + mainRotaImage.day + ')' } );
			portfolioCtrl.fix();
			whiteFader.hide( 200 );
		} );
	} else {
		blackFader.show( 1, 400, function() {
			$.cookie( 'night', 'yes' );
			$( 'body' ).get( 0 ).className = 'night';
			if ( $("#main_rota_image").length )
				$("#main_rota_image").css( { 'background-image' : 'url(' + mainRotaImage.night + ')' } );
			portfolioCtrl.fix();
			blackFader.hide( 200 );
		} );
	}
	return false;
}

var contact_form = {
	_form_elem : null,
	open : function () {
		( $.cookie( 'night' ) == 'yes' ? blackFader : whiteFader ).show( 0.8, 500 );
		var ps = $.pageSize(), vps = $.viewPortSize();
		contact_form._getFormElem().css( {
			'z-index' : ( $.maxZIndex() + 1 ),
			'display' : 'block',
			'width' : ps.width + 'px',
			'left' : '0px',
			'top' : vps.centerTop + 'px'
		} ).find( "div.content" ).addClass( "before" ).removeClass( "after" );
		contact_form._getFormElem().find( 'a.submit-button' ).show();
		contact_form._getFormElem().find( 'a.cancel-button' ).show();
		return false;
	},
	
	success : function () {
		contact_form._getFormElem()
		.find( "div.content" )
		.addClass( "after" )
		.removeClass( "proccessing" );
		contact_form._getFormElem().find( 'a.cancel-button' ).hide();
	},
	
	close : function () {
		contact_form._getFormElem().hide();
		( $.cookie( 'night' ) == 'yes' ? blackFader : whiteFader ).hide( 250 );
	},
	
	_getFormElem : function() {
		return contact_form._form_elem || ( contact_form._form_elem = $("#contact_form") );
	}
};

var portInAnimation = false;

$( document ).ready(
	function () {
		$( "div.day-link-container a, div.night-link-container a" ).click( switcherDayNight );
//		switcherDayNight( 1 );
		portfolioCtrl.capture();
		topNewsCtrl.capture();
		$("#offer_menu a").click( function() {
			$("div.offer-groups div.offer-group").hide();
			$("#offer_menu a").removeClass( 'active' );
			$( this ).prependTo( $( this ).parent() ).addClass( 'active' );
			$("#offer_group_" + this.href.match(/group,([^/]+)/)[1] ).fadeIn('normal');
			return false;
		} )

		$(".portfolio-wraper a.closed-only").click( function() {
			if ( portInAnimation )
				return false;
			portInAnimation = true;

			var
			opened, before = false; 
			elem = this;
			$( ".portfolio-wraper div.item a.closed-only" ).each(
				function () {
					if ( $( this ).parent().hasClass( 'opened' ) )
						opened = this;
					if ( this === elem )
						before = !opened;
				}
			);
			
			if ( before ) {
				// вначале открываем, потом закрываем
				
			} else {
				// вначале закрываем, потом открываем
				
			}
			
			$( this ).parent().find( '.top-spacer' ).css( { 'height' : '0px' } ).animate( { 'height' : '52px' } );
			$( this ).parent().find( '.bottom-spacer' ).css( { 'height' : '0px' } ).animate( { 'height' : '60px' } );
			$( this ).parent().addClass( 'in-animation' ).find( 'a.closed-only img' )
			.animate(
				{ 'width' : '630px', 'height' : '413px', 'opacity' : '0' },
				before ? 300 : 500,
				function () {
					$( this ).parent().parent().css( { 'opacity' : '0' } ).removeClass( 'closed' ).addClass( 'opened' ).animate( { 'opacity' : '1' }, 150, function () {
						$( this ).find( 'a.closed-only img' ).attr( 'style', null );
						var pi = portImagesCache[ $( this ).attr( 'id' ) ];
						$( this ).find( '.huge-image' ).attr( { 'src' : pi.first.huge, 'alt' : pi.first.alt } );
						$( this ).find( '.set img' ).each( function () {
							$( this ).attr( 'src', pi.set[ this.id ].small );
						} );
						portInAnimation = false;
						$( this ).attr( 'style', null ).removeClass( 'in-animation' );
					} );
				}
			);
			
			$( ".portfolio-wraper div.opened:not(.in-animation)" ).addClass( 'in-animation' )
				.animate(
					{ 'width' : '123px', 'height' : '74px', 'opacity' : '0.1' },
					before ? 500 : 300,
					function () {
						$( this ).removeClass( 'opened' ).addClass( 'closed' ).removeClass( 'in-animation' );
						$( this ).animate( { 'opacity' : '1' }, 100, function () {
							$( this ).attr( 'style', null );
							portInAnimation = false;
						} );
					}
				)
				.find( '.top-spacer, .bottom-spacer' ).animate( { 'height' : '0px' } );
			
			
			return false;
			elem = $( opened ).parent().get( 0 );
			
			var gcb = function ( celem, cb ) {
				return function() {
					$(".portfolio-wraper div.item").removeClass( 'opened' ).addClass( 'closed' );
					$( celem ).addClass( 'opened' ).removeClass( 'closed' );
					if ( cb )
						setTimeout( cb, 70 );
				};
			};
			var
				reached = false,
				order = [
					gcb( $( this ).parent(),
					function () {
						var newlyOpened = $( ".portfolio-wraper div.item.opened" ), pi = portImagesCache[ newlyOpened.attr( 'id' ) ];
						newlyOpened.find( '.huge-image' ).attr( { 'src' : pi.first.huge, 'alt' : pi.first.alt } );
						newlyOpened.find( '.set img' ).each( function () {
							$( this ).attr( 'src', pi.set[ this.id ].small );
						} );
						
					}
					)
				];
			$( this ).parent()[ !before ? 'prevAll' : 'nextAll' ]().each(
				function () {
					if ( !reached ) {
						order.push( gcb( this, order.length > 0 ? order[ order.length - 1 ] : null ) );
					}
					reached = ( this === elem ) || reached;
				}
			);
			order[ order.length - 1 ]();
			
			return false;
		} )

		$( ".portfolio-wraper  div.set img" ).click( function() {
			if ( portInAnimation )
				return false;
			var mat = $( this ).parent().parent().parent().parent(), pi = portImagesCache[ mat.attr( 'id' ) ][ 'set' ][ this.id ];
			$( this ).parent().parent().find( '.night-glow, img' ).removeClass( 'current' );
			$( this ).addClass( 'current' );
			$( this ).parent().addClass( 'current' );
			mat.find( '.huge-image' ).data( { 'oldSrc' : pi.huge, 'oldAlt' : pi.alt } );
			return false;
		} )
		$( ".portfolio-wraper div.set img" ).mouseover( function() {
			if ( portInAnimation )
				return false;
			var
				mat = $( this ).parent().parent().parent().parent(),
				pi = portImagesCache[ mat.attr( 'id' ) ][ 'set' ][ $( this ).attr( 'id' ) ],
				hi = mat.find( '.huge-image' );
			$( this ).addClass( 'hovered' );
			$( this ).parent().addClass( 'hovered' );
			hi.data( { 'oldSrc' : hi.attr( 'src' ), 'oldAlt' : hi.attr( 'alt') } );
			hi.attr( 'src', '/common/images/empty.gif' ).attr( { 'src' : pi.huge, 'alt' : pi.alt } );
			return false;
		} )
		$( ".portfolio-wraper div.set img" ).mouseout( function() {
			if ( portInAnimation )
				return false;
			var
				mat = $( this ).parent().parent().parent().parent(),
				pi = portImagesCache[ mat.attr( 'id' ) ][ 'set' ][ $( this ).attr( 'id' ) ],
				hi = mat.find( '.huge-image' );
			$( this ).removeClass( 'hovered' );
			$( this ).parent().removeClass( 'hovered' );
			if ( !hi.data( 'oldSrc' ) )
				return false;
			hi.attr( 'src', '/common/images/empty.gif' );
			hi.attr( { 'src' : hi.data( 'oldSrc' ), 'alt' : hi.data( 'oldAlt' ) } );
			hi.data( { 'oldSrc' : null, 'oldAlt' : null } );
			return false;
		} )
	}
);



