/**
 * popIt2.js
 * version 1, 20 apr 2007;
 * creates an accessible popup, see;
 * http://accessify.com/features/tutorials/the-perfect-popup/
 * for the theory behind this implimentation;
 */
 
var _POPUP_FEATURES = 'location=0, statusbar=0, menubar=0, scrollbars=yes, resizable=yes';

/**
 * returns a closure which opens the popup window;
 * @param    url            new window location;
 * @param    features    list of properties for the new window;
 * @param    target        window target;
 */
function doPopup( url, features, target ) {
    //if no target supplied create one;
    if( !target ){
        var target  = '_blank';
    }

    //return a closure which opens a new popup;
    return function( e ){
        var theWindow = window.open( url, target, features );
        theWindow.focus();
        //stop the browser following the link, in common.js;
        return stopDefault( e );
    }
}

function getWindowWidth(){
    var de = document.documentElement;
    return    self.innerWidth ||
            de && de.clientWidth ||
            document.body.clientWidth;
}

/**
 * gets all link instances with a rel attribute with a value
 * of popup and binds a click handler to these instances;
 */
function getPopups(){
       
    var links = document.getElementsByTagName( "a" );

    for( var i = 0; i < links.length; i++ ){
        //if the link contains a rel attr with a value of popup...;
        if( links[i].getAttribute("rel") &&
            links[i].getAttribute("rel").indexOf("popup")!= -1 ){
            
            var props = getProperties( links[i] );
            var target = links[i].getAttribute( "target" );
            var url = links[i].getAttribute( "href" );
             
            //bind event, see common.js;
            addEvent( links[i], "click", doPopup( url, props, target ) );
        }
    }
}

/**
 * extracts any addition poppup properties with in the rel attribute;
 * @param    elem    link element;
 */
function getProperties( elem ){

    //default properties;
    var features = null;
    var type = "standard";
    var width = "820";
    var height = "580";
    var top = 100;
    var spacer = ", ";
    
    //extract link additional properties;
    var props = elem.getAttribute("rel").split(" ");
    var h = props[2] || height;
    var w = props[3] || width;
   
    features = _POPUP_FEATURES;
    features += spacer;
    features += "height=" + h;
    features += spacer;
    features += "width=" + w;
    features += spacer;
    features += "left=" + Math.ceil( getWindowWidth()/2  - parseInt(w)/2 );
    features += spacer;
    features += "top=" + top;
    
    return features;
}

addEvent( window, "load", getPopups ); 