/**
 * @author Nathan Kelly - http://www.nathan-kelly.com/
 * @copyright 2009 Nathan Kelly
 * @package nk-utils (external links and popup [yuk] windows)
 * @version 1.0.0
 * @dependencies 
 * 	prototype.js 1.6.0.2+ (http://www.prototypejs.org/)
 *	scriptaculous.js 1.8.1+ (http://script.aculo.us/)
 * @license
 *	Creative Commons Attribution 2.5 Australia License
 *	http://creativecommons.org/licenses/by/2.5/au/
 */
nk_utils = Class.create({
	
	_popupWidth: 1024,
	_popupHeight: 768,
	
	initialize: function() {
		
		var links = $$('a');
		
		for (var i=0; i<links.length; i++) {
			
			if ( $('ccm-main-nav') !== null && links[i].descendantOf($('ccm-main-nav')) ) {
				
				// don't touch these ones, they are the editing controls
				
			} else {
			
				if ( $('popup') ) {
	
					if (links[i].rel !== 'window' ){
						
						links[i].writeAttribute('rel', 'popdown');
						
					}
					
				}
				
			}
			
			if ( String(links[i].getAttribute('rel')) == 'external' ) {
				
				window.location = links[i].href;
				
			} else if ( String(links[i].getAttribute('rel')) == 'popup' ) {
				
				this.popUp(links[i]);
				
			} else if ( String(links[i].getAttribute('rel')) == 'popdown' ) {
				
				this.popDown(links[i]);
				
			}
		}
	},
	
	popUp: function(link) {
		
		Element.observe(link, 'click', (function(e) {
			
			var wd = document.viewport.getDimensions();
			
			var popx = (wd.width - this._popupWidth) * 0.5;
			
			var popy = (wd.height - this._popupHeight) * 0.5;
			
			window.open(link.href,'yuk','width='+this._popupWidth+',height='+this._popupHeight+',top='+popy+',left='+popx+',menubar=0,location=0,toolbar=0,personalbar=0,status=0,scrollbars=1,allowresize=1,dependent=1');
			
			Event.stop(e);
			
		}).bindAsEventListener(this));
		
	},
	
	popDown: function(link) {
		
		Element.observe(link, 'click', (function(e) {
			
			//alert(window.parent);
			
			//var mum = window.parent;
			var mum = window.opener;
			
			self.close();
			
			mum.location = link.href;
			
			Event.stop(e);
			
		}).bindAsEventListener(this));
		
	}

});

document.observe('dom:loaded', function () { new nk_utils(); });
