/*
	==========================
	Event handling
	==========================
*/

/**
 * cross browser event handling function,written 
 * by Dean Edwards, 2005 http://dean.edwards.name/
 * 
 * PB 05/07/07 Note:
 * Has the advantages of;
 * Works in all browsers.
 * The 'this' keyword is available in all bound 
 * functions pointing to the current element.
 * Normalises all broswer specific functions for 
 * preventing default actions.
 * Queues events.
 */
function addEvent(element, type, handler) {
    // assign each event handler a unique ID
    if (!handler.$$guid) handler.$$guid = addEvent.guid++;
    // create a hash table of event types for the element
    if (!element.events) element.events = {};
    // create a hash table of event handlers for each element/event pair
    var handlers = element.events[type];
    if (!handlers) {
        handlers = element.events[type] = {};
        // store the existing event handler (if there is one)
        if (element["on" + type]) {
            handlers[0] = element["on" + type];
        }
    }
    // store the event handler in the hash table
    handlers[handler.$$guid] = handler;
    // assign a global event handler to do all the work
    element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
    // delete the event handler from the hash table
    if (element.events && element.events[type]) {
        delete element.events[type][handler.$$guid];
    }
};

function handleEvent(event) {
    // grab the event object (IE uses a global event object)
    event = event || window.event;
    // get a reference to the hash table of event handlers
    var handlers = this.events[event.type];
    // execute each event handler
    for (var i in handlers) {
        this.$$handleEvent = handlers[i];
        this.$$handleEvent(event);
    }
};

/**
 * stops the default broswer action for an event;
 * @param	e	triggered event;
 */
function stopDefault( e ){
	//if gecko...;
	if( e && e.preventDefault ){
		//...use w3c method...;
		e.preventDefault();
	}else{
		//...else shortcut to stop action in IE;
		window.event.returnValue = false;
	}
	return false;
}

/*
	==========================
	Pop up generation
	==========================
*/

/**
 * 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=no, 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 ){
            
            if ( links[i].rel.indexOf("noicon") == -1 ){
				links[i].style.backgroundImage = "url(Images/Icons/pop-up.gif)";
				links[i].style.backgroundPosition = "0 center";
				links[i].style.backgroundRepeat = "no-repeat";
				links[i].style.paddingLeft = "15px";
			}
			
			// add info to title attribute to alert fact that it's a pop-up window
			links[i].title = links[i].title + " [Opens in pop-up window]";
            
            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 ); 

