$(document).ready(function(){
	
	$("#category-tabs div.tab a").click(switch_category_tabs);
	$("div.filters fieldset legend").click(show_filter_form);
	$("div.parameters input.param").click(show_price_param);
	
	$(".chose_mode").click(hide_user_reg_form);
	hide_user_reg_form();
	//$(".chose_mode").trigger('click');
	
	$(".delivery_chose").click(hide_d_address_form);
	hide_d_address_form();
	//$(".delivery_chose").trigger('change');
	
	$(".billing_chose").click(hide_b_address_form);
	hide_b_address_form();
	$(".billing_chose").trigger('change');
	
	set_up_order_steps();
	hide_producers_filter();
	
		
	// All elemnts with this class submit form in change state
	$(".auto_submit").change(function(){
		$(this).parents('form').submit();

	})
	//LightBox for all link with target image 
	$('a[href$="jpg"], a[href$="jpeg"], a[href$="gif"], a[href$="png"], a[href$="JPG"], a[href$="JPEG"], a[href$="GIF"], a[href$="PNG"]').fancybox();
	$("a#inline").fancybox({
		'hideOnContentClick': true
	});


	sort_products();
	sort_products_detail();
	$(".product_slides").scrollable({circular: true});
	$(".product_slides_detail").scrollable({circular: true});
	
	
 /* $('#slider').nivoSlider();*/
 /*$('#slider').bxSlider();*/
});

function sort_products()
{

	var ps = $('.product_slides');

$('.product_list', ps).each(function(){
		var slist = {};
		var sklist = []; // end sorted array by groups
		
		if ($(this).find('a').length == 0)
		{
			$(this).parents('div.text').remove();
			return;
		}
		
		var pl = $(this);
		$('a', pl).each(function(){
			var c = $(this).attr('class').split('_')[1];
			slist[c] = c;
		});
		
		var i = 0;
		for(var v in slist) {
			sklist[i++] = parseInt(v);
		}
		sklist = sklist.sort(sortNumber);
		
		var num = 0;
		for(i = 0; i < sklist.length; i++)
		{
			pl.prev('.sorted_list').append('<div class="product_group p_'+sklist[i]+'"></div>');
			var group_div = pl.prev('.sorted_list').find('div.p_'+sklist[i]);
			$('a', pl).each(function(){
				if ($(this).hasClass('p_'+sklist[i]))
				{
					group_div.append($(this));
					num++;
				}
			});
		}
		if (num <= 6 && sklist.length <= 1)
		{
			pl.next('.slide_r').remove();
		}
	});
}

function sort_products_detail()
{
	var ps = $('.product_slides_detail');
	$('.product_list', ps).each(function(){
		var pl = $(this);

		var i = 0;
		var group_div = false;
		$('a', pl).each(function(){
			if ((i++ % 6) == 0)
			{
				pl.prev('.sorted_list').append('<div class="product_group p_'+i+'"></div>');
				group_div = pl.prev('.sorted_list').find('div.p_'+i);
			}
			group_div.append($(this));
		});
	});
}

/**
 * Sorting numbers method
 */
function sortNumber(a,b)
{
	return a - b;
}



/**
 * Method to hide/show some elements in order step3 registration form
 */
function hide_d_address_form()
{
	var target = $(".delivery_chose:checked");
	var tr = target.parents('tr');
	
	//alert(target.val());
	
	switch(target.val())
	{
		case 'exist_address':
			tr.nextAll(':lt(1)').show();
			tr.nextAll(':gt(0)').hide();
		break;				
		case 'new_address':
			tr.nextAll(':lt(1)').hide();
			tr.nextAll(':gt(0)').show();
		break;
	}
	
}

/**
 * Method to hide/show some elements in order step3 registration form
 */
function hide_b_address_form()
{
	var target = $(".billing_chose:checked");
	
	var tr = target.parents('tr');
	
	switch(target.val())
	{
		case 'same_delivery':
			tr.nextAll().hide();
		break;		
		case 'exist_address':
			tr.nextAll(':lt(1)').show();
			tr.nextAll(':gt(0)').hide();
		break;				
		case 'new_address':
			tr.nextAll(':lt(1)').hide();
			tr.nextAll(':gt(0)').show();
		break;
	}
}

/**
 * Function for order steps icons manipulation
 */
function set_up_order_steps()
{
	$('.step-icon').each(function(){
		if (!$(this).hasClass('disabled')) {
			var nextstep = $(this).next();
			nextstep.addClass('clickable');
			nextstep.click(function(){
				$(".nextstep").trigger('click');
			});		
			return;	
		}		
	});		
}

/**
 * Method to hide/show some elements in order step1 registration form
 */
function hide_user_reg_form()
{
	var target = $(".chose_mode:checked");
	var tr = target.parents('tr');
	
	switch(target.val())
	{
		case 'register':
			tr.nextAll(':gt(4)').show();
		break;
		case 'non_register':
			tr.nextAll(':gt(4)').hide();
		break;
	}
}

/**
 * Transfer goods params to detail box
 */
function show_price_param()
{
	var param_value = $(this).val();
	$('td.data div.content span.price').text(prices[param_value]['price_vat']);
	$('td.data div.content span.price-without-vat').text(prices[param_value]['price']);
	$('td.data div.content span.price-price-default').text(prices[param_value]['price_default']);
	$('td.data div.content span.price-normal-vat').text(prices[param_value]['price_normal']);
	$('td.data div.content span.code').text(prices[param_value]['code']);
}
/**
 * The method hides the box producers, unless none selected
 */
function hide_producers_filter()
{
	var hide = true;
	$("div.producer-box fieldset input").each(function(){
		if ($(this).get(0).checked) {
			hide = false;
			return;
		}
	});
	if (hide) {
		$("div.producer-box fieldset").toggleClass('closed');
		$("div.producer-box fieldset div").toggleClass('hidden');
	}
}
/**
 * Method to hide/show content "fieldset"
 */
function show_filter_form()
{	
	$(this).toggleClass('closed');
	$(this).parent('fieldset').find("div").toggleClass('hidden');
}
/**
 * Method used for switching tabs categories and producers
 */
function switch_category_tabs()
{
	$(this).blur();		
	if (!$(this).parents('div').hasClass('active')) {
		$("#category-tabs div.tab").each(function(){
			$(this).toggleClass('active');
			id  = $(this).attr('id');
			$("#div-"+id).toggleClass('active');
		});
	}
	return false;
}

function redirect(url, message)
{
	if ( '' == message )
	{
		$.fancybox.showActivity('redirect');
	}
	
	$.fancybox(
		'<p>'+message+'</p>',
		{
       		'autoDimensions'	: false,
			'width'        		: 350,
			'height'       		: 'auto',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'hideOnOverlayClick': false,
			'showNavArrows'     : false,
			'enableEscapeButton': false,
			'showCloseButton'   : false
		}
	);
	
	window.location = url;
}


