function showBox(caller, id, showOnTop) {
  var o = Element.cumulativeOffset(caller);
  
  $$('.helpbox_content').each(function(el) { el.hide(); });
  
  var top = o[1] + 10;
  
  if(showOnTop) {
    if(isIE()) 
      top -= $(id).getHeight() + 16;
    else
      top -= $(id).getHeight() + $(caller).getHeight() + 18;
  } else {
    if(isIE()) {
      top += $(caller).getHeight()/2 + 3;
    }
  }
  
  $(id).toggle();
  $(id).setStyle({ top: top + 'px', left: o[0] + 10 + 'px' });
}

function openWindow(url) {
  window.open(url, 'ill', 'toolbar=0,width=500,height=700');
}

function isIE() {
  var agt = navigator.userAgent.toLowerCase();
  return agt.indexOf('msie') != -1;
}

function drawChart(div, values, colors, textOutputDiv) {
  

  var drawVisualization = function() {
    // Create and populate the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Header');
    data.addColumn('number', 'Percentage');
    data.addColumn('string', 'Explanation');
    data.addRows(values);
  
    // Create and draw the visualization.
    var chart = new google.visualization.PieChart(document.getElementById(div));
    chart.draw(
      data, { 
        is3D:true, 
//         title: "Ekonomiska risker", 
        legendFontSize: 13, 
//         titleFontSize: 30, 
//         tooltipFontSize: 12,
//         tooltipWidth: 500,
//         tooltipHeight: 200,
        colors: colors,
        fontName: "Verdana",
        legend: 'left',
//         legendTextStyle: { fontSize: 
//         height: 150,
        backgroundColor: {  }, // Removes white background color so it becomes transparent
        width: 600,
        pieSliceText: 'none'
      }
    );
    
    var selectHandler = function() {
      var hdr = data.getValue(chart.getSelection()[0].row, 0);
      var text = data.getValue(chart.getSelection()[0].row, 2);
      var str = '<h3>' + hdr + '</h3><p>' + text + '</p>';
      
      textOutputDiv.html(str);
      $.scrollTo(textOutputDiv);
      textOutputDiv.effect("highlight", {}, 2000);
    };
    
    google.visualization.events.addListener(chart, 'select', selectHandler);
    
    x = chart;
    d = data;
  }
  google.load('visualization', '1', {
    packages: ['corechart'],
    callback: drawVisualization
  });
}

find90sRows = function($frm) {
  var namesFor90sRows = [
    'heat_central',
    'stairway'
  ];
  
  return $frm.find('select').filter(function() {
    return $.inArray($(this).attr('name'), namesFor90sRows) != -1;
  });
}

findNon90sRows = function($frm) {
  return $frm.find('select').not(find90sRows($frm));
}

setShow1990sInSelectBoxes = function($frm, doShow) {
  find90sRows($frm).each(function(idx, sel) {
    if(doShow) {
      $(sel).showOptions();
    } else {
      $(sel).hideOptions(function($opt) {
        if($opt.val() == '') return false;
        
        return $opt.val() < 1990;
      });
    }
  });
}

infoBox = function(msg) {
  if($('#js_infoBox').size() == 0)
    $('body').append($('<div id="js_infoBox" style="display: none">'));
  
  // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
  $("#js_infoBox").dialog("destroy");
  
  $("#js_infoBox").html(msg);
  
  $("#js_infoBox").dialog({
    modal: true,
    buttons: {
      Ok: function() {
        $(this).dialog('close');
      }
    }
  });
};

$(function() {
  $('body').append($('<div id="question-dialog" style="display: none">'));
  
  $('#question-dialog').text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean gravida lectus eu sapien placerat quis viverra metus dignissim. Curabitur ut est et metus commodo ullamcorper id non nulla. Phasellus quis felis sed felis porttitor egestas id id nisi. Maecenas lobortis ornare tincidunt. Maecenas neque velit, ornare lobortis blandit sagittis, bibendum non arcu. Nam sed placerat tellus.");
  
  $('a.questionbuttontext').click(function(event) {
    // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    $("#dialog").dialog("destroy");
    
    event.preventDefault();
  
    $("#question-dialog").html($(this).data('question'));
    
    $("#question-dialog").dialog({
      modal: true,
      buttons: {
        Ok: function() {
          $(this).dialog('close');
        }
      }
    });
  });
  
  $('a.questionbuttonlink').click(function(event) {
    event.preventDefault();
    
    window.open(
      $(this).attr('href'), 'newwin', 
      'menubar=no,toolbar=no,height=700,width=740,scrollbars=yes'
    );
  });
  
  $('a.js_new_window').click(function(event) {
    event.preventDefault();
    
    window.open(
      $(this).attr('href'), 'newwin', 
      'menubar=no,toolbar=no,height=700,width=607,scrollbars=yes'
    );
  });
  
  $('a.js_new_window_fullsize').click(function(event) {
    event.preventDefault();
    
    window.open(
      $(this).attr('href'), 'newwin_fullsize', 
      'menubar=no,toolbar=no,height=700,width=776,scrollbars=yes'
    );
  });
  
  /**
   * Wipes the content in the elements supplied and puts an ajax loader in each element.
   * The content will keep its height as long as the loader is present (the new div with the loader
   * in it will get its height copied from the current content).
   * @param options Allows you to specify custom values. The only value currently supported is the
   *                height key. It allows you to manually override the height of the ajax loader.
   */
  $.fn.boanalysAjaxLoader = function(options) {
    if(!options) options = {};
    
    $(this).each(function() {
      var $loader = $('<div class="app_ajaxloader_large" />');
      var o = $.extend({ height: $(this).outerHeight(true) }, options);
      $loader.css({ height: o.height });
      
      $(this).html('');
      $(this).append($loader);
    });
    
    return $(this);
  };
  
  /**
   * Hides all .fielderror errors in the desired form, and instead makes the related <strong>-tags
   * that contains the field titles red upon errors. This only works on table-forms.
   * 
   * This function should only be run on form tags: $('form#myform').makeFormTableHeadersRed()
   */
  $.fn.makeFormTableHeadersRed = function() {
    var clearErrorClasses = function(event, json, $frm) {
      if(!$frm) $frm = $(this);
      $frm.find('.app_field_error').removeClass('app_field_error');
    };
    
    var updateErrorClasses = function() {
      clearErrorClasses(null, null, $(this));
      
      $(this).find('.badfield').each(function() {
        var $strong = $(this).parents('tr:first').find('strong');
        $strong.addClass('app_field_error');
      });
      
      $(this).find('.fielderror').addClass('app_important_hidden');
    };
    
    $(this).each(function() {
      $(this).bind('pg.ajaxform.success', clearErrorClasses);
      $(this).bind('pg.ajaxform.exception', updateErrorClasses);
    });
  };
  
    
  $('a.v_new_window').click(function(event) {
    event.preventDefault();
    
    window.open(
      $(this).attr('href'), 'moreinfo', 
      'menubar=no,toolbar=no,height=700,width=607,scrollbars=yes'
    );
  });
});

dbg = function(msg) {
  $(function() {
    if(typeof($dbg) == 'undefined') $dbg = $('<div>').appendTo($('body'));  
    
    $dbg.append($('<p>').html(msg));
  });
}

