//$$Revision: 1 $
//$$Workfile: aniteDivs.js $
// get the dimensions of the browser's *visible* area,
// that is, not including scrollbars
function getBrowserViewportDimensions() {
	var dimensions = {};

	if( browser.isSafari ){
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	} else if( (typeof( document.documentElement ) != "undefined") && (typeof( document.documentElement.clientWidth ) != "undefined" ) ){
		dimensions.width = document.documentElement.clientWidth;
		dimensions.height = document.documentElement.clientHeight;
	} else if( typeof( window.innerWidth ) != "undefined" ) {
		//Non-IE
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		dimensions.width = document.body.clientWidth;
		dimensions.height = document.body.clientHeight;
	}

	return dimensions;
}



function getBrowserScrollingOffsets(){
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	return { x: x, y: y };
}



function setBrowserScrollingOffsets( x, y ){

	if( document.documentElement && document.documentElement.scrollTop ){
		// Explorer 6 Strict
		document.documentElement.scrollLeft = x;
		document.documentElement.scrollTop = y;
	}
	else if( document.body ){
		document.body.scrollLeft = x;
		document.body.scrollTop = y;
	}
}



// get the coordinates of the centre of the browser viewport,
// measured from the top-left corner of the pages
function getBrowserViewportCentre(){
	var scrollOffsets = getBrowserScrollingOffsets();
	var viewportSize = getBrowserViewportDimensions();
	
	var centre = {};
	centre.x = scrollOffsets.x + Math.floor( viewportSize.width / 2 );
	centre.y = scrollOffsets.y + Math.floor( viewportSize.height / 2 );
	
	return centre;
}



////////////////////////////////////////////////////////////////////////

var stickyLocked="no";
var currentSticky="";
var currentStickyParent="";

function stickyShow( owner, sticky, url, evalJs) {
    var sticky = $( sticky );

	// don't try doing anything if the sticky hasn't loaded yet
	if( sticky == null ){ return; }

	sticky.style.display = "block";

	var position = calculateBestPopupPosition( owner, sticky );

	sticky.style.left = position.x + "px";
	sticky.style.top = position.y + "px";

	if (url) {aniteAjaxReq(sticky,url,evalJs)};
}

function stickyHide( sticky ){
	// make sure our arguments are nodes, not just IDs
	var sticky = $( sticky );

	// don't try doing anything if the sticky hasn't loaded yet
	if( sticky == null ){ return; }

	// hide the sticky
	sticky.style.display = "none";

	
	// remove any close handler we had registered for this overlay
	//globalOverlayClearCloseHandler( sticky.id );
}



// calculate the position of a popup (ie. blinky/sticky) next to its owner
// returns an (x,y) pixel offset from the page origin
function calculateBestPopupPosition( owner, popup ) {
	// make sure our arguments are nodes, not just IDs
	var owner = $( owner );
	var popup = $( popup );

	// get some useful measurements of the popup owner
	var ownerWidth = Element.getWidth( owner );
	var ownerHeight = Element.getHeight( owner );
	var ownerPosition = Position.cumulativeOffset( owner );
	var ownerLeft = ownerPosition[0];
	var ownerTop = ownerPosition[1];
	var ownerRight = ownerLeft + ownerWidth;
	var ownerBottom = ownerTop + ownerHeight;

	// middle of the popup owner
	var ownerCentreX = ownerLeft + Math.round( ownerWidth / 2 );
	var ownerCentreY = ownerTop + Math.round( ownerHeight / 2 );

	// how big is our popup?
	var popupWidth = Element.getWidth( popup );
	var popupHeight = Element.getHeight( popup );

	// find the position and size of the current browser viewport
	var viewportDimensions = getBrowserViewportDimensions();
	var scrollingOffsets = getBrowserScrollingOffsets();

	// use this information to find the edges of the viewport
	var viewportLeft = scrollingOffsets.x;
	var viewportTop = scrollingOffsets.y;
	var viewportRight = viewportLeft + viewportDimensions.width;
	var viewportBottom = viewportTop + viewportDimensions.height;

	// work out how much space we have in the viewport around our popup owner
	var spaceLeft = ownerLeft - viewportLeft;
	var spaceRight = viewportRight - ownerRight;
	var spaceTop = ownerTop - viewportTop;
	var spaceBottom = viewportBottom - ownerBottom;

	var x, y; // the position of the popup
	
	// will it fit to the right, top-aligned with its owner?
	if( (popupWidth < spaceRight) && (popupHeight < (spaceBottom + ownerHeight)) ) {
		x = ownerRight;
		y = ownerTop;
	} else if( popupWidth < spaceRight ){
		x = ownerRight;
		y = ownerCentreY - (popupHeight - spaceBottom);
		y = (y <= 0 ? 0 : y);
	} else if( (popupHeight < spaceTop) && (popupHeight > spaceBottom) ){
		x = ownerCentreX - Math.round( popupWidth / 2 );
		// if the popup goes over the right hand edge of the viewport, then move it left so that it doesn't
		if( x + popupWidth > viewportRight ){ x = viewportRight - popupWidth; }
		x = (x < 0 ? 0 : x);
		y = ownerTop - popupHeight;
	} else {
		x = ownerCentreX - Math.round( popupWidth / 2 );
		// if the popup goes over the right hand edge of the viewport, then move it left so that it doesn't
		if( x + popupWidth > viewportRight ){ x = viewportRight - popupWidth; }
		x = (x < 0 ? 0 : x);
		y = ownerBottom;
	}

	return { x: x, y: y };
}

// position an element in the centre of the browser window
function positionElementInViewportCentre( element ){
	var element = $( element );

	// get the viewport centre position and offset it by half the width
	// and height of the element
	var viewportCentre = getBrowserViewportCentre();
	var x = viewportCentre.x - Math.floor( Element.getWidth( element ) / 2 );
	var y = viewportCentre.y - Math.floor( Element.getHeight( element ) / 2 );

	positionElementFromPageOrigin( element, x, y );
}

///////grey out and show div ///////////////////////////////////////
function greyandShow(owner, sticky, url, evalJs) {
	var overlay = $( sticky );
	// don't try to do anything if we can't find the overlay
	if( overlay == null ){ return; }

	// put up the transparent grey shield
	modalWindowOverlayShow();	

	// put it in the middles of the screen
	positionElementInViewportCentre( overlay );

	// put a shim behind the overlay for IE
	//shimShowBehind( overlay );
	
	// display it
	overlay.style.display = "block";
	if (url) {aniteAjaxReq(sticky,url,evalJs)};
}

function greyOutBrowserWindow(){
	var overlay = $( "ModalWindowOverlay" );
	
	if( overlay == null ){
		overlay = modalWindowCreateOverlay();
		greyOutBrowserWindow();
	}
	else{
		var overlayWidth = Math.max(pageSize.width, browserSize.width);
		var overlayHeight = Math.max(pageSize.height, browserSize.height);
		
		alert(overlayWidth);
	}
}

function greyandHide(sticky){
	var overlay = $( sticky );
	// don't try to do anything if we can't find the overlay
	if( overlay == null ){ return; }

	overlay.style.display = "none";
	//shimHide( overlay );
	modalWindowOverlayHide();
	
}

// MODAL OVERLAY FUNCTIONS /////////////////////////////////////////////

function getPageSize(){
	if( (typeof( window.innerHeight ) != "undefined") && (typeof( window.scrollMaxY ) != "undefined") ){
	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
	}

	return {"height": yScroll, "width": xScroll};
}


function modalWindowOverlayShow(){
	var overlay = $( "ModalWindowOverlay" );

	// if the overlay doesn't already exist in the page, then make a new one
	if( overlay == null ){
		overlay = modalWindowCreateOverlay();
	}

	// find the dimensions of the page and the browser window
	var pageSize = getPageSize();
	var browserSize = getBrowserViewportDimensions();
	
	// make the overlay big enough to cover the page (and the browser window in case the page is smaller than the window)
	var overlayWidth = Math.max( pageSize.width, browserSize.width );
	var overlayHeight = Math.max( pageSize.height, browserSize.height );
	
	overlay.style.width = overlayWidth + "px";
	overlay.style.height = overlayHeight + "px";

	// show the overlay
	overlay.style.display = "block";

	// put a shim behind the overlay for IE
	//shimShowBehind( overlay );
}


function modalWindowCreateOverlay(){
	var body = $( document.body );

	new Insertion.Bottom( body, '<div id="ModalWindowOverlay"></div>' );

	return $( "ModalWindowOverlay" );
}



function modalWindowOverlayHide(){
	var overlay = $( "ModalWindowOverlay" );
	overlay.style.display = "none";

	// remove the shim in IE
	//shimHide( overlay );
}

function positionElementFromPageOrigin( element, x, y ){
	// make sure our arguments are nodes, not just IDs
	var element = $( element );

	// find out where the relative parent starts
	var offsetParent = Position.offsetParent( element );
	var offsetParentPosition = Position.cumulativeOffset( offsetParent );

	// convert the desired coordinates into an offset against the
	// relative parent
	var offsetX = x - offsetParentPosition[0];
	var offsetY = y - offsetParentPosition[1];

	element.style.left = offsetX + "px";
	element.style.top  = offsetY + "px";
}

/*Very simple modal overlay*/
 function show_book(id)	{
	document.getElementById("Pax_Offer"+id).style.display="block";
}

function hide_book(id) {
	document.getElementById("Pax_Offer"+id).style.display="none";
}