$(document).ready(function(){
	
	$("#category-tabs div.tab a").click(switch_category_tabs);
	$("div.filters fieldset legend").click(show_filter_form);
	
	$("input[type='radio'].price_change").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
	});
	
	// 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(':has(#email)').hide();
			tr.nextAll(':gt(3)').show();
		break;
		case 'non_register':
			tr.nextAll(':has(#email)').show();
			tr.nextAll(':gt(3)').hide();
		break;
	}
}

function show_price_param() {
	var material_id = $(this).val();
	
	if ( material[material_id] ) {
		if ( params[material[material_id]] ) {
			var price = params[material[material_id]];
			$('div.pricesblock span.price-without-vat').html(price.price);
			$('div.pricesblock span.price-vat').html(price.price_vat);
			$('div.pricesblock span.normal-price-vat').html(price.normal_price_vat);
		}
	}
}


function formatPrice( number, decimals, decPoint, thousandsSeparator, currencySymbol, positiveFormat, negativeFormat, stripZero ) {
    number = Math.round(number);
    
    var price = numberFormat( number, decimals, decPoint, thousandsSeparator );
    
    var priceText='';
    
    if ( number >= 0 ) {
        priceText = positiveFormat;
        priceText = priceText.replace('00', price);
        priceText = priceText.replace('Symb', currencySymbol);
        
    } else {
        priceText = negativeFormat;
        priceText = priceText.replace('00', price);
        priceText = priceText.replace('Symb', currencySymbol);
    }
    
    if ( stripZero ) {
            priceText = priceText.replace( ',00', '' );
    }

    return priceText;
}

function numberFormat( number, decimals, decPoint, thousandsSeparator ) {
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');

    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator,
        dec = (typeof decPoint === 'undefined') ? '.' : decPoint,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };

    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');

    if ( s[0].length > 3 ) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }

    if ( ( s[1] || '' ).length < prec ) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }

    return s.join(dec);
}
        
/**
 * 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;
}


