$(document).ready(function(){

/* slide show */

	var count = $('#gallery #text div').length;
	var one = $('#gallery #text div').width();
	var fullWidth = one * count;
	var ifWidth = one*(count-1);
	$('#gallery #face').width(fullWidth);
	
	/* add highlight */
		var addLi = "";
		for (var i=0; i<count; i++) {
			addLi += "<li></li>";
		}
		$('#gallery #hl').html(addLi);
		if(count >= 1){
			$(".controls span i").text('1');
			$(".controls span b").text(count);
			$("#gallery #hl li:first-child").addClass("act");
		}
	/* add highlight */
	
	$('#gallery .left').bind('click', moveLeft);
		function moveLeft(){
			$('#gallery .left').unbind();
				if (($('#gallery #face').position().left >= -ifWidth) && ($('#gallery #face').position().left < 0 )){	
					$('#gallery #face').animate({left: "+=" + one},500, function(){
						
						$('#gallery #image').animate({left: "+=" + one},700);
						$('#gallery #text').animate({left: "+=" + one},1000);
						var state = parseInt($(".controls span i").text());
						$(".controls span i").text(state-1);
						$('#gallery #hl .act').removeClass("act");
						$("#gallery #hl li:nth-child("+(state-1)+")").addClass("act");
						$('#gallery .left').bind('click', moveLeft);
					});
				}else{
					$('#gallery .left').bind('click', moveLeft);
				}
		};

	$('#gallery .right').bind('click', moveRight);
		function moveRight(){
			$('#gallery .right').unbind();
				if ($('#gallery #face').position().left > -ifWidth){	
					$('#gallery #face').animate({left: "-=" + one},500, function(){
						$('#gallery #image').animate({left: "-=" + one},700);
						$('#gallery #text').animate({left: "-=" + one},1000);
						var state = parseInt($(".controls span i").text());
						$(".controls span i").text(state+1);
						$('#gallery #hl .act').removeClass("act");
						$("#gallery #hl li:nth-child("+(state+1)+")").addClass("act");
						$('#gallery .right').bind('click', moveRight);
					});
				}else{
					$('#gallery .right').bind('click', moveRight);
				}
		};

	
	$('#gallery #hl li').each(function(){
		
		$(this).bind('click', move);
		
		function move(){
			var lishka = $(this);
			var state = lishka.prevAll().length;
			$('#gallery #hl li').unbind();
			$('#gallery #face').animate({left: -one*state},500, function(){
				$('#gallery #image').animate({left: -one*state},700);
				$('#gallery #text').animate({left: -one*state},1000);
				$(".controls span i").text(state+1);
				$('#gallery #hl .act').removeClass("act");
				lishka.addClass("act");
				$('#gallery #hl li').bind('click', move);
			});
		};
	});
	
	$("a.big_photo").fancybox();
	
	
	$("#enter").fancybox({
		
			'hideOnOverlayClick' : true,
			'showCloseButton' 	   : false,
			'autoScale'			   : false,
			'autoDimensions'	   : false,
			'width'					   : '450',
			'height'					   : '305',
			'onComplete'			: function(){SetLoginForm()},
			'content' : $("#login-form").html()
			
		
		});
	
	$(".sublined").addClass("lined").removeClass("sublined").wrapInner('<div></div>');
		
	function SetLoginForm()
	{
		$(":text").labelify();
		
		SocialAuth.add({
			onUserLoggedOut: function () {			
				$('#fancybox-content .reg_form').submit(function(){
					$("#fancybox-content .login-form .error").hide();
					return SocialAuth.login(
						$('#fancybox-content .login-form input[type=text]').attr('value'),
						$('#fancybox-content .login-form input[type=password]').attr('value'));
				});
			},
			onUserLoggedIn: function(userInfo){

			},
			onError: function(error) {
				$("#fancybox-content .login-form .error").show();
		}
});
//--></script>
		
	}
		

  $(":text").labelify();


		
/* slide show */

/*shares*/
	ODKL.init();

});
