//var $J = jQuery.noConflict();
function isValidEmail(email) 	{
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false)
	{
		return false;
	}
return true;
}
jQuery(document).ready(function(){
	$('a.lightbox').lightBox();
	
	$('#topmenu li:last').css({background: 'none'});
	
// убрать при программировании! необходимо только для теста
	/*$('#sidemenu.test a').live('click',function(){
		var id = $(this).attr('rel');
		$('#sidemenu a').removeClass('active');
		$(this).addClass('active');
		$('.collage').attr({id: id, src: '/data/img/' + id + '.png'});
		return false;
	});*/
	
	/*$('.special1').cornerz({radius: 11});
	$('.special2').cornerz({radius: 13});
	$('.special3').cornerz({radius: 14});
	$('.special4').cornerz({radius: 15});*/
	
	/*$('.change_pic').live('click',function(){
		var src = $('img', this).attr('src');
		$('#pic_m img').attr({src: src});
	});

	$('.table_strips tr:even').each(function(){
		$('td', this).addClass('colored');
	});*/
	
	/*
	var ident    = 1;
	var height   = 0;
	var num_cols = 3;
	var a = new Array();
	$(".item_block").each(function(index){
		var h = $(this).height();
		if(ident <= num_cols){
			if(height <= h) height = h; 
				a[index] = index;
					if(ident == num_cols){
						for (var key in a) $('.item_block').eq(key).height(height); 
					a = new Array();
					height = 0;
				}
			ident++;
		}
		if(ident > num_cols)ident = 1;
	});
	*/
	$('.button').click(function(){
	var error = "";
	var name  = $('#fio_input').val();
	var email = $('#email_input').val();
	var phone = $('#phones_input').val();
	var text  = $('#text').val();
	var captcha = $('#captcha').val();
	if(isValidEmail(email) !== true){
	error += "Неверный формат E-Mail. \n";
	}
	if(name ==""){ error += "Не заполнено поле Имя. \n";}
	if(text ==""){ error += "Не заполнено поле Сообщение. \n";}
	if(phone ==""){ error += "Не заполнено поле Телефон. \n";}
	if(captcha ==""){ error += "Не введен Код на картинке. \n";}
	if(error !==""){
	alert(error);
	}else{
		$.post('/ru/q/',{email: email, name: name, phone: phone, text:text, captcha:captcha},
				function(data){
					if (data!=0) {
						if(data == 'error_code'){
							alert("Неверно введен код на картинке!");
						}else{
							$('#fio_input').val("");
							$('#email_input').val("");
							$('#phones_input').val("");
							$('#text').val("");
							$('#captcha').val("");
						alert(data);
						}
					}
				});
		}
		return false;
	});
	    $('.open').live('click',function(){
	        showPopup();
			$("#YMapsIDPop").data('map').redraw();
	    });
	    $('.close, #opaco').live('click',function(){
	        closePopup();
	    });
});

$(window).resize(function()
{
});

function closePopup(){
    $('#opaco').toggleClass('hidden').removeAttr('style');
    $('#open').toggleClass('hidden');
    return false;
}
function showPopup(){
    if($.browser.msie){
        $('#opaco').height($(document).height()).toggleClass('hidden');
    } else {
        $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7);
    };
    $('#open').alignCenter().toggleClass('hidden');
    return false;
}
$.fn.alignCenter = function() {
	var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
	var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
	return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
};

