$(document).ready(on_dom_load);
function on_dom_load(){
    $(".tabs_navigation a").live("click", select_chapter);
    $(".full_view_purchase input[type='checkbox']").live("click", show_count);
    $(".full_view_purchase .goods_count input[type='text']").bind("change", recount);
    $(".full_view_purchase .goods_count input[type='text']").bind("focus", clearer);
    $(".full_view_purchase .goods_count input[type='text']").bind("blur", reclearer);
}

function clearer(){
    if($(this).val()=='0'){
	this.value='';
    }
}

function reclearer(){
    if(($(this).val()=='')||(isNaN($(this).val()))){
	this.value='0';
    }
}

function select_chapter(event){
    event.preventDefault();
    this.blur();
    var selected=$(".tabs_navigation li.selected")[0];
    $(selected).toggleClass("selected");    
    $("#"+this.parentNode.parentNode.parentNode.className+"_chapter").show();    
    $(this.parentNode.parentNode.parentNode).toggleClass("selected");    
    $("#"+selected.className+"_chapter").hide();
}

function show_count(event){
   var container = this.parentNode.parentNode.parentNode;
   $($(".goods_count",container)[0]).toggle();
   $(".goods_count input[type='text']",container)[0].value='0';
   recount();
}

function recount(event){
    this.value=parseFloat($(this).val());   
    var summary = 0;
    var collection = $(".full_view_purchase .goods_count input[type='text']");
    for(i=0;i<collection.length;i++){
	if(!isNaN(parseFloat($(collection[i]).val()))){
	    
	    var container = collection[i].parentNode.parentNode;
	    summary+=parseFloat($(collection[i]).val())*$("#"+$(".goods_name input[type='checkbox']",container)[0].getAttribute("id")+"_price").val();
	}
    }
    $("#summary_price").text(summary+" руб.");
}
