// EMC Flyout
// By: R Brown
// May 2009

$.emc = $.emc || {};  // Make the EMC namespace availible

(function ( $ ) {                  // Compliant with jquery.noConflict()
	$.fn.emc_flyout = function(o) {
		
		o = $.extend({
				flyoutLink : '', //the id of the link that opens the flyout
				offset_left : 0, //use to offset the default positioning
				offset_top : 0 //use to offset the default positioning
		}, o || {});
		
		var flyout, flyoutLink;
		
		flyout = $(this);
		
		init();
		
		function init(){
			//get the flyout link
			flyoutLink = o.flyoutLink;
			
			//if the flyout link doesn't exist then we stop here
			if(!flyoutLink.length) return false;
			
			flyoutLink.click( showFlyout );
			
			//add the flyout to the flyouts array
			if($.emc.flyouts) $.emc.flyouts.push(flyout);
			else $.emc.flyouts = new Array(flyout);
			
			addCloseButton();
		}
		
		function addCloseButton(){
			$('<div class="close"><a href="#"><img src="/media/aerlinguscom/styleassets/images/js/flyout-login-btn-close.gif" alt="Back to content" width="20" height="20" /></a></div>').insertAfter($("div.ml",flyout)).find('a').click( closeFlyout );
		}
		
	
		function positionFlyout() {
		}
		
		function closeFlyout(){
			flyout.hide();
			if($.pcIE6) flyout.find('div.hideSelect').remove();
			
			$('body').unbind('click',closeFlyout);
			
			flyoutLink.focus();
			return false;
		}
		
		function showFlyout(){
			flyoutLink = $(this);
			//hide all other flyouts
			for(n=0;n<$.emc.flyouts.length;n++){
				if($.pcIE6) $.emc.flyouts[n].find('div.hideSelect').remove();
				$.emc.flyouts[n].hide();
			}
			
			//first position the flyout of screen so we can display it and get it's width
			flyout.css( 'left', '-10000px');
			flyout.show();
			
			//calculate the default position
			var obj = flyoutLink.get(0);
			x = obj.offsetLeft;            // Get left position from the parent object
			y = obj.offsetTop;            // Get top position from the parent object
			while(obj.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
				oParent = obj.offsetParent;    // Get parent object reference
				x += oParent.offsetLeft; // Add parent left position
				y += oParent.offsetTop; // Add parent top position
				obj = oParent;
			}
			
			x = x + o.offset_left;
			y = y + o.offset_top;
			
			//ensure the flyout doesn't appear off screen
			var flyout_width = flyout.innerWidth();
			var flyout_height = flyout.innerHeight();
			var window_width = $(window).width();
			var window_height = $(window).height();
			
			if(x < 10) x = 10; //too far to the left
			else if(x + flyout_width + 10 > window_width) x = window_width - flyout_width - 10;	//too far to the right
			if(y - 10 < window.pageYOffset) y = window.pageYOffset + 10; //too high
			else if(y + flyout_height + 10 > window.pageYOffset + window_height) y = window.pageYOffset + window_height - flyout_height - 10;	//too low
			
			flyout.css( { 'top':y, 'left':x } );
			
			$('body').bind('click',closeFlyout);
			
			if($.pcIE6){
				var iframe = flyout.find('iframe');
				if(!iframe.length) $('<div class="hideSelect"><iframe name="blank" src="/ie6_flyout_hideSelect.html" scrolling="no" frameborder="0"></iframe></div>').css({'width':(flyout_width-6),'height':(flyout_height-6)}).prependTo(flyout).find('iframe').css({'width':(flyout_width-6),'height':(flyout_height-6)});
			}
			
			flyout.focus();
			return false;
		}
		
			
	}
})(jQuery);
