$.fn.overlay = function(options)
{
	var jContent, jOverlayBG, jBtn, jEntryContent, jHeaderLine, jHeaderButtons, jMiddle, jMiddleTitle, jLeftCorner, jClose, jCloseBtn, jCloseTxt, jHeader, jHeaderLine2, jRightCorner, jNavBrowser;
	var jFindObj 			= $(this);
	var sDataRel 			= jFindObj.attr('data-rel');
	var sDataRoot 		= jFindObj.attr('data-root');
	var aArray 				= [];
	var iCurrent 			= 0;
	var img						= new Image();
	var bNB						= false;
	var iWindowWidth 	= ($(window).width()/1.1);
	
	var settings = {
		'background-color': '#000000',
		'transparency': '60',
		'width': '800',
		'height': '600',
		'left': '50%',
		'top': '50%',
		'draggable': false,
		'locked': false,
		'navbrowser': true,
		'class': sDataRoot,
		'click': true,
		// Jones - bspw. Kontaktformular DCI Webseite in iFrame -> gr��e genau bekannt -> iframe style overflow = hidden, damit keine Scrollbar
		'overflow-iframe': 'auto',
		'initJSOs' : false,
		'print' : false
	}
	
	if(options) $.extend(settings, options);
	else        options = {}; // Jones - weil sp�ter mal verwendet, damit options.width kein Problem
	
	
	if (!settings.height || (settings.height > (jQuery(window).height() - 100))) settings.height = jQuery(window).height() - 100;
	
	
	$.fn.hideOverlay = function() {
		$(this).click(function() {
			$('.jOverlayBG').fadeOut('fast', function() {
				$(this).remove();
			});
			
			$('.jContent').fadeOut('fast', function() {
				$(this).remove();
			});
		});
		return true;
	}
	
	var aDataTypeConfig = {
		'image' : ['jpg', 'gif', 'png', 'jpeg'],
		'data' : ['pdf']
	};
	
	writeArray();
	var iMax = aArray.length-1;
	
	if(settings['click'] == true) {
		jFindObj.click(function() {
		    initialize();
			return false;
		});
	} else {
	    initialize();
	}

	function loadHTML() {
		var aElements	= createOverlay();
		var jOverlayBG	= aElements['overlay'];
		jContent		= aElements['content'];
		
		var jWrap = $('<div/>').addClass(settings['class']+'Overlay');
		jWrap.append(aArray[iCurrent].clone());
		jEntryContent.append(jWrap);
		showContent();
		
		return [jOverlayBG, jContent];
	}
	
	function loadImage() {
		var aElements	= createOverlay();
		var jOverlayBG	= aElements['overlay'];
		jContent		= aElements['content'];
		
		function loadImg() {
			if(img.complete != true) {
				setTimeout(loadImg, 1000);
			} else {
				jEntryContent.append(img);
				resizeImage(img);
				showImage(img);
				$('.jMiddle').remove();
				$('.load').remove();
			}
		}
		
		img			= new Image();
		img.src	= aArray[iCurrent].attr('href');
		
		if((!img.width || img.width == 0) || img.complete == false) {
			loadImg();
			jEntryContent.append('<img src="http://data.dci-se.de/img/loading_small.gif" class="load"/>');
		}
		else {
			jEntryContent.append(img);
			resizeImage(img);
			showImage(img);
		}
		
		jEntry.append(jEntryContent);
		jContent.append(jEntry);
		return [jOverlayBG, jContent];
	}
	
	function loadSite() {
		var aElements 	= createOverlay();
		var jOverlayBG 	= aElements['overlay'];
		jContent 		= aElements['content'];
		
		// Jones - falls options width angegeben, iwindowWidth �berschreiben!
		//jEntryContent.append('<iframe src="'+aArray[iCurrent].attr('href')+'" style="width:'+(iWindowWidth-26)+'px; height:'+($(window).height()-100)+'px; border:0px;"/>');
		jEntryContent.append('<iframe src="'+aArray[iCurrent].attr('href')+'" style="overflow: '+settings['overflow-iframe']+'; width:'+((options.width ? options.width : iWindowWidth) - 26)+'px; height:'+((options.height ? options.height : $(window).height()) - 100)+'px; border:0px;"/>');
		
		showExtern();
		return [jOverlayBG, jContent];
	}
	
	function createOverlay() {
		jOverlayBG			= $('<div></div>').addClass('jOverlayBG');
		jContent 				= $('<div></div>').addClass('jContent');
		jBtn						= $('<div></div>').addClass('btn');
		jEntry					= $('<div></div>').addClass('jEntry');
		jEntryContent		= $('<div></div>').addClass('jEntryContent');
		jClose					= $('<div></div>').addClass('jClose');
		jCloseBtn				= $('<div></div>').addClass('jCloseBtn');
		jCloseTxt				= $('<div></div>').html(window.oL.close).addClass('jCloseTxt');
		jHeader					= $('<div></div>').addClass('jHeader');
		jHeaderButtons	= $('<div></div>').addClass('header_buttons');
		jLeftCorner			= $('<div></div>').addClass('left_corner');
		jHeaderLine			= $('<div></div>').addClass('header_line');
		jHeaderLine2		= $('<div></div>').addClass('header_line2');
		jRightCorner		= $('<div></div>').addClass('right_corner');
		jMiddle					= $('<div></div>').addClass('jMiddle');
		jMiddleTitle		= $('<div></div>').addClass('jMiddleTitle');

		jBtn.append(jClose);
			jClose.append(jCloseTxt).hideOverlay();
			jClose.append(jCloseBtn).hideOverlay();
		jEntry.append(jEntryContent);
		jContent.append(jEntry);
		jContent.css({
			'left': settings['left'],
			'top': settings['top']
		});
		jOverlayBG.css({
			'filter': 'alpha(opacity='+settings['transparency']+');',
			'-moz-opacity':'0.'+settings['transparency']+'',
			'opacity': '0.'+settings['transparency']+'',
			'background-color': settings['background-color']
		});
		
		if(settings['locked'] != true) jOverlayBG.hideOverlay();
		
		if(aArray.length>1) {
			initNavBrowser();
		} else {
			jHeaderButtons.css({
				'background': 'url("http://data.dci-se.de/img/overlay/landingpage_btn4.png") repeat scroll 0 0 transparent',
				'width': '86px'
			});
			jCloseTxt.css('left', '7px');
			jCloseBtn.css('left', '62px');
			bNB = true;
		}
		
		var aElements = {
			'overlay': jOverlayBG,
			'content': jContent
		}
		return aElements;
	}
	
	function writeArray() {
		if(jFindObj.attr('data-root') !== undefined && jFindObj.attr('data-rel') !== undefined) {
				jFindObj.parents('.'+sDataRoot).find('.'+sDataRel).each(function() {
					aArray.push($(this));
				});
				if(aArray.length>1) {
					if(jFindObj.hasClass(sDataRel) == true)	iCurrent = jFindObj.parents('.'+sDataRoot).find('.'+sDataRel).index(jFindObj);
					else									iCurrent = jFindObj.parents('.'+sDataRoot).find('.'+sDataRel).index(jFindObj.parents('.'+sDataRel));
				}
				else
					iCurrent = 0;
		}
		else {
			var jTmp = $('<div></div>').append(jFindObj.clone().show());
			jTmp.children('*').each(function() {
				aArray.push($(this));
			});
			iCurrent			= 0;
			settings['click']	= false;
		}
	}
	
	function initialize(bSwitcher) {
		var sHref			= aArray[iCurrent] && aArray[iCurrent].attr('href') ? aArray[iCurrent].attr('href') : '' ;
		var aHref			= sHref.toLowerCase().split('.');

		if(sHref == '' || sHref == '#')
			var aFunction = loadHTML();
		else if($.inArray(aHref[aHref.length-1], aDataTypeConfig['image']) != -1)
				var aFunction = loadImage();
		else //if ($.inArray(aHref[aHref.length-1], aDataTypeConfig['data']) != -1 || sHref.search('^(http:\\/\\/|https:\\/\\/)?([\\-0-9a-zA-Z]+\\.)+[a-zA-Z]+(\\/[\\?%\\\\,!#\\+;=&:\\.\\-_0-9a-zA-Z]*)*$') != -1)
			var aFunction = loadSite();
		
		if(settings['draggable'] == true) {
			jContent.draggable();
			jContent.css({'position': 'absolute', 'top': '50%'});
		}
		
		if(settings['navbrowser'] == false) {
			jHeader.find('.header_buttons').css({
				'background': 'url("http://data.dci-se.de/img/overlay/landingpage_btn4.png") repeat scroll 0 0 transparent',
				'width':'86px'
			});
			
			
			if(!options['width'] || !options['height'] || !options['width'] && !options['height']) {
				jHeaderLine.css('width', (iWindowWidth-102)+'px');
				jContent.css({
					'width': iWindowWidth+'px',
					'height': ($(window).height()-80)+'px'
				});
				jHeader.css({
					'width' : iWindowWidth+'px'
				});
				jEntryContent.css({
					'width': (iWindowWidth-25)+'px',
					'height': ($(window).height()-80)+'px'
				});
			} else {
				jHeaderLine.css('width', (settings['width']-77)+'px');
				jContent.css({
					'width': settings['width']+'px',
					'height': settings['height']+'px'
				});
				jHeader.css({
					'width': (parseInt(settings['width'])+22)+'px'
				});
				jEntryContent.css({
					'width': settings['width']+'px',
					'height': settings['height']+'px'
				});
			}
			
			jCloseBtn.css('left', '62px');
			jCloseTxt.css('left', '6px');
		}
		
		if(bSwitcher == true) {
			$('.jContent').fadeOut('fast', function() {
				$(this).remove();
					aFunction[1].appendTo('body').css({
						'margin-left': '-'+(aFunction[1].width()/2)+'px',
						'margin-top': '-'+(aFunction[1].height()/2)+'px'
					}).hide().fadeIn('fast');
			});
		}
		else {
			$.each(aFunction, function()
			{
				$(this).appendTo('body').css({
					'margin-left': '-'+(aFunction[1].width()/2)+'px',
					'margin-top': '-'+(aFunction[1].height()/2)+'px'
				}).hide().fadeIn('fast');
			});
		}
	}
	
	function initNavBrowser() {
		jNavBrowser		= $('<div></div>').addClass('jNavBrowser');
		var jInfo		= $('<div></div>').addClass('jsc_navBrowserInfo').text((iCurrent+1)+'/'+aArray.length);
		
		var jPrevButton = $('<a></a>').attr('href', '#').addClass(iCurrent > 0 ? 'jsc_active_left' : 'jsc_inactive_left').addClass('jsc_navBrowserPrev').click(function() {
			if(iCurrent > 0 ) {
				iCurrent--;
				jInfo.text((iCurrent+1)+'/'+aArray.length);
				if(iCurrent == 0) $(this).removeClass('jsc_active_left').addClass('jsc_inactive_left');
				if(iCurrent == aArray.length-1) jNextButton.removeClass('jsc_inactive_left').addClass('jsc_active_left');
				initialize(true);
			}
			return false;
		}).appendTo(jNavBrowser);
		
		var jNextButton = $('<a></a>').attr('href', '#').addClass(iCurrent < (aArray.length-1) ? 'jsc_active_right' : 'jsc_inactive_right').addClass('jsc_navBrowserNext').click(function() {
			if(iCurrent < (aArray.length-1) ) {
				iCurrent++;
				jInfo.text((iCurrent+1)+'/'+aArray.length);
				if(iCurrent == aArray.length-1) $(this).removeClass('jsc_active_right').addClass('jsc_inactive_right');
				if(iCurrent == 1) jPrevButton.removeClass('jsc_inactive_right').addClass('jsc_active_right');
				initialize(true);
			}
			return false;
		}).appendTo(jNavBrowser);
		
		jNextButton.before(jInfo);
		jBtn.prepend(jNavBrowser);
		
	}
	
	function resizeImage(img) {
		var iWwidth = $(window).width();
		var iWheight = $(window).height();
		
		if(img.width > iWwidth || img.height > iWheight) {
			var iRatio = img.width/img.height;
			
			while(img.width > iWwidth || img.height > iWheight) {
				img.width--;
				img.height = img.width / iRatio;
			}
			
			if(img.width > img.height) {
				img.width = img.width - 100;
				img.height = img.width / iRatio;
			}
			else {
				img.height = img.height - 100;
				img.width = img.height * iRatio;
			}
			jContent.css({
				'width': (img.width+22)+'px',
				'height': img.height+'px',
				'margin-left': '-'+(img.width/2)+'px',
				'margin-top': '-'+(img.height/2)+'px'
			});
			jEntryContent.css({
				'width': img.width+'px'
			});
			jHeaderLine.css({
				'width': (img.width-151)+'px'
			});
		}
	}
	
	function showImage(img) {
	    
	    var sTitle = aArray[iCurrent].attr('title');
	    if(sTitle != '') jEntryContent.append('<div style="margin-top:10px;">'+sTitle+'</div>');
	    
		jContent.css({
			'width': (img.width+22)+'px',
			'height': img.height+'px',
			'margin-left': '-'+(img.width/2)+'px',
			'margin-top': '-'+(img.height/2)+'px'
		});
		jHeader.css({
			'position': 'relative',
			'top': '-14px',
			'width': (img.width+22)+'px'
		});
		
		if(bNB == true) jHeaderLine.css({'width': (img.width-77)+'px'});
		else            jHeaderLine.css({'width': (img.width-151)+'px'});
		
		appendElems();
	}
	
	function showContent() {
		jContent.css({
			'width': settings['width']+'px',
			'height': settings['height']+'px',
			'margin-left': '-'+(settings['width']/2)+'px',
			'margin-top': '-'+(settings['height']/2)+'px'
		});
		jEntry.css({
			'overflow': 'auto',
			'width': settings['width']+'px',
			'height': settings['height']+'px',
			'position': 'relative',
			'top': '-17px'
		});
		jHeader.css({
			'position': 'relative',
			'top': '-14px',
			'width': settings['width']+'px'
		});
		
		if(bNB==false)	jHeaderLine.css({'width': (settings['width']-173)+'px'});
		else			jHeaderLine.css({'width': (settings['width']-99)+'px'});

		appendElems();
		jContent.find('.'+sDataRel+'Overlay').find('.jso_overlay').css('display','none');
	}
	
	function showExtern() {
		var iWindowHeight = ($(window).height()-80);
		
		// Jones - falls options.height usw �bergeben, das nehmen!
		var iThisHeight = options.height ? options.height : iWindowHeight;
		var iThisWidth =  options.width  ? options.width  : iWindowWidth;
		
		jContent.css({
			'width': iThisWidth+'px',
			'height': iThisHeight+'px',
			'margin-left': '-'+(iThisWidth/2)+'px',
			'margin-top': '-'+(iThisHeight/2)+'px'
		});
		jHeader.css({
			'position': 'relative',
			'top': '-14px',
			'width': iThisWidth+'px'
		});
		
		if(bNB==false)	jHeaderLine.css({'width': (iThisWidth-173)+'px'});
		else			jHeaderLine.css({'width': (iThisWidth-99)+'px'});
		
		appendElems();
	}
	
	function appendElems() {
		jHeader.append(jLeftCorner);
		jHeader.append(jHeaderLine);
		jHeader.append(jHeaderButtons);
		jHeaderButtons.append(jBtn);
		jHeader.append(jHeaderLine2);
		jHeader.append(jRightCorner);
		jHeader.appendTo(jContent);
		jEntry.append(jEntryContent);
		jContent.append(jEntry);
		
	    if(settings['print'] && settings['print'] == true) {
	        jEntryContent.prepend('<div class="entriesPdfWrapper"><a target="_blank" href="#" class="jso_EntriesPDF jsc_actionButton" title="" style="margin: 0pt 20px 0pt 0pt; background-image: url(&quot;http://data.dci-se.de/img/actionButton-bg_x.png&quot;);"><img alt="" src="http://data.dci-se.de/img/bullets/actions/pdf.png" class="jsc_bullet"><span>' + window.oL.save_as_pdf + '</span><img src="http://data.dci-se.de/img/actionButton-left.png" style="left: -5px;"><img src="http://data.dci-se.de/img/actionButton-right.png" style="margin-right: 0px !important; right: -5px;"></a></div>');    
	    }
		
		if(settings['initJSOs'] && settings['initJSOs'] == true && window.aes) {
		    window.aes.initElements(jEntry, '', 2);    
		}
	}

	return this;
}
