$(function(){

	topnews();

		// zIndex ie7 fix
	if ( $.browser.msie && $.browser.version == 7 ) {
		$('.i_want_perech li,.combwrap').hover(function(){
			$(this).css({'zIndex':'10','zoom':'1'});
		}, function(){
			$(this).css({'zIndex':'auto','zoom':'0'});
		});
	}

        // perech

    $('.i_want_perech a').live('mouseenter',function(){
        $(this).parent().parent().addClass('active');
    }).live('mouseleave',function(){
        $(this).parent().parent().removeClass('active');
    });
    $('.i_want_perech ul li a').click(function(){
	    $(this).parent().parent().addClass('active2').siblings().removeClass('active2');
    });
    $('.i_want_perech span').live('click',function(){
        $(this).siblings().show();
    });
    $('.i_want_perech ul').live('mouseleave',function(){
        $(this).hide();
    });
	$('.i_want_perech.list a').live('click',function(){
        var t = $(this).text();
        $(this).parents('ul').siblings('span').children('strong').text(t);
        $(this).parent().parent().parent().hide();
    });
	$('.i_want_perech span.no-active').click(function(){
        return false;
    });

		// active button
	$('.iw-but').click(function(e){
		e.stopPropagation();
	});
	$('.iw-but').mousedown(function(){
		$(this).addClass('active');
	});
	$('html').click(function(){
		$('.iw-but').removeClass('active');
	});


    $('.combwrap > span').click(function(){
        $(this).siblings('div.com-box').show();
    });

    $('.com-box').mouseleave(function(){
        $(this).hide();
    });

    $('.com-b-i span').click(function(){
        var comtext = $(this).text();
        $(this).parents('.com-box').children('h3').text(comtext);
        $(this).parents('.combwrap').children('span').children('strong').text(comtext);

        $(this).parents('.com-box').hide();

	    $(this).parents('.com-b-i').find('li').removeClass('active2');
	    $(this).parent().addClass('active2');

    });

        // ini pie
	if ( $.browser.msie && $.browser.version <= 8 ) {
		$('.top_inn,.wish .number span').each(function() {
			PIE.attach(this);
		});
	}

        // carousel
	$('#mycarousel').jcarousel();
	$('#realtors-carousel').jcarousel({
        visible: 6
    });
	$('.aff-carousel #jcarousel').jcarousel({
        visible: 3
    });
	$('.aff-carousel #jcarousel2').jcarousel({
        visible: 3
    });

    $('.affiliates-c .jcarousel-skin-tango a').click(function(){
        var p = $(this).attr('data-bimg');
        $(this).parents('.aff-carousel').find('div:first img').attr('src',p);
        $(this).addClass('active').parent().siblings().children('a').removeClass('active');
        return false;
    });


	if ($('#datepicker').length) {
		$('#datepicker').datepicker({
			showOn: "button",
			buttonImage: baseUrl + '/static/img/picker.png',
			buttonImageOnly: true
		});
		$.datepicker.regional['ru'] = {
			closeText: 'Закрыть',
			prevText: '&#x3c;Пред',
			nextText: 'След&#x3e;',
			currentText: 'Сегодня',
			monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
			monthNamesShort: ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря'],
			dayNames: ['Воскресенье','Понедельник','Вторник','Среда','Четверг','Пятница','Суббота'],
			dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
			dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
			weekHeader: 'Не',
			dateFormat: 'd M, yy',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: false,
			yearSuffix: ''};
		$.datepicker.setDefaults($.datepicker.regional['ru']);
	}


	if ( $('#sortable').length ) {
		$( "#sortable" ).sortable({
			placeholder: "ui-state-highlight",
			revert:true,
			containment:'parent'
		});
	}

	$('#sortable li').hover(function(){
		$(this).addClass('active');
	}, function(){
		$('#sortable li .wrap').removeClass('active');
	});



    ////////////////////

		// login form
	$('body').click(function(){
		$('.login form').hide();
	});
	$('.close').click(function(){
		$(this).parent().hide();
	});
	$('.login form').click(function(e){
		e.stopPropagation();
	});
	$('.log_inn span').click(function(e){
		$(this).siblings('form').show();
		e.stopPropagation();
	});


    $('.main-form-wrap form:first-child').show();
    $('.this').show();

    $('.appl-for-serv-inn.fir .rbls a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var f = $(this).parent().parent().index();
        $('.appl-for-serv-inn.fir .main-form-wrap > div').each(function(){
            $(this).children('form').hide().eq(f).show();
        });
        $(this).parents('ul.rbls').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });

    $('.appl-for-serv-inn.sec .rbls a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var f = $(this).parent().parent().index();
        $('.appl-for-serv-inn.fir .main-form-wrap > div').each(function(){
            $(this).children('form').hide().eq(f).show();
        });
        $(this).parents('ul.rbls').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });

    $('.filter .rbls a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var f = $(this).parent().parent().index();
        $('.filter .main-form-wrap > div').each(function(){
            $(this).children('form').hide().eq(f).show();
        });
        $(this).parents('ul.rbls').hide();
    });

    $('.analytics .rbls a').live('click',function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var f = $(this).parent().parent().index();
        $('.analytics .main-form-wrap > div').each(function(){
            $(this).children('form').hide().eq(f).show();
        });
        $(this).parents('ul.rbls').hide();
    });



    $('.iwant .rbls a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var f = $(this).parent().parent().index();
        $('.iwant .main-form-wrap > div').each(function(){
            $(this).children('form').hide().eq(f).show();
        });
        $(this).parents('ul.rbls').hide();
    });



    $('.appl-for-serv-inn.fir .hacnb a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var b = $(this).parent().parent().index();
        $('.appl-for-serv-inn.fir .main-form-wrap > div').removeClass('this').hide().eq(b).show().addClass('this');
        $(this).parents('ul.hacnb').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });
    $('.appl-for-serv-inn.sec .hacnb a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var b = $(this).parent().parent().index();
        $('.appl-for-serv-inn.fir .main-form-wrap > div').removeClass('this').hide().eq(b).show().addClass('this');
        $(this).parents('ul.hacnb').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });
    $('.iwant .hacnb a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var b = $(this).parent().parent().index();
        $('.iwant .main-form-wrap > div').removeClass('this').hide().eq(b).show().addClass('this');
        $(this).parents('ul.hacnb').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });
    $('.filter .hacnb a').click(function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var b = $(this).parent().parent().index();
        $('.filter .main-form-wrap > div').removeClass('this').hide().eq(b).show().addClass('this');
        $(this).parents('ul.hacnb').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });
    $('.analytics .hacnb a').live('click',function(){
        var t = $(this).text();
        $(this).parents('ul').siblings().children('strong').text(t);
        var b = $(this).parent().parent().index();
        $('.analytics .main-form-wrap > div').removeClass('this').hide().eq(b).show().addClass('this');
        $(this).parents('ul.hacnb').hide();
        $('input[name=email]').parent().removeClass('error');
        $('input[name=email]').siblings().remove('span');
    });



    $('.ap-m li').click(function(){
        $('.appl-for-service > div').hide().eq($(this).index()).show();
    });

	$('.submit').click(function(){
		$(this).parents('form').submit();
	});

    $('.iw-but a').click(function(){
        $(this).parents('form').submit();
    });

		// validate form
   $('.appl-for-service .main-form-wrap form, form.riel-room-inn').submit(function(){
        if ( !$(this).find('input[name=email]').val() || !$(this).find('input[name=email]').val().match(/.+@.+\..+/) ) {
            $(this).find('input[name=email]').parent().addClass('error');
            $(this).find('input[name=email]').parent().append('<span>Неверно набранный email</span>').find('span').next().remove();
        } else {
            $(this).find('input[name=email]').parent().removeClass('error');
            $(this).find('input[name=email]').siblings('span').remove();
        }
		if ( $(this).find('*').hasClass('error') ) {
			return false;
		}
   });

	$('.login form').submit(function(){
		if ( !$(this).find('dd input[type=text]').val() || !$(this).find('dd input[type=text]').val().match(/.+@.+\..+/) ) {
			$(this).find('dd input[type=text]').parent().addClass('error');
		} else {
			$(this).find('dd input[type=text]').parent().removeClass('error');
		}
		if ( !$(this).find('dd input[type=password]').val() ) {
			$(this).find('dd input[type=password]').parent().addClass('error');
		} else {
			$(this).find('dd input[type=password]').parent().removeClass('error');
		}
		if ( $(this).find('dd').hasClass('error') ) {
			return false;
		}
	});

		// search focus
    $('.search input').focus(function(){
		var $this = $(this);
        $this.css('color','#010101');
        if ($this.val() == 'поиск') {
            $this.val('');
        }
    }).blur(function(){
		var $this = $(this);
        if ($this.val() == '') {
            $this.val('поиск');
            $this.css('color','#bfbfbf');
        }
    });

		// js grid keyboard
	document.onkeydown = function checkKeycode(event){
		var keycode, keyShift, keyCtrl;
		if(!event) var event = window.event;
		if (event.keyCode) keycode = event.keyCode;
		else if(event.which) keycode = event.which;
		keyShift = event.shiftKey;
		keyCtrl = event.ctrlKey;
		if ( keycode == 71 && keyShift && keyCtrl ) {
			$('#wrapper').toggleClass('grid');
		}
	}

        // top newd shadow add;
    $('.top_inn .box div').append('<ins/>');

        // top news refresh;
	$('.refresh').click(function(){
        $('.top_inn .box div p.active').animate().stop();
        $('.top_inn .box p').css('margin','0');

        if ($('.top_inn div:visible p:last').hasClass('active')) {
            $('.top_inn div:visible p:first-child').addClass('active').siblings().removeClass('active');
        } else {
            $('.top_inn .box div:visible p.active').next('p').addClass('active').siblings().removeClass('active');
        }

        topnews();

	});

        // top news switch;
	$('.top_menu li').click(function(){
        $(this).addClass('active').siblings().removeClass().parents('.top_inn').find('.box > div').hide().eq($(this).index()).show();
        $('.top_inn .box div p.active').animate().stop();
        $('.top_inn .box p').css('margin','0');
        topnews();
    });

		// table style
    $('.content.text table').wrap('<div class="table-wrap"/>');
//    $('.content.text tr:not(:first-child) td').wrapInner('<span/>');



    $('.reset-form').click(function(){
        $(this).parents('form').find('input,textarea').val('');
    });

    $('.ap-m li').click(function(){
        $(this).removeClass('notactive').addClass('active').siblings().removeClass('active').addClass('notactive');

    });

	// sort

	$('.search-page-wrap .sort li').live('click',function(){
		$(this).addClass('descending').siblings().removeClass();
	});

	$('.search-page-wrap .sort li.descending').live('click',function(){
		$(this).removeClass('descending').addClass('ascending');
	});

	$('.search-page-wrap .sort li.ascending').live('click',function(){
		$(this).removeClass('ascending').addClass('descending');
	});


	$('.table-list th div span').live('click',function(){
		$(this).parent().addClass('descending').parent().siblings().children('div').removeClass();
	});

	$('.table-list th div.descending span').live('click',function(){
		$(this).parent().removeClass('descending').addClass('ascending');
	});

	$('.table-list th div.ascending span').live('click',function(){
		$(this).parent().removeClass('ascending').addClass('descending');
	});


	// display mode
	$('.display-mode li').click(function(){
		$(this).removeClass('no-act').addClass('active').siblings().removeClass('active').addClass('no-act');
	});

	// show all
	$('.show-all span').click(function(){
		if ( $(this).parent().siblings('.other-points').is(':hidden') ) {
			$(this).text('скрыть');
		} else {
			$(this).text('показать все');
		}
		$(this).parent().parent().find('.other-points').toggle();
	});

	$('.block-s4 .block-ss4 .show-all span').click(function(){
		$(this).parent().next().show();
		$(this).parent().remove();
	});

	// reset filter
	$('.filter .reset').click(function(){
		$(this).parent().parent().find('input[type=text]').val('');
		$(this).parent().parent().find('input[type=checkbox]').removeAttr('checked');

	});

	// object-box-inner
	$('.object-box-inner li').click(function(){
		$(this).removeClass('notactive').addClass('active').siblings().removeClass('active').addClass('notactive').parent().parent().find('.box > div').hide().eq($(this).index()).show();
	});

	$('.plan').click(function(){
		$('#jcarousel2').jcarousel({
			visible: 3
		});
    });

	$('.jc-param').click(function(){
		$('#jcarousel').jcarousel({
			visible: 3
		});
    });

	$('.comm-add-but .submit').click(function(){
		$('.jc-param').click();
	});

	$('.cooment-add-wrap h3').click(function(){
		$(this).siblings().toggle().parent().toggleClass('active');
	});

	$('.personal-information .right .add a').click(function(){
		$(this).parents('dl.add').prev('dl').clone().insertBefore($(this).parent().parent().parent()).find('input').val('');
	});

	$('.pers-nav li').click(function(){
		$('#manag-wrap > div').hide().eq($(this).index()).show();
	});

	initSelect();


/*
Text on Homepage
*/
	$('#more_text').hide();
	$('.more_link').click(function(){
		$(this).hide();
		$('#more_text').show();
	});



}); // end doc. ready


function topnews() {

	$('.top_inn .box').css({'overflow':'visible','width':'auto'});
		var d = $('.top_inn .box div:visible p.active').width();
	$('.top_inn .box').css({'overflow':'hidden','width':'65%'});
		var dd = $('.top_inn .box').width();
		var tod = d+dd;
		var speed = tod*16;

	$('.top_inn .box div:visible p.active').delay(0).animate({
	   marginLeft: '-'+tod+'px'
	}, speed, 'linear', function(){

		if ( $('.top_inn .shares p:last').hasClass('active') ) {
			$('.top_inn .shares p:first').addClass('active').siblings().removeClass();
		}
        if ( $('.top_inn .proposition p:last').hasClass('active') ) {
			$('.top_inn .proposition p:first').addClass('active').siblings().removeClass();
		}

		$(this).next('p').addClass('active').siblings().removeClass();
		$(this).siblings().css('margin','0');

		topnews();
	});
}

function chalign1() {
	var d = $('.graphic.gr1 canvas').eq(1).css('left');
	$('.graphic.gr1 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr1 canvas').eq(1).css('left','0');
}
function chalign2() {
	var d = $('.graphic.gr2 canvas').eq(1).css('left');
	$('.graphic.gr2 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr2 canvas').eq(1).css('left','0');
}
function chalign3() {
	var d = $('.graphic.gr3 canvas').eq(1).css('left');
	$('.graphic.gr3 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr3 canvas').eq(1).css('left','0');
}
function chalign4() {
	var d = $('.graphic.gr4 canvas').eq(1).css('left');
	$('.graphic.gr4 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr4 canvas').eq(1).css('left','0');
}
function chalign5() {
	var d = $('.graphic.gr5 canvas').eq(1).css('left');
	$('.graphic.gr5 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr5 canvas').eq(1).css('left','0');
}
function chalign6() {
	var d = $('.graphic.gr6 canvas').eq(1).css('left');
	$('.graphic.gr6 canvas').eq(0).css('left','-'+d+'');
	$('.graphic.gr6 canvas').eq(1).css('left','0');
}

function formshow() {
	$('.main-form-wrap > div, .main-form-wrap form').hide();
	$('.main-form-wrap form:first-child').show();
	$('.this').show();
	$('.analytics form *').css('zoom','1');
}

function initSelect() {
	var sels = ['.rbls', '.hacnb'];
	for (var key in sels) {
		$('#analytics > .period ' + sels[key] + ' a').click(function(){
			var t = $(this).text();
			$(this).parents('ul').siblings().children('strong').text(t);
			$(this).parents('ul.rbls').hide();
		});
	}
}
