var msie = '';


$(document).ready(function() {

	$('.print-link').click(function() {
		window.print();
		return false;
	});

	var bar = $('.bar:first');
	
	$(bar).css('position', 'absolute');
	var bar_default_pos = $(bar).position();	
	$(bar).css('position', 'relative');

	$(bar).css({top: 0});
	if(typeof(bar_default_pos) == 'object')  {
		$('.menu a').hover(function(){				
			$(bar).stop();			
			$(bar).animate({top: $(this).position().top-bar_default_pos.top});		
		}, function() {		
			$(bar).stop();
			$(bar).animate({top: 0});			
		});
	}

	$('.image-block').lightBox();

	$('#left-block-link').click(function() {
		document.location.href = $(this).attr('href');
	});

	$('#right-block-link').click(function() {
		document.location.href = $(this).attr('href');
	});

	$.makeFormArray = function (obj)
	{
		var a = new Array();
		for(t=0; t < obj.length; t++)
		{
			value = $(obj[t]).val();
			name = 	$(obj[t]).attr('name');
			if(!$(obj[t]).attr("disabled"))
			{
				switch($(obj[t]).attr("type")){
					case 'checkbox':
						if($(obj[t]).attr("checked"))
							a.push({name: name, value: value});
						break;
					default:
						a.push({name: name, value: value});
				}
			}
		}

		a.push({name: 'ajax_request', value: 1});

		return a;
	}

	$('.validate-form').submit(function() {
		var send = true;
		var error_message = '';
		var form = this;
		$('#error_form_message_div').remove();
		$(this).find('.required').removeClass('error_input');
		$(this).find('.required').each(function(i){
			if(($(this).attr("value")=='' || $(this).attr("value")=='http://') && !$(this).attr("disabled") || ($(this).attr("value")==$(this).attr('defaultValue') && !$(this).hasClass('nodef'))) {
				$(this).addClass('error_input');
				error_message = messages.required + '<br/>';
				send = false;
			}
		});

		re_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2,}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;

		$(this).find('.validate-email').each(function() {
			if(!$(this).hasClass('error_input') && !$(this).attr("value").match(re_email)) {
				$(this).addClass('error_input');
				error_message += '<div>' + messages.email + '</div>';
				send = false;
			}
		});

		$(this).find('.validate-password').each(function() {
			if(	!$(form).find('.validate-password:first').hasClass('error_input') 
				&&  !$(form).find('.validate-password-repeat:first').hasClass('error_input') 
				&& $(form).find('.validate-password:first').val()!= $(form).find('.validate-password-repeat:first').val()
			   ) {
				$(form).find('.validate-password:first').addClass('error_input');
				$(form).find('.validate-password-repeat:first').addClass('error_input');
				error_message += '<div>' + messages.not_similar_password + '</div>';
				send = false;				
			}
		});

		$(this).find('.checkbox-agreement').each(function() {
			if(!$(this).is(':checked')) {
				error_message += '<div>'+ messages.check_agreement + '</div>';
				send = false;				
			}
		});

		if(!send) {
		   error_message = $('<div class="error" id="error_form_message_div"  style="display: none;">' +  error_message + '</div>');
		   error_message.insertBefore(this);
		   error_message.fadeIn();
		} else {
		   message = $('<div class="wait"   id="error_form_message_div"  style="display: none;">'+ message_loading +'</div>');
		   message.insertBefore(this);
		   message.fadeIn();

		   if($(this).hasClass('ajax-post')) {

				send = false;

				form_array = $(this).map(function(){ return $.makeFormArray(this.elements); });

				var forma = $(this);

				$.ajax({
				type: "POST",

				url: $(this).attr('action'),

				dataType: 'json',

				data: form_array,

				success: function(data) {
					if(data.result!= undefined && data.result) {
						if(data.redirect) {
							window.location.href = data.redirect;
						} else if(data.refresh) {
							window.location.href = window.location.href.replace(/\#.*$/, '');
						} else if(data.send) {
							send = true;
						}else {
							if(data.reload_link) {
								$('#' +$('#'+data.reload_link).attr('rel')).load($('#'+data.reload_link).attr('href') + '?ajax_request=1');
							} else {
								$(forma).map(function(){ return this.reset(); });
							}
						}

						if(data.message) {
							$('#error_form_message_div').removeClass('wait');
							$('#error_form_message_div').addClass('successful');
							$('#error_form_message_div').html(data.message);
							$('#error_form_message_div').show();

							setTimeout(function() {
								$('#error_form_message_div').slideUp();
							}, 7000);


						}

					} else if(data.result == undefined) {
						$('#error_form_message_div').removeClass('wait');
						$('#error_form_message_div').addClass('error');
						$('#error_form_message_div').html(messages.form_not_send);
					} else {
						error_message = data.message;
						$('#error_form_message_div').removeClass('wait');
						$('#error_form_message_div').addClass('error');
						$('#error_form_message_div').html(data.message);

					}
				},
				error: function() {
						$('#error_form_message_div').removeClass('wait');
						$('#error_form_message_div').addClass('error');					
					$('#error_form_message_div').html(messages.form_not_send);
				}

				});

		   }
		}
		return send;

	});

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};
	

	$('.bottom-column').each(function(){
		$(this).parent().find('span:first').hide();
		$(this).hide();
	});

	$('.bottom-column').each(function(){
		
		var col = $(this).css('height');
		$(this).css('height', 0);
		$(this).show();
		$(this).animate({height: col}, 2000, 'easeOutBounce', function(){$(this).parent().find('span:first').slideDown()});
	});

	$('.submit').click(function() {
		$('#'+$(this).attr('rel')).trigger('submit');
		return false;
	});

	$('.map-link').click(function(){		
		var mapDiv = $('#map-div');

		if(mapDiv.is(':visible')) {
			mapDiv.fadeOut();
		} else {
			$('#map-div').fadeIn();
			var iframe  = $('#iframe-map');
			if(!iframe.is(':visible')) {
				iframe.attr('src',$(this).attr('href'));
				iframe.fadeIn();
			}
		}
		return false;

	});
	$('.close-map-div-link').click(function() {
		$(this).parent().parent().fadeOut();
		return false;
	});

	$('.news-block').hover(function(){$(this).addClass('news_over');}, function(){$(this).removeClass('news_over')});

	$('#performance-arhive-link').click(function(){
			var $list = $('#performance-archive-list');
			$list.stop();
			if($list.is(':visible'))
				$list.slideUp();
			else 
				$list.slideDown();

			$(this).toggleClass('arhive-title-on');

			return false;
	});

});

