$(function(){
	
	var time = setInterval(function(){
		$('.current .gallery').trigger('click');
	}, 5000);
	clearInterval(time);
	
	var exposed = false;
	var processing = false;
	var menu = false;
	
	var ribbon = setInterval(function(){
		if(!exposed && !processing){
			carousel.next();
		}
	}, 10000);
	
	$('.current .gallery').trigger('click');
	
	var variant = ['Хорошего понедельника', 'Хорошего вторника', 'Хорошей среды', 'Хорошего четверга', 'Хорошей пятницы', 'Хорошей субботы', 'Хорошего воскресения'];
	var today = new Date();
	$('.hail').html(variant[today.getDay() - 1]);	
	
	var mouseblock = false;
	
	$('a[rel]').bind('click', function(e){
		e.preventDefault();
		$('.menu input').val('');
		$('.menu .wrong').removeClass('wrong');
		var _id = $(this).attr('rel');
		$('.element:not(:nth-child(' + _id + '))').css('z-index', '0');
		$('.element:nth-child(' + _id + ')').css('z-index', '1').animate({
			right: 0
		}, function(){
			$('.element:not(:nth-child(' + _id + '))').css('right', '-240px');
		});
		$('.error').animate({
			right: -240
		});
		menu = true;
	});
	
	$('a.cancel').bind('click', function(e){
		e.preventDefault();
		$('.menu input').val('');
		$('.menu .wrong').removeClass('wrong');
		$('.element').animate({
			right: -240
		});
		$('.error').animate({
			right: -240
		});
		menu = false;
	});
	
	$('.enter, .redeem').bind('click', function(){
		var _message;
		($(this).hasClass('enter')) ? _message = 'Такого логина не существует' : _message = 'Такого кода не существует';
		$('.menu input:not([type=password])').addClass('wrong');
		$('.error').empty().html(_message).css('right', '-240px').animate({
			right: 0
		});
	});
	
	$('.submit').bind('click', function(){
		var _empty = false;
		$('table input,textarea').removeClass('wrong');
		$('table input,textarea').each(function(){
			if($(this).val() == ''){
				$(this).addClass('wrong');
				_empty = true;
			}
		});
		if(_empty){
			$('table .hint').addClass('red');
		}else{
			$('table .hint').removeClass('red');;
			$('#submit').submit();
		}
	});
	
	var _visible = $('.carousel .block').length;
	if(_visible > 5){
		_visible = 5;
	}
	_visible += _visible % 2 - 1;
	
	var carousel = $('.carousel').carousel({
		visible: _visible
	});
	
	carousel.jump(1);
	
	$('.next').bind('click', function(){
		if(processing == false){
			if($('.expanded').length == 0){
				carousel.next();
			}
			else{
				contract(function(){
					carousel.next(function(){
						expand($('.current'));
					});
				});
			}
		}
	});
	
	$('.prev').bind('click', function(){
		if(processing == false){
			if($('.expanded').length == 0){
				carousel.prev();
			}
			else{
				contract(function(){
					carousel.prev(function(){
						expand($('.current'));
					});
				});
			}
		}
	});
			
	$(document).bind('mousemove', function(e){
		_offset = function(){
			if(menu){
				return 149;
			}else{
				return 0;
			}
		}
		_yleft = e.pageY - 75;
		_yright = e.pageY - 75;
		if(_yleft < 20){_yleft = 20;}
		if(_yleft > 1400){_yleft = 1400;}
		if(_yright < 20 + _offset()){_yright = 20 + _offset()};
		if(_yright > 1400){_yright = 1400;}
		$('.button.prev').css('top', _yleft);
		$('.button.next').css('top', _yright);
	});
	
	$('.button').bind('mouseover', function(){
		$(this).stop().animate({
			opacity: 1
		});
	});
	
	$('.button').bind('mouseout', function(){
		$(this).stop().animate({
			opacity: 0.1
		});
	});	
	
	$('.block').live('click', function(index){
		if(processing == false){
			var next = $(this).position().left;
			var current = $('.block[index=' + carousel.getCurrent() + ']').position().left;
			if($(this).attr('index') == carousel.getCurrent() && $('.expanded').length == 0){
				expand($(this));
			}
			else if(next > current && $('.expanded').length != 0){
				contract(function(){
					carousel.next(function(){
						expand($('.block[index=' + carousel.getCurrent() + ']'));
					});
				})
			}
			else if(next > current){
				carousel.next(function(){
					expand($('.block[index=' + carousel.getCurrent() + ']'));
				});
			}
			else if(next < current && $('.expanded').length != 0){
				contract(function(){
					carousel.prev(function(){
						expand($('.block[index=' + carousel.getCurrent() + ']'));
					});
				})
			}		
			else if(next < current){
				carousel.prev(function(){
					expand($('.block[index=' + carousel.getCurrent() + ']'));
				});
			}
		}
	});
	
	$('.close').live('click', function(){
		$.mask.close();
	});
	
	$('.nav, .gallery').live('click', function(){
		clearInterval(time);
		time = setInterval(function(){
			$('.current .gallery').trigger('click');
		}, 5000);
	});
	
	function expand(_obj, _callback){
		processing = true;																
		$(_obj).addClass('expanded');
		
		mouseblock = true;
		
		$('.carousel_content').expose({
			api: true,
			opacity: 0.5,
			color: '#f3f3f3',
			onClose: function(){
				contract();
			},
			closeOnClick: false
		});
		
		$('.loader', _obj).fadeIn(function(){
			$('.small', _obj).hide();
			$('.big', _obj).show(0, function(){
				$('.loader', _obj).delay(250).fadeOut(function(){
					
					time = setInterval(function(){
						$('.current .gallery').trigger('click');
					}, 5000);
					
					var height = $('.text', _obj).height() + 390 + $('.navigation', _obj).height();
					
					$(_obj).animate({
						width: '620px',
						height: height
					});
					
					$('.carousel').animate({
						height: height + 30
					});
					
					var left = $('.items').position().left - 175;
		
					$('.items').animate({
						left: left + 'px'
					});
					
					$('.gallery img', _obj).animate({
						width: 620
					}, function(){
						$('.text', _obj).slideDown(function(){
							$('.close', _obj).show(function(){
								$('.close div:last', _obj).slideDown(250);
							});
							
							exposed = true;
							if(_callback){
								_callback();
							}
							processing = false;
						});
					});
					
					$('.title').slideUp();
				});
			});
		});
		
	}
	
	$('#exposeMask').live('click', function(){
		if(exposed == true){
			$.mask.close();
		}
	});
	
	$('.block').bind('mouseover', function(){
		if($('.expanded').length == 0){
			$('.close div:first', this).show();
		}
	});
	
	$('.block').bind('mouseout', function(){
		$('.close :first', this).hide();
	})
		
	function contract(_callback){
		clearInterval(time);
		clearInterval(ribbon);
		ribbon = setInterval(function(){
			if(!exposed && !processing){
				carousel.next();
			}
		}, 10000);
		
		
		processing = true;
		$('.current .close div:last').hide();
		
		mouseblock = false;
		
		$('.expanded .text').slideUp(function(){
			$('.expanded .gallery img').animate({
				width: 270
			}, function(){
				$('.expanded .loader').fadeIn(function(){
					$('.expanded .big').hide();
					$('.expanded .small').show(0, function(){
						$('.expanded .loader').delay(250).fadeOut(function(){
							if(_callback){
								_callback();
							}
							clearInterval(time);
							$('.expanded').removeClass('expanded');
							exposed = false;
							processing = false;
						});
					});
				});
			});
			
			$('.current').animate({
				width: '270px',
				height: '195px'
			});
			
			var left = $('.items').position().left + 175;
			
			$('.items').animate({
				left: left + 'px'
			});
			
			$('.carousel').animate({
				height: '240px'
			});
			
			$('.title').slideDown();
		});
	}
	
	var zindex = 0;
	var current_link = 0;
	
	$('[link]').bind('click', function(e){
		menu = true;
		e.preventDefault();
		var link = $(this).attr('link');
		if(current_link != link){
			$('.input90').removeClass('wrong');
			$('.input150').removeClass('wrong');
			$('.topmenu input').val('');
			$('.error').stop().animate({
				left: '240px'
			});
			zindex++;
			$('[object=' + link + ']').css('z-index', zindex).css('left', '240px').animate({
				left: '0px'
			});
			current_link = link;
		}		
	});
	
	$('.gallery').gallery();
	
	map();
});

function map(){
	var center = new google.maps.LatLng(55.75, 37.616667);
	
	var myOptions = {
		scrollwheel: false,
		zoom: 8,
		center: center,
		disableDefaultUI: true,
		navigationControl: false,
		scaleControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var map = new google.maps.Map($("#map").get(0), myOptions);
	
	var marker = new google.maps.Marker({
		position: center,
		map: map,
		title: "Телекомпания Прайд (нажмите для приближения)"
	});
}
