  var rand = Math.random(0, 10000);

function create_menu(cats, level){
  var html = '';
  var click = '';
  var img_src = 'http://interesniy.kiev.ua/maps/media/images/icons/gif/';
  var icon;
  var bold = '';
  var cc;
  var openTag;
  var closeTag;
  var cc2;
  var Sicon;
  for(var i = 0; i < cats.length; i++){
	icon = cats[i]['icon'];
	icon = icon.replace(/\.png$/i, '.gif');
	if (level == 0) bold = 'font-weight:bold;';
	if (cats[i]['child'].length > 0){
//		cc = ' class="submenu closed" ';
		cc = 'class="submenu" id="li_'+cats[i]['id']+'"';
		openTag = '<span style="' + bold + '">';
		closeTag = '</span>';
		cc2 = '';
		Sicon = '&nbsp;';
	} else {
		cc = '';
		openTag = '<a id="c_'+cats[i]['id']+'" style="' + bold + '" href="catalog.php?cat=' + cats[i]['id'] + '">';
		closeTag = '</a>';
		cc2 = 'background:url(\'' + img_src + icon + '\') no-repeat scroll 0 50%; ';
		Sicon = '<span style="'+cc2+'padding-left:30px;padding-top:3px;padding-bottom:3px;width:32px;height:32px;">&nbsp;</span>';
	}
	html += '<li '+cc+'>' + Sicon + openTag + cats[i]['name'] + closeTag;
	if (cats[i]['child'].length > 0){
		html += '<ul>';
	    	html += create_menu(cats[i]['child'], level + 1);
		html += '</ul>';
	}
	html += '</li>';
  }
  return html;
}


/*
// настройки хранить в Cookies 1 день
function setCookie2(name, value){
 var DAY = 24 * 60 * 60 * 1000;
 var date = new Date();
 date.setTime(date.getTime() + (1 * DAY)); // 1 день
 $.cookie(name, value, {expires: date});
 // alert("Cookie set: "+name+"="+value);
}
*/

function getLI(){	var ret = [];
	$('#treemenu1 li').each(function (){		var res = $(this).attr('id');
		var state = $(this).hasClass('collapsable') ? 1 : '';
		if (res && state){			ret.push(res);
		}	});
	return ret.join(',');
}


$(function(){  getMapSizeForScreen();
  var url = 'http://' + document.location.host + '/maps/bb/get_cats_JSON.php?rn=' + rand;
//alert(url);

  $.getJSON(url, function (d_cats){
	  var html = '<ul id="treemenu1" class="treeview-famfamfam">';
	  html += create_menu(d_cats, 0);
	  html += '</ul>';
	  $('#cats').html(html);
	$('#treemenu1').treeview({		collapsed: true
		, toggle: function(){				var menu = getLI();
//alert(menu);
//				setCookie2('treemenu1', menu);
/*
 var DAY = 24 * 60 * 60 * 1000;
 var date = new Date();
 date.setTime(date.getTime() + (1 * DAY)); // 1 день
 $.cookie('treemenu1', menu, {expires: date});
*/var DAY = 24 * 60 * 60 * 1000;
var exdate = new Date();
exdate.setDate(exdate.getTime() + (1 * DAY));
document.cookie='treemenu1' + "=" + escape(menu) +
((DAY == null) ? "" : ";expires=" + exdate.toGMTString());		}	});

	$('#treemenu1 li.submenu input:checkbox').click(function(){
	  var el = $(this).parent('li').find('input:checkbox');
	  el.attr('checked', $(this).is(':checked'));
	});
	$('#treemenu1 input:checkbox').click(function(){
	  get_checked_box();
	});

	var e = '#c_' + $('#nowcat').val();
	$(e).addClass('selected');

//	var open_nodes = $.cookie("treemenu1");
var open_nodes = '';
var c_name = "treemenu1";
if (document.cookie.length > 0)
  {
  var c_start = document.cookie.indexOf(c_name + "=");
  if (c_start != -1)
    {
    c_start = c_start + c_name.length + 1;
    c_end=document.cookie.indexOf(";", c_start);
    if (c_end == -1) c_end = document.cookie.length;
    open_nodes = unescape(document.cookie.substring(c_start, c_end));
    }
  }

	if(open_nodes) {
	  var nodes = open_nodes.split(',');

	  if(nodes[0]){
	   for(var node in nodes){
	    e = '#' + nodes[node];
	    $(e).removeClass('expandable');
	    $(e).addClass('collapsable');
	    $(e+' div').removeClass('expandable-hitarea');
	    $(e+' div').addClass('collapsable-hitarea');
	    $(e+' ul').css("display", "block");
	   }
	  }
	}

  });
});


