﻿var scrollListTop = 0;
var scrollListStep = 30;
var scrollClipHeight = 0;
var scrollTotalHeight = 0;

var tableScrollIndex = 1;
var tableScrollMax = 0;
var tableScrollDisplay = 3;

var iconWidth = 82;//88

$(document).ready(function() {

	//inputHelper($('#top-search'), 'поиск');

	if ($('.column-right').length > 0) {
		if ($('.column-right').height() > $('.text').height()) {
			$('.text').css({minHeight: $('.column-right').height()+'px'});
		}
	}

	initSelectBlock();

	initProductCarousel();
	initCatalogIcoMenu();

	initScrollList();

	/*
	$('.jcarousel-prev').hover(startCarousel, stopCarousel);
	$('.jcarousel-next').hover(startCarousel, stopCarousel);
	$('.scroll-top').hover(startCarousel, stopCarousel);
	$('.scroll-bot').hover(startCarousel, stopCarousel);
	*/
	
	$('.jcarousel-prev').mousedown(startCarousel);
	$('.jcarousel-prev').mouseup(stopCarousel);
	$('.jcarousel-next').mousedown(startCarousel);
	$('.jcarousel-next').mouseup(stopCarousel);
	$('.scroll-top').mousedown(startCarousel);
	$('.scroll-top').mouseup(stopCarousel);
	$('.scroll-top').mouseout(stopCarousel);
	$('.scroll-bot').mousedown(startCarousel);
	$('.scroll-bot').mouseup(stopCarousel);
	$('.scroll-bot').mouseout(stopCarousel);

	initProductPicture();


	if ($('.series-char').length > 0) {
		$('.series-char li').hover(function(){
			$(this).addClass('hover');
			$('.series-char p').html($(this).find('i').html());
		}, function() {
			$(this).removeClass('hover');
			$('.series-char p').html('');
		});
	}

	initCompareTable();

	if ($('.table').length > 0) {
		$('.table td').hover(function(){
			$(this).parents('tr').addClass('hover');
		}, function(){
			$(this).parents('tr').removeClass('hover');
		});
	}

	if ($('.compare').length > 0) {
		$('.compare a').click(function() {
		    var href = $(this).attr('href');
			if ($(this).parents('.compare').hasClass('compare-added')) {
				return true;
			} else {
				var compareButton = $(this).parents('.compare');
				$.get(href, function(data) {
					if (data == '0') {
					   compareButton.addClass('compare-added').find('a').attr('href', BASE_URL + '/catalog/compare/');
				    } else if (data == '1') {
				        if (confirm(confirmMessage)) {
				            $.get(href, { or : 1 }, function(data) {
				                compareButton.addClass('compare-added').find('a').attr('href', BASE_URL + '/catalog/compare/');
				            });
				        }
				    } else if (data == '-1') {

				    }
				});
				return false;
			}
		});
	}

});

function checkTableScrollButtons() {
	if (tableScrollIndex <= 1) {
		$('.table-scroll-prev').addClass('disabled');
	} else {
		$('.table-scroll-prev').removeClass('disabled');
	}
	if (tableScrollIndex >= tableScrollMax - tableScrollDisplay) {
		$('.table-scroll-next').addClass('disabled');
	} else {
		$('.table-scroll-next').removeClass('disabled');
	}

	var h = $('.compare-table .head td').innerHeight() + $('.compare-table .info td').innerHeight() - 10;
	var m = parseInt((h-23) / 2);

	$('.table-scroll-prev').css({height: h+'px'});
	$('.table-scroll-next').css({height: h+'px'});
	$('.table-scroll-prev i').css({marginTop: m+'px'});
	$('.table-scroll-next i').css({marginTop: m+'px'});
}
function checkScrollListButtons() {
	if (scrollClipHeight - scrollListTop >= scrollTotalHeight) {
		$('.scroll-bot').addClass('disabled');
	} else {
		$('.scroll-bot').removeClass('disabled');
	}
	if (scrollListTop >= 0) {
		$('.scroll-top').addClass('disabled');
	} else {
		$('.scroll-top').removeClass('disabled');
	}
}
function resetScrollHeight() {
	scrollClipHeight = $('.scroll-list-clip').height();
	scrollTotalHeight = $('.scroll-list-content').height()+15;

	$('.scroll-top').addClass('disabled');
	if (scrollClipHeight >= scrollTotalHeight) {
		$('.scroll-bot').addClass('disabled');
	} else {
		$('.scroll-bot').removeClass('disabled');
	}
}

function hideCatalogIcoSubmenu() {
	var submenu = $('.catalog-ico-menu .submenu-active');
	submenu.removeClass('submenu-active');
	submenu.appendTo($('.ico-active'));
	$('.ico-active').removeClass('ico-active');
	$('.catalog-ico-menu li a').removeClass('hover');

}

function initSelectBlock() {
	if ($('.select-block').length > 0) {
		$('.select-block .select-button').click(function(){
			$(this).parents('.select-block').find('.select-options').toggle();
		});
		$('.select-block p').click(function(){
			$(this).parents('.select-block').find('.select-options').show();
		});
		$('.select-options li span').hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		}).click(function(){
			$(this).parents('.select-options').find('li').removeClass('c');
			$(this).parents('li').addClass('c');
			$(this).parents('.select-block').find('p').html($(this).html());
			$(this).parents('.select-options').hide();

			if ($(this).parents('.select-block').find('.select-formvalue').length > 0) {
				$(this).parents('.select-block').find('.select-formvalue').val($(this).attr('formvalue'));
			}
		});
		/*$('.select-options li').hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		}).click(function(){
			$(this).parents('.select-options').find('li').removeClass('c');
			$(this).addClass('c');
			$(this).parents('.select-block').find('p').html($(this).html());
			$(this).parents('.select-options').hide();

			if ($(this).parents('.select-block').find('.select-formvalue').length > 0) {
				$(this).parents('.select-block').find('.select-formvalue').val($(this).attr('formvalue'));
			}
		});*/

		$('body').click(function() {
			$('.select-options').hide();
		});
		$('.select-block').click(function(e) {
			e.stopPropagation();
		});
	}
}

function initProductCarousel() {
	if ($('.product-carousel').length > 0) {
		$('.product-carousel .product-list').jcarousel({
			scroll: 1
		});
		$('.product-carousel .jcarousel-prev').append($('<i>'));
		$('.product-carousel .jcarousel-next').append($('<i>'));
	}
	if ($('.product-ext-carousel').length > 0) {
		$('.product-ext-carousel .product-ext-list').jcarousel({
			scroll: 1
		});
		$('.product-ext-carousel .jcarousel-prev').append($('<i>'));
		$('.product-ext-carousel .jcarousel-next').append($('<i>'));
	}
}

function initCatalogIcoMenu() {
	if ($('.catalog-ico-menu').length > 0) {
		$('.catalog-ico-menu ul').jcarousel({
			scroll: 1
		});
		$('.catalog-ico-menu .jcarousel-prev').append($('<i>'));
		$('.catalog-ico-menu .jcarousel-next').append($('<i>'));

		//submenu
		$('.catalog-ico-menu li a').click(function(){
			if ($(this).parents('.submenu').length == 0) {
				if ($('.catalog-ico-menu .submenu-active').length > 0) hideCatalogIcoSubmenu();
				$(this).addClass('hover');
				var li = $(this).parents('li');
				li.addClass('ico-active');
				if (li.find('.submenu').length > 0) {
					var submenu = li.find('.submenu');
					submenu.find('p:first').addClass('f');
					$('.catalog-ico-menu').append(submenu);
					submenu.css({left: ( li.index()*iconWidth + parseInt($('.catalog-ico-menu .jcarousel-list').css('left')) - parseInt((submenu.width() - iconWidth)/2) )+'px'}).addClass('submenu-active');
				}
				return false;
			}
		});
		$('body').click(function() {
			hideCatalogIcoSubmenu();
		});
		$('.catalog-ico-menu .submenu').click(function(e) {
			e.stopPropagation();
		});

		//
		//$('.catalog-ico-menu li a:first').click();
	}
}

function initProductPicture() {
	if ($('.product-pic-thumb').length > 0) {
		$('.product-pic-thumb li a').click(function() {
			$('.product-pic-thumb li').removeClass('c');
			$(this).parents('li').addClass('c');
			if ($('.product-pic').length > 0) {
				$('.product-pic-preview img').attr({src: $(this).attr('href')});
				if($(this).attr('alt')!='')
				{
					$('.product-pic-preview span').html("<p>"+$(this).attr('alt')+"</p>");
				}
			}
			if ($('.series-right').length > 0) {
				$('.series-right').css({backgroundImage: 'url('+$(this).attr('href')+')'});
			}
			return false;
		});
		//$('.product-pic-thumb li a').lightBox();
	}
	if ($('.product-ext-list').length > 0) {
		/*$('.product-ext-list li a').click(function() {
			$('.product-ext-list li').removeClass('c');
			$('.product-pic-thumb li').removeClass('c');
			$(this).parents('li').addClass('c');
			//if ($('.product-pic').length > 0) {
			//	$('.product-pic-preview img').attr({src: $(this).attr('href')});
			//	$('.product-pic-preview p').html($(this).attr('alt'));
			//}
			if ($('.series-right').length > 0) {
				$('.series-right').css({backgroundImage: 'url('+$(this).attr('href')+')'});
			}
			return false;
		});*/
		//$('.product-ext-list li a').lightBox();
		var liIndex = 1;
		$('.product-ext-list li').each(function() {
			if ($(this).find('a.lb-im').length > 0) {
				$(this).attr('id', 'lbitem'+liIndex );
				$('#lbitem'+liIndex+' a.lb-im').lightBox();
				liIndex++;
			}
		});
	}
}

function initScrollList() {
	if ($('.scroll-list').length > 0) {
		$('.scroll-list').append(
			$('<div>').addClass('scroll-top').append($('<i>'))
		).append(
			$('<div>').addClass('scroll-bot').append($('<i>'))
		);
		$('.scroll-top').hover(function() {
			$(this).find('i').addClass('hover');
		}, function() {
			$(this).find('i').removeClass('hover');
		});
		$('.scroll-bot').hover(function() {
			$(this).find('i').addClass('hover');
		}, function() {
			$(this).find('i').removeClass('hover');
		});

		resetScrollHeight();

		$('.scroll-bot').click(clickScrollBot);
		$('.scroll-top').click(clickScrollTop);
	}
}

function initCompareTable() {
	if ($('.compare-table').length > 0) {
		//init
		$('.compare-table tr').each(function() {
			tableScrollMax = $(this).find('td').length;
			for (i=0;i<tableScrollDisplay+1;i++) $($(this).find('td').eq(i)).show();
		});
		$('.compare-table td').hover(function(){
			if (!$(this).parents('tr').hasClass('head') && !$(this).parents('tr').hasClass('info')) {
				$(this).parents('tr').addClass('hover');
			}
		}, function(){
			$(this).parents('tr').removeClass('hover');
		});

		//scroller
		$('.compare-table').append(
			$('<div>').addClass('table-scroll-prev').append($('<i>'))
		).append(
			$('<div>').addClass('table-scroll-next').append($('<i>'))
		);
		$('.table-scroll-prev').hover(function() {
			$(this).find('i').addClass('hover');
		}, function() {
			$(this).find('i').removeClass('hover');
		});
		$('.table-scroll-next').hover(function() {
			$(this).find('i').addClass('hover');
		}, function() {
			$(this).find('i').removeClass('hover');
		});
		$('.table-scroll-prev').addClass('disabled');
		if (tableScrollMax <= tableScrollDisplay) {
			$('.table-scroll-next').addClass('disabled');
		}
		$('.table-scroll-prev').click(clickTableScrollPrev);
		$('.table-scroll-next').click(clickTableScrollNext);

		//delete
//		$('.compare-table .delete').click(function() {
//			if (tableScrollMax-1 > 2) {
//				var index = $(this).parents('td').index();
//				$('.compare-table tr').each(function() {
//					$($(this).find('td').eq(index)).remove();
//				});
//				if (index < tableScrollMax-1) {
//					$('.compare-table tr').each(function() {
//						$($(this).find('td').eq(tableScrollIndex+tableScrollDisplay-1)).show();
//					});
//				}
//				if (index >= tableScrollMax-tableScrollDisplay) {$('.table-scroll-prev').click();}
//				tableScrollMax--;
//				if (tableScrollMax-1 <= tableScrollDisplay) {
//					$('.table-scroll-next').addClass('disabled');
//				}
//				if (tableScrollMax-1 <= 2) {
//					$('.compare-table .delete').hide();
//				}
//				checkTableScrollButtons();
//			}
//			return false;
//		});

		$('.compare-table .info li a').click(function() {
			$(this).parents('li').toggleClass('c');
			checkTableScrollButtons();
			return false;
		});

		checkTableScrollButtons();
	}
}

function clickTableScrollPrev(e) {
	if (tableScrollIndex > 1) {
		tableScrollIndex--;
			$('.compare-table tr').each(function() {
				$($(this).find('td').get(tableScrollIndex)).show();
				$($(this).find('td').get(tableScrollIndex+tableScrollDisplay)).hide();
			});
	}
	checkTableScrollButtons();
	e.preventDefault();
}
function clickTableScrollNext(e) {
	if (tableScrollIndex < tableScrollMax - tableScrollDisplay) {
			$('.compare-table tr').each(function() {
				$($(this).find('td').get(tableScrollIndex)).hide();
				$($(this).find('td').get(tableScrollIndex+tableScrollDisplay)).show();
			});
		tableScrollIndex++;
	}
	checkTableScrollButtons();
	e.preventDefault();
}


function clickScrollTop(e) {
	if (carouselEvent) {
		if (scrollListTop < 0) {
				scrollListTop += (scrollListTop <= (-1)*scrollListStep)?scrollListStep:dt;
				$('.scroll-list-content').animate({marginTop: scrollListTop+'px'}, 100);
		}
		checkScrollListButtons();
	}
	e.preventDefault();	
}
function clickScrollBot(e) {
	if (carouselEvent) {
		if (scrollClipHeight - scrollListTop < scrollTotalHeight) {
				dt = scrollTotalHeight - (scrollClipHeight - scrollListTop);
				scrollListTop -= (dt >= scrollListStep)?scrollListStep:dt;
				$('.scroll-list-content').animate({marginTop: scrollListTop+'px'}, 100);
		}
		checkScrollListButtons();
	}
	e.preventDefault();
}

var carouselElem = false, carouselTimer = false, carouselEvent = false;
function startCarousel(e) {
	carouselEvent = true;
	carouselElem = $(e.target);
	carouselClicker();
	//carouselTimer = setTimeout(carouselClicker, 500);
	e.preventDefault();
	return false;
}
function stopCarousel(e) {
	carouselEvent = false;
	clearTimeout(carouselTimer);
	carouselElem = false;
	e.preventDefault();
	return false;
}
function carouselClicker() {
	if (carouselElem) {
		carouselElem.click();
		carouselTimer = setTimeout(carouselClicker, 100);
	}
}


function inputIn(obj, text) {
	if ($(obj).val() == text) {
		if (text != 'http://') $(obj).val('');
		$(obj).removeClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', false);
   }
}
function inputUp(obj, text) {
	var but = '#'+$(obj).attr('id')+'-button';
	if ($(but).length > 0) $(but).attr('disabled', ($(obj).val() == '' || $(obj).val() == text));
}
function inputOut (obj, text) {
	if ($(obj).val() == '' || $(obj).val() == text) {
		$(obj).val(text).addClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', true);
	}
}
function inputHelper(obj, text) {
	$(obj)
		.bind ('focus', function () {
			inputIn (this, text);
		})
		.bind ('keyup', function () {
			inputUp (this, text);
		})
		.bind ('blur', function () {
			inputOut (this, text);
		});
	inputOut(obj, text);
}


