/* @name:		Enhancement scripts
 *					NOW INCLUDES FORM ENHANCEMENT
 *
 * @site:		ME Bank
 * @author:		VDilkesFrayne, ME Bank, http://www.membersequitybank.com.au
 * @desc:      Cut down version of enhance.js developed by RBurnie of Hugeobject
 *
 *	REQUIRES: mootools
 *
 *	CONTENTS
 *		# GENERAL DOMREADY ENHANCEMENT (window domready)
 *		# Class: Window_links		- runs automatically
 *
 */


/* = GENERAL DOMREADY ENHANCEMENT = ____________________________________________________ */
// AUTOMATED CALL on domready ============================================================

window.addEvent('domready',function(){	
	// = RUN DEVELOPEMNT SCRIPTS !!!!!!!!!!!!!!!!!!!
	// new ME_development();// <-- COMMENT OUT FOR LIVE
	// At the mo' the class above will add the getRate code in the actual
	// markup so you can see it easy - for checking if right code used
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	// = Add hover events for ie6 (for non a tags) =
	if(window.ie6) {
		// == buttons ==
		var buttons = $$('button');
		buttons.each(function(btn){
			btn.addEvent('mouseover',function(){ this.addClass('hover'); });
			btn.addEvent('mouseout',function(){ this.removeClass('hover'); });
		});
	}
	new Window_links();

});



/* = Class: New_window_links = __________________________________________________________
	Generic window opening
	Usage:
		*	ALL EXTERNAL LINKS HAVE FUNCTIONALITY APPLIED AUTOMATICALLY
			TO PREVENT THIS ADD THE ATTRIBUTE rel="same-window" TO THE <a>
			- The windows will have menu's etc.
			
		* 	PDF LINKS  HAVE FUNCTIONALITY APPLIED AUTOMATICALLY
			- The window will have NO menu's etc.
			
		*	EXTERNAL LINKS WITH NO MENUS OR DIFFERENT OPTIONS
			- 	Adding rel="new-window" will make the new window open with no
				menus
			-	adding a json string of options will add/overwrite any standard options
				ex.
				rel="new-win[{width:640,height:480,location:'yes'}]"
				
		*	OPEN INTERNAL LINK IN NEW WINDOW
			- add rel="new-win" OR
			- with options rel="new-win[{width:640,height:480,location:'yes'}]"
				BY DEFAULT THERE WILL BE NO MENU BARS ETC. THIS CAN BE FORCED BY ADDING
				CUSTOM OPTION allmenus:'yes'		
		
*/
var Window_links = new Class({
	initialize: function() {
		var local_domain = document.location.href.split('/');
		local_domain = local_domain[0]+'//'+local_domain[2];
		$$('a').each(function(a,i){
			var rel = ($chk(a.getProperty('rel')) ? a.getProperty('rel') : '');
			// Exit if theres a rel="same-window" (used to prevent enhancement)
			if(rel.indexOf('same-window')>-1) { return; }
			
			var title = ( $chk(a.getProperty('title')) ? a.getProperty('title') : '' );
			var new_title = ( title=='' ? '[opens in new window]' : a.getProperty('title')+' [opens in new window]' );
			var win_title = ( title=='' ? 'new_win'+i : title.replace(/ /g,'_').replace(/\W/g,'') );
			
			var open_new = false;
			// if its an external link
			if(a.href.indexOf('http')>-1 && a.href.indexOf(local_domain)<0){ 
				a.addClass('external-link');
				a.addEvent('click',function() {
				   pageTracker._trackPageview('/outgoing/'+this.href.split("://")[1]);
				});
				open_new = true;
			} else if(a.href.slice(a.href.length-4).toLowerCase() == '.pdf') {
				// If a .pdf document
				a.addClass('pdf-link');
				a.addEvent('click',function() {
				   pageTracker._trackPageview(this.href.split(location.hostname)[1]);
				});
				open_new = true;
			} else if(rel.indexOf('new-win')>-1) {
				// If internal with new-win rel
				a.addClass('new-win-link');
				open_new = true;
			}
			
			if(open_new) {
				var optns = this.return_options(a,rel);
				a.setProperty('title',new_title);
				a.addEvent('click',function(e){
					new Event(e).stop();
					var win = window.open(a.href,win_title,optns);
					win.focus();
				});	
			}
			
		},this);
	},
	
	return_options: function(a,rel) {
		// NEW WINDOW OPTIONS DEFAULTS
			// Defaults based on old sites settings for opening new windows
			// can be over written by setting json string in rel attribute of link
			// EX: 
			// A 640 * 480 window with no scroll bar and a location bar would be
			// <a href="whatever" rel="new-win[{width:640,height:480,scrollbars:'no',location:'yes'}]">
			// Any additional option added this way will be added to the options
			
			var s_optns = '';
			var optns = {
				toolbar:'yes',
				status:'yes',
				location:'yes',
				menubar:'yes',
				directories:'yes',
				scrollbars:'yes',
				resizable:'yes'
			};
			
			var rel_optns = (rel.indexOf('new-win[')>-1 ? rel.split('[')[1].replace(']','') : '');
			// If the link has a rel value or rel_options has "allmenus" or its a pdf-link
			// hide the menu options. NB: if any of those options are specified they will
			// be re-written 
			if( (rel!='' && !(rel_optns.indexOf('allmenus')>-1)) || a.hasClass('pdf-link')) {
				optns.toolbar = 'no';
				optns.location = 'no';
				optns.menubar = 'no';
				optns.directories = 'no';
			}
			// CHECK FOR NEW OPTIONS AND ADD/REPLACE
			//if(rel_optns){
				// only filter options if the new-win contains a json string
				if(rel_optns.charAt(0)=='{' && rel_optns.charAt(rel_optns.length -1)=='}') {
					rel_optns = eval('('+rel_optns+')');
					$H(rel_optns).keys().each(function(k){
						optns[k] = rel_optns[k];
					});
				}
			//}
			// If no width or height supplied set it to be slightly smaller than screen
			if(!$chk(optns.width) || !$chk(optns.height)) {
				optns.width = screen.availWidth - 40;
				optns.height = screen.availHeight - 75;
				optns.screenX =0;
				optns.screenY =0;
				optns.top =10;
				optns.left =10;
			}
			
			// CONVERT OPTIONS TO STRING
			var ignore = ['allmenus'];
			$H(optns).keys().each(function(k) {
				if(ignore.indexOf(k)==-1) {
					s_optns += k+'='+optns[k]+',';
				}
			});
			s_optns = s_optns.slice(0,s_optns.length-1);
			
			return s_optns;
	}
});
