/**
 * jQuery AjaxBox plugin
 * This jQuery plugin was inspired and based on jquery-lightbox-0.5 by Leandro Vieira Pinho - http://leandrovieira.com
 * @copyright (c) 2009 Epto electronics (http://www.epto.it)
 */

(function($) {
	$.fn.ajaxBox = function(settings) {
		settings = jQuery.extend({
			overlayBgColor: 		'#808080',
			overlayOpacity:			0.1,
			containerBorderSize:	10,
			containerResizeSpeed:	300,
            boxhover:0,
            hrefPath: '',
            hrefTitle:'',
            boxWidth: 400,
            boxHight: 450,
			tlc:'http://images.epto.it/images/tlc.gif',
			tcc:'http://images.epto.it/images/tcc.gif',
			trc:'http://images.epto.it/images/trc.gif',
			brs:'http://images.epto.it/images/brs.gif',
			brc:'http://images.epto.it/images/brc.gif',
			bcc:'http://images.epto.it/images/bcc.gif',
			blc:'http://images.epto.it/images/blc.gif',
			keyToClose:				''
		},settings);

		var jQueryMatchedObj = this;

		function _ajax_initialize() {
			_preload_box_images();
			_start(this,jQueryMatchedObj);
			return false;
		}

		function _start(objClicked,jQueryMatchedObj) {
			$('embed, object, select').css({ 'visibility' : 'hidden' });
            settings.hrefPath=objClicked.getAttribute('href');
            settings.hrefTitle=objClicked.getAttribute('alt');
			_set_interface();
			_resize_container_image_box(settings.boxWidth,settings.boxHight);
		}
		function _set_interface() {

		   $('body').append('<div id="jquery-ajaxbox-overlay"></div><div id="jquery-ajaxbox"><div id="ajaxbox-container-box"><table cellspacing="0" cellpadding="0" border="0" width="100%" height="100%"><tr><td><table border="0" width="100%" cellspacing="0" cellpadding="0"><tr>    <td width="4" valign="top"><img src="http://images.epto.it/images/tlc.gif" border="" alt=""></td>    <td valign="top" width="100%" background="http://images.epto.it/images/tcc.gif"><table border="0" cellpadding="0" cellspacing="0" width="100%" height="29">                     <tr><td align="left" class="main">'+settings.hrefTitle+'</td>                         <td width="60" align="right" class="HomeText"><a class="chiudi" href="#" id="ajaxbox-btnClose" >Chiudi <img src="http://images.epto.it/images/closeico.gif" border="" alt=""></a></td>                     </tr></table></td>    <td width="4" valign="top"><img src="http://images.epto.it/images/trc.gif" border="" alt=""></td>  </tr></table><table border="0" width="100%" cellspacing="0" cellpadding="0" class="">  <tr>'+
'    <td width="1" style="background-image: url(\'http://images.epto.it/images/bls.gif\');background-repeat:repeat-y;background-position:bottom left"><img src="http://images.epto.it/images/pixel_trans.gif" border="" alt="" width="1" height="1"></td>' +
'    <td width="99%" class="infoBoxHeading" height="400"><table border="0" width="100%" height="400" cellspacing="0" cellpadding="2" class="sfobianco">'+
'  <tr>'+
'    <td align="center" class="boxText_s">                    <div id="container-box-contents">'+
'                      <img src="http://images.epto.it/images/loading.gif" border="" alt="">'+
'                    </div></td>  </tr></table></td>    <td width="1" style="background-image: url(\'http://images.epto.it/images/brs.gif\');background-repeat:repeat-y;background-position:bottom left"><img src="http://images.epto.it/images/pixel_trans.gif" border="" alt="" width="1" height="1"></td>'+
'  </tr>  <tr>    <td><img src="http://images.epto.it/images/blc.gif" border="" alt=""></td>    <td width="99%" background="http://images.epto.it/images/bcc.gif"><img src="http://images.epto.it/images/pixel_trans.gif" border="" alt="" width="1" height="1"></td>    <td><img src="http://images.epto.it/images/brc.gif" border="" alt=""></td>'+
'  </tr></table>                  </td>                 </tr>                </table>              </div></div>');


			var arrPageSizes = ___getPageSize();
			$('#jquery-ajaxbox-overlay').css({
				backgroundColor:	settings.overlayBgColor,
				opacity:			settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();

			var arrPageScroll = ___getPageScroll();

			$('#jquery-ajaxbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();

			$('#jquery-ajaxbox-overlay').click(function() {
				_finish();
			});
			$('#ajaxbox-container-box').hover(function() { settings.boxhover=1;},function() { settings.boxhover=0;})
			$('#jquery-ajaxbox').click(function() {
                if(!settings.boxhover){
			      _finish();
			    }
			});

			$('#ajaxbox-btnClose').click(function() {
				_finish();
				return false;
			});

			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#jquery-ajaxbox-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				var arrPageScroll = ___getPageScroll();
				$('#jquery-ajaxbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
		}
		function _resize_container_image_box(intBoxWidth,intBoxHeight) {
			var intCurrentWidth = $('#ajaxbox-container-box').width();
			var intCurrentHeight = $('#ajaxbox-container-box').height();
			var intWidth = (intBoxWidth + (settings.containerBorderSize * 2));
			var intHeight = (intBoxHeight + (settings.containerBorderSize * 2));
			var intDiffW = intCurrentWidth - intWidth;
			var intDiffH = intCurrentHeight - intHeight;
			$('#ajaxbox-container-box').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_ajax(); });
			if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
				if ( $.browser.msie ) {
					___pause(250);
				} else {
					___pause(100);
				}
			}
		};
		function _show_ajax() {
            _enable_keyboard_navigation();
            ajax_loadContent('container-box-contents',settings.hrefPath,'',false);
		};
		function _enable_keyboard_navigation() {
			$(document).keydown(function(objEvent) {
				_keyboard_action(objEvent);
			});
		}
		function _disable_keyboard_navigation() {
			$(document).unbind();
		}
		function _keyboard_action(objEvent) {
			if ( objEvent == null ) {
				keycode = event.keyCode;
				escapeKey = 27;
			} else {
				keycode = objEvent.keyCode;
                if(objEvent.DOM_VK_ESCAPE){
				     escapeKey = objEvent.DOM_VK_ESCAPE;
				}else{
                     escapeKey=27;
				}
			}
			key = String.fromCharCode(keycode).toLowerCase();
			if ( ( key == settings.keyToClose ) || ( keycode == escapeKey ) ) {
				_finish();
			}
		}
		function _preload_box_images() {
            jQuery("<img>").attr("src", settings.tlc);
            jQuery("<img>").attr("src", settings.tcc);
            jQuery("<img>").attr("src", settings.trc);
            jQuery("<img>").attr("src", settings.brs);
            jQuery("<img>").attr("src", settings.brc);
            jQuery("<img>").attr("src", settings.bcc);
            jQuery("<img>").attr("src", settings.blc);
		}
		function _finish() {
			$('#jquery-ajaxbox').remove();
			$('#jquery-ajaxbox-overlay').fadeOut(function() { $('#jquery-ajaxbox-overlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else {
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth;
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) {
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) {
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}
			if(xScroll < windowWidth){
				pageWidth = xScroll;
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		 function ___pause(ms) {
			var date = new Date();
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 };
		return this.unbind('click').click(_ajax_initialize);
	};
})(jQuery);
