/***************************************************************
*  Copyright notice
*
*  (c) 2009 Markus Friedrich (markus.friedrich (at) dkd.de)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * JS library for 'ml_maps' extension.
 *
 * @author	Markus Friedrich <markus.friedrich (at) dkd.de>
 * @package	TYPO3
 * @subpackage	tx_mlmaps
 */
var tx_mlmaps = {
	/**
	 * Used to adjust the horizontal position of the layers
	 * 
	 * @var	integer		correctX 
	 */
	correctX: 15,
	
	/**
	 * Used to adjust the vertical position of the layers
	 * 
	 * @var	integer		correctY 
	 */
	correctY: 15,

	/**
	 * The jQuery library
	 * 
	 * @var	object		jQuery
	 */
	jQuery: {},

	/**
	 * The current layer
	 * 
	 * @var	object		layer
	 */
	layer: null,



	/**
	 * Returns the window height
	 * 
	 * @return	integer		the window height
	 */
	getWindowHeight: function() {
		var height = 0;
		
		
		if (window.innerHeight) {
			height = window.innerHeight + window.pageYOffset;
		} else if (document.body) {
			height = document.body.offsetHeight;
			
			if(document.documentElement.scrollTop > 0) {
				height += document.documentElement.scrollTop;
			} else if (document.body.scrollTop > 0) {
				height += document.body.scrollTop;
			}
		} 
		
		return parseInt(height);
	},



	/**
	 * Returns the window width
	 * 
	 * @return	integer		the window width
	 */
	getWindowWidth: function() {
		var width = 0;
		
		if (window.innerWidth) {
			width = window.innerWidth + window.pageXOffset;
		} else if (document.body) {
			width = document.body.offsetWidth;
			
			if(document.documentElement.scrollLeft > 0) {
				width += document.documentElement.scrollLeft;
			} else if (document.body.scrollLeft > 0) {
				width += document.body.scrollLeft;
			}
		} 
		
		return parseInt(width);
	},



	/**
	 * Hides the given layer
	 * 
	 * @param	string		layer: the layer id
	 * @return	void
	 */
	hideLayer: function(layer) {
		if (this.layer !== null) {
			this.layer.css('visibility', 'hidden');
			this.layer.css('top', 0);
			this.layer.css('left', 0);
			this.layer = null;
		}
	},



	/**
	 * Initializes 
	 * 
	 * @return	void
	 */
	init: function() {
		this.correctX = parseInt(this.correctX);
		this.correctY = parseInt(this.correctY);
		
		this.jQuery().mousemove(function(e){
			tx_mlmaps.updatePosition(e.pageX, e.pageY);
		}); 
	},


	/**
	 * Shows the given layer
	 * 
	 * @param	string		layer: the layer id
	 * @return	void
	 */
	showLayer: function(layer) {
		this.layer = this.jQuery('#' + layer);
		this.layer.css('visibility', 'visible');
	},
	
	
	
	/**
	 * Updates the layers position
	 * 
	 * @param	integer		x
	 * @param	integer		y
	 * @return	void
	 */
	updatePosition: function(x,y) {
		if (this.layer !== null) {
			
			if (this.getWindowHeight() > (y + this.correctY + this.layer.height())) {
				this.layer.css('top', (y + this.correctY));
			} else {
				this.layer.css('top', (y - this.correctY - this.layer.height()));
			}

			if (this.getWindowWidth() > (x + this.correctX + this.layer.width())) {
				this.layer.css('left', (x + this.correctX));
			} else {
				this.layer.css('left', (x - this.correctX - this.layer.width()));
			}
		}
	}
}