$(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();
	
	//Make JS tabs from structure with class tabs  	
	$(".tabs").tabs({
		event: 'click',
		selected: 0
	});
	
	$('#login-login,label[for="login-login"]').click(function(){
		var label = $("#login-login").parents('div:first').find('label:first');
		if (label.text() != '')
			label.attr('txt', label.text());
		label.text('');
	});
	$('#login-login').blur(function(){
		if ($(this).val() == '') {
			var label = $("#login-login").parents('div:first').find('label:first');
			label.text(label.attr('txt'));
		}
	});
	$('#login-password,label[for="login-password"]').click(function(){
		var label = $("#login-password").parents('div:first').find('label:first');
		if (label.text() != '')
			label.attr('txt', label.text());
		label.text('');
	});
	$('#login-password').blur(function(){
		if ($(this).val() == '') {
			var label = $("#login-password").parents('div:first').find('label:first');
			label.text(label.attr('txt'));
		}
	});
	
	
	// 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();
});



/**
 * 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 tr:not(.under-price) span.price').text(prices[param_value]['price_vat']);
	$('td.data div.content tr:not(.under-price) span.price-without-vat').text(prices[param_value]['price']);
	$('td.data div.content tr:not(.under-price) span.price-price-default').text(prices[param_value]['price_default']);
	$('td.data div.content tr:not(.under-price) span.price-normal-vat').text(prices[param_value]['price_normal']);
	$('td.data div.content tr:not(.under-price) 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;
}


