var countInPage = 16
var stockCountInPage = 5

$(document).ready(function(){
	$('.popup .mark a').click(function() {
		var img = $(this).find('img');
		
		if (img.attr('src') == '/images/arrow-up.png') {
			$('.popup .source-block').show();
			img.attr('src', '/images/arrow-down.png');
		}
		else {
			$('.popup .source-block').hide();
			img.attr('src', '/images/arrow-up.png');
		}
		
		return false;
	});
	
	$("a.bigImage").fancybox({
		hideOnContentClick	:	false
	});
	
	$(document).everyTime(1000, function(i) {
		switch($('div.action a').css('color')) {
			case 'rgb(225, 237, 195)':
				$('div.action a').css('color', 'rgb(122, 193, 66)');
				$('div.action span').css('color', 'rgb(225, 237, 195)');
				break;
			case 'rgb(122, 193, 66)':
				$('div.action a').css('color', 'rgb(225, 237, 195)');
				$('div.action span').css('color', 'rgb(122, 193, 66)');
				break;
			case '#e1edc3':
				$('div.action a').css('color', '#7ac142');
				$('div.action span').css('color', '#e1edc3');
				break;
			case '#7ac142':
				$('div.action a').css('color', '#e1edc3');
				$('div.action span').css('color', '#7ac142');
				break;
		}
	});
	
	$('#search').click(function() {
		if ($('#searchForm').css('display') == 'none') $('#searchForm').css('display', 'block');
		else $('#searchForm').css('display', 'none');
		return false;
	});
	
	var thisParent = location.pathname;
	thisParent = thisParent.split('/');
	thisParent = '/' + thisParent[1] + '/';
	
	$('.hs_area').hover(
		function() {
			$(this).children('.slide_description').css('display', 'block');
		},
		function() {
			$(this).children('.slide_description').css('display', 'none');
		}
	);
	
	if (thisParent == '//') {
		$('li[rel=main]').hover(
			function() {
				$('li[rel=main]').children('ul.submenu').css('display', 'none');
				$('li[rel=main]').removeClass('thisIn');
				$(this).addClass('thisIn');
				$(this).children('ul.submenu').css('display', 'block');
			}, 
			function() {}
		);
	}
	else
	{
		$('li[rel=main]').each(function(i) {
			if ($(this).children('a').attr('href') == thisParent) {
				$(this).addClass('thisIn');
				$(this).children('ul.submenu').css('display', 'block');
				$('li[other=parent]').addClass('parent');
				thisParent = this;
			}
		});
		
		$('li[rel=main]').hover(
			function() {
				//$('li[rel=main]').removeClass('thisIn').removeClass('parent');
				//$(this).addClass('thisIn');
				//if ($(this).attr('other') == 'parent') $(this).addClass('parent');
				//$(thisParent).children('ul.submenu').css('display', 'none');
				//$(this).children('ul.submenu').css('display', 'block');
				if (this != thisParent) {
					$('li[rel=main]').removeClass('thisInTwo');
					$(this).addClass('thisInTwo');
				}
			}, 
			function() {
				//$('li[rel=main]').removeClass('thisIn').removeClass('parent');
				//$(thisParent).addClass('thisIn');
				//$(this).children('ul.submenu').css('display', 'none');
				//$(thisParent).children('ul.submenu').css('display', 'block');
				//$('li[other=parent]').addClass('parent');
				$('li[rel=main]').removeClass('thisInTwo');
			}
		);
	}
	
	var stocks = $('.prices .item');
	
	stocksCreating(stocks);

	$('#imagePagination li a').click(function() {
		$('#imagePagination li').removeClass('current');
		$(this).parent().addClass('current');
		
		var thisNumber = $(this).attr('page');
		
		var start = (thisNumber - 1) * stockCountInPage;
		var end = thisNumber * stockCountInPage;
		
		stocks.each(function(i) {
			if (i < start || i >= end) {
				$(this).css('display', 'none');
			}
			else {
				$(this).css('display', 'block');
			}
		});
	
		return false;
	});
	
	var images = $('.img-block .sample');
	
	if (images.length > 0) $(window).load(function(){
		portfolioCreating(images)
		$('#imagePagination li a').click(function() {
			var images = $('.img-block .sample');
			
			$('#imagePagination li').removeClass('current');
			$(this).parent().addClass('current');
			
			var thisNumber = $(this).attr('page');
			
			var start = (thisNumber - 1) * countInPage;
			var end = thisNumber * countInPage;
			
			images.each(function(i) {
				if (i < start || i >= end) {
					$(this).css('display', 'none');
				}
				else {
					$(this).css('display', 'block');
				}
			});
		
			return false;
		});
		
		$("a.bigImage").fancybox({
			hideOnContentClick	:	false
		});
	});
	
	$('.inner-content.faq p').each(function(i) {
		if ($(this).hasClass('question')) {
			$('.FAQList').append('<div><a href="#faq' + i + '">' + $(this).text() + '</a></div>');
			$(this).prepend('<a name="faq' + i + '"></a>');
		}
	});
	
	$('.column .block').hover(
		function() {
			$(this).children('.block-content').children('.img-block').children('img').css('display', 'none');
			$(this).children('.block-content').children('.img-block').children('div.hidden').css('display', 'block');
		}, 
		function() {
			$(this).children('.block-content').children('.img-block').children('img').css('display', 'block');
			$(this).children('.block-content').children('.img-block').children('div.hidden').css('display', 'none');
		}
	);
	
	var subWidth = $('.content-links .active').width();
	if (subWidth > 0) subWidth = subWidth + 20;
	$('.content-links .active').css('width', subWidth + 'px');
	$('.content-links .other').css('margin-left', subWidth + 'px');
	
	$('.sitemap div.levelTwo').css('display', 'none').children('.sitemap div.levelThree').css('display', 'none').children('.sitemap div.levelFour').css('display', 'none');
	$('.sitemap .tup').click(function() {
		if ($(this).parent().children('div').css('display') == 'none') {
			$(this).parent().children('div').css('display', 'block');
		}
		else {
			$(this).parent().children('div').css('display', 'none');
		}
		return false;
	});
	
	$('.sitemap .urlRel').click(function() {
		location = $(this).attr('rel');
	});
	
	var thisSub = $('.thisSub');
	thisSub.css('background-image', 'none');
	thisSub.prev().css('background-image', 'none');
	var thisBlock = $('.thisBlock');
	thisBlock.children('.separator').text('');
	thisBlock.prev().children('.separator').text('');
	$('div.content-simple-links .separator:last').text('');
	
	$('.map .point').click(function() {
		$('.point').removeClass('active');
		$(this).addClass('active');
		$('.hiddImage img').attr('src', $(this).attr('imagesrc'));
		$('.hiddImage').css('display', 'block');
	});
	
	$('table[align=center]').css('margin', '0 auto');
});

function portfolioCreating(images) {
	var width = 112;
	var height = 112;
	var otstup = 5;
	
	if (images.length >= 16) {
		images.width(width + 'px');
		images.height(height + 'px');
	}
	else {
		countInPage = 4;
		width = 226;
		height = 226;
		otstup = 10;
		images.width(width + 'px');
		images.height(height + 'px');
	}
	
	images.each(function(i) {
		var img = $(this).children('img')
		var imWidth = img.width();
		var imHeight = img.height();
		
		var coof = 1;
		coof = imWidth / imHeight;
		
		if (coof > 1) {
			var newWidth = width - otstup * 2;
			var newHeight = Math.ceil(newWidth / coof);
			var mLeft = otstup;
			var mTop = Math.ceil((height - newHeight) / 2);
		}
		else if (coof < 1) {
			var newHeight = height - otstup * 2;
			var newWidth = Math.ceil(newHeight * coof);
			var mTop = otstup;
			var mLeft = Math.ceil((width - newWidth) / 2);
		}
		else {
			var newWidth = width - otstup * 2;
			var newHeight = height - otstup * 2;
			var mTop = otstup;
			var mLeft = otstup;
		}
		
			img.css('width', newWidth + 'px');
			img.css('height', newHeight + 'px');
			img.css('margin-top', mTop);
			img.css('margin-left', mLeft);
		
		if (i >= countInPage) $(this).css('display', 'none');
	});
	
	var countPages = Math.ceil(images.length / countInPage);
	
	for (i = 1; i <= countPages; i++) {
		if (i == 1) {
			$('#imagePagination').append('<li class="current"><a href="#" page="' + i + '">' + i + '</a></li>');
		}
		else {
			$('#imagePagination').append('<li><a href="#" page="' + i + '">' + i + '</a></li>');
		}
	}
	
	for (i = images.length; i < countPages * countInPage; i++) {
		$('.img-block').append('<div class="sample" style="display: none; width: ' + width + 'px; height: ' + height + 'px;"></div>');
	}
};

function stocksCreating(stocks) {
	stocks.each(function(i) {
		if (i >= stockCountInPage) $(this).css('display', 'none');
	});
	
	var countPages = Math.ceil(stocks.length / stockCountInPage);
	
	for (i = 1; i <= countPages; i++) {
		if (i == 1) {
			$('#imagePagination').append('<li class="current"><a href="#" page="' + i + '">' + i + '</a></li>');
		}
		else {
			$('#imagePagination').append('<li><a href="#" page="' + i + '">' + i + '</a></li>');
		}
	}
};
