/* Popups */
/*
 *
 * @className PopUps 
 * @author POP webdev [tw]
 * @version 0.1
 * @classDescription PopUps is a class to call window.open of a link's href if the user has js. 
 * @param elPopups {Array}	Array of desired A or other element to create popup
 * @param hDefaultWinOptions {Object} hash of default window options/specifications
 * @return {Object}	Returns a new  object.
 * This class depends on Prototype v1.6.
 * Individual links can override any of the window specifications by providing an object literal within it's rel attribute.
 * Example usage:
 * <a class="popup" href="http://www.google.com">popup</a>
 * <a class="popup" href="http://www.google.com" rel="{windowName:'mySpecificName1', windowPosition:'center'}">popup</a>
 * <div _popup="{url: 'http://www.google.com', winOptions: {windowName:'mySpecificName2', windowPosition:'top-right'}}">popup</div>
 * new PopUps('a.popup', {windowName: 'a_default_window_name', windowPosition: 'top-left'});
*/
var PopUps = Class.create({
	initialize: function (elPopups, hDefaultWinOptions) {
		// create a default options hash
		this.defaultOptions = $H({
			width: 608,
			height: 490,
			//windowPosition: 'center', //['top-left'|'top-center'|'top-right'|'center'|'bottom-left'|'bottom-center'|'bottom-right]
			//left:  #,					// DIY left position of the window
			//top:  #,					// DIY top position of the window
			hOffset: 0,					// Horizontal offset
			vOffset: 0,					// Vertical offset
			location: 'no',
			menubar: 'no',
			status: 'no',		
			toolbar: 'no',
			scrollbars: 'no',
			resizable: 'no',
			fullscreen: 'no',			// Display the window in theater mode?
			channelmode: 'no',			// Display the browser in full-screen mode? (must also be in theater mode)
			titlebar: 'no',
			windowName: 'popup'
		}).update(hDefaultWinOptions);

		var linkEventHandle = this.__LinkClick.bindAsEventListener(this);
		var elementEventHandle = this.__ElementClick.bindAsEventListener(this);
	
		elPopups.each(function(el){
			el.observe('click', (el.nodeName == 'A') ? linkEventHandle : elementEventHandle);
		});

	},

	__LinkClick: function (e){
		var el = Event.findElement(e, 'a');
 		var url = el.readAttribute('href');
 		var instanceOptions;
 		if(el.readAttribute('rel')){
 			instanceOptions = $H(el.readAttribute('rel').evalJSON());
 		}
		e.preventDefault();
		
		this.openWindow(url, instanceOptions);
	},
	
	__ElementClick: function (e){
		var el = e.element();
		var params = el.readAttribute('_popup').evalJSON();
 		var url = el.readAttribute('_popup');
 		var instanceOptions;
 		if(params.winOptions){
 			instanceOptions = $H(params.winOptions);
 		}
		this.openWindow(params.url, instanceOptions);
	},	
	
	openWindow: function(url, instanceWinOptions){
		// Merge specific options hash set in the rel with default options
		var options = (instanceWinOptions) ? this.defaultOptions.merge($H(instanceWinOptions)) : this.defaultOptions.clone();
		
		// position the window if specified
		if(options.get('windowPosition')){
			var pos = options.unset('windowPosition');	
			var left, top;
			var availHeight = screen.availHeight, availWidth = screen.availWidth;
			var popupHeight = options.get('height'), popupWidth = options.get('width');
			var vOffset = options.get('vOffset'), hOffset = options.get('hOffset');
			switch(pos){
				case 'top-left': 
					top = 0 + vOffset;
					left = 0 + hOffset;
					break;
				case 'top-center': 
					top = 0 + vOffset;
					left = ((availWidth - popupWidth)/2) + hOffset;
					break;
				case 'top-right': 
					top = 0 + vOffset;
					left = availWidth - (popupWidth + hOffset);
					break;
				case 'center': 
					top = ((availHeight - popupHeight)/2) + vOffset;
					left = ((availWidth - popupWidth)/2) + hOffset;
					break;
				case 'bottom-left': 
					top = availHeight - (popupHeight + vOffset);
					left = 0 + hOffset;
					break;
				case 'bottom-center': 
					top = availHeight - (popupHeight + vOffset);
					left = ((availWidth - popupWidth)/2) + hOffset;
					break;
				case 'bottom-right': 
					top = availHeight - (popupHeight + vOffset);
					left = availWidth - (popupWidth + hOffset);
					break;
			}
			options.set('top', top);
			options.set('left', left);
		}

		var win = window.open(url, escape(options.unset('windowName')), options.invoke('join', '=').join(','));
		if (window.focus) {
			win.focus()
		}
	}
});
//document.observe('dom:loaded', function() { popUps = new PopUps($$('a.popup'), {windowName: 'a_default_window_name', windowPosition: 'top-left'}); });
//<a class="popup" href="http://www.google.com" rel="{windowName:'my_specific_name'}">popup</a>
/* /Popups */

/* PopOver */
var PopOver = Class.create ({
    initialize: function(trigger, popover, closer)
    {
		this.trigger = $(trigger);
		this.popover = $(popover);
		this.closer = $(closer);
		
		if(!this.trigger || !this.popover){return;}//element check
		
		this.popover.hide();
		
		// attach a click handler to trigger element
		Event.observe(this.trigger, 'click', this.__Click.bindAsEventListener(this));
		if(this.closer){
			// attach a click handler to close link
			Event.observe(this.closer, 'click', this.__Click.bindAsEventListener(this));
		}
    },

    // Deal with a clicked link
    __Click: function(e) {
		e.stop();
		this.popover.toggle();
    }    
});
/* /PopOver */
document.observe('dom:loaded', function(){
	homePagePopUp = new PopUps($$('.popup'), {windowName: 'defaultWindowName', windowPosition: 'top-left'});
});
