$(document).ready(function () {

	if($('#vk_groups2').length){
			var ep = $('.photos.cf'),
				ew = $('#vk_groups2'),
				s1 = ew.offset().top,
				s2 = ep.offset().top + ep.height();

			var h = s2 - s1;

			VK.Widgets.Group("vk_groups2", {mode: 2, width: "300", color3 : '#008594', color2 : '#006F74', color1 : '#b0d9de', height:h}, 55284725);
	}		


	$('a[href^="http"], a[href^="//"]').attr('target','_blank');

	if($('.sync-height').length){
		$('.negative .sync-height li').each(function(index, element) {
            var negativeHeight = $(element).height();
			var positiveHeight = $('.positive .sync-height li').eq(index).height();
			if (positiveHeight > negativeHeight)
				$(element).height(positiveHeight);
			else 
				$('.positive .sync-height li').eq(index).height(negativeHeight);
        });
	}

    $('.imgholder').each(function(){
    	var hld = $(this),
    		img = hld.find('.img');
    	$('<img src="'+img.attr('src')+'"/>').load(function(){
    		var r = this.width / this.height;
    		if(r > 1){
    			var h = hld.height(),
    				w = Math.round(h * r);
    			img.attr('style','height:'+h+'px !important;width:'+w+'px !important;position:absolute;left:'+Math.round((hld.width() - w)/2)+'px !important');
    		}
    		else{
    			var w = hld.width(),
    				h = Math.round(w/r);
    			img.attr('style','height:'+h+'px !important;width:'+w+'px !important;position:absolute;top:'+Math.round((hld.height() - h)/2)+'px !important');
    		}
    	});
    })

	var topMenu = $('#header .menu-box');
	var topMenuTop = topMenu.offset().top;

	function fixingMenus(){
		$(window).on('scroll',function(e) {
            if ($(window).scrollTop() > topMenuTop){
            	if(!topMenu.is('.fixed')){
            		topMenu.addClass('fixed');
            	}
				topMenu.css({
					'margin-left' : -$(window).scrollLeft() + 'px'
				});
			} else{
				topMenu.removeClass('fixed');
			}
        });

	}fixingMenus();
	


	var userBox = new function(){
		var e = $('#wrap .user-box'),
			buttons = e.find('.user-red-btn,.user-blue-btn,.user-white-btn');

		if(!e.is('.min')){
			buttons.addClass('hover');
			return;
		}

		var appearTime = 150,
			openTime = 200,
			hoverTimeout = false,
			hoverAllowTimeout = false,
			mouseover = false,
			mouse = {x:0,y:0},
			min = function(){
				clearTimeout(hoverTimeout);
				clearTimeout(hoverAllowTimeout);
				e.addClass('min');
				buttons.removeClass('hover');
			},
			hold = false;

		e.find('input').
			focus(function(){hold = true}).
			blur(function(){hold = false; if(!mouseover) min()});
		

		e.mousemove(function(ev){
			mouse.x = ev.pageX;
			mouse.y = ev.pageY;
		});

		e.not('.picket').hover(
			function(ev){
				mouseover = true;
				clearTimeout(hoverTimeout);
				hoverTimeout = setTimeout(function(){
					e.removeClass('min');
					clearTimeout(hoverAllowTimeout);
					hoverAllowTimeout = setTimeout(function(){
						buttons.addClass('hover');
						buttons.each(function(){
							var e = $(this),
								o = e.offset(),
								r = {
									x : o.left,
									y : o.top,
									w : e.width(),
									h : e.height()
								};
								
							if(mouse.x > r.x && mouse.x < r.x + r.w && (mouse.y < r.y || mouse.y > r.y + r.h)){
								$('body,html').animate({scrollTop:$(window).scrollTop()+25},500);
							}
						});
					},openTime);


				},appearTime);
			},
			function(){
				mouseover = false;
				if(hold) return;
				min();
			}
		);
	}
	
//	!$val.match("^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}$")
	function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
    }
    
    var checkM = function() {
	var val = $(this).children('input[type="text"]').val();
	
	if(!isValidEmailAddress(val)){ 
		$(this).children('input[type="text"]').css({ color: '#e6322b'});
		$(this).children('input[type="text"]').unbind('keyup');
		$(this).children('input[type="text"]').bind('keyup',checkMM);
		return false;
	}
	};
	$('#subForm').submit(checkM);
	var checkMM = function() {
	var val = $(this).val();
	
	if(!isValidEmailAddress(val)){ ;
		$(this).css({ color: '#e6322b'});
	} else {
		$(this).css({ color: '#000000'});
	}
	}
	//$('#subForm').children('input[type="text"]').focus(function() {
	//	$(this).css({ color: '#000000'});
	//})
	
	$('.ikraLink').click(function(){ location.href = "http://www.infocreative.ru/#fromsite"; return false; });



(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||
function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new
Date();a=s.createElement(o),
m=s.getElementsByTagName(o)
[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
 })(window,document,'script','../www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-41885477-1', 'navalny.ru');
  ga('send', 'pageview');


  if(window.scroll_ga_function){
	  $(window).on('scroll.gascroll',function(){
		var w = $(window),
			d = $(document);
		if(w.scrollTop() >= d.height() - w.height()*2){
			scroll_ga_function();
			w.unbind('scroll.gascroll');
		}
	  });
	}

});

// Facebook.
/*
(function(){
	var check = location.hash == '#lol' ? new TestCheck : new CookieCheck;

	function CookieCheck(){
		function get_cookie(cookie_name){
		  var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); 
		  if (results)
		  	return (unescape(results[2]));
		  else
		  	return null;
		}
		this.showed = function(){
			return (get_cookie('popup') == 'yes');
		}
		this.after = function(){
	        document.cookie = "popup=yes";
		}
	}

	function TestCheck(){
		var showed = false;
		this.showed = function(){
			return showed;
		}
		this.after = function(){
	        showed = true;
		}
	}
    
    var leaveTimeout;

	$(document).on('mouseenter', function() {
        entered = true;
        clearTimeout(leaveTimeout);
    });

	$(document).on('mouseleave', function() {
        leaveTimeout = setTimeout(function(){
    		if(!check.showed()){
	    			check.after();
		            $.colorbox({
		            	html:$('#away_popup').html(),
		            	width:500,
                        height:305,
		        	    opacity:0.3
		            });
                var o = 
                $('#cboxLoadedContent>div>center').append($('#vk_groups'));
		    }
        }, 500);
	});

})();
*/
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "../connect.facebook.net/ru_RU/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
