function images_show()
{
    var first = ''
    
    $('.images > img ').each(function(i){
	if (first == '') {first = this.src}
	wheelThumbnail = $.create('div', {'id':'wheelThumbnail'}, 
	    ["img", {'src':this.src, "alt":this.alt}, []]
	);
	$('div#wheelThumbnails').append($(wheelThumbnail));	    
    });
    if (document.getElementById('wheelThumbnail'))
    {
	view_node = $.create("div",{'class':'view','id':'viewimg'},
	    ["img", {'src':first},[]]
	);
	brclear = $.create("br",{'clear':'all'},[])
	$('div#wheelThumbnails').append(brclear);
	$('div#wheelThumbnails').append(view_node);
	$('.images').html('');                      
    
    
	$('div#wheelThumbnails > div > img').each(function(i){
	    $(this).click(function(){
		_inode = $.create("img", {'src':this.src, "alt":this.alt},[]);
		$('#viewimg').html('');
		$('#viewimg').append(_inode);
		$('div#wheelThumbnails > div > img').removeClass('picked');
		$(this).addClass('picked');
		})
	}); 
    }
}

var up = false

$(document).ready(function() {
//var title = $("#messageform input[name=]")
//alert(title)//$('#id_name')

    $("#messageform").validate({
	errorClass: "invalid",
	rules:{
	    name:"required",
	    email:{
		required: true,
		email: true
		},
	    question:"required"
	    },
	messages:{
	    name:"Обязательное поле.",
	    email:{
		required: "Обязательное поле.",
		email: "Укажите правильный адрес электронной почты."
		},
	    question:"Обязательное поле."
	    },
	submitHandler: function(form) {
	    $.ajax({
		type: "POST",
		url: '/wheel/'+calc_wheel['id']+'/message',
		data:{
		    'name'    : $('#id_name').val(),
		    'email'   : $('#id_email').val(),
		    'question': $('#id_question').val(),
		    'wheel_id'   : $('#id_wheel_id').val(),
		    'csrfmiddlewaretoken' : $('#csrfmiddlewaretoken').val()
		    },
		beforeSend: function(){
		    $('#el').fadeIn("slow")
		    },
		complete: function(){
		    $('#el').fadeOut("slow")
		    },
		error: function(){
		    alert('Нет данных.')
		    },
		success:function(data){
		   $('#dmsgf').html('<p>Сообщение отправлено<\/p>')
		}
	    })
	    return false;
	    }
	}	)

    $('#updatebutton').click(function(){
	$.ajax({
		type: "POST",
		url: '/wheel/'+calc_wheel['id']+'/update',
		data:{
			'id_update':1, 
			'city':0,
		    'csrfmiddlewaretoken' : $('#csrfmiddlewaretoken').val()
			},
		beforeSend: function(){
		    $('#ub').css({'background':'#E5FFEC url(/static/images/ul.gif) no-repeat 12% 50%'})
		    },
		complete: function(){
		    $('#ub').css({'background':'#E5FFEC'})
		    },
		error: function(){
		    alert('Нет данных.')
		    },
		success:function(data){
		    obj = eval( data )[0]
		    tarif = eval( data )[1]
		    tarif_sum = tarif.fields.sum
		    tarif_minsum = tarif.fields.minsum
		    calc_wheel['sum'] = tarif_sum
		    calc_wheel['minsum'] = tarif_minsum
		    rsup = calc_wheel['weight']*tarif_sum
		    if(rsup < tarif_minsum){rsup = tarif_minsum}
//		    rw = 12.71*calc_wheel['weight']
//		    if(rw<1001){rw=1001}
//		    rsup = rsup+rw
		    calc_wheel['russia_sup'] = rsup
		    y = $('#yna').val()
		    id = obj.pk
		    pc = obj.fields.price_current
		    calc_wheel['price'] = pc
		    $('#calc_price').val(pc)
		    calc_data()
		    calc_view()
		    pb = obj.fields.price_blitz
		    _pb = parseInt(pb)
		    _pc = parseInt(pc)
		    if(_pc > 0 && _pb == 0)
		    {
			step=0
		    }
		    else if (_pc == 0)
		    {
			step=0
		    }
		    else
		    {
			step=1
		    }
		    ed = obj.fields.note
		    bids = obj.fields.bids
		    status = obj.fields.status
		    if(status=='closed'){$('#closedlotblock').fadeIn('slow')}
		    dn = obj.fields.disksnum
		    isrez = obj.fields.withrub
		    date  = getDateFromFormat(obj.fields.updated, 'yyyy-MM-dd HH:mm:ss')
		    date = new Date(date)
		    $('#udate').text('Лот только что обновлен!')
		    $('#udate').css({'color':'#000000'})
		    css_top = {'border-top':'1px solid green','border-right':'1px solid green','border-left':'1px solid green','background':'#E5FFEC'}
		    css_middle = {'border-right':'1px solid green','border-left':'1px solid green','background':'#E5FFEC'}
		    css_bottom = {'border-right':'1px solid green','border-left':'1px solid green','border-bottom':'1px solid green','background':'#E5FFEC'}
		    $('#wheelinfo > ul > li').each(function(i){
			if(i==0)
			{
			    if (pc > 0)
				s = '<span class="f-left">Текущая цена:</span>¥ '+_pc
			    else
			    {
    				s = '<span class="f-left">Цена без торга:</span>¥ '+_pb
				_pb = 0
			    }
			    $(this).fadeOut("slow")
			    $(this).html(s)
			    $(this).css(css_top)
			    $(this).fadeIn("slow")
			}
			if(i==1 && _pb > 0)
			{
			    $(this).fadeOut("slow")
			    s = '<span class="f-left">Цена без торга:</span>¥ '+_pb
			    $(this).html(s)
			    $(this).css(css_middle)
			    $(this).fadeIn("slow")
			}
			if(i==1+step){
			    if (status != 'closed'){
				$(this).fadeOut("slow")
				s=obj.fields.annotation
				s = "<span>Осталось до конца торгов:<\/span>"+s
				$(this).html(s)
				$(this).css(css_middle)
				$(this).fadeIn("slow")
				} else {
				    $(this).css(css_middle)
				}
			    }
			if(i==2+step){
			    $(this).fadeOut("slow")
			    s=''
			    s = '<span class="f-left">Завершение торгов:</span> '+ed
			    $(this).html(s)
			    $(this).css(css_middle)
			    $(this).fadeIn("slow")
			    }
			if(i==3+step){
			    $(this).fadeOut("slow")
			    if(bids){
				s='<span class="f-left">Ставок:</span>'+bids
				$(this).html(s)
				}
			    $(this).css(css_bottom)
			    $(this).fadeIn("slow")
			    }
			})
		    }
	    })
    return false
	})
})

function calc_data(){
	if(document.getElementById('calc_new').checked)
	{
	    ise = 700
	}
	else 
	{
	    if(document.getElementById('calc_tire').checked)
	    {
		ise = 310
	    }
	    else
	    {
		ise = 450
	    }
	}
	calc_wheel['invlad_sup'] = calc_wheel['weight']*ise
	calc_wheel['calc_invlad_tarif'] = ise

	/*calc_wheel['russia_sup'] = parseInt($('#calc_russia_sup').text())*/
	y = parseFloat($('#yna').val())

	price = calc_wheel['price']
	if ($('#calc_count').val() == '4')
	{
	    price *= 4
	}
	calc_wheel['pre_price'] = parseInt(Math.ceil(price/(y*1000))*1000)
	if (calc_wheel['pre_price'] < 10000){calc_wheel['pre_price']=10000}
	calc_wheel['japan_tax'] = 0
	calc_wheel['invlad_sup'] = ise*calc_wheel['weight']
	calc_wheel['yresult'] = price +calc_wheel['japan_tax']+calc_wheel['japan_sup']+calc_wheel['many_reciving']+calc_wheel['invlad_sup']
	cs = 0.15 * price
	if(cs<2000){cs=2000}
	calc_wheel['comm_samurai'] = cs
	//  + calc_wheel['russia_sup']
	calc_wheel['result'] = parseInt(Math.ceil((calc_wheel['yresult']/y+calc_wheel['comm_samurai']/y)/100)*100)
	
	}
    
function calc_view(){
	$('#calc_japan_tax').html(    parseInt(calc_wheel['japan_tax']) )
	$('#calc_sheepment').val(    calc_wheel['japan_sup'] )
	$('#calc_many_reciving').html(calc_wheel['many_reciving'] )
	$('#calc_invlad_sup').html(   calc_wheel['invlad_sup'] )
	$('#calc_russia_sup').html(  parseInt(calc_wheel['russia_sup']) )
	$('#calc_yresult').html(  parseInt( calc_wheel['yresult']) )
	$('#calc_invlad_tarif').html(  parseInt( calc_wheel['calc_invlad_tarif']) )
	$('#calc_comm_samurai').html( parseInt(calc_wheel['comm_samurai']) )
	$('#calc_result').html(       calc_wheel['result'] )
	$('#pred_price').html(calc_wheel['pre_price'])
	}
	
$(document).ready(function()
{
    $('#calc_button').click(function()
    {
	calc_data()
	calc_view()
    })
    
    $('#calc_weight').change(function()
    {
	w = parseInt($('#calc_weight').val())
	var rsup = calc_wheel['sum']*w
	if(rsup < calc_wheel['minsum']){rsup=calc_wheel['minsum']}
	calc_wheel['russia_sup'] = rsup
    })
    
    $('#calc_tire').change(function()
    {
	w = parseInt($('#calc_weight').val())
	if(this.checked)
	{
	    $('#calc_weight').val(w*2)
	    calc_wheel['weight'] = w*2
	    calc_wheel['russia_sup'] = (calc_wheel['russia_sup'])*2
	    calc_wheel['invlad_sup'] = w*2*310
	    calc_wheel['japan_sup'] = w*2*120
	    calc_wheel['calc_invlad_tarif'] = 310
	}
	else
	{
	    $('#calc_weight').val(w/2)
	    calc_wheel['russia_sup'] = ((calc_wheel['russia_sup'])/2)
	    calc_wheel['weight'] = w/2
	    calc_wheel['invlad_sup'] = w*450/2
	    calc_wheel['japan_sup'] = w*120/2
	    calc_wheel['calc_invlad_tarif'] = 450
	}
	calc_data()
	calc_view()
    })
    
    $('#calc_price').change(function()
    {
	w = parseFloat($('#calc_price').val())
	calc_wheel['price'] = w
	calc_data()
	calc_view()
    })
    
    $('#calc_sheepment').change(function()
    {
	w = parseFloat($('#calc_sheepment').val())
	calc_wheel['japan_sup'] = w
    })
    
    $('#calc_weight').change(function()
    {
	w = parseFloat($('#calc_weight').val())
	calc_wheel['weight'] = w
    })
})    
