$(document).ready(function(){

    var mainMenu=null;
    $('#nav .sub_idnet').hide();
	
	$('.auth input').focusin(function(){
		if (this.value==this.defaultValue) this.value = '';
	}).focusout(function(){
		if (this.value=='') this.value = this.defaultValue;
	});
	$('select').customSelect();
	$('select').each(function(index, element){
		$(this).parent().css('z-index', -1*index+100);
	});



	$('.headers li').click(function(){
		if ($(this).hasClass('current')) return;
		$('.headers .block').fadeOut();
		$('body').css('background-image', 'url(/img/'+($(this).index()+1)+(kzlang?'.kz':'')+'.jpg)')
		$('.headers li').removeClass('current');
		$(this).attr('class', 'current');
		$('.block', this).fadeIn();
	});
	
	$('.headers .city dl').hover(
		function(){$('dd', this).fadeIn()},
		function(){$('dd', this).fadeOut()}
	);

    $('#nav>li').hover(
    			function(){

                    this.hoverOut=false;
                    if (mainMenu!=null)
                    {
                        if ($(this).parents('li').get(0)!=mainMenu.get(0) && this!=mainMenu.get(0))
                        {
                            $('#nav li.current').removeClass('current').children('.sub_idnet').hide();
                        }
                    }

                    $(this).children('.sub_idnet').show();
                    if ($(this).children('.sub_idnet').length>0) $(this).addClass('current');
                    mainMenu=$(this);

//                    return false;
    			},
    			function(){
//                    if (this.hoverOut)
//                    {
//                        $(this).children('.sub_idnet').hide();
//                        $(this).removeClass('current');
//                    }
//                    return false;
    			}
    		);
    $('#nav .sub_idnet ul').hover(
        function(){},
        function(){
            $('#nav li.current').removeClass('current').children('.sub_idnet').hide();
            return false;
        });

    $('#current_city').toggle(function(){
        $('#city_list').fadeIn();
    },function(){
        $('#city_list').fadeOut();
    });
	
	if ($.browser.msie && parseFloat($.browser.version)<=6){
		$('.fn.org .logo, .translation li, #nav li, #nav a, .auth, .auth .registration, .buttons li, .buttons a, .buttons span, .headers h2, .headers .city, .headers .city div, .qa_block').css({'-pie-png-fix':true, 'behavior':'url(/js/PIE.htc)'});
		$('#nav li').hover(
			function(){
				$(this).css('background-position', '0 -35px');

			},
			function(){
				if (!$(this).hasClass('current')) $(this).css('background-position', '0 0');
			}
		);
        
		$('.headers h2.second').first().parent().css('margin-left', '-11px');
		$('.headers li').hover(
			function(){
				$('h2', this).css('background-position-y', '-73px');
			},
			function(){
				if (!$(this).hasClass('current')) $('h2', this).css('background-position-y', '0');
			}
		);
		$('.headers li').click(function(){
			$('.headers li h2').css('background-position-y', '0');
			$('h2', this).css('background-position-y', '-73px');
		});
	}
	
	if ($.browser.msie && parseFloat($.browser.version)<=8){
		$('.hentry, .content, .city dd, .users form, .users .pagination, .maps li a, .maps, .sub_idnet').css({'behavior':'url(/js/PIE.htc)'});
	}
	
	if ($.browser.opera) $('#footer .hentry').css('margin-top', '-155px');
	
	$("a[rel=facebox]").facebox({
		opacity: 0.8,
		loadingImage: '/img/facebox/loading.gif',
		closeImage: '/img/facebox/closelabel.png'
	});
	
	var classname = 'first';
	$('.strips ul li, .streets li').each(function(){
		if ($(this).index()) {
			$(this).attr('class', classname);
			classname = classname=='first'?'second':'first';
		}
	});
	
	$(".msgs tr:odd td").css('background', 'none');
	
	$('.popup input[type=submit]').live('click', function(){ submitForm(); return false; });
	$('.popup form').live('submit', function(){ submitForm(); return false; });
	function submitForm() {
		var form = $('.popup form').first();
		$.ajax({
			type: 'post',
			url: form.attr('action'),
			dataType: 'html',
			data: form.serialize(),
			beforeSend: function() { },
			success: function(data) { $('.popup .content').html(data); },
			error: function(xhr, textStatus, errorThrown) { alert('An error occurred! ' + ( errorThrown ? errorThrown : xhr.status )); },
			complete: function() { }
		});
		$('.popup .content').html('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>');
		return false;
	}
	
	$('.profile .users dd').hover(
		function(){ $('.delFriend').css('display', 'block') },
		function(){ $('.delFriend').css('display', 'none') }
	);

        jQuery("#f-user-register").validationEngine();
});
