function toolboxLoadScript(fileurl, type, callback){
	var tag;
	if(type == 'css'){
		tag = document.createElement("link");
		tag.setAttribute('type', 'text/css');
		tag.setAttribute('rel', 'stylesheet');
	}else if (type == 'js'){
		tag = document.createElement("script");
		tag.setAttribute('type', 'text/javascript');
	}else{
	  return;
	}

	if(callback){
    if (tag.readyState) {
      //IE
      tag.onreadystatechange = function() {
        if (tag.readyState == "loaded" || tag.readyState == "complete") {
          tag.onreadystatechange = null;
          callback();
        }
      };
    } else {
      //Others
      tag.onload = function() {
        callback();
      };
    }
  }

	if(type == 'css'){
		tag.setAttribute('href', fileurl);
	}else{
		tag.setAttribute('src', fileurl);
	}

	if(typeof fileurl!="undefined"){
		document.getElementsByTagName('head')[0].appendChild(tag);
	}
}

function toolboxIsValidZipCode(value) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}

function toolboxAltCorners(header, footer){
  curvyCorners({
      tl: { radius: 8 },
      tr: { radius: 8 },
      antiAlias: true
    }, header );
  curvyCorners({
      bl: { radius: 8 },
      br: { radius: 8 },
      antiAlias: true
    }, footer );
}

function toolboxInitWidget(toolbox_data){
  document.write('<div id="mie-toolbox-wrapper-'+ toolbox_data.toolbox_id +'"></div>');

  toolboxLoadScript(toolbox_data.css_file, 'css');

  // load and init GA when needed
  if( toolbox_data.reports.log === true && toolbox_data.reports.ga.use === true){
    if (typeof(_gat) == 'undefined') {
      toolboxLoadScript('http://www.google-analytics.com/ga.js', 'js', function(){
        toolboxInitGA(toolbox_data);
      });
    } else {
      toolboxInitGA(toolbox_data);
    }      
  }

  if (typeof(jQuery) == 'undefined') {
    toolboxLoadScript(toolbox_data.base_path + '/js/jquery-1.3.2.min.js', 'js', function(){
      toolboxRenderWidget(toolbox_data);
    });
  } else{
    toolboxRenderWidget(toolbox_data);
  }
}

function toolboxInitGA(toolbox){
  gaToolbox = _gat._getTracker(toolbox.reports.ga.code);
  
  // google tracking. track as both page views and events for now
  gaToolbox._trackPageview(toolboxFormatTitle('toolbox', toolbox.toolbox_id, toolbox.toolbox_title));
  gaToolbox._trackEvent('toolbox', 'view', toolboxFormatTitle('toolbox', toolbox.toolbox_id, toolbox.toolbox_title));
  for(var i in toolbox.toolbox_items){
    gaToolbox._trackPageview(toolbox.toolbox_id +'/'+ toolboxFormatTitle('item', toolbox.toolbox_items[i].id, toolbox.toolbox_items[i].title));
    gaToolbox._trackEvent('toolbox-item', 'view',  toolboxFormatTitle('item', toolbox.toolbox_items[i].id, toolbox.toolbox_items[i].title));
  }
}

function toolboxFormatTitle(type, id, title){
  return type +': '+ id +' - '+ title;
}

function toolboxRenderWidget(toolbox_data){
  var toolbox = jQuery('#mie-toolbox-wrapper-' + toolbox_data.toolbox_id);
  toolbox.append(toolbox_data.widget_html);

  if (jQuery.browser.msie && jQuery.browser.version < 7){
    toolboxLoadScript(toolbox_data.base_path + '/js/pngfix.js', 'js');
  }

  // set the content containers width to avoid jumping during animation (also on window resize)
  jQuery('div.mie-toolbox-item-content', toolbox).css('width', jQuery('.mie-toolbox', toolbox).css('width'));
  jQuery(window).resize(function(){
    jQuery('div.mie-toolbox-item-content', toolbox).css('width', jQuery('.mie-toolbox', toolbox).css('width'));
  });

  // need to round corners for all except IE6. Call alt method for IE, else use CSS
  if(toolbox_data.round_corners && !(jQuery.browser.version < 7 && jQuery.browser.msie)){
    if(jQuery.browser.msie || jQuery.browser.opera){
      jQuery(document).ready(function() {
        if (typeof(curvyCorners) === 'function') {
          toolboxAltCorners('#mie-toolbox-wrapper-' + toolbox_data.toolbox_id + ' .mie-toolbox-header', '#mie-toolbox-wrapper-' + toolbox_data.toolbox_id + ' .mie-toolbox-footer');
        } else {
         toolboxLoadScript(toolbox_data.base_path + '/js/curvycorners.js', 'js',
          function(){toolboxAltCorners('#mie-toolbox-wrapper-' + toolbox_data.toolbox_id + ' .mie-toolbox-header', '#mie-toolbox-wrapper-' + toolbox_data.toolbox_id + ' .mie-toolbox-footer');} );
        }
      });
    } else {
      jQuery('div.mie-toolbox-header', toolbox).addClass('mie-toolbox-header-corners');
      jQuery('div.mie-toolbox-footer', toolbox).addClass('mie-toolbox-footer-corners');
    }
  }

  jQuery('div.mie-toolbox-item-header', toolbox).hover(
    function(){
      var visible = (jQuery(this).next().css('display') != 'none');
      jQuery(this).css('background-color', toolbox_data.theme.open_color);
      if(visible){
        jQuery('div.mie-toolbox-item-more-text', this).text(toolbox_data.theme.more_text_close).show();
      } else {
        jQuery('div.mie-toolbox-item-more-text', this).text(toolbox_data.theme.more_text_open).show();
      }
    },
    function(){
      var visible = (jQuery(this).next().css('display') != 'none');
      if(!visible){
        jQuery(this).css('background-color', toolbox_data.theme.closed_color);
      }
      jQuery('div.mie-toolbox-item-more-text', this).html('&nbsp;');
    }
  );

  jQuery('div.mie-toolbox-item-header', toolbox).click(function(){
    var header = jQuery(this);
    var visible = (header.next().css('display') != 'none');
    var more_text = toolbox_data.theme.more_text_close;
    var bgcolor = toolbox_data.theme.open_color;
    if(visible){
      bgcolor = toolbox_data.theme.closed_color;
      more_text = toolbox_data.theme.more_text_open;
      jQuery('img.mie-toolbox-item-more-image', this).attr('src', toolbox_data.base_path + "/images/arrow.png");
    } else {
      jQuery('img.mie-toolbox-item-more-image', this).attr('src', toolbox_data.base_path + "/images/arrow_down.png");
    }
    header.siblings('div.mie-toolbox-item-header').each(function(){
      jQuery(this).css('background-color', toolbox_data.theme.closed_color);
      jQuery('img.mie-toolbox-item-more-image', this).attr('src', toolbox_data.base_path + "/images/arrow.png");
    });
    header.next('div.mie-toolbox-item-content').slideToggle('slow', function(){
      jQuery('div.mie-toolbox-item-more-text', header).text(more_text);
      header.css('background-color', bgcolor);
    }).siblings("div.mie-toolbox-item-content").slideUp("fast");

    // log the click only when opening
    if(!visible && toolbox_data.reports.log === true){
      var item_id = jQuery('input.toolbox-item-id', this).val();
      // google tracking
      if(toolbox_data.reports.ga.use === true){
        gaToolbox._trackEvent('toolbox-item', 'open', toolboxFormatTitle('item', item_id, toolbox_data.toolbox_items[item_id]['title']));
      }
      
      jQuery.ajax({
        type: 'GET',
        url: toolbox_data.host +'/toolbox_reports/item_open/'+ item_id +'/'+ toolbox_data.toolbox_id
      });
    }
  });

  jQuery("form.toolbox_rl_form", toolbox).submit(function() {
    var zip = jQuery("input.toolbox_rl_zip", this).val();
    var cats = jQuery("input.toolbox_rl_categories", this).val();
    var results_container = jQuery(this).next('div.toolbox_rl_results');
    if (toolboxIsValidZipCode(zip)) {
      results_container.text("Loading...").show();
      jQuery.ajax({
        type: 'GET',
        url: 'https://oeapi.one-economy.com/JSONService/v4/GetResourceCountByCategoryWithCallback',
        data: "language=en&key=d1768281-8951-4dbb-b5af-10fb24c772a7&CategoryIds=" + cats +"&zipcode=" + zip,
        dataType: 'jsonp',
        timeout: 3000,
        success: function(ret, textStatus){
          var result = '';
          results_container.text('').hide();
          if(ret===0){
            result = 'Please enter a valid 5 digit zip code.';
          } else {
            result = jQuery('<ul />');
            for(var i in ret.data){
              var count = 0;
              if(ret.data[i].resourcecount > 0){
                var a = jQuery('<a />')
                  .attr('href', 'http://beelocal.thebeehive.org/results?bl_category-id=' + ret.data[i].categoryid + '&bl_zipcode=' + zip)
                  .attr('target', '_blank')
                  .text(ret.data[i].categorytitle + ' ('+ ret.data[i].resourcecount +')');

                result.append(jQuery('<li />').append(a));
                count ++;
              }
            }
            if(count === 0){
              result = 'Sorry, we found no results for that zip code.';
            }
          }
          results_container.append(result).fadeIn();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
          results_container.text("Sorry, there was a problem: "+ textStatus +".").fadeIn();
        }
      });
      // google tracking
      if(toolbox_data.reports.log === true && toolbox_data.reports.ga.use === true){
        gaToolbox._trackEvent('resource-locator', 'submit', zip);
      }             
    } else {
      results_container.text("Please enter a valid 5 digit zip code.").show().fadeOut(5000);
    }

    return false;
  });

  jQuery('div.mie-toolbox-share-header', toolbox).click(function(){
    var header = jQuery(this);
    var visible = (header.next().css('display') != 'none');
    var more_text = toolbox_data.theme.more_text_close;
    if(visible){
      more_text = toolbox_data.theme.more_text_open;
      jQuery('img', this).attr('src', toolbox_data.base_path + "/images/arrow.png");
      // google tracking
      if(toolbox_data.reports.log === true && toolbox_data.reports.ga.use === true){
        gaToolbox._trackEvent('toolbox', 'share', toolboxFormatTitle('toolbox', toolbox_data.toolbox_id, toolbox_data.toolbox_title));
      }       
    } else {
      jQuery('img', this).attr('src', toolbox_data.base_path + "/images/arrow_down.png");
    }
    header.next('div.mie-toolbox-share-content', toolbox).slideToggle('slow');
  });

  // track item link clicks
  if(toolbox_data.reports.log === true){
    jQuery('div.mie-toolbox-item-content', toolbox).each(function(){
      var item_id = jQuery('input.toolbox-item-id', jQuery(this).prev('div.mie-toolbox-item-header')).val();
      jQuery('a', this).click(function(){
        // google tracking
        if(toolbox_data.reports.ga.use === true){
          gaToolbox._trackEvent('toolbox-item', 'click', 'Link: '+ escape(jQuery(this).attr('href')));
        } 
       
        jQuery.ajax({
          type: 'GET',
          data: 'url='+ escape(jQuery(this).attr('href')),
          url: toolbox_data.host +'/toolbox_reports/link_click/'+ item_id +'/'+ toolbox_data.toolbox_id
        });
      });
    });    

    // track header/footer clicks
    jQuery('div.mie-toolbox-header, div.mie-toolbox-footer', toolbox).each(function(){
      jQuery('a', this).click(function(){
        // google tracking
        if(toolbox_data.reports.ga.use === true){
          gaToolbox._trackEvent('toolbox-theme', 'click', 'Link: '+ escape(jQuery(this).attr('href')));
        }
      
        jQuery.ajax({
          type: 'GET',
          data: 'url='+ escape(jQuery(this).attr('href')),
          url: toolbox_data.host +'/toolbox_reports/link_click/'+ toolbox_data.theme.theme_id +'/'+ toolbox_data.toolbox_id
        });
      });
    });
  }
}var toolbox_data_ = { "toolbox_id": "220", "toolbox_title": "emergencyzone.org new", "toolbox_items": { "118": { "title": "Emergency Zone", "id": "118", "image": "\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-flashlight.gif\" alt=\"ez-flashlight.gif\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e", "description": "Prepare. React. Recover.", "body": "\x3cp\x3eEmergencies happen. \x3ca href=\"http://www.emergencyzone.org/\"\x3eGet the information you need to prepare for emergencies.\x3c/a\x3e\x3c/p\x3e" }, "122": { "title": "Types of Emergencies ", "id": "122", "image": "\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-wildfires.jpg\" alt=\"ez-wildfires.jpg\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e", "description": "Every emergency is different. Get tips on what to do in each. ", "body": "\x3cp\x3e\x3ca href=\"http://www.emergencyzone.org/types-emergencies\"\x3eLearn about the different types of emergencies\x3c/a\x3e, because what you should do in an emergency really depends on what type of emergency it is.\x3c/p\x3e" }, "119": { "title": "Be Prepared", "id": "119", "image": "\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-prepare.jpg\" alt=\"ez-prepare.jpg\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e", "description": "Emergencies happen. Make sure you have a plan.", "body": "\x3cp\x3eThe best way to protect yourself and your family during an emergency is to be prepared before one even happens. Planning for an emergency is easy to do. The key is getting started. \x3ca href=\"http://www.emergencyzone.org/prepare\"\x3eLearn more.\x3c/a\x3e\x3c/p\x3e" } }, "base_path": "http://toolbox.pic.tv/sites/all/modules/custom/toolbox_widget", "host": "http://toolbox.pic.tv", "round_corners": true, "widget_html": "\x3cdiv class=\"mie-toolbox\" style=\"\" \x3e\n	\x3cdiv class=\"mie-toolbox-header\" style=\"background-color:#fdc41f\"\x3e\n	  \x3cdiv class=\"mie-toolbox-header-inner\"\x3e\n	  \x3cimg  width=\"94\" height=\"36\" alt=\"\" src=\"http://toolbox.pic.tv/sites/default/files/imagefield_default_images/toolbox_title.png?1249520892\" /\x3e	  \x3c/div\x3e\n	\x3c/div\x3e\n\n	\x3cdiv class=\"mie-toolbox-items\"\x3e\n			\x3cdiv class=\"mie-toolbox-item-header\" style=\"background-color:#303539\"\x3e\n		  \x3cinput class=\"toolbox-item-id\" type=\"hidden\" value=\"118\" /\x3e\n		  \x3cdiv class=\"mie-toolbox-item-header-inner\"\x3e\n  		  \x3ctable class=\"mie-toolbox-item-header-table\" width=\"100%\"\x3e\n  		    \x3ctr\x3e\n  		        		      \x3ctd style=\"width:1px;\"\x3e\n        			\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-flashlight.gif\" alt=\"ez-flashlight.gif\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e  			    \x3c/td\x3e\n  			      			    \x3ctd\x3e\n        			\x3ch3 class=\"mie-toolbox-item-title\"\x3eEmergency Zone\x3c/h3\x3e\n        			\x3cdiv class=\"mie-toolbox-item-subhead\"\x3ePrepare. React. Recover.\x3c/div\x3e\n  			    \x3c/td\x3e\n  			    \x3ctd style=\"width:1%;\"\x3e\n  			      \x3cdiv class=\"mie-toolbox-item-more\"\x3e\n        				\x3cdiv class=\"mie-toolbox-item-more-text\"\x3e\x26nbsp;\x3c/div\x3e\n        				\x3cimg width=\"15px\" height=\"15px\" class=\"mie-toolbox-item-more-image\" src=\"http://toolbox.pic.tv/sites/all/modules/custom/toolbox_widget/images/arrow.png\" /\x3e\n      				\x3c/div\x3e\n  			    \x3c/td\x3e\n  			  \x3c/tr\x3e\n  			\x3c/table\x3e\n			\x3c/div\x3e\n		\x3c/div\x3e\n\n		\x3cdiv class=\"mie-toolbox-item-content\" style=\"background-color:#181818\"\x3e\n		  \x3cdiv class=\"mie-toolbox-item-content-inner\"\x3e\x3cp\x3eEmergencies happen. \x3ca href=\"http://www.emergencyzone.org/\"\x3eGet the information you need to prepare for emergencies.\x3c/a\x3e\x3c/p\x3e\x3c/div\x3e\n		\x3c/div\x3e\n			\x3cdiv class=\"mie-toolbox-item-header\" style=\"background-color:#303539\"\x3e\n		  \x3cinput class=\"toolbox-item-id\" type=\"hidden\" value=\"122\" /\x3e\n		  \x3cdiv class=\"mie-toolbox-item-header-inner\"\x3e\n  		  \x3ctable class=\"mie-toolbox-item-header-table\" width=\"100%\"\x3e\n  		    \x3ctr\x3e\n  		        		      \x3ctd style=\"width:1px;\"\x3e\n        			\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-wildfires.jpg\" alt=\"ez-wildfires.jpg\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e  			    \x3c/td\x3e\n  			      			    \x3ctd\x3e\n        			\x3ch3 class=\"mie-toolbox-item-title\"\x3eTypes of Emergencies \x3c/h3\x3e\n        			\x3cdiv class=\"mie-toolbox-item-subhead\"\x3eEvery emergency is different. Get tips on what to do in each. \x3c/div\x3e\n  			    \x3c/td\x3e\n  			    \x3ctd style=\"width:1%;\"\x3e\n  			      \x3cdiv class=\"mie-toolbox-item-more\"\x3e\n        				\x3cdiv class=\"mie-toolbox-item-more-text\"\x3e\x26nbsp;\x3c/div\x3e\n        				\x3cimg width=\"15px\" height=\"15px\" class=\"mie-toolbox-item-more-image\" src=\"http://toolbox.pic.tv/sites/all/modules/custom/toolbox_widget/images/arrow.png\" /\x3e\n      				\x3c/div\x3e\n  			    \x3c/td\x3e\n  			  \x3c/tr\x3e\n  			\x3c/table\x3e\n			\x3c/div\x3e\n		\x3c/div\x3e\n\n		\x3cdiv class=\"mie-toolbox-item-content\" style=\"background-color:#181818\"\x3e\n		  \x3cdiv class=\"mie-toolbox-item-content-inner\"\x3e\x3cp\x3e\x3ca href=\"http://www.emergencyzone.org/types-emergencies\"\x3eLearn about the different types of emergencies\x3c/a\x3e, because what you should do in an emergency really depends on what type of emergency it is.\x3c/p\x3e\x3c/div\x3e\n		\x3c/div\x3e\n			\x3cdiv class=\"mie-toolbox-item-header\" style=\"background-color:#303539\"\x3e\n		  \x3cinput class=\"toolbox-item-id\" type=\"hidden\" value=\"119\" /\x3e\n		  \x3cdiv class=\"mie-toolbox-item-header-inner\"\x3e\n  		  \x3ctable class=\"mie-toolbox-item-header-table\" width=\"100%\"\x3e\n  		    \x3ctr\x3e\n  		        		      \x3ctd style=\"width:1px;\"\x3e\n        			\x3cimg src=\"http://toolbox.pic.tv/sites/default/files/imagecache/toolbox_item_thumb/images/toolbox_items/ez-prepare.jpg\" alt=\"ez-prepare.jpg\" title=\"\"  class=\"mie-toolbox-item-image\" width=\"40\" height=\"40\" /\x3e  			    \x3c/td\x3e\n  			      			    \x3ctd\x3e\n        			\x3ch3 class=\"mie-toolbox-item-title\"\x3eBe Prepared\x3c/h3\x3e\n        			\x3cdiv class=\"mie-toolbox-item-subhead\"\x3eEmergencies happen. Make sure you have a plan.\x3c/div\x3e\n  			    \x3c/td\x3e\n  			    \x3ctd style=\"width:1%;\"\x3e\n  			      \x3cdiv class=\"mie-toolbox-item-more\"\x3e\n        				\x3cdiv class=\"mie-toolbox-item-more-text\"\x3e\x26nbsp;\x3c/div\x3e\n        				\x3cimg width=\"15px\" height=\"15px\" class=\"mie-toolbox-item-more-image\" src=\"http://toolbox.pic.tv/sites/all/modules/custom/toolbox_widget/images/arrow.png\" /\x3e\n      				\x3c/div\x3e\n  			    \x3c/td\x3e\n  			  \x3c/tr\x3e\n  			\x3c/table\x3e\n			\x3c/div\x3e\n		\x3c/div\x3e\n\n		\x3cdiv class=\"mie-toolbox-item-content\" style=\"background-color:#181818\"\x3e\n		  \x3cdiv class=\"mie-toolbox-item-content-inner\"\x3e\x3cp\x3eThe best way to protect yourself and your family during an emergency is to be prepared before one even happens. Planning for an emergency is easy to do. The key is getting started. \x3ca href=\"http://www.emergencyzone.org/prepare\"\x3eLearn more.\x3c/a\x3e\x3c/p\x3e\x3c/div\x3e\n		\x3c/div\x3e\n		\x3c/div\x3e\n\n    \n	\x3cdiv class=\"mie-toolbox-footer\" style=\"background-color:#fdc41f\"\x3e\n	  \x3ca href=\"http://thebeehive.org\"\x3e\x3cimg  id=\"mie-toolbox-footer-image\" width=\"101\" height=\"40\" alt=\"\" src=\"http://toolbox.pic.tv/sites/default/files/imagefield_default_images/beehive_logo.png?1249521160\" /\x3e\x3c/a\x3e	\x3c/div\x3e\n\x3c/div\x3e", "theme": { "theme_id": "219", "base_color": "#fdc41f", "open_color": "#181818", "closed_color": "#303539", "more_text_open": "open", "more_text_close": "close" }, "css_file": "http://toolbox.pic.tv/sites/all/modules/custom/toolbox_widget/css/toolbox_widget.css?1313479074", "reports": { "log": true, "ga": { "use": 1, "code": "UA-15172132-1" } } }; toolboxInitWidget(toolbox_data_);
