var timeout = 100;
var closetimer = 0;
var ddmenuitem = 0;
var xhrObject = null;
function updateStatus()
{
  $('#vv_body').ajaxLoader();
  if (($(".location_col input:checked").size() > 0) && ($("#fund_types input:checked").size() > 0)) // && ($("#industry input:checked").size() > 0)
  {   
	$('.jquery-ajax-loader').html("Please wait while the map loads. <br /> (We're sorting through a lot of data!)").css({color: 'white', fontWeight: 'bold', textAlign: 'center'});
	xhrObject = $.post('main/getdata', $("#form").serialize() , function(data) {
		// If the profiler is running
		if ((index = data.indexOf('<div')) != -1)
		{
                    // Get profiler info
                    profiler_data = data.substring(index, data.length - 1);
                    data = data.substring(0, index);

                    // Then insert the profiler data into the bottom
                    $('#codeigniter_profiler').html(profiler_data);
		}
                // Fix the response
                data = $.parseJSON(data);
		$('.jquery-ajax-loader').html("Done!").css({color: 'white', fontWeight: 'bold', textAlign: 'center'});
		$('#infovis').empty();
		clearBackground();
		data = prepare(data);
		graph = init();
		graph.loadJSON(data);
		graph.compute('end');
		graph.select(graph.root);
		graph.refresh();
		setInfoBoxes();
	});
  }else{
      if (($(".location_col input:checked").size() > 0) && ($("#fund_types input:checked").size() < 1)) // && ($("#industry input:checked").size() < 1)
      {
           $(".location_col input:checked").each(function(){
               val = "#label_" + $(this).val().toString();  
           });
           var json_data = {
                "id": "0_0",
                "name": $(val).text() + ' Investors',
                "children": []
           };
           $('#infovis').empty();
           clearBackground();
           json_data = prepare(json_data);
           graph = init();
           graph.loadJSON(json_data);
           graph.compute('end');
           graph.select(graph.root);
           graph.refresh();
           setInfoBoxes();
      }else{
           setBackground();
      }
  }
  //$('#vv_body').ajaxLoaderRemove();
}
function Search(){
   $('#vv_body').ajaxLoader();
   $('.jquery-ajax-loader').html("Please wait while the map loads. <br /> (We're sorting through a lot of data!)").css({color: 'white', fontWeight: 'bold', textAlign: 'center'});
   $.post('main/search', $("#formsearch").serialize() , function(data) {  
         if(data != ""){
           $('#infovis').empty();
           clearBackground();
           data = prepare(data);
           graph = init();
           graph.loadJSON(data);
           graph.compute('end');
           graph.select(graph.root);
           graph.refresh();
           setInfoBoxes();
        }
  });
  //$('#vv_body').ajaxLoaderRemove();
}
function prepare(json)
{
    var arr = json.children, len = arr.length;
    var m = Array();
    var orient = 'left';
    for(var i=0; i < len; i++) {
      if(arr[i].id in m)
      {
         arr[i].data.$orn = m[arr[i].id];
         $jit.json.each(arr[i], function(n) {
              n.data.$orn = m[arr[i].id];
         });
      }else{
         m[arr[i].id] = orient;
         arr[i].data.$orn = orient;
         $jit.json.each(arr[i], function(n) {
              n.data.$orn = orient;
         });
         if(orient == "left")
          {
              orient = "right";
          }else{
              orient = "left";
          }
      }
    }
    return json;
}
function setInfoBoxes(){
    $("a.info").bind("click", function() {
	$.ajax({
		type	: "POST",
		cache	: true,
		url	 : 'main/gettooltip',
		data	: 'id='+$(this).attr('id'),
		success: function(data) {
                        if(data != '')
                        {
			  $.fancybox(data, {
                                'transitionIn': 'elastic',
                                'transitionOut': 'elastic',
                                'speedIn': 300,
                                'speedOut':200,
                                'autoScale': true
                        });
                        }
		}
	});
	return false;
     });
}
function clearBackground()
{
   $("#infovis").css("display", "block");
   $("#infovis-canvaswidget").css("display", "block");
   setHowTo(0);
}
function setBackground()
{
   $("#infovis").css("display", "none");
   $("#infovis-canvaswidget").css("display", "none");
   setHowTo(1);
   $('#vv_body').ajaxLoaderRemove();
}
function setHeaderPopup()
{
    $("a.contact").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'speedIn': 300,
            'speedOut':	200,
            'width': 400,
            'height': 300
    });
    $("a.addinvestor").fancybox({
        'width'	: 650,
	'height': 1200,
        'speedIn': 300,
        'speedOut':	200,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
	'type': 'iframe'
    });
    setHowTo(1);
    //$(".node").corner();
}
function setHowTo(status){
  if(status == 0)
  {
     $('#vv_maparea').unbind('click');
  }else{
     var thisurl = getThisURL().split('#')[0];
     thisurl += 'assets/css/images/vv_howto.jpg';
     $("#vv_maparea").unbind('click').click(function() {
        $.fancybox({
                'padding' : 0,
		'href'	: thisurl,
                'titleShow'     : false,
		//'transitionIn'	: 'elastic',
		//'transitionOut'	: 'elastic',
                'width'	: '100%',
                'height': '100%'
        });
      });
  }
}
function getThisURL(){
      var hash_url = location.href;
      hash_url = hash_url.split('main');
      return hash_url[0];
}
function bindActions(){
    $("#location").slideToggle('slow');
}
function mouseLeave(){
    //$('#location').mouseleave(function() {
       // bindActions();
    //});
}
function checkToggle(id){
    xhrObject && xhrObject.abort();
    if($('#'+id).is(':checked'))
    {
        $('#'+id).removeAttr('checked');
        $('#lnk'+id).removeAttr('class');
    }
    else
    {
        // For now lets restrict the map to one location
        $('.vv_selected').each(function() {
            elem = $(this);
            linkId = elem.attr('id');
            // Fund type links have lnkf where as locations just have lnk in
            // the id attr. If you split something and the array returned has
            // length 1 that means that no split was done and thus element
            // found is not a fund link.
            if (linkId.split('lnkf').length == 1)
            {
                tempId = linkId.split('lnk')[1];
                $('#' + tempId).removeAttr('checked');
                $('#lnk' + tempId).removeAttr('class');
            }
        });

        // Continue onto select and check the location clicked on
        $('#'+id).attr('checked', 'checked');
        $('#lnk'+id).attr('class', 'vv_selected');
    }
}
function mopen(id)
{
	mcancelclosetime();
        if (!mclose())
        {
            ddmenuitem = document.getElementById(id);
            ddmenuitem.style.visibility = 'visible';
        }
}
function mclose()
{
	if(ddmenuitem != 0 && ddmenuitem.style.visibility == 'visible')
        {
            ddmenuitem.style.visibility = 'hidden';
            return true;
        }
        else
            return false;
}
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

