﻿function set_data_table(filename,delimiter,days,hd_array,container){
var s_main='';
     $j.getJSON(filename,function(json) {
		len_main=json.length;
			if (len_main>0) {
				(json[0].cost_index!=null)?cost_index=json[0].cost_index:cost_index=0;
				(json[0].com!=null)?com=json[0].com:com=1;
				(json[0].transfer!=null)?transfer=json[0].transfer:transfer=0;
				//(json[0].days!=null)?days=json[0].days:days=1;
				(days!=null)?days=days:days=1;
					//if	(footer!="") {
					//	s1='';
					//	s1+='<tfoot><tr><td class="op_td">'+footer+'</td></tr></tfoot>';
					//	$j(".op_table").append(s1);
					//}
					for (index=1;index<len_main;index++) {
					s1='';
					len=json[index].values.length;
						if (len>0) {
							way=json[index].way;
							head=json[index].head;
							//alert(head);
							if (head!="") s1+='<tr><th class="op_hyper op_td">'+head+'</th></tr>';
								for (i=0;i<len;i++){
									tmp=json[index].values[i];
									(tmp.cat!=null)?name=tmp.name+' '+tmp.cat+'*':name=tmp.name;
									//alert(header);
									s1+='<tr>';
									s1+='<td>';
									if (way!=null) {
										//alert(way);
										s1+='<a class="op_tip op_hyper" href="'+way+tmp.file+'">';
										if (json[0].pop!="") s1+='<span>'+json[0].pop+'</span>';
											s1+=name+'</a>';
									}
									else
										s1+='<span class="op_hyper op_color_white">'+name+'</span>';
									
									if (tmp.comment!="") s1+='<br/ > ('+tmp.comment+')';
										s1+='</td>';
									(tmp.dbl!=null)
										?s1+='<td class="op_td">'+Math.round((tmp.dbl*days+transfer+cost_index)/com)+'</td>'
										:s1+='<td class="op_td">'+delimiter+'</td>';
									(tmp.sngl!=null)
										?s1+='<td class="op_td">'+Math.round((tmp.sngl*days+transfer*2+cost_index)/com)+'</td>'
										:s1+='<td class="op_td">'+delimiter+'</td>';
									(tmp.exb!=null)
										?s1+='<td class="op_td">'+Math.round((tmp.exb*days+transfer+cost_index)/com)+'</td>'
										:s1+='<td class="op_td">'+delimiter+'</td>';
									(tmp.chd!=null)
										?s1+='<td class="op_td">'+Math.round((tmp.chd*days+transfer+cost_index)/com)+'</td>'
										:s1+='<td class="op_td">'+delimiter+'</td>';
									s1+='</tr>';
								}
							//alert(s1);
							//alert(h_d+s1);
							//$j(".op_table").append(s1);
							s_main+=s1;
							
						}
					}
				//s1='<tr><td>'+foot+'</td></tr>';
				//$j(".op_table").append(s1);
				//form_head(hd_array);
				$j(container).html(form_head(hd_array)+s_main);
		}
    });
} 

//$(function() {
//	f_name="/wp-content/uploads/main/data/china_ex.json";
//	set_data_table(f_name);   
//});
function load_sel_values (sel_id,min,max){
		for (var i=min;i<=max;i++) {
			  $j(sel_id).append('<option value='+i+'>'+i+'</option>');
		}
	}

function set_curr_sel_value (filename,delimiter,sel_id,hd_array) {
	 var a=$j(sel_id).val();
	 set_data_table(filename,delimiter,a,hd_array);
}

function form_head (hd_array) {
	var s='<tr>';
	for (var i=0;i<hd_array.length;i++){
	s+='<td class="op_h op_td">'+hd_array[i]+'</td>';
 }
 s+='</tr>';
 //$j(".op_table").append(s);
 return s;
}


$(function() {

if ($j("#selector").length>0) {
  load_sel_values ("#selector",7,14);
  $j("#selector").bind("change",function(event){
	var f_name="/wp-content/uploads/main/data/indonesia_ex.json";
	var hd_array=["название","1/2 DBL","SNGL","EXB","CHD"];
	//alert(hd_array);
	set_curr_sel_value (f_name,'OR',"#selector",hd_array);
	});
}
});