/*
 * customized by Igor Gladkov (igor.gladkov@gmail.com)
 * from Thickbox 3.1 (by Cody Lindley (http://www.codylindley.com))
 *
 * 1.1: content logic updated, no clonning needed anymore, to preserve the ids of the elements
 * 2.0: code rewrite, scoping added
*/

;(function($)
{
	$.extend({
		tb_params: null,
		
		tb_show: function(params) //function called when the user clicks on a thickbox link
		{
			params = $.extend({
				title: '',
				height: 630,
				width: 440,
				scope: $('body'),
				modal: false,
				showTitle: true,
				structure: '<div id="tb_overlay"></div><div id="tb_window"><div id="tb_title"><div id="tb_window_title"></div><div id="tb_close_window"><a href="#" id="tb_close_window_button">close</a> or Esc Key</div></div><div id="tb_content"></div></div>',
				url: null,
				contentID: null,
				contentParentID: null,
				widget: null
			}, params);
			
			if(params.contentID) {
				params.contentParentID = $('#' + params.contentID).parent().attr('id');
			}
			
			try {
				if(document.getElementById('tb_overlay') === null && document.getElementById('tb_window') === null) {
					$('body').append(params.structure);
				}
				
				if($.browser.msie && parseInt($.browser.version) < 7 && typeof window['XMLHttpRequest'] == 'undefined') { //if IE
					$('html').css('overflow', 'hidden');
					$('html, body').css({
						height: '100%',
						width: '100%'
					});
					
					if($('#tb_hideselect').length == 0) { //iframe to hide select elements in ie6
						$('body').append('<iframe id="tb_hideselect"></iframe>');
					}
				
					$(window).resize(function(e) {
						$('#tb_overlay').width($(document).width());
						$('#tb_hideselect').width($(document).width());
						$('#tb_window').css({
							left : ($('html').get(0).clientWidth / 2 - $('#tb_window').width() / 2 + $('html').get(0).scrollLeft) + 'px'
						});
					});
				}
				
				// if FF mac
				if($.browser.firefox && navigator.userAgent.toLowerCase().indexOf('mac') >= 0) {
					$('#tb_overlay').addClass('MacFFBGHack'); //use png overlay so hide flash
				}
				
				$('#tb_content').width(params.width).height(params.height);
				
				if(params.modal) {
					$('#tb_content').addClass('tb_modal');
					$('#tb_close_window').hide();
				}
				else {
					$('#tb_close_window_button, #tb_overlay').click(function(e) {
						$.tb_remove();
					});
					
					if($('#tb_hideselect').length) {
						var frameDoc = $('#tb_hideselect')[0].contentDocument
							|| ($('#tb_hideselect')[0].contentWindow ? $('#tb_hideselect')[0].contentWindow.document : window.frames['tb_hideselect'].document);
						
						$(frameDoc).click(function(e) {
							$.tb_remove();
						});
					}
					
					$(document).keyup(function(e) {
						if((e.keyCode || e.which) == 27) { // close
							$.tb_remove();
						}
					});
				}
				
				if(params.showTitle) {
					$('#tb_window_title').html(params.title);
					$('#tb_title').show();
				}
				
				$('#tb_window').css({
					width: params.width + 'px'
				});
				
				if(!($.browser.msie && parseInt($.browser.version) < 7 && typeof window['XMLHttpRequest'] == 'undefined')) { // take away IE6
					$('#tb_window').css({
						marginLeft: '-' + parseInt((params.width / 2), 10) + 'px',
						marginTop: '-' + parseInt((params.height / 2), 10) + 'px'
					});
				}
				
				$('#tb_overlay').css('opacity', 0).fadeTo(600, 0.75, function() {
					$('#tb_window').show();
				});
				
				if(params.widget) {
					widget.load(params.widget, $('#tb_content'), params.widgetParams);
				}
				else if(params.contentID) {
					$('#' + params.contentID).appendTo($('#tb_content')).show();
				}
				else if(params.url) {
					// add loading animation
					$('#tb_content').addClass('tb_loading').append('<div id="tb_content_loading"></div><div id="tb_content_loading_anim"></div>');
					$('#tb_content_loading').css('opacity', 0.3);
					
					// add iframe
					$('#tb_content').append('<iframe frameborder="0" hspace="0" src="' + params.url + '" id="tb_iframe" name="tb_iframe ' + Math.round(Math.random() * 1000) + '"></iframe>');
					$('#tb_iframe').width(params.width).height(params.height).one('load', function(e) {
						// fade in iframe and fade out loading animation at the same time
						$(this).fadeIn(500);
						$('#tb_content_loading_anim').fadeOut(500);
						$('#tb_content_loading').fadeOut(1000);
						$('#tb_content').removeClass('tb_loading');
					});
				}
				
				$(window).trigger('resize');
			}
			catch(ex) { }
			
			$.tb_params = params;
		},
		
		tb_remove: function()
		{
			if($('#tb_window').length == 0) return false;
			
			if($.tb_params.contentID) {
				$('#' + $.tb_params.contentID).appendTo($('#' + $.tb_params.contentParentID)).hide();
			}
			else if($.tb_params.widget) {
				widget.unload($.tb_params.widget, document.getElementById('tb_content'));
			}
			
			$('#tb_window').remove();
			
			$("#tb_overlay").fadeOut(600, function() {
				$('#tb_hideselect, #tb_overlay').remove();
			});
			
			if($.browser.msie && parseInt($.browser.version) == 6) {//if IE 6
				$('html').css('overflow', '');
				$('body', 'html').css({
					height: 'auto',
					width: 'auto'
				});
			}
			
			$(document).unbind('keyup');
			$(window).unbind('resize');
			
			return false;
		},
		
		tb_hide: function()
		{
			$('#tb_window').hide();
			$('#tb_content').empty();
			
			return false;
		}
	});
})(jQuery);
