// extend prototype Event with a method to determine of activation occurred - ie mouse click or enter
// :NOTE: problems with isLeftClick in prototype library
Object.extend(Event, {
  isActivationKey: function(event) {
    if (event.type != "undefined" && event.type == "click") { return true; }
    var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
    return (charCode == Event.KEY_RETURN) ? true:false;
  }
});

// stores settings for dialog windows and the actual instances themselves
var dialog; 

// see what version of apple webkit are using
var returnFalseFix = false;
if (/Safari/.test(navigator.userAgent)) {
  returnFalseFix = true;
/**
 * This was only to use return false fix for WebKit of 4 and above
 * ie Safari on Tiger. But as it happens require it for older versions too.
 * The bug was actually related to the returned event instead (see openDialog method)
  var matches = navigator.userAgent.match(/AppleWebKit\/([0-9])/);
  if (matches && matches.length == 2) {
    if (parseInt(matches[1]) == 4) {
      returnFalseFix = true;
    }
  }
*/  
}

function __initDialogs() {
	var anchors = $A(document.getElementsByTagName('a')), width, height;
	anchors.each(function(item) {
		var matches = item.className.match(/^dialog_([0-9]+)_([0-9]+)$/);
		if ((matches instanceof Array) && matches.length > 0) {
      item.width = matches[1];
      item.height = matches[2];      
			Event.observe(item, 'click', function(evt){openDialog(evt);}, false);
			Event.observe(item, 'keypress', function(evt){openDialog(evt);}, false);
      if (returnFalseFix) {
				item.onclick = function() {return false;}
			}		
		}
    else {
      if (item.className.match(/(new|thumb)/)) {
        // using the lazy approach here!! should not work in xhtml but appears safe at moment!
        item.target = "_blank";
        item.title = item.title + " (Opens in a new window)";
      }
    }
	});
}
	
function openDialog(evt, width, height) {
	if (!Event.isActivationKey(evt)) {
		return 0;	
	}
	Event.stop(evt);
	
	var element = Event.element(evt);	
  
  // pickup bug in older webkit versions WRT bubbling where need to
  // step out to the anchor node. This will also handle the IMG element
  // that is clicked sometimes for banners.
  if (element.nodeName.toUpperCase() != 'A') {
    while (element.nodeName.toUpperCase() != 'A') {
      element = element.parentNode;
    }
  }
  
  var dialog_name = "dialog";
  
  // if clicked on a banner image the event element will
  // be the image so need to step out to the anchor element
  // if (element.nodeName == 'IMG') { element = element.parentNode; }
  
  if (typeof dialog == "object" && !dialog.closed) {
    if (dialog.location.href == element.href) { dialog.focus(); return 1; } 
    else {dialog.close();} 
  }
  
  dialog = window.open(element.href, dialog_name, "status=yes,scrollbars=yes,menubar=yes,"
                                                    + "location=no,resizable=1,"
                                                    + "height=" + element.height 
                                                    + ",width=" + element.width);	
  dialog.focus();																											

}

Event.observe(window, 'load', __initDialogs); 

