// include the ability to extract metadata from elements (and add it in too)
Element.addMethods({
  getMetaData: function(element){
    element = $(element);
    var found = element.className.match(/\{.*\}/);
    if (found) {
      try {
        return eval("(" + found[0] + ")");
      } catch(e) {
        return {};
      }
    } else {
      return {};
    }
  },

  setMetaData: function(element, md){
    element = $(element);
    // strip out any existing metadata (sorry!)
    var found = element.className.match(/\{.*\}/);
    if (found) {
      element.removeClassName(found);
    }
    // add in the new stuff
    element.addClassName(Object.toJSON(md));
  }
});

// brutal windows
function openWindow(windowSource,h,w) {
	params = 'left=100,top=100,height='+h+',width='+w+',directories=no,menubar=no,resizable=no,scrollbars=yes,titlebar=no,toolbar=no,alwaysraised=yes';
	window.open(windowSource,'LRUSUVL3Win',params);
}

// modal windows
function openModal(thediv) {
	$j("#"+thediv).modal({
		onOpen : function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.hide();
				dialog.container.fadeIn('fast');
				dialog.data.slideDown('fast');
			});
		}
		,
		onClose : function (dialog) {
			dialog.data.fadeOut('fast');
			dialog.container.slideUp('fast');
			dialog.overlay.fadeOut('fast', function () {
				$j.modal.close(); // must call this!
			});
		}
		, overlayClose : true
	});
};

$j(document).ready(function(){
	// wire up all the buttons to the dialog boxes
	$j('.open2bModal').click(function(){
		openModal($j(this).metadata().open_id);
		return false;
	});
	// and the close buttons
	$j('.popover-close-btn').click(function(){
		$j.modal.close();
		return false;
	});

	// wire up the cert-buttons
	$j('.cert-button').click(function(){
		openWindow('/certified',510,702);
		return false;
	});

});
