$(document).ready(function(){


	jQuery.extend( jQuery.easing,
	{
		easeInQuart: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		easeOutQuart: function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeInOutQuart: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		}
	});
	
	
	// tabs	// class=".js-tabs .js-tabs_xxx" where xxx is tabs class // qty & order should be equal
	$(".js-tabs LI").click(function(e){
		var t = $(this);
		t.addClass("cur").siblings(".cur").removeClass("cur");
		$("." + t.closest(".js-tabs").get(0).className.split("js-tabs_")[1].split(" ")[0]).hide().eq(t.prevAll("LI").length).show();
		return false;
	}).filter(".cur").removeClass("cur").click();


	// menu__tabs
	$(".menu__tabs LI").click(function(){
		var menu = $(this).closest(".menu"),
			arr = $(".menu__page__arr", menu),
			cur = $(".menu__page:visible", menu);
		
		if (arr.css("top") != $(this).position().top) {
			if ($(this).position().top > cur.outerHeight()) {
				arr.stop(true).addClass("menu__page__arr_bl").css({left:cur.position().left, top:cur.outerHeight()});
			} else {
				arr.stop(true).removeClass("menu__page__arr_bl").css({left:"28%"}).animate({top:$(this).position().top}, 400, "easeOutQuart");
			}
		}
	});


	// idrop
	$(".idrop").each(function(){
		var par = $(this),
			head = $(".idrop__head", this),
			tale = $(".idrop__tale", this),
			mhead = $(".menu_idrop__head", this),
			mtale = $(".menu_idrop__tale", this);
			
		head.click(function(e){
			$(".idrop_open").not(par).removeClass("idrop_open");
			if (par.hasClass("idrop_open")) {
				par.removeClass("idrop_open");
			}
			else {
				par.addClass("idrop_open");
				if (head.width() > tale.width()) {
					tale.width(head.width());
				}
				tale.css({marginLeft:-tale.width()/2-parseInt(tale.css("paddingLeft"))});
			}
			return false;
		});

		mhead.click(function(e){
			$(".idrop_open").not(par).removeClass("idrop_open");
			if (par.hasClass("idrop_open")) {
				par.removeClass("idrop_open");
			}
			else {
				par.addClass("idrop_open");
				if (mhead.width() > mtale.width()) {
					mtale.width(mhead.width());
				}
				mtale.css({marginLeft:-mtale.width()/2-parseInt(mtale.css("paddingLeft"))});
			}
			return false;
		});





	});
	$(document).bind("click", function(){
		$(".idrop_open").removeClass("idrop_open");
	});

	
	// link-scroll
	$(".link-scroll").click(function(){
		var targ = $("#" + this.href.split("#")[1])
		if (targ.length) {
			$(window).scrollTo(targ , 800, {easing: "easeInOutQuart", offset: -20});
		}
		return false
	});
	
	
	// popgal
	$(".popgal").each(function(){
		var par = $(this),
			tint = $(".tint"),
			pop_close = $(".popgal__close", this),
			ul = $("UL", this),
			li = $("LI", this),
			larr = $(".popgal__prev", this),
			rarr = $(".popgal__next", this);
		
		pop_close.add(tint).add(li).click(function(){
			tint.hide();
			par.hide();
			return false;
		});
		
		larr.click(function(){
			var pos = $("LI.cur", par).prevAll().length;
			pos > 0 ? popgalSet(par, pos-1) : popgalSet(par, li.length-1);
			return false;
		});
		rarr.click(function(){
			var pos = $("LI.cur", par).prevAll().length;
			pos < li.length-1 ? popgalSet(par, pos+1) : popgalSet(par, 0);
			return false;
		});


		// keyw
		$(document).bind('keydown', 'right space', function(){
			$(".popgal__next", par).click();
			return false;
		});
		$(document).bind('keydown', 'left', function(){
			$(".popgal__prev", par).click();
		});
		$(document).bind('keydown', 'esc', function(){
			$(".popgal__close", par).click();
		});

	});
	
	function popgalSet(par, n) {
		var li = $("li", par),
			cur = $("LI.cur", par),
			next = li.eq(n),
			larr = $(".popgal__prev I", par),
			rarr = $(".popgal__next I", par);
			
		// if (cur.prevAll().length == next.prevAll().length) {return false;}
			
		cur.removeClass("cur");
		next.addClass("cur");
		
		par.css({
			height: next.height(),
			marginTop: -next.height()/2
			});
		$("UL", par).css({
			width: next.width()
			});
		$(".popgal__close", par).css({
			marginRight: -next.width()/2
			});
		
		larr.css({right:50+next.width()/2});
		rarr.css({left:50+next.width()/2});
			
	}
	
	$(".js_popgal LI").click(function(e){
		var t = $(this),
			gal = $("#" + t.closest(".js_popgal").get(0).className.split("js_popgal_")[1].split(" ")[0]).show(),
			tint = $(".tint").show();
		popgalSet(gal, t.prevAll("LI").length);
		return false;
	})
	
	
	// fix font in webkit
	if ($.browser.webkit) {
		$(window).bind("load", function(){
			$(".sidenav LI A,.contacts A").css({marginLeft:1, marginRight:1});
		});
		
	}
	
		
}); // dom ready


// jQuery.ScrollTo
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
