
jQuery.fn.extend({
	over: function(bg) {
		return this.each(function() { 

			$(this).click(function(e) {
				e.preventDefault();
				
				if(!bg) { bg = '#000'; }
				$(this).blur();

				if($('.textboxOverlay').length < 1) {
					$('body').append('<div class="textboxOverlay"></div>');
					$('.textboxOverlay').css({ width: '100%', height: '100%', display: 'none', position: 'absolute', zIndex: 99 }).animate({ opacity: 0 }, 0);
					$('.textboxOverlay').click(function() {
						$(this).fadeOut().animate({ opacity: 0 }, 0);
						if($('#textbox').length > 0) {
							$('#textbox').fadeOut().html('').dequeue();
						}
						if($('.textboxTitle').length > 0) { 
							$('.textboxTitle').fadeOut().animate({ opacity: 0 }, 0).html(''); 
						}
						if($('.closeImg').length > 0) {
							$('.closeImg').fadeOut();
						}
					});
				}
				if($('.closeImg').length < 1) {
					$('body').append('<img src="/js/textbox/textboxClose.png" class="closeImg" />');
					$('.closeImg').hide().css({ cursor: 'pointer', position: 'absolute', zIndex: 102 });
					$('.closeImg').click(function() { $('.textboxOverlay').trigger('click'); });
				}
				//$('.textboxOverlay').queue('iBox');
				$('.textboxOverlay').css({ width: $(document).width() + 'px', height: $(document).height() + 'px', top: 0, left: 0, background: ''+bg+'' });
				$('.textboxOverlay').show().animate({ opacity: 0.8 }, 'slow');
				
			});

		});
	},
	iBox: function(padding) {
		return this.each(function() { 
			$(this).click(function(e) {
				e.preventDefault();
				if(!padding) { padding = 20; }
				
				var src = $(this).attr('href');
				var title = $(this).attr('title');
				var alt = $(this).find('img:first-child').attr('alt');
				$(this).blur();
				
				if(title == "") {
					if(alt != "") { title = alt; }
				} else { title = title; }

				if(title != "" && $('.textboxTitle').length < 1) {
					$('body').append('<div class="textboxTitle"></div>');
					$('.textboxTitle').css({ position: 'absolute', zIndex: 110, width: $(document).width() + 'px', padding: '0', fontFamily: 'Arial', color: '#fff', fontSize: '20px', textAlign: 'center', background: '#000', borderBottom: '1px solid #ccc' }).animate({opacity: 0}, 0);
				}
				
				if($('#textbox').length < 1) { $('body').append('<div id="textbox"></div>'); }
				if($('.textboxOverlay').length < 1 || $('.textboxOverlay').css('display') == 'none') {
					if($('.textboxClose').length < 1) { $('body').append('<div class="textboxClose"></div>'); }
					$('.textboxClose').css({ width: $(document).width() + 'px', height: $(document).height() + 'px', top: 0, left: 0, position: 'absolute', zIndex: 99 }).show();
					$('.textboxClose').click(function() {
						$(this).fadeOut();
						$('#textbox').fadeOut().html(''); 
					});
				}
				
				var bg = $('#textbox').css('background');

				$('#textbox').css({ top: ((($(window).height() - $('#textbox').height()) / 2) + $(document).scrollTop()) + 'px', left: (($(window).width() - $('#textbox').width()) / 2) + 'px' });
				$('#textbox').css('background', '').addClass('loading').animate({opacity: 1}, 0).fadeIn('fast');
				var img = new Image();
				$(img).load(function() {
					$(this).hide();
					$('#textbox').html('').append(this).removeClass('loading');
					$('#textbox').css('background', bg)
					var w = $(this).width();
					var h = $(this).height();
					//var padding = 30;
					$('#textbox').animate({
						width: (w + padding) + 'px',
						height: (h + padding) + 'px',
						top: ((($(window).height() - (h + padding)) / 2) + $(document).scrollTop()) + 'px', 
						left: (($(window).width() - (w + padding)) / 2) + 'px'
					}, 700, '', function() {
						$(img).css('margin', (($('#textbox').height() - h) / 2) + 'px auto');
						$(img).fadeIn();
						var oSet = $(this).offset();
						$('.closeImg').css({ left: (oSet.left + $(this).width() - ($('.closeImg').width() / 2)) + 'px', top: (oSet.top - ($('.closeImg').height() / 2)) + 'px' }).fadeIn();
						$('.textboxTitle').append(title).css({ top: $(document).scrollTop() + 'px' }).show().animate({opacity: .7 }, 'fast');
					});
				}).attr('src', src).stop();
			});
		});
	},
	box: function(padding) {
		return this.each(function() { 
			$(this).click(function(e) {
				e.preventDefault();
				if(!padding) { padding = 20; }
				
				var src = $(this).attr('href');
				$(this).blur();

				if($('#textbox').length < 1) { $('body').append('<div id="textbox"></div>'); }
				if($('.textboxOverlay').length < 1 || $('.textboxOverlay').css('display') == 'none') {
					if($('.textboxClose').length < 1) { $('body').append('<div class="textboxClose"></div>'); }
					$('.textboxClose').css({ width: $(document).width() + 'px', height: $(document).height() + 'px', top: 0, left: 0, position: 'absolute', zIndex: 99 }).show();
					$('.textboxClose').click(function() {
						$(this).fadeOut();
						$('#textbox').fadeOut().html('');
					});
				}

				$('#textbox').css({ top: ((($(window).height() - $('#textbox').height()) / 2) + $(document).scrollTop()) + 'px', left: (($(window).width() - $('#textbox').width()) / 2) + 'px' });
				$('#textbox').animate({opacity: 1}, 0).fadeIn('fast');

				$('#textbox').append('<div class="textboxContent" style="display: none"></div>');
				$.get("ajax.php?sivu=1", function(data) {
					if(data != "") {
						$('.textboxContent').hide().html(data);
						$('.textboxContent').animate({opacity: 0}, 1000, function() {
							$(this).animate({opacity: 1}, 1);

						$('.loading').remove();
						var w = $('.textboxContent').width();
						var h = $('.textboxContent').height();

						if(w < 200) { w = 200 - padding; }
						if(h < 200) { h = 200 - padding; }

						$('#textbox').animate({
							width: (w + padding) + 'px',
							height: (h + padding) + 'px',
							top: ((($(window).height() - (h + padding)) / 2) + $(document).scrollTop()) + 'px', 
							left: (($(window).width() - (w + padding)) / 2) + 'px'
						}, 700, '', function() {
							$('.textboxContent').css('margin', (($('#textbox').height() - h) / 2) + 'px auto');
							$('.textboxContent').fadeIn();
							var oSet = $(this).offset();
							$('.closeImg').css({ left: (oSet.left + $(this).width() - ($('.closeImg').width() / 2)) + 'px', top: (oSet.top - ($('.closeImg').height() / 2)) + 'px' }).fadeIn();
						});

						});
					}
				});

			});
		});
	}
});
