
var cons = {
"prop":{"form":{"id":null, "img":null},
"gamut":{"id":null, "name":null, "flowers":[]},
"sizes":{"id":null, "name":null, "count":null}
},

"parseHtml":function(){
var count_small=10;
var count_big=20;
	
if(this.prop.form.id!==null){	
	
$(".case_form").html($("<img />").attr({'src': this.prop.form.img, 'alt': "", 'width': 100, 'height': 100, 'class': "fll bord_img"}));
$(".st3_form").attr("src",this.prop.form.img);
}	
if(this.prop.gamut.id !== null){
 $(".case_gamut").text(this.prop.gamut.name);
 $(".st3_gamut").text(this.prop.gamut.name);
 $(".b_byket_cveti").text('');	
var j=1;
for(var i=0; i<this.prop.gamut.flowers.length; i++){
var class_name=(j>=4)?"mrnull":"";	
 
 	var item = $('<div class="fll byket_cveti '+class_name+'"></div>').append($("<img />").attr({'src': this.prop.gamut.flowers[i].src, 'alt': "", 'width': 50, 'height': 50, 'class': "bord_img"})).append('<div class="both_st0">').append('<span class="lgreen">'+this.prop.gamut.flowers[i].name+'</span>');
    $(".b_byket_cveti").append(item);
    if(j>=4){
        $(".b_byket_cveti").append('<div class="both_st10"></div>');
     j=1;	
    }    
j++;	
 }

 $(".step3_items").html('');
var price=0; 
for(var i=0; i<this.prop.gamut.flowers.length; i++){
price+=parseFloat(this.prop.gamut.flowers[i].price);	
 	var item = $('<div class="fll byket_cveti"></div>').append($("<img />").attr({'src': this.prop.gamut.flowers[i].src, 'alt': "", 'width': 50, 'height': 50, 'class': "bord_img"})).append('<div class="both_st0">').append('<span class="lgreen">'+this.prop.gamut.flowers[i].name+'</span>');
    $(".step3_items").append(item);
} 
price = price/this.prop.gamut.flowers.length;
$(".step3_price0").text(Math.round(price*count_small)+70);
$(".step3_price1").text(Math.round(price*count_big)+70);

}
},

"check":function(id_gamut, name_gamut){
if(document.forms['items'].item.length>0){	
var items = new Array();	
this.prop.gamut.flowers = [];
var count=0;
for(var i=0; i<document.forms['items'].item.length;){

if(document.forms['items'].item[i].checked){
   var src = $(document.forms['items'].item[i]).parent().find("img").attr("src");
   var id =  document.forms['items'].item[i].value;
   var name = $(document.forms['items'].item[i]).parent().find("label").text();
   var price = $(document.forms['items'].item[i]).next("input[@name=price]").val();

   var item = new Array();
   item["id"]=id;
   item["name"]=name;
   item["src"]=src;
   item["price"]=price;
   

   this.prop.gamut.id = id_gamut;
   this.prop.gamut.name = name_gamut;
   this.prop.gamut.flowers[count] = {"id":id, "name":name, "src":src, "price":price};

  
   count++;


 }
   
i++;
}

this.parseHtml();
}

},

"add_form":function(id, img){
	this.prop.form.id = id;
	this.prop.form.img = img;
    this.parseHtml();
},



"switch_step":function(id, from_step, to_step){
var from = id+from_step;
var to = id+to_step;
var valid = true;

if(to_step > from_step){

  
    switch(to_step){
    case 3:
    valid = (cons.prop.gamut.id !== null && cons.prop.gamut.id !== null);	
    (valid==false) && jAlert('Выберите гамму букета!','Конструктор букетов');
    break;
    
    case 2:{
    valid = (cons.prop.form.id !== null);	
    (valid==false) && jAlert('Выберите форму букета!','Конструктор букетов');
    }
    
    break;
    }
    	
}
 if(valid===true){
 	    $('#'+from).css({'display':'none'});
 	   	$('#'+to).css({'display':'block'});
        (to_step==3) && $('.right').css({'display':'none'}) && $('.finish').css({'display':'block'});
        (to_step==2) && $('.right').css({'display':'block'}) && $('.finish').css({'display':'none'});
        (to_step==1) && $('.right').css({'display':'block'}) && $('.finish').css({'display':'none'});
        
 };
 return false;
},

"add_bouquet":function(){
if(!$(":radio[name=size]:checked").val()){
jAlert('Выберите размер букета!','Конструктор букетов');
return;	
}	
   
var cons_ajax=new Object();
cons_ajax.form_id=this.prop.form.id;
cons_ajax.size_id=$(":radio[name=size]:checked").val();
cons_ajax.gamut_id=this.prop.gamut.id;
cons_ajax.items = [];
for(var i=0; i<this.prop.gamut.flowers.length;){
	cons_ajax.items[i]=this.prop.gamut.flowers[i].id;
	i++;
}

		
$.ajax({type:"POST",

        url:        "/construct/.add",
        data:       {cons: cons_ajax},
        success:    function(res) {
                     if(res == 'ERR'){
                      	jAlert('Ошибка при добавлении товара!','Корзина');
                     }else{
                     	 jAlert('Товар успешно добавлен в корзину','Корзина');
                        if($(".count_items").text()==''){
                           $(".bask").append('<span class="white flr col_tov">Товаров:&nbsp;&nbsp;<span class="count_items">'+res+'</span></span>');
                        }else{
                        $(".count_items").text(res);
                        }
                    } 
                  }
});
}

}

function print_r(arr, level) {
    var print_red_text = "";
    if(!level) level = 0;
    var level_padding = "";
    for(var j=0; j<level+1; j++) level_padding += "    ";
    if(typeof(arr) == 'object') {
        for(var item in arr) {
            var value = arr[item];
            if(typeof(value) == 'object') {
                print_red_text += level_padding + "'" + item + "' :\n";
                print_red_text += print_r(value,level+1);
		} 
            else 
                print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    } 

    else  print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
    return print_red_text;
}


var a=11;
