(function($) {

	$.fn.lightBox = function(settings) {
		settings = jQuery.extend({
			overlayBgColor: 		'#000',		
			overlayOpacity:			0.8,
			auto: 'false'
		},settings);
		var jQueryMatchedObj = this; 
		function _initialize() {
			_start(this,jQueryMatchedObj);
			return false;
		}
		function _start(objClicked,jQueryMatchedObj) {
			$('embed, object, select').css({ 'visibility' : 'hidden' });
			$('div#TellAFriend select').css({'visibility': 'visible'});
			_set_interface();
		}
		function radioFunctions(){
			$j('div.TellAFriend div.frow-radio input').unbind('click').click(function(){
				var productcolor = $j(this).attr("id");
				
				$j('div.TellAFriend div.frow-radio input').each(function(){
					$j(this).parent().parent().parent().parent().removeClass($j(this).attr('id')+'-selected');
					$j('div.product-shot').removeClass($j(this).attr('id'));
					var newImage = 'graphics/uk/components/tell_a_friend/'+$j(this).attr('id')+'text_off.gif'
					$j(this).parent().siblings('span.label').children('img').attr('src', newImage);
				});
				$j(this).parent().parent().parent().parent().addClass($j(this).attr('id')+'-selected');
				$j('div.product-shot').addClass(productcolor);
				var newImage = 'graphics/uk/components/tell_a_friend/'+productcolor+'text_on.gif'
					$j(this).parent().siblings('span.label').children('img').attr('src', newImage);
			});
		}
		
		function _set_interface() {
			$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box">'+$j('div#TellAFriendContainer').html()+'</div></div>');	
			$j('div#TellAFriendContainer').remove();
			radioFunctions();
			var arrPageSizes = ___getPageSize();
			$('#jquery-overlay').css({
				backgroundColor:	settings.overlayBgColor,
				opacity:			settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
			var arrPageScroll = ___getPageScroll();
			$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
			$('div#TellAFriend h1 em').unbind('click').click(function() {
				_finish();									
			});
			
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				var arrPageScroll = ___getPageScroll();
				$('#jquery-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
		}
		
		function _finish() {
			
			$('#content').append('<div id=\'TellAFriendContainer\'>'+$('#lightbox-container-image-box').html()+'</div>');
			$('#TellAFriend h1 em').unbind('click');
			$('#jquery-lightbox').remove();
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
			$('embed, object, select').css({ 'visibility' : 'visible' });
			$j('div#TellAFriendContainer').css({display: 'none'});
			
		}
		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) {	// 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) { 
				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);
		 };
		 
		
		  if(settings.auto == 'true'){
			 _initialize();
		 	}else{
			return this.unbind('click').click(_initialize);
		 }
	};
})(jQuery); 