// JavaScript Document


function debug(vsr) {
	$("body").append('<div id="debug" style="position:fixed; z-index: 999; left:50%; bottom: 10%; width:300px; margin: 0 0 0 -150px; padding: 8px 12px 10px; background: #fff; border: 1px solid #666; font-size: 12px;">'+vsr+'</div>');
}

(function($){
	$.fn.accMenu = function() {
		var $open = null;
		return this.each(function(){
			var $this = $(this);
			var $acc = $this.next().css({"height":'0px', "display":'block'});
			if ($acc.innerHeight()!=0) {
				$open = $acc;
			}
			$this.bind("click", function(){
				if (!$(this).parent().hasClass("sz-ma")) {
					$(this).parent().addClass("sz-ma");
					$acc.animate({"height": $("ul",$acc).innerHeight()+'px'},500, function(){$acc.css({"height":'auto'})});
					if ($open!=null) {					
						$open.parent().removeClass("sz-ma");				
						$open.animate({"height": '0px'},500);
					}
					$open = $acc;
				}
				return false;
			});
		});
	}
})(jQuery);

(function($){
	$.fn.seosContent = function(settings) {
		var settings = {
			sHeight : '6.1em'
		};
		var o = $.extend(settings, o);
		return this.each(function(){
			var $this = $(this);
			var sh = o.sHeight;
			var sblink = $this.find("div.seoslink");
			var lnk = sblink.find("a");
			var bhide = $this.find("div.seostxt");
			var hblock = $this.find("div.seoh");
			if (bhide.height()<hblock.height()) {
				sblink.show();
				$("body").bind("click", function(){
					sblink.show();
					bhide.css({"height": sh});							 
				});
				bhide.bind("click", function(event){
					event.stopPropagation();							 
				});
				lnk.bind("click", function(event){
					event.stopPropagation();
					sblink.hide();
					bhide.css({"height":'auto'});
					return false;
				});
			}
			else {
				bhide.css({"height": 'auto'});
				sblink.hide();
			}
		});
	}
})(jQuery);

(function($){
	$.fn.loadPhoto = function(bigimg) {		
		return this.each(function(){
			var $ta = $("a img",$(this)).parent();
			var $li = $(this).find("li");
			var $img = $("#"+bigimg).find("img").eq(0);
			var $load = $("#"+bigimg).find("div.loading");
			var imgPreloader = new Image();
			$ta.bind("click", function(){
				var $tt = $(this);
				$load.css({"visibility":'visible'});
				imgPreloader.onload = function(){
					setTimeout(function(){
					imgPreloader.onload = null;
					$img.attr("src",imgPreloader.src);
					$li.removeClass("active");
					$tt.parent().parent().addClass("active");
					$load.css({"visibility":'hidden'});
					}, 1000);
				}
				imgPreloader.src = $(this).attr("href");
				return false;
			})
		});
	}
})(jQuery);

function focusSearch() {
	var ss = $("#top-search");
	ss.bind("focus", function() {
		ss.parent().parent().parent().parent().addClass("search-a");
		if (ss.val()=='поиск') {
			ss.val('').removeClass("scdef");
		}
	});
	ss.bind("blur", function() {
		ss.parent().parent().parent().parent().removeClass("search-a");
		if (ss.val()=='') {
			ss.val('поиск').addClass("scdef");
		}
	});	
}

function itemMore ($obj, $this) {
	$item = $($obj);
	$item.show();
	$($this).parent().hide();	
}

function OfilterNew(th) {
	
	var x = th.options[th.selectedIndex].value;
	
	if (th.options[th.selectedIndex].text != "Все"){
		$('#brends_zakaz option').eq(0).attr('selected', 'selected');
		jQuery('.brends_all').text("Все › " + th.options[th.selectedIndex].text);
		var seconds = $("#second");
		$("#seconds").each(function(){
		$("option", this).hide();
		$("." + x, this).show();
		
		})
	}
	else{
		jQuery('.brends_all').text("Все");
		var seconds = $("#second");
		$("#seconds").each(function(){
		$("option", this).show();
		})
	}
	jQuery('.brends_all').show();
	$('.brends_all').attr('selected',"selected");
}

function selectZakazBrend(th){
	if (th.selectedIndex != 0){
		$('#firsts option').eq(0).attr('selected', 'selected');
		jQuery('.brends_all').text("Все");
		$('#seconds option').eq(0).attr('selected', 'selected');
		$('#seconds option').show();
		
	}
}

function selectBrend(th){
	if (th.selectedIndex != 0){
		$('#brends_zakaz option').eq(0).attr('selected', 'selected');
	}
}

function vsTabs(tab) {
	jQuery('.navs-menu li a').removeClass("dd-hover")
	jQuery('.vsearch').addClass("dnone");
	jQuery("#"+tab+"").addClass("dd-hover");
	jQuery("#"+tab+"-np").removeClass("dnone");
}

function isAll ($obj) {
	var $this = $($obj);
	$this.parent().next().css("height",'auto');
	$this.removeClass("dott").addClass("nodott");	
}

function rubAll ($obj, $lnk) {
	var $this = $("#"+$obj);
	if (!$this.is(":visible")) {
		$this.show();
		$($lnk).html("скрыть<em>&uarr;</em>");
	}	
	else {
		$this.hide();
		$($lnk).html("все рубрики<em>&darr;</em>");	
	}
}

(function($){
	$.fn.clearSearch = function() {		
		return this.each(function(){
			var $search = $("input.tts-inp", $(this));
			var $close = $("a.tts-close", $(this));
			var $button = $("input.tts-button", $(this));
			var $ival = $search.val();
			$close.bind("click", function(){
				$search.val("");
				$search.focus();
				return false;
			});
			$button.bind("mousedown", function(){
				$button.addClass("tts-button-focus")						   
			});
			$search.bind("focus blur", function(event){
				if (event.type == "focus") {
					$search.parent().parent().parent().parent().addClass("tt-search-focus");
				//	if ($search.val()=='Поиск в каталоге товаров и услуг') {
						$search.removeClass("tts-gr");
						$search.val("");
				//	}
				}
				else {
					if ($search.val()=='') {
						$search.val($ival);
					}
					$search.parent().parent().parent().parent().removeClass("tt-search-focus");
				//	if ($search.val()=='Поиск в каталоге товаров и услуг') {
						$search.addClass("tts-gr");
				//	}
				}
			});
			$search.parent().bind("mouseenter mouseleave", function(event) {
				if (event.type == "mouseenter") {
					$search.parent().parent().parent().parent().addClass("tt-search-hover");
				}
				else {
					$search.parent().parent().parent().parent().removeClass("tt-search-hover");
				}
			});
		});
	}
})(jQuery);

function indexMenu() {
	var $im = $("#index-menu");
	var $lnk = $("td a", $im);
	var $lia = $("td.tm-a", $im);
	var $plash = $(".tm-aplash", $im);
	var $prods = $("div.prodlist-index", $("#index-load"));
	var $cats = $("div.rubrik-cats", $("#index-load"));
	$lnk.bind("click", function(){
		if ($(this).attr("id")!='rub-uslugi_cat') {
			if (!$(this).parent().hasClass("tm-a")) {
				$lnk.parent().removeClass("tm-a");
				$(this).parent().addClass("tm-a");
				$prods.hide();
				$("#"+$(this).attr("id")+"-prods").show();
				$cats.hide();
				$("#"+$(this).attr("id")+"-category").show();
				$plash.css({"width":pW($(this).parent()).wid+'px',"left":pW($(this).parent()).pos+'px',"visibility":'visible'});
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;	
		}
	});
	var pW = function(obj) {
		var $w, $p;
		if ($lnk.parent().index(obj)==0) {
			$w = obj.innerWidth()+obj.prev().innerWidth()+obj.next().innerWidth()/2;
			$p = obj.prev().position().left;
			$("i",$("td",$im)).css("visibility",'visible');
			$("i",obj.next()).css("visibility",'hidden');
		}
		else if ($lnk.parent().index(obj)==$lnk.length-1) {
			$w = obj.innerWidth()+obj.prev().innerWidth()/2+obj.next().innerWidth()+2;
			$p = obj.prev().position().left+obj.prev().innerWidth()/2;								
			$("i",$("td",$im)).css("visibility",'visible');
			$("i",obj.prev()).css("visibility",'hidden');
		}
		else {
			$w = obj.innerWidth()+obj.prev().innerWidth()/2+obj.next().innerWidth()/2;								
			$p = obj.prev().position().left+obj.prev().innerWidth()/2;								
			$("i",$("td",$im)).css("visibility",'visible');
			$("i",obj.next()).add($("i",obj.prev())).css("visibility",'hidden');		
		}
		return {"pos": $p, "wid": $w};
	}
	$plash.css({"width":pW($lia).wid+6+'px',"left":pW($lia).pos+'px',"visibility":'visible'});
	$(window).bind("resize", function(){
		$plash.css({"width":pW($("td.tm-a", $im)).wid+'px',"left":pW($("td.tm-a", $im)).pos+'px',"visibility":'visible'});
	});
}

function cmenuZakaz() {
	var rz = function(){
		var $sz = $("p.top-menu-sz");
		if ($(window).width()<1100){
			$sz.addClass("top-menu-sz2");
		}
		else {
			$sz.removeClass("top-menu-sz2");
		}	
	}
	rz();
	$(window).bind("resize", function(){
		rz();	
	})	
}

(function($){
	$.fn.focusButton = function(settings) {
		var settings = {
			focusClass : 'button-focus'
		};
		var o = $.extend(settings, o);		
		return this.each(function(){
			$this = $(this);
			$button = $this.find("button");
			$button.bind("click", function(event){
				$this.addClass(o.focusClass);
				event.stopPropagation();
			});
			$("body").bind("click", function(){
				$this.removeClass(o.focusClass);
			});
		});
	}
})(jQuery);
/*табы в событиях журнала*/
function changeTabs($lid, $lbl)
{
	var $link = document.getElementById($lid),
			$blok = document.getElementById($lbl),

	$children = $link.children;
	for ( var i = 0; i < $children.length; i++ )
	{
		$children[i].onclick = function(i)
		{
			var removeC = function()
			{
				for (var j = 0; j < $children.length; j++)
				{
					$blok.children[j].style.display = 'none';
					$link.children[j].className = '';
				}
			}
			return function()
			{
				if ($link.children[i].className == 'active')
					return;
				removeC();
				$blok.children[i].style.display = 'block';
				$link.children[i].className = 'active';
			}
		}(i)
	}
}

(function($){
	$.fn.indAccord = function(o) {
		var defaults = {start: 0};
    	var o = $.extend(defaults, o);
		return this.each(function(){
			var $this = $(this);
			var $item = $this.find("div.accord-item");
			var $pix = $item.find("div.event-pix");
			var $lnk = $item.find("div.event-more");
			$pix.css('height', 0);
			$item.eq(o.start).addClass("accord-a");
			$pix.eq(o.start).css({height: '250px'});
			$lnk.bind("mouseenter", function(){
				var $ind = $lnk.index($(this));
				$pix.stop(true, true);
				$pix.filter(':not(:eq('+$ind+'))').animate({height: 0},300);
				$pix.eq($ind).animate({height: 250},300);				
				$("div.accline", $this).show();
				$item.eq($ind).find("div.accline").hide();
				if ($ind>0) {
					$item.eq($ind).prev().find("div.accline").hide();
				}
				$item.filter(':not(:eq('+$ind+'))').removeClass("accord-a");
				$item.eq($ind).addClass("accord-a");
			});
		});
	}
})(jQuery);

(function($)
{
	$.fn.autoVslider = function(options)
	{
		var o = 
		{
			time : 4000,
			second_time : 100,
			speed_1 : 325,
			speed_2 : 415,
			top_b : '.pres-img',
			bottom_b : '.pres-ph-wrdesc'
		}
		this.each(function()
		{
			if(options)
				$.extend(o, options)
				var $first_height = $(o.top_b).children().outerHeight(),
						$length = $(o.top_b).children().length,
						$second_height = $(o.bottom_b).children().outerHeight(),
						is_animate = true,
						is_animate_bug = true;
				
				/*$(o.top_b, this).css("top", -($length * $first_height) + $first_height);
				$(o.bottom_b, this).css("top", -($length * $second_height) + $second_height);*/
				
				function startRotate()
				{
					is_animate_bug = false;
					$(o.top_b).animate({'marginTop' : $first_height}, o.speed_1, function()
					{
						$(o.top_b).children().eq($length - 1).prependTo(o.top_b);
						$(this).css('marginTop', '0');
					});
					
					setTimeout(function()
					{
						$(o.bottom_b).children().eq($length - 1).prependTo(o.bottom_b);
						$(o.bottom_b).css('marginTop', -$second_height);
						$(o.bottom_b).animate({'marginTop' : 0}, o.speed_2, function()
						{
							
							
							is_animate_bug = true;
						});
					}, o.second_time);
					is_animate_bug = true;
				}
				
				$(this).bind("mouseenter", function()
				{
					is_animate = false;
				});
				$(this).bind("mouseleave", function()
				{
					is_animate = true;
				});
				setInterval(function()
				{
					if (is_animate && is_animate_bug)
						startRotate()
				}, o.time);
		});
	}
})(jQuery);
//замена оформления заголовка элемента select
/*(function($){ очень глючная
  $.fn.selectStyle = function(options){
	  
	  return $(this).each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  
	}
 })(jQuery);*/

$(document).ready(function(){
	$('a.share-email-btn').click(function(){
		$('#share-popup').show().find('textarea').val('Рекомендую '+$(location).attr('href'));
		return false;
	});
	$('#share-popup .close-btn').click(function(){
		$('#share-popup').hide();
	});
	//$('.pres-tabs #filter-select select').selectStyle();
});

function dom_by_journal(){
	$.ajax({
	  type: "POST",
	  url: "/addon/secret/get_secret.php",
	  async: false,
	  data: "asdf=dom_by",
	  success: function(data){
	    document.getElementById('journal_foo').value = data;
	  }
	});
}
