(function($) {
	$.takeOver = function(target, options) {
		var defaults = {
			div		: '',
			width	: 0,
			height	: 0,
			pad		: 5,
			toHtml	: '\
					<div id="takeOver"> \
					  <div id="to_top"> \
					    <div class="to_corner tl"></div> \
					    <div class="to_mid"></div> \
					    <div class="to_corner tr"></div> \
					  </div> \
					  <div id="to_mid"> \
					    <div class="to_vMid"></div> \
					    <div id="to_content"></div> \
					    <div class="to_vMid"></div> \
					  </div> \
					  <div id="to_bottom"> \
					    <div class="to_corner bl"></div> \
					    <div class="to_mid"></div> \
					    <div class="to_corner br"></div> \
					  </div> \
					</div>',
			bgcolor	: '#FFF',
			dimColor: '#000',
			dim		: true,
			dimLevel : 70,
			border: true,
			delay: 0,
			close_btn: true,
			close_img: '',
			closePad: 0,
			top: 0,
		};
		if (typeof(options) == 'object') {
			$.takeOver.config = $.extend(defaults, options);
		} else {
			$.takeOver.config = defaults;	
		}
		$.takeOver.config.div = target;
		if ($.takeOver.config.dim) $.takeOver.blackOut();
		else $.takeOver.display();
		$(window).bind("resize", function() {
			if ($.takeOver.config.height < $(window).height()) {
				if ($.takeOver.config.dim) $("#toBlackout").css("height", $(window).height() );
			} else {
				if ($.takeOver.config.dim) $("#toBlackout").css("height", $.takeOver.config.height() + $.takeOver.config.top + 20);
			}
			$.takeOver.align();
		});
		return this;
	}
	
	$.takeOver.blackOut = function() {
		$('<div id="toBlackout"></div>').appendTo('body');
		var h = $(document).height();
		if (($.takeOver.config.height + $.takeOver.config.top + 20) > $(document).height()) {
			h = $.takeOver.config.height + $.takeOver.config.top + 20;
		}
		$('#toBlackout').addClass('toBlackout').css({'height': h, 'background-color': $.takeOver.config.dimColor }).stop().animate({
													  opacity: $.takeOver.config.dimLevel / 100
													}, 300, function() {
														$.takeOver.displayto();
													});
	}
	
	$.takeOver.hideBorder = function() {
		$('.to_corner', '#takeOver').removeClass('tl tr bl br');
		$('.to_vMid', '#takeOver').css({'background-image':"url('')"});
		$('.to_mid', '#takeOver').css({'background-image':"url('')"});
		
	}
	
	$.takeOver.displayto = function() {
		$('body').append($.takeOver.config.toHtml);
		$('#takeOver').css( { opacity: 0 } );
		$('#takeOver #to_content').css( { backgroundColor: $.takeOver.config.bgcolor } );
		if (!$.takeOver.config.border) $.takeOver.hideBorder();
		var h = $.takeOver.config.height;
		if ($.browser.msie) {
			h = $.takeOver.config.height + $.takeOver.config.top + 20;
		}
		if ($('#'+$.takeOver.config.div).attr('href') != undefined) {
			$.takeOver.size( $.takeOver.config.width, $.takeOver.config.height);
			$('<iframe src="'+$('#'+$.takeOver.config.div).attr('href')+'" width="'+$.takeOver.config.width+'" height="'+h+'" scrolling="no" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>').appendTo('#takeOver #to_content');
		} else {
			var t = $('#'+$.takeOver.config.div).clone().appendTo('#takeOver #to_content');
			$.takeOver.size( $('#'+$.takeOver.config.div).outerWidth(), $('#'+$.takeOver.config.div).outerHeight());
		}

		$.takeOver.align();
		if ($.takeOver.config.close_btn) $.takeOver.closeBtn();
		//FADE IN - NOT IE FUNCTIONAL 
		
		$('.to_corner', '#takeOver').fixPNG('scale', true);
		$('.to_vMid', '#takeOver').fixPNG('scale', true);
		$('.to_mid', '#takeOver').fixPNG('scale', true);
		$('#takeOverCloseBtn .image').fixPNG('scale', true);
		$('#takeOver').stop().delay($.takeOver.config.delay).animate( {
				opacity: 1.0
			}, 300, function() {
				$('#takeOverCloseBtn').stop().animate( {opacity: 1.0}, 300);
				if (t != undefined) {
					if (t.css('display') == 'none') {
						t.css( { opacity: 0 } );
						t.css('display', 'inherit');
						t.animate( { opacity: 1.0 }, 300 );
					}
				}
				if ($.isFunction($.takeOver.config.cb)) {
					var obj = new Object();
					$.takeOver.config.cb.call( this, null, '#takeOver #to_content #'+$.takeOver.config.div );
				}
		});
		
		
		/* ZERO FADE IN*/
		/*
if ($.isFunction($.takeOver.config.cb)) {
			var obj = new Object();
			$.takeOver.config.cb.call( this, null, '#takeOver #to_content #'+$.takeOver.config.div );
	    }
*/
	}
	
	$.takeOver.size = function(w, h) {
		var cw = w;
		var ch = h;

		$('#takeOver').css('width', (cw+20)+'px');
		$('#takeOver #to_top').css('width', (cw+20)+'px');
		$('#takeOver #to_mid').css('width', (cw+20)+'px');
		$('#takeOver #to_mid .to_vMid').css('height', ch+'px');
		$('#takeOver #to_bottom').css('width', (cw+20)+'px');
		$('#takeOver .to_mid').css('width', cw+'px');
		$('#takeOver #to_mid #to_content').css('width', cw+'px');
		$('#takeOver #to_mid #to_content').css('height', ch+'px');
	}
	
	$.takeOver.align = function() {
		var cw = $('#'+$.takeOver.config.div).outerWidth();
		var ch = $('#'+$.takeOver.config.div).outerHeight();
		var ph = $(document).height();
		var pw = $(document).width();
		var st = $('body').scrollTop();
		if (st == 0) {
			st = $('html').scrollTop();
		}
		if ($.takeOver.config.top > 0) {
			//if (ph > ch) {
				var t = Math.floor( $(window).height() / 2 ) - Math.floor( $('#takeOver').height()/2 ) ;
			    $('#takeOver').css('top', st+t);
			//}
		} else {
			$('#takeOver').css('top', st);
		}
		if (!('left' in $.takeOver.config)) {
		    if (pw > cw) {
		    	var l = Math.floor( ($(document).width() / 2) - ( ($('#takeOver #to_content').width() / 2) + 20) );
				$('#takeOver').css('left', l);
		    }
		} else {
			$('#takeOver').css('left', $.takeOver.config.left);
		}
		if ($('#takeOverCloseBtn').length > 0) $.takeOver.closeBtn();
	}
	
	$.takeOver.closeBtn = function() {
		var cw = 0;
		if ($.takeOver.config.width > 0) {
			cw = $.takeOver.config.width;
		} else {
			cw = $('#'+$.takeOver.config.div).outerWidth();
		}
		if (!$.takeOver.config.border) {
			cw = cw - 16;
		}
		if (!$('#takeOverCloseBtn').length > 0) {
			$('body').append('<div id="takeOverCloseBtn"><div class="image"></div></div>');
			if ($.takeOver.config.close_img > '') {
				$('#takeOverCloseBtn .image').css({'background-image':"url('"+$.takeOver.config.close_img+"')"});
			}
			$('#takeOverCloseBtn').css( { opacity: 0 } );
			$('#takeOverCloseBtn').hover(function() {
				$('#takeOverCloseBtn').css( { 'cursor': 'pointer' } );
			},
			function() {
				$('#takeOverCloseBtn').css( { 'cursor': 'arrow' } );
			});
		}
		$('#takeOverCloseBtn').css('zIndex', parseInt($('#takeOver').css('zIndex'))+1);
		$('#takeOverCloseBtn').css('left', parseInt($('#takeOver').css('left')) + cw + $.takeOver.config.closePad);
		var toffset = ($.takeOver.config.border ? 21:0);
		if ($.browser.msie) {
			toffset = toffset + 10;
		}
		$('#takeOverCloseBtn').css('top', parseInt($('#takeOver').css('top')) - toffset);
		$('#takeOverCloseBtn').click($.takeOver.close);
	}
	
	$.takeOver.close = function() {
		//$('#takeOver #to_content').first('div').css('display', 'none');
		if ($.takeOver.config.dim) $('#toBlackout').stop().animate( { opacity: 0 }, 300);
		$('#takeOverCloseBtn').stop().animate( { opacity: 0 }, 300);
		$('#takeOver').stop().animate( {
				opacity: 0
			}, 300, function() {
				if ($.isFunction($.takeOver.config.ecb)) {
					$.takeOver.config.ecb.call( this, null, '#takeOver #to_content #'+$.takeOver.config.div );
				}
				$('#takeOverCloseBtn').remove();
				$('#takeOver').remove();
				if ($.takeOver.config.dim) $('#toBlackout').remove();
		});
	}
})(jQuery);
