// the functions in this file require the supplementary library prototype.js

// isUndefined copied from lib.js, is supposed to be a microsoft compat fix
function isUndefined(v) {
    var undef;
    return v===undef;
}

// These defaults should be changed the way it best fits your site
var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,scrollbars=1,width=950,height=600';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return false;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    // to be passed as an event listener
    // pops up a window grabbing the url from the event source's href
    link_popup(e.currentTarget);
    e.preventDefault();
}

function event_popup_features(features) {
    // generates an event listener similar to event_popup, but allowing window features
    return function(e) { link_popup(e.currentTarget, features); e.preventDefault() }
}

// obiki specific code - rewrite all <a class='popup' href='...'> to do the right obiki thing 
function link_popup_rewrite(src, features) {
    var href = src.getAttribute('href_popup');
    if (!href) {
    	href = src.getAttribute('href');
        href = href.replace(/html$/, 'popup');
    	src.setAttribute('href_popup', href);
        //this is a safari hack - since safari doesnt allow canceling events
        // AFAIK. TODO: fix or make it specific to safari)
        src.setAttribute('href', 'javascript:;');
    }
    return raw_popup(href, src.getAttribute('target') || '_blank', features);
}

function event_popup_rewrite(e) {
    if (e.currentTarget) {
        link_popup_rewrite(e.currentTarget);
    } else {
        link_popup_rewrite(e.srcElement);
    }
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

Event.observe(window, 'load', function() {
  $$('a.popup').each(function(el, index) {
      Event.observe(el, 'click', event_popup_rewrite);
  });
});



