﻿$(document).ready(function() {
//		settingsImageBox = {
//			imageLoading:			'/images/loading.gif',
//			imageBtnPrev:			'/images/prevlabel.gif',
//			imageBtnNext:			'/images/nextlabel.gif',
//			imageBtnClose:		'/images/closelabel.gif',
//			imageBlank:	'/images/lightbox-blank.gif',
//			txtImage:				'Фото',
//			txtOf:					'из'
//		};
//
//		$("a[@rel*=lightbox]").lightBox(settingsImageBox);

//		$("a.showGoods").each(function() {
//			$(this).attr("href", $(this).attr("href") + "?small=true")
//		});

		
		$(document).keydown(function (event) {
			NavigateThrough(event);
		});
		
		change_variation_property(1);

/*		
		$("#cart_form").submit(function (){
			var size = $(this).find("select[name=size]");
			
			if (size && size.find("option:selected").attr("value") == -1) {
				alert("Необходимо выбрать размер"); 
				return false;
			}
			
			var size = $(this).find("select[name=color]");
			
			if (size && size.find("option:selected").attr("value") == -1) {
				alert("Необходимо выбрать цвет"); 
				return false;
			}
		});
*/
		
		$('.goods').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
		$('button').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
		
	 	$("button").mouseup(function(){
	      $(this).addClass('clicked');
	    }).mousedown(function(){
	      $(this).removeClass('clicked');
		});
		
		$("a.fbox").fancybox({
			'frameWidth':			620,
			'frameHeight':		451,
			'overlayShow':		true,
			'overlayOpacity':	0.7,
 			'zoomSpeedIn':		0,
 			'zoomSpeedOut':		0
		});
});


var hVariation;

function change_variation_property(i)
{
	if (!$('.pr_' + i).length) {
		return;
	}
	
	id = '';
	
	for (k = 1; k <= i; k++)
	{
		if (k == 1) {
			hVariation = hVariationsTree;
		}
		
		if (k == 1) {
			key = $('.pr_' + k + ' :selected').attr('value');
		} else {
			key = $('.pr_' + k + ' :checked').attr('value');	
		}
		hVariation = hVariation[key];
		
		id = id + '_' + key;
	}
	
	k = i + 1;
	
	// скрываем все на уровне
	$('.pr_' + k).hide();
	
	// отображаем все возможные на уровне
	$('.prv' + id).show();
	
	// если среди видимых нет выбранного
	if (!$('.pr_' + k + ':visible input:checked').length) {
		$('.pr_' + k + ':visible input:first').click();	// выбираем первое видимое значение
	} else {
		change_variation_property(k);
	}
	
	j = k + 1;
	
	// если это последняя характеристика
	if (!$('.pr_' + j).length) {
		$('#goods-price').html(hVariation["price"]);	// выводим цену
		if (hVariation["stock"]) {
			$('#in_stock').css('color', 'green').html('Товар в налиии');
		} else {
			$('#in_stock').css('color', 'red').html('Товара нет на складе');
		}
	}
}


function variation_price(i)
{
	return hVariationsTree;
}

function variation_price_get(i, a)
{
	return a[i];
}


function NavigateThrough (event)
{
	if (!document.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey)
	{
		var link = null;
		var href = null;

		switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
		{
			case 0x27:
				link = document.getElementById ('NextLink');
				break;

			case 0x25:
				link = document.getElementById ('PrevLink');
				break;

			case 0x26:
				link = document.getElementById ('UpLink');
				break;

			case 0x28:
				link = document.getElementById ('DownLink');
				break;

			case 0x24:
				href = '/';
				break;
		}

		if (link && link.href) document.location = link.href;
		if (href) document.location = href;
	}			
}
