$(document).ready(function (){
$("*").onclick = new Function('', 'alert(this.innerHTML)');
//Свап
$('#mainNav').replaceAll("#StaticMainNav");
$('#mainNav').css('display','block');


//Инициализация
$("#mainNav").find(".nav").find("li:visible").addClass("clearfix");
//var init_category="";

function change_city(city_cl) {
			//anew=$(".head").children("a");
			anew=$(".head").children("a:not(.close)");
            current_class=$(anew).attr("class");
            current = $(anew).children("i:first").text();
		    newcity_class=$(city_cl).attr("class");
            newcity = $(city_cl).text();
		 if (newcity_class!=current_class){
         $(anew).attr("class",newcity_class).attr("title",newcity).children("i:first").text(newcity);
		 $(city_cl).attr("class",current_class).attr().text(current);
		 $("#mainNav").find(".nav").children("li:not(:first)").children("div:visible").hide();
		 $("#mainNav").find(".nav").find("li."+newcity_class+":hidden").show().addClass("clearfix").siblings("li."+current_class).hide();
		                           }
                               }

         city_cl = $(".box5").children(".bc").children("p").children("a."+init_city);
         if ($(".head").children("a").attr("class")!=init_city) {change_city(city_cl)}
		 else {$(".nav").children("li:visible").addClass("clearfix");}
//$("#"+init_category).addClass("active clearfix").css("display","list-item");

//Левый аккордеон
$("#mainNav").find(".nav").children("li:not(.nonacc)").children("a").click(function (){


			var scrol = null;
            newlink=$(this).children("i").attr("class");
            textlink = $(this).html();
		    text = $(this).children("i").text();
			var addlink="<a href='"+newlink+"' >"+text+"</a>";
			litags=$(this).parent().parent().children(".active");
		    text1 = $(litags).children("a").children("i").text();
           $(litags).children("a").children("i").stopTime();


		   if (text1=='') text1=text;
		   $(litags).children("div").slideUp(900).parent().removeClass('active').children("a").children("i:first").text(text1);
		   $(this).siblings(".ul:hidden").slideDown(900).siblings(".scroller").slideDown(900).parent().addClass("active clearfix").children("a").children("i:first").text(text);

		  if ($(this).siblings(".scroller").is(":visible")) scrol = $(this).siblings(".scroller");


		if ($(this).parent().is(".active")) {$(this).children("i:first").oneTime(2000,"atimer", function() {
 $(this).html(addlink)});} else {$("i").stopTime();};

		        if (scrol!=null){
                         function scroller(scr) {

			 		                  var parent = $(scr).parent();
									  var sibling= $(parent).children('.ul').children('ul');
					                  var number = $(sibling).children('li').size();

					        if  (number > 7){

				                var heightUl = sibling.height() - 112;
                                var intOverallDelta = heightUl;

				        	$(scr).slider({
				    		    orientation: "vertical",
				    		    range: "min",
				    		    min: 0,
				    		    max: heightUl,
				    		    value: heightUl,
				    		    slide: function(event, ui) {
				    		        intOverallDelta = ui.value;

				    			        var sdvig = heightUl - ui.value;
                                        var prop = 112/heightUl;
                                        var sdvig1 = 112 - ui.value*prop;

				    		 sibling.css({'margin-top': -sdvig + 'px'});
							 parent.find('.scroll-point').css({'top': +sdvig1 + 'px'});
				    		                                }
				    	                                     });

						     parent.mousewheel(function(objEvent, intDelta){

						         if (intDelta > 0 && intOverallDelta < heightUl) {intOverallDelta += 12;}
							     else if (intDelta < 0 && intOverallDelta > 0){intOverallDelta -= 12;}
							 objEvent.stopPropagation();
 		                     objEvent.preventDefault();
						     parent.find(".scroll-field").slider('option', 'value', intOverallDelta);

						         var sdvig = heightUl - intOverallDelta;
							     var prop = 112/heightUl;
                                 var sdvig1 = 112 - intOverallDelta*prop;

			    			  sibling.css({'margin-top': -sdvig + 'px'});
							  parent.find('.scroll-point').css({'top': +sdvig1 + 'px'});
						                                                     });
					                        }
										   // else{$(scr).remove();}
			                                            };
					          scroller(scrol);
							                }	//endif



$(this).hover(function () {
						$(this).children("i").children("a").css("colors","#084783")},
			  function () {
					    $(this).children("i").children("a").removeAttr("style")
})


});



$("#page:not(.box5)").click(function () {$(".box5").hide();});

$("#mainNav").find(".head a").toggle(function () {
	                            $(".box5").show().children("div").show();},
				    function () {
						        if ($(".box5").is(":hidden")) $('.box5').show();
});



//Рекламные блоки
$("#adv-hide").children("a").click(function() {
				$("#advert-200").find("div.bc").animate({height: 132}, 1000).find("#adv-hide").hide().next(".adv-show").show();
});

$(".adv-show").find(".showhide").click(function() {
				$("#advert-200").find("div.bc").animate({height:85}, 1000).find(".adv-show").hide().prev("#adv-hide").show();
});

$("#adv-hide240").children("a").click(function() {
				$("#advert-240").find("div.bc").animate({height: 282}, 1000).find("#adv-hide240").hide().next(".adv-show240").show();
});

$(".adv-show240").find(".showhide").click(function() {
				$("#advert-240").find("div.bc").animate({height:70}, 1000).find(".adv-show240").hide().prev("#adv-hide240").show();
});

//Чат
$(".chat-controls").children("span:first").children("a").click(function() {

        arrayPageScroll=___getPageScroll();
		arrPageSizes=___getPageSize();

        $("#popupMessage").css("top",arrayPageScroll[1]+(arrPageSizes[3] / 10)).show(1000);NFFix();
});

$("#respond_form dl").children("dd:last").children("input").click(function() {
         $("#popupMessage").hide("slow");
});

$("#closeform").click(function(){$("#popupMessage").hide("slow");});

//Центральный блок
$(".city-select").find("ul li").click(function() {
         var index = $(this).index(this)+1;
		 var tagcity = $(this).children("a").children("i:first").text();
		 if (tagcity == 'Казань') {
		    Showkazan();
		 }
         if (tagcity == 'Санкт-Петербург') {
		    Showsanktpeterburg();
		 }
		 if (tagcity == 'Екатеринбург') {
		    Showekaterinburg();
		 }
		 if (tagcity == 'Краснодар') {
		     Showkrasnodar();
		 }
		 if (tagcity == 'Красноярск') {
		   Showkrasnoyarsk();
		 }
		 if (tagcity == 'Нижний Новгород') {
		    Shownizhnij_novgorod();
		 }
		 if (tagcity == 'Челябинск') {
		     Showchelyabinsk();
		 }
		 if (tagcity == 'Москва') {
		    Showmoskva();
		 }
                 if (tagcity == 'Омск') {
 		    Showomsk();
		 }
                 if (tagcity == 'Новосибирск') {
		    Shownovosibirsk();
		 }



                 rpl= tagcity+'<i class="hlbg"/><i class="hlbg hrbg"/>'
		 $(this).siblings(".active").removeClass().children("a").wrapInner("<i></i>").find(".hlbg").remove();
		 $(this).addClass("active").children("a").children("i").replaceWith(rpl);
		 $("#cityMap").children(".bc").children("img").fadeOut(500).css("opacity","0.4").attr("src", "images/map"+index+".jpg").fadeIn(1500);
});

//Подвал
$(".f-link").toggle(function() {
                  var h=$("#f-hide").height();
				  $("#f-hide").slideDown(1000);
			             if (!$.browser.msie) $("#subfooter").animate({height: 162+h}, 1000);
				  $("#footer").animate({height: 162+h}, 1000);
				  $(".f-bg3").animate({height: 96+h}, 1000);
				  $("#f-bg").animate({marginTop: -162-h, height:162+h}, 1000);
                               },
				  function() {
                  $("#f-hide").slideUp(1000);
				        if (!$.browser.msie)  $("#subfooter").animate({height: 165}, 1000);
				  $("#footer").animate({height: 165}, 1000);
				  $(".f-bg3").animate({height: 99}, 1000);
				  $("#f-bg").animate({marginTop: -165, height:165}, 1000);
});

$(".f-banners a").hover(function(){
                       $(this).children("img").animate({opacity:1}, 1000)
                                  },
                        function(){
                       $(this).children("img").animate({opacity:0.5}, 1000)
});

//Смена города
$(".box5").children(".bc").children("p").children("a").click(function() {
		   change_city(this);
 });

  //Контекст
var cnt;
$(document).ready(function () {
  cas = $(".kontext > a");
  tes = $(".kontext");
  tes =$('.kontext:has(a)');
  if (cas.length == 0) return;
  cnt = '';

  cnt = ('<div class="b-begun">');

  cnt += ('  <h3>Рекламные объявления</h3>');

  cnt += ('  <div class="begun_context_advert"><span id="begunSpan1">');
  cnt += ('    <ul class="bgul1" id="begunRoot" style="width: 210px; padding:0;">');
  cnt += ('	   </ul></span>');
  cnt += ('  </div>');
  cnt += ('</div>');
  $("#context-ad").empty();
  $("#context-ad").append(cnt);
  cnt = '';
  for(var i=0;i<cas.length;i++){

	  href = cas[i].href;
      href2 = href.replace('http://','');
      href2 = href2.substring(0,href2.indexOf('/'));
	  title = cas[i].title;
	  hrefinnerHTML = cas[i].innerHTML;
	  startHTML = tes[i].firstChild.data;
	  if (startHTML == null) {startHTML='';}
	  endHTML = tes[i].lastChild.data;
	  if (endHTML == null) {endHTML=''};
	  rnd = 3;
	  cnt += ('<li class="bgulli70">');
	  cnt += ('<div class="bgbanner">');
	  cnt += ('<div style="text-transform:capitalize;"><a class="begun snap_noshots" style="color: rgb(9, 101, 155); font-size: 10pt; font-weight: bold; cursor: pointer;" target="_blank" href="http://click02.begun.ru/click.jsp?url=LwtFDxkfHh*0dOCsLCvE1SXfMJk0PycsTSSfzqiEyIU6hwKNV79bWQnz-bvB*Z45zjZ8XjA0Oq6fKE*CWrbLds4gWReQRNwJ5WZ1F5kMy9X9E-3pFk8kdccRZxas3vbTsd0-oKdG3Ts*x69qm8Dot52UNIYAZTigWVlQS8U6X1A2-vW3x*WIbNl7AW9eLrN5RDkwRuwvi10nnXoEJHb*coaAkGPa8Tm48DxRzq8URdf0pdgkvsF5CoCxplv8Ng-VRT9evTLxNH50c53FHJX0ozHEECUa52Fr37zTWW-6hzHy328QX9Djf5p1BQ6VvPZyQhaDbw" rel="'+href+'"  title="'+title+'">'+hrefinnerHTML+'</a></div>');
	  cnt += ('<div style="margin-top: 3px; a:first-letter{text-transform:capitalize;}"><a class="begun snap_noshots" target="_blank" href="http://click0'+rnd+'.begun.ru/click.jsp?url=LwtFDxkfHh*0dOCsLCvE1SXfMJk0PycsTSSfzqiEyIU6hwKNV79bWQnz-bvB*Z45zjZ8XjA0Oq6fKE*CWrbLds4gWReQRNwJ5WZ1F5kMy9X9E-3pFk8kdccRZxas3vbTsd0-oKdG3Ts*x69qm8Dot52UNIYAZTigWVlQS8U6X1A2-vW3x*WIbNl7AW9eLrN5RDkwRuwvi10nnXoEJHb*coaAkGPa8Tm48DxRzq8URdf0pdgkvsF5CoCxplv8Ng-VRT9evTLxNH50c53FHJX0ozHEECUa52Fr37zTWW-6hzHy328QX9Djf5p1BQ6VvPZyQhaDbw" rel="'+href+'" style="font-size: 9pt; color: rgb(51, 51, 51); text-decoration: none;"  title="'+title+'">'+startHTML+hrefinnerHTML+endHTML+'</a></div>');
	  cnt += ('<div style="margin-top: 3px; p:first-letter: "><a class="begun snap_noshots" target="_blank" href="http://click0'+rnd+'.begun.ru/click.jsp?url=LwtFDxkfHh*0dOCsLCvE1SXfMJk0PycsTSSfzqiEyIU6hwKNV79bWQnz-bvB*Z45zjZ8XjA0Oq6fKE*CWrbLds4gWReQRNwJ5WZ1F5kMy9X9E-3pFk8kdccRZxas3vbTsd0-oKdG3Ts*x69qm8Dot52UNIYAZTigWVlQS8U6X1A2-vW3x*WIbNl7AW9eLrN5RDkwRuwvi10nnXoEJHb*coaAkGPa8Tm48DxRzq8URdf0pdgkvsF5CoCxplv8Ng-VRT9evTLxNH50c53FHJX0ozHEECUa52Fr37zTWW-6hzHy328QX9Djf5p1BQ6VvPZyQhaDbw" rel="'+href+'" style="white-space: nowrap; font-size: 9pt; color: rgb(171, 171, 171); text-decoration: none;"  title="'+title+'">'+href2+'</a> <span style="white-space: nowrap;"></span></div>');
	  cnt += ('</div></li>');
  }
  $("#begunRoot").empty();
  $("#begunRoot").append(cnt);
  $(".snap_noshots").click(function(){
   hr = $(this).attr("rel");
   //$(this).attr("href",hr);
   window.open(hr);
   return false;
  });
  $(".kontext").remove();
});

//Правый аккордеон
$("#newsList").find(".nav").children("li").children("a").click(function(){

            newlink=$(this).children("i").attr("class");
            textlink = $(this).html();
			text = $(this).children("i").text();
			var addlink="<a href='"+newlink+"' >"+text+"</a>";
			litags=$(this).parent().parent().children(".active");
			text1 = $(litags).children("a").children("i").text();
            $(litags).children("a").children("i").stopTime();

			 if (text1=='') text1=text;
		  	 $(litags).children("div").slideUp(900).parent().removeClass('active').children("a").children("i:first").text(text1);
		   $(this).siblings(".dl:hidden").slideDown(900).siblings(".scroller").slideDown(900).parent().addClass("active clearfix").children("a").children("i:first").text(text);

		  if ($(this).siblings(".scroller").is(":visible")) scrol = $(this).siblings(".scroller");

			if ($(this).parent().is(".active")) {$(this).children("i:first").oneTime(2000,"atimer", function() {
 $(this).html(addlink)})} else {$("i").stopTime();};

		  	var scrol =  $(this).siblings('.scroller');
				if (scrol!=null){

		              function rscroller(scr) {

			 		             var parent = $(scr).parent();
					             var number =parent.find('dl dd').size();

					     if (number > 1){

				                 var heightUl = parent.find('dl').height() -180;
				                 var intOverallDelta = heightUl;

				    	 parent.find(".scroller").slider({
				    		orientation: "vertical",
				    		range: "min",
				    		min: 0,
				    		max: heightUl,
				    		value: heightUl,
				    		slide: function(event, ui) {
				    		intOverallDelta = ui.value;
				    			 var sdvig = heightUl - ui.value;
                                 var prop = 181/heightUl;
                                 var sdvig1 = 179 - ui.value*prop;
				    			 parent.find('dl').css({'margin-top': -sdvig + 'px'});
								 parent.find('.scroll-point').css({'top': +sdvig1 + 'px'});
				    		                           }
				    	                                 });

						 parent.mousewheel(function(objEvent, intDelta){
						     if (intDelta > 0 && intOverallDelta < heightUl){intOverallDelta += 12;}
							 else if (intDelta < 0 && intOverallDelta > 0){intOverallDelta -= 12;}
						 objEvent.stopPropagation();
 		                 objEvent.preventDefault();
						 parent.find(".scroll-field").slider('option', 'value', intOverallDelta);
						     var sdvig = heightUl - intOverallDelta;
							 var prop = 181/heightUl;
                             var sdvig1 = 179 - intOverallDelta*prop;
			    		 parent.find('dl').css({'margin-top': -sdvig + 'px'});
						 parent.find('.scroll-point').css({'top': +sdvig1 + 'px'});
						                                                  });
					     }
						 else{$(scr).remove();}
			                                          };
					    rscroller(scrol);}	//endif


$(this).hover(function () {
						$(this).children("i").children("a").css("color","#084783")},
			  function () {
					    $(this).children("i").children("a").removeAttr("style")
                          })


});
});


jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		guid: 1,
		global: {},
		regex: /^([0-9]+)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseInt(result[1], 10);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;

			if (jQuery.isFunction(label)) {
				if (!times)
					times = fn;
				fn = label;
				label = interval;
			}

			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}

			times = times || 0;
			belay = belay || false;

			if (!element.$timers)
				element.$timers = {};

			if (!element.$timers[label])
				element.$timers[label] = {};

			fn.$timerID = fn.$timerID || this.guid++;

			var handler = function() {
				if (belay && this.inProgress)
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};

			handler.$timerID = fn.$timerID;

			if (!element.$timers[label][fn.$timerID])
				element.$timers[label][fn.$timerID] = window.setInterval(handler,interval);

			if ( !this.global[label] )
				this.global[label] = [];
			this.global[label].push( element );

		},
		remove: function(element, label, fn) {
			var timers = element.$timers, ret;

			if ( timers ) {

				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.$timerID ) {
							window.clearInterval(timers[label][fn.$timerID]);
							delete timers[label][fn.$timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}

					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}

				for ( ret in timers ) break;
				if ( !ret )
					element.$timers = null;
			}
		}
	}
});

if (jQuery.browser.msie)
	jQuery(window).one("unload", function() {
		var global = jQuery.timer.global;
		for ( var label in global ) {
			var els = global[label], i = els.length;
			while ( --i )
				jQuery.timer.remove(els[i], label);
		}
	});