/**
 * jQuery.ghc.autopopup
 * @author ramima3 (Marco Ramirez)
 * @version 1.0
 * Date: 2009-06-19
 * 
 * Usage: Use "autopopup" in the rel attribute to specify a link that should open in a popup,
 * the following attibutes can also be specified in the rel tag
 * mode: mConsole | mStandard | mFullscreen | mPdf
 * width: w#, i.e. w350
 * height: h#
 * top: t#
 * left: l#
 * 
 * Example:
 * <a href="mypage.jhtml" rel="autopopup mConsole w450 h300 t50 l50">My Link</a>
 * 
 * Or use $.fn.popup(settings), settings should be an object literal and can have the
 * following properties:
 * width {Integer},
 * height {Integer},
 * top {Integer},
 * left {Integer},
 * mode {Integer} jQuery.ghc.autopopup.modes.standard or jQuery...modes.console or jQuery...modes.fullscreen
 * 
 * Example:
 * 
 * $('a.dynamicContent').popup({width:600, height:400, top: 10, left: 10, mode: $.ghc.autopopup.console});
 */

(function($){
	
	$.ghc = $.ghc || {};
	$.ghc.autopopup = {
  	version: '1.0',
  	modes: {
  		standard: 1,
  		console: 2,
			pdf: 3,
  		fullscreen: 4
  	}
  };
	
	
	var modes = $.ghc.autopopup.modes; //shortcut to modes
	
	//parse settings from rel attribute
	var regex = {
		m: /(?:^| )m(standard|console|fullscreen|pdf)( |$)/i, //mode
		w: /(?:^| )w(\d+)( |$)/,	//width
		h: /(?:^| )h(\d+)( |$)/,	//height
		l: /(?:^| )l(\d+)( |$)/,	//left
		t: /(?:^| )t(\d+)( |$)/		//top
	};

	var config = {
		l: 30,		//default left
		t: 30,		//default top
		w: 580,		//default width
		h: 780,		//default height
		pw: 550,	//pdf width
		ph: 700,	//pdf height
		br: 250,	//boundary - right
		bb: 300		//boundary - bottom
	};
	
	$.fn.extend({
		
		//creates popup windows from anchor tags by parsing settings from rel attribute
		autoPopup: function(){
			return this.each(function(){
				var opts = $(this).attr('rel');
				var settings = {};
				if(regex.m.test(opts)){ settings.mode = modes[regex.m.exec(opts)[1].toLowerCase()]; }
				if(regex.w.test(opts)){ settings.width = parseInt(regex.w.exec(opts)[1], 10); }
				if(regex.h.test(opts)){ settings.height = parseInt(regex.h.exec(opts)[1], 10); }
				if(regex.l.test(opts)){ settings.left = parseInt(regex.l.exec(opts)[1], 10); }
				if(regex.t.test(opts)){ settings.top =  parseInt(regex.t.exec(opts)[1], 10); }
				$(this).popup(settings);
			});
		},
		
		//does same as above but is backwards compatible with previous autopopup.js
		classicAutoPopup: function(){
			return this.each(function(){
				var opts = $(this).attr('rel').split(' ');
				var settings = {};
				if(opts.length >= 2){settings.mode = modes[opts[1].toLowerCase()];}
				if(opts.length >= 3 && /^\d+$/.test(opts[2])){ settings.width = parseInt(opts[2], 10); }
				if(opts.length >= 4 && /^\d+$/.test(opts[3])){ settings.height = parseInt(opts[3], 10); }
				$(this).popup(settings);
			});
		},
		
		//attaches event handler that opens popup windows (this function does the heavy lifting)
		popup: function(s){
			
			//settings
			s = $.extend({
				mode: modes.standard,
				width: config.w,
				height: config.h,
				left: config.l,
				top: config.t
			}, s);

			//bind the click handler to the element
			return this.unbind('click.autopopup').bind('click.autopopup',function(e){

				var features = '';
				//initialize the features string
				switch(s.mode){
					case modes.fullscreen:
					case modes.console:
						features = 'resizable=yes';
					break;
					case modes.pdf:
						features = 'resizable=yes,location=yes';
					break;
					default:
						features = 'resizable=yes,location=yes,toolbar=yes,scrollbars=yes,menubar=yes';
					break;
				}
				
				//set dimensions
				var width = s.mode === modes.pdf ? config.pw :
					s.mode === modes.fullscreen ? window.screen.availWidth : s.width;
					
				var height = s.mode === modes.pdf ? config.ph :
					s.mode === modes.fullscreen  ? window.screen.availHeight : s.height;
					
				var left = s.mode === modes.pdf ? config.l :
					s.mode === modes.fullscreen ? 0 : s.left;
					
				var top = s.mode === modes.pdf ? config.t :
					s.mode === modes.fullscreen ? 0 : s.top;
				
				//make sure dimensions don't put the window outside of the screen (too much)
				left = Math.min(window.screen.availWidth - config.br, left);
				top = Math.min(window.screen.availHeight - config.bb, top);
				width = Math.min(width, window.screen.availWidth - left);
				height = Math.min(height, window.screen.availHeight - top);
				
				//open popup on correct screen in FF
				var screeLeft = typeof window.screen.left !== 'undefined' ? window.screen.left : 0;
				var screenTop = typeof window.screen.top !== 'undefined' ? window.screen.top : 0;
				left = screeLeft + left;
				top = screenTop + top;
				
				//complete the features string
				features +=
					',width=' + width +
					',height=' + height +
					',left=' + left +
					',top=' + top;

				window.open($(this).attr('href'), '_blank', features);
				e.preventDefault();
			});
		}
	});
	
})(jQuery);

//load autopopup when document is ready
jQuery(function(){

	//grab all anchors and filter only external urls (http or https and not ghc.org subdomains), ghc-specific
	jQuery('a').filter(function(){
		var $this = jQuery(this); 
		var href = $this.attr('href');
		if(/^https?:\/\//i.test(href) && !/https?:\/\/(ghc.org|[^\/]+\.ghc.org)(\/|$)/.test(href)){
			var redirector = window.location.hostname.search(/www/i) === 0 ? '/redirector.jhtml' : '/public/redirector.jhtml';
			
			//update url to go through redirector
			$this.attr({'href': redirector + '?url=' + encodeURIComponent(href)});
			return true;
		}else{
			return false;
		}
	}).popup();

	//handle classic autopopups
	jQuery('a[rel~=popup]').classicAutoPopup();

	//handle autopopups
	jQuery('a[rel~=autopopup]').autoPopup();

	//handle pdfs, ghc-specific
	jQuery('a[href*=.pdf]').popup({mode:jQuery.ghc.autopopup.modes.pdf});
});