/**
 * jQuery smartBox plugin
 * This jQuery plugin was inspired and based on Lightbox 2 by Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)
 * This jQuery plugin was inspired and based on jquery-lightbox-0.5.js by Leandro Vieira Pinho (http://leandrovieira.com)
 * @author T.M. - http://www.wirdesign.de
 */

(function($) {
	$.fn.smartBox = function(settings) {
		settings = jQuery.extend({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			slideDownTime: 250,
			imageLoading: '/_layout/images/loading.gif',
			spaceToBottom: 60,
			textClose: 'schliessen',
			imageClose: '/_layout/images/btn_close.gif',
			errorHTML: '<h1>Fehler 404</h1><p class="error">Die Seite kann nicht geladen werden!</p>'
		},settings);
		function _initialize() {
			_start(this);
			return false;
		}
		function _start(objClicked) {
			$('embed, object, select').css({ 'visibility' : 'hidden' });
			_set_interface();
			var loadURL = objClicked.getAttribute('href');
			$("#smartcontent").load(loadURL+' #popframe', function(response, status, xhr) {
				if (status == "error") {
					$('#smartcontent').css({height: 100});					
					$('#smartcontent').html(settings.errorHTML);
				}
				if (status == "success") {
    			var arrPageSizes = ___getPageSize();
		    	var arrPageScroll = ___getPageScroll();
					var objOffset = $(".popwrapper").offset();
					var maxHeight = arrPageSizes[3] - objOffset.top + arrPageScroll[1] - settings.spaceToBottom;
					if ( $('.popwrapper').height() > maxHeight ) {$('.popwrapper').css({height: maxHeight});}
				}
			});
		}
		function _set_interface() {
			$('body').append('<div id="smart-overlay"></div><div id="smart-frame"><div id="smartbox"><div id="smarttop">&nbsp; &#160;</div><div id="smartcontent"><div id="smartbox-loading"><img src="'+settings.imageLoading+'"></div></div><div id="smartbutton"><div id="smartbox-close"><img src="'+settings.imageClose+'" alt="'+settings.textClose+'" /></div></div><div id="smartbottom">&nbsp; &#160;</div></div></div>');	
			var arrPageSizes = ___getPageSize();
			var arrPageScroll = ___getPageScroll();
			$('#smart-overlay').css({
				backgroundColor: settings.overlayBgColor,
				opacity: settings.overlayOpacity,
				width: arrPageSizes[0],
				height: arrPageSizes[1]
			}).fadeIn();
			$('#smart-frame').css({
				top: arrPageScroll[1] + (arrPageSizes[3] / 20),
				left:	arrPageScroll[0]
			}).show();
			$('#smartbox').slideDown(settings.slideDownTime);
			$('#smartbox-close').click(function() {
				_finish();
				return false;
			});
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#smart-overlay').css({
					width: arrPageSizes[0],
					height: arrPageSizes[1]
				});
				var arrPageScroll = ___getPageScroll();
				$('#smart-frame').css({
					top: arrPageScroll[1] + (arrPageSizes[3] / 20),
					left:	arrPageScroll[0]
				});
			});
		}
		function _finish() {
			$('#smart-frame').remove();
			$('#smart-overlay').fadeOut(function() { $('#smart-overlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
		}
		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // all but Explorer Mac
				xScroll = document.documentElement.scrollWidth;
				yScroll = document.documentElement.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
		 /**
		  * Stop the code execution from a escified time in milisecond
		  *
		  */
		 function ___pause(ms) {
			var date = new Date(); 
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 };
		// Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
		return this.unbind('click').click(_initialize);
	};
})(jQuery);
