var last_tab = 1;
var paytype;


function number_format( number, decimals, dec_point, thousands_sep ) {    
	// Format a number with grouped thousands
    // 
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
 
    var i, j, kw, kd, km;
 
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
 
 
    return km + kw + kd;
}

$(document).ready(function(){
	
	
	function switch_tab (item,index)
	{
		$('.about-block li').not(item).removeClass('active');
		item.toggleClass('active');
		
		var tabs = $('.tabs');
		if (index > 0) {
		
		
		
			if (tabs.css('display') == 'none') {
				fadeOpacity(tabs.get(0), 0.98);
				fadeOpacity($('.svetozar').get(0), 0.0);
				fadeOpacity($('.buy-button').get(0), 0.0);
			}
			else {
				if (index == last_tab) {
					fadeOpacity(tabs.get(0), 0.0);
					switch_tab( $('.about-block li').eq(0),0)
				}
			}
			
			tabs.find('.middle .tab').hide();
			tabs.find('.middle .tab').eq(index).css('display', 'block');
			
			last_tab = index;
			
			
		}
		else	
			if (index == 0)
			{
				fadeOpacity(tabs.get(0), 0.0);
				fadeOpacity($('.svetozar').get(0), 0.98);
				fadeOpacity($('.buy-button').get(0), 0.98);
			}
	}
	
	
	$('.about-block li').click(function(){
		
		var index = $('.about-block li').index($(this));
		
		switch_tab($(this),index);
		
		return false;
	})
	
/*	$('.top-menu li').hover(
		function()
		{
			if ($(this).hasClass('first-')) $(this).addClass('first-hover')
			else	
				if  ($(this).hasClass('last-')) $(this).addClass('last-hover')
				else
					$(this).addClass('hover');
			
			var submenu = $(this).find('.sub-menu');
			
			 
			if (submenu.get(0))
			{
				submenu.stop();
				var width = submenu.width();
				if (submenu.parent().width() < width) submenu.width(width)
				else submenu.attr('style','!');
				submenu.slideDown(250);
			}		
		},
		function()
		{
			if (!$(this).hasClass('active'))
			if ($(this).hasClass('first-')) $(this).removeClass('first-hover')
			else	
				if  ($(this).hasClass('last-')) $(this).removeClass('last-hover')
				else
					$(this).removeClass('hover');
			
			var submenu = $(this).find('.sub-menu');
			if (submenu.get(0))
			{
				submenu.stop();
				submenu.attr('style','!');
				submenu.slideUp(350);	
			}
		}
	)
	*/
	
	if ($('#red-light').get(0)) flashembed('red-light', {src: 'http://' + window.location.hostname + '/flash/redSpark4.swf', wmode: 'transparent', width: '178', height: '159'});
		
	$('.thickbox').fancybox({ 
		'zoomSpeedIn': 200, 
		'zoomSpeedOut': 200,
		'frameWidth': 100,
		'frameHeight': 100,
		'overlayShow': true,
		'hideOnContentClick' :false,
		'centerOnScroll' : false,
		'callbackOnStart': function(){$('body').css('overflow-x','hidden');},
		'callbackOnClose': function(){$('body').attr('overflow-x','visible');} 
	});
	
	
	$('.question-form .count input').keyup(function(){
		var val = $(this).val();
		val = val.replace(/[^0-9]/g, '');
		if (parseInt(val) > 10) val = 10; 
		$(this).val(val);
		count = parseInt(val)*parseInt($('.question-form').find('#price').val());
		var percent = 0;
		$('.question-form .paytype input').each(function(){
			if (($(this).val() == 'cod') && ($(this).attr('checked'))){
				percent = 0.09;
			}
		});
		count = count - 160*(val-1);
		count = count + count*percent; 
		$('.question-form').find('.summ').text(number_format(Math.ceil(count),0,'',' ')+' р.');
		$('.question-form').find('#summ').val(Math.ceil(count));
		
	})
	
	$('.question-form .count .arr-up').click(function(){
		val = parseInt($('.question-form .count input').val());
		val = val + 1;
		var percent = 0;
		$('.question-form .paytype input').each(function(){
			if (($(this).val() == 'cod') && ($(this).attr('checked'))){
				percent = 0.09;
			}
		}
		)
		if (val < 10) {
			$('.question-form .count input').val(val);
			count = parseInt(val)*parseInt($('.question-form').find('#price').val());
			count = count - 160*(val-1);
			count = count + count*percent;
			$('.question-form').find('.summ').text(number_format(Math.ceil(count), 0, '', ' ') + ' р.');
			$('.question-form').find('#summ').val(Math.ceil(count));
		}
		else
		{
			$('.question-form .count input').val(10);
			count = 10*parseInt($('.question-form').find('#price').val());
			count = count - 160*9;
			count = count + count*percent;
			$('.question-form').find('.summ').text(number_format(Math.ceil(count), 0, '', ' ') + ' р.');
			$('.question-form').find('#summ').val(Math.ceil(count));	
		}
		return false;
	})
	
	$('.question-form .count .arr-down').click(function(){
		val = parseInt($('.question-form .count input').val());
		val = val - 1;
		var percent = 0;
		$('.question-form .paytype input').each(function(){
			if (($(this).val() == 'cod') && ($(this).attr('checked'))){
				percent = 0.09;
			}
		}
		)
		if (val > 1) {
			$('.question-form .count input').val(val);
			count = parseInt(val)*parseInt($('.question-form').find('#price').val());
			count = count + count*percent-160*(val -1);
			$('.question-form').find('.summ').text(number_format(Math.ceil(count), 0, '', ' ') + ' р.');
			$('.question-form').find('#summ').val(Math.ceil(count));
		}
		else {
			$('.question-form .count input').val(1);
			count = parseInt($('.question-form').find('#price').val());
			count = count + count*percent; 
			$('.question-form').find('.summ').text(number_format(Math.ceil(parseInt(count)), 0, '', ' ') + ' р.');
			$('.question-form').find('#summ').val(Math.ceil(count));
		}
		return false;
	})
	
	
	$('.content .shop-cities').click(function(){
		$(this).parent().next().slideToggle(250);
		return false;
	})
	
	$('.question-form .paytype input').click(function(){
		
		paytype = $(this);	
		
		val = parseInt($('.question-form .count input').val());
		percent = ($(paytype).val() == 'cod')?(0.09):(0);
		count = parseInt(val) * parseInt($('.question-form').find('#price').val());
		if (val > 1) {
			count = count - 160 * (val - 1);
		}
		count = count + count*percent;
		
		$('.question-form').find('.summ').text(number_format(Math.ceil(count), 0, '', ' ') + ' р.');
		$('.question-form').find('#summ').val(Math.ceil(count));
		
		if ($(this).val() == 'cod')
		{
			$('.question-form .delivery input').eq(1).attr('disabled','disabled');
				$('.question-form .delivery input').eq(0).attr('checked','checked');
			$('.question-form .delivery input').eq(1).removeAttr('checked');
			$('.question-form .delivery span').eq(2).addClass('disabled');
		}
		else
		{
			$('.question-form .delivery input').eq(1).removeAttr('disabled');
			$('.question-form .delivery span').eq(2).removeClass('disabled');
		}
	})
	
})