function faceBookScripts() {
	$('.b-article-comments a em').each(function(){
		q = parseInt($(this).text());
		if ($('.b-copyright p:eq(0)').text().indexOf('А') > -1) {
			if (q != 0) {
				if (isNaN(q)) {
					r = 'Комментарии';
				} else {
					r = q + ' ' + getNumWord(q,'комментарий','комментария','комментариев');
				}
			} else {
				r = 'Kомментариев пока нет';
			}
		} else {
			if (q != 0) {
				if (isNaN(q)) {
					r = 'Comments';
				} else {
					r = q + ' ' + getNumWord(q,'comment','comments','comments');
				}
			} else {
				r = 'No comments yet';
			}			
		}
		$(this).text(r);
		$(this).parent().parent().show();
	});
}

function blogImageLoaded($img) {
	if ($img.filter(':visible').length == 1) {
		$img.parent().animate({height:$img[0].height + 'px'},600,function(){
			$img.attr('height',$img[0].height).hide().css('visibility','visible').fadeIn(200);
		});
	} else {
		$img.attr('height',$img[0].height);
	}
}

function imageLoaded() {	
	$('.gallery-block-image').animate({width: $('.gallery-block-image span img').innerWidth() + 20 + 'px'},1000,function(){
		$('.gallery-block-image span').hide().css('visibility','visible');
		$('.gallery-block-image span').fadeIn(300);
		if ($('.b-price').length == 1) {
			lines = $('.thumbs-wrapper td.selected .code').html().split('|');
			head = $.trim(lines[0]);
			lines_ = new Array();
			for (j=1;j<lines.length;j++) {
				lines_[j-1] = $.trim(lines[j]);
			}
			delete(lines[0]);
			code = '<dl style="display:none;"><dt>' + head + '</dt><dd>' + lines_.join('<br/>') + '</dd></dl>';
			$('.sale-block').html(code);
			$('.b-price i').html($('.thumbs-wrapper td.selected img').attr('class').replace('price-','') + '$');
			$('.b-price').fadeIn(200);
			$('.sale-block').css('visibility','visible').show();
			$('.sale-block dl').fadeIn(200);
		}
	});
	
	prts = document.location.href.split('#');
	prts[1] = $('.gallery-block-image span img').attr('src').substring(window.pictureUrl.indexOf('/photo/') + 7);
	document.location.href = prts.join('#');

	enableButtons();
	
}

function disableButtons() {
	
	$('.gallery-arrow:visible').fadeOut(200);
	
}

function enableButtons() {
	
	if (typeof $('.b-price')[0] != 'undefined' && $('.thumbs-wrapper td a').length == 1) {
		return false;
	}
	$('.gallery-arrow').fadeIn(200);
	
}

function loadImage() {
	
	window.pictureUrl = $('.thumbs-wrapper td.selected img').attr('src').replace('60x60xM','0x380xIW');
	
	disableButtons();

	code = '<img src="' + window.pictureUrl + '" alt="" onload="imageLoaded();" />';
	$('.gallery-block-image span').html(code);
	
	$('.gallery-block-image span').css('visibility','hidden');
	
	if ($('.b-price').length == 1) {
		$('.b-price').fadeOut(200);
		$('.sale-block').css('visibility','hidden');
	}
	
}


function thumbsButtonsSetup() {
	
	if (Math.round((-1) * $('.thumbs-wrapper table').css('left').replace('px','') / 70) == 0) {
		$('.button-left').addClass('blocked').removeClass('hover');
	} else {
		$('.button-left').removeClass('blocked');
	}
	
	if ($('.thumbs-wrapper td:visible').length + Math.round(1 * $('.thumbs-wrapper table').css('left').replace('px','') / 70) == 11) {	
		$('.button-right').addClass('blocked').removeClass('hover');
	} else {
		$('.button-right').removeClass('blocked');
	}
	if ($('.thumbs-wrapper td:visible').length < 11) {
		$('.button-right').addClass('blocked');
	} 
	
}

function selectThumb() {
	
	prts = document.location.href.split('#');
	if (typeof prts[1] != 'undefined') {
		if ($('.thumbs-wrapper td:visible').find('img[src$="' + prts[1] + '"]').length == 1) {
			$('.thumbs-wrapper td:visible').find('img[src$="' + prts[1] + '"]').parent().parent().addClass('selected');
		} else {
			$('.thumbs-wrapper td:visible:eq(0)').addClass('selected');
		}
	} else {
		
		$('.thumbs-wrapper td:visible:eq(0)').addClass('selected');
	}
		
	thumbsButtonsSetup();
	loadImage();

}


function getNumWord(q,w1,w2,w12) {
	
	if (q % 10 == 1 && q % 100 != 11) {
		return w1;
	} else if ((q % 10) < 5 && (q % 100 > 20 || q % 100 < 10) && q % 10 != 0) {
		return w2;
	} else{
		return w12;
	}
	
}

$(document).ready(function(){
	
	$('.categories ul li:visible:last a:eq(0)').click(function(){
		$(this).parent().find('ul').toggleClass('dnone');
		return false;
	});
	
	$('.tabs li span').click(function(){
		if ($(this).parent().hasClass('active')) {
			return false;
		}
		$('.tabs li').removeClass('active');
		$(this).parent().addClass('active');
		
		window.cat_id = 1 * $(this).attr('class').replace('cat-','');		
		$('.thumbs-wrapper td').removeClass('selected').hide();
		$('.thumbs-wrapper table').css('left',0);
		
		$('.thumbs-wrapper td.thumbs-cat-' + window.cat_id).show();
		
		selectThumb();
		
		return false;
	});
	
	if ($('.thumbs-wrapper td:visible').length > 0) {
		
		selectThumb();
		
	}
	
	$('.thumbs-wrapper td a').click(function(){
		if ($(this).parent().hasClass('selected')) {
			return false;
		}
		
		$('.thumbs-wrapper td').removeClass('selected');
		$(this).parent().addClass('selected');
		
		loadImage();
		return false;
		
	});
	
	$('.gallery-arrow').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	
	$('.button-right,.button-left').hover(function(){
		if (!$(this).hasClass('blocked')) {
			$(this).addClass('hover');
		}
	},function(){
		$(this).removeClass('hover');
	});
	
	$('.button-right').click(function(){
		if ($(this).hasClass('blocked')) {
			return false;
		}
		$('.button-left,.button-right').addClass('blocked');
		diff = $('.thumbs-wrapper td:visible').length + Math.round(1 * $('.thumbs-wrapper table').css('left').replace('px','') / 70) - 11;
		if (diff > 0) {
			if (diff > 3) {
				diff = 3;
			}
			$('.thumbs-wrapper table').animate({left:1 * $('.thumbs-wrapper table').css('left').replace('px','') - 70 * diff},300,function(){
				$('.button-left,.button-right').removeClass('blocked');
				thumbsButtonsSetup();
			});
		}
	});
	
	$('.button-left').click(function(){
		if ($(this).hasClass('blocked')) {
			return false;
		}
		$('.button-left,.button-right').addClass('blocked');
		diff = Math.round((-1) * $('.thumbs-wrapper table').css('left').replace('px','') / 70);
		if (diff > 0) {
			if (diff > 3) {
				diff = 3;
			}
			$('.thumbs-wrapper table').animate({left:1 * $('.thumbs-wrapper table').css('left').replace('px','') + 70 * diff},300,function(){
				$('.button-left,.button-right').removeClass('blocked');
				thumbsButtonsSetup();
			});
		}
	});
	
	$('.button-next').click(function(){
		if ($('.thumbs-wrapper td.selected').next('td:visible').length == 1) {
			$('.thumbs-wrapper td.selected').next('td:visible').addClass('selected');
			$('.thumbs-wrapper td.selected:eq(0)').removeClass('selected');
		} else {
			$('.thumbs-wrapper td:visible:eq(0)').addClass('selected');
			$('.thumbs-wrapper td.selected:eq(1)').removeClass('selected');
		}
		
		loadImage();
		
		return false;
	});
	
	$('.button-preview').click(function(){
		if ($('.thumbs-wrapper td.selected').prev('td:visible').length == 1) {
			$('.thumbs-wrapper td.selected').prev('td:visible').addClass('selected');
			$('.thumbs-wrapper td.selected:eq(1)').removeClass('selected');
		} else {
			$('.thumbs-wrapper td:visible:last').addClass('selected');
			$('.thumbs-wrapper td.selected:eq(0)').removeClass('selected');
		}
		
		loadImage();
		
		return false;
	});
	
	$('.b-price b').click(function(){
		$('.order-block').show();
		$('.thanks').hide();
		$('.overlay').show();//fadeIn(100,function(){
		$('.lightbox').show();
		$('.p-block img').attr('src',window.pictureUrl.replace('500x500','170x170'));
		$('#order-form input:last').val(window.pictureUrl);
		code = '<span class="img-name">' + $('.sale-block dl dt').html() + '</span>';
		code += $('.sale-block dl dd').html();
		code += '<span class="price">' + $('.b-price i').html() + '</span>';
		$('.p-block dd').html(code);
			//fadeIn(100);
		//});
		return false;
	});
	
	$('.icon-close,.overlay').click(function(){
		$('.lightbox').hide();
		$('.overlay').fadeOut(200);
	});
	
	$('.order-block input,.order-block textarea').each(function(){
		$(this).attr('title', $(this).val());
	});
	
	$('.order-block input,.order-block textarea').focus(function(){
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}).blur(function(){
		if ($.trim($(this).val()) == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	$('#order-form').submit(function(){
		postData = $('#order-form :input').serialize();
		$.post('?', postData, function(data) {
			$('.order-block').fadeOut(200,function(){
				$('.thanks').html(data).fadeIn(200);
			});//l.remove();
			
		});
		return false;
	});
	
	$('.information-title h2').click(function(){
		$('.information-title').fadeOut(100,function(){
			$('.wrap').fadeIn(400);	
		});
	});
	
	$('.wrap h2').click(function(){
		$('.wrap').fadeOut(100,function(){
			$('.information-title').fadeIn(400);	
		});
	});
	
	$('.jump-ru').click(function(){
		document.location.href = document.location.href.replace('/en/','/');
		return false;
	});
	
	$('.jump-en').click(function(){
		prts = document.location.href.split('/');
		prts[2] += '/en';
		document.location.href = prts.join('/');
		return false;
	});
	
	$('.target-blank').attr('target','_blank');
	
	$('.b-article-comments dt a').click(function(){
		$(this).parent().parent().toggleClass('b-article-comments-open');
		return false;
	});
	
	$('.big-img-gallery .next-arrow,.big-img-gallery span img').not('.no-clicks img').click(function(){
		$gal = $(this).parents('.big-img-gallery:eq(0)');
		if ($gal.find('.jump').length) {
			return false;
		}
		if ($gal.find('>span img:visible').next('img').length) {
			$gal.find('>span img:visible').next('img').addClass('jump');
		} else {
			$gal.find('>span img:eq(0)').addClass('jump');
		}
		if (typeof $gal.find('.jump').attr('height') != 'undefined') {
			$gal.find('>span img:visible').fadeOut(200,function(){
				$gal.find('dl dt:visible').fadeOut(100);
				h = $gal.find('.jump').attr('height');
				$gal.find('>span').animate({height:h + 'px'},600,function(){
					$gal.find('.jump').hide().css('visibility','visible').fadeIn(200,function(){
						n = $gal.find('.jump').prevAll().length;
						$gal.find('dl dt:eq(' + n + ')').fadeIn(100);
						n++;
						$gal.find('.previous-arrow em').text(n);
						$gal.find('.jump').removeClass('jump');
					});
				});
			});
		} else {
			$gal.find('.jump').removeClass('jump');
		}
		return false;
	});
	
	$('.big-img-gallery .previous-arrow').click(function(){
		$gal = $(this).parents('.big-img-gallery:eq(0)');
		if ($gal.find('.jump').length) {
			return false;
		}
		if ($gal.find('>span img:visible').prev('img').length) {
			$gal.find('>span img:visible').prev('img').addClass('jump');
		} else {
			$gal.find('>span img:last').addClass('jump');
		}
		if (typeof $gal.find('.jump').attr('height') != 'undefined') {
			$gal.find('>span img:visible').fadeOut(200,function(){
				$gal.find('dl dt:visible').fadeOut(100);
				h = $gal.find('.jump').attr('height');
				$gal.find('>span').animate({height:h + 'px'},600,function(){
					$gal.find('.jump').hide().css('visibility','visible').fadeIn(200,function(){
						n = $gal.find('.jump').prevAll().length;
						$gal.find('dl dt:eq(' + n + ')').fadeIn(100);
						n++;
						$gal.find('.previous-arrow em').text(n);
						$gal.find('.jump').removeClass('jump');
					});
				});
			});
		} else {
			$gal.find('.jump').removeClass('jump');
		}
		return false;
	});
	
	window.blogPostsLimit = 5;
	
	if ($('.b-article').length > 1) {
		$('.b-article').hide();
		showMorePosts();
		$('.b-more-articles a').click(function(){
			showMorePosts();
			return false;
		});
	}
	
})

function showMorePosts() {
	
	$('.b-article:hidden:lt(' + window.blogPostsLimit + ')').show();
	rest = $('.b-article:hidden').length;
	if (rest > window.blogPostsLimit) {
		rest = window.blogPostsLimit;
	}
	if (rest == 0) {
		$('.b-more-articles').hide();
	} else {
		code = rest + ' ' + getNumWord(rest,'статья', 'статьи', 'статей');
		$('.b-more-articles').show().find('a i span').html(code);
	}
	
}
