/**
 * popup.js
 * PB 14/04/08
 * creates an accessibly popup that degrades nicely :-);
 *
 * Depends:
 * ===================================
 * base.js  http://dean.edwards.name/weblog/2006/03/base/
 * jQuery.js http://http://jquery.com/
 */
var Popup = Base.extend({

   $: null,
   title: null,

    constructor: function(){
        if( ! jQuery ){
            return false;
        }
    },
    
    init: function( $ ){
         this.$ = $;
         this.title = "Opens in pop-up window";
    },
    
    doPopup: function( elem ){
        var props = this.getProps( elem );
	    var target = elem.target || '_blank';
	    var url = elem.href;
	    var self = this;
        // set title attribute;
        if( elem.title ){
            elem.title = this.title + ", " + elem.title;
	    }else{
            elem.title = this.title;
	    }
	    // bind handler to links;
	    this.$( elem ).bind( "click", function( e ){
	            var args = [ e, props, target, url ];
	            self.fire.apply( self, args );
	            return stopDefault( e );
	    });
    },
    
    fire: function(){
        var win = window.open( 
                        arguments[3],
                        arguments[2], 
                        arguments[1]    );
        win.focus();
    },
    
    getWindowWidth: function(){
        var de = document.documentElement;
        return  self.innerWidth ||
                de && de.clientWidth ||
                document.body.clientWidth;
    },
    
    getProps: function( 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.rel.split(" ");
        var h = props[2] || height;
        var w = props[3] || width;
       
        features = 'location=0, statusbar=0, menubar=0, scrollbars=yes, resizable=yes';
        features += spacer;
        features += "height=" + h;
        features += spacer;
        features += "width=" + w;
        features += spacer;
        features += "left=" + Math.ceil( this.getWindowWidth()/2 - parseInt(w)/2 );
        features += spacer;
        features += "top=" + top;
        
        return features;
    },
    
    setCSS: function( elem, css ){
        this.$( elem ).css( css );
    },
    
    getPopups: function(){
        var links = this.$("a");
        var self = this;
        links.each(function(){
            if( this.rel &&
                this.rel.indexOf("popup") != -1 ){
                if ( this.rel.indexOf("noicon") == -1 ){
				    var css = {
				        background: "url(SiteImages/Icons/pop-up.gif) top left no-repeat",
				        position: "0 center",
				        padding: "0 0 0 15px",
				        border: "solid 1px #900"
				    };
				    self.setCSS.call( self, this, css );
			    }
			    self.doPopup.call( self, this );
            }
        });
    }
});