jQuery.zoomifyCounter = 0;

jQuery.fn.zoomify = function(params) {
	
	var opts = {
		backOut: false,
		showNavigator: true
		};
	jQuery.extend(opts, params);
	
	if(opts.backOut) {
		if(window.location.hash == 'zoomed' || window.location.hash == '#zoomed') window.location.hash = '#';
		}
	
	var overlay = jQuery('<div>').css({
			height: '100%',
			width: '100%',
   			position:'fixed',
			left: 0,
			top: 0,
			background: 'url('+jQuery('link[@rel=home]').attr('href')+'shared/images/overlay.png)',
			zIndex: 10000
			});
	
	return this.each(function(){
		
		var o = jQuery(this);
		var z = null;
		var i;
		var ie6 = jQuery.browser.msie&&(jQuery.browser.version <= 6);
		
		o.click(open);
		o.css('cursor', 'pointer');
		
		function open() {
			overlay.appendTo('body').click( function() { close() } );
			if(ie6) {
				jQuery('html,body').css({height: '100%', width: '100%'});
				overlay.css({opacity: 0.6, background: '#000', position: 'absolute', top: document.documentElement.scrollTop || document.body.scrollTop, left: document.documentElement.scrollLeft || document.body.scrollLeft});
				}
			
			var w = jQuery(window).width();
			var h = jQuery(window).height();
			z = jQuery('<div>').css({
				height: h*0.9,
				width: w*0.9,
				position: (ie6 ? 'absolute' : 'fixed'),
				left: (ie6 ? document.documentElement.scrollLeft || document.body.scrollLeft : 0) + w*0.05,
				top: (ie6 ? document.documentElement.scrollTop || document.body.scrollTop : 0) + h*0.05,
				background: '#FFF',
				border: '1px solid #444',
				zIndex: 10001
				});
			z.appendTo('body');
			jQuery('<div>').attr('id', "zoomify_object_"+(++jQuery.zoomifyCounter)).appendTo(z);
			
			swfobject.embedSWF(jQuery('link[@rel=home]').attr('href')+'shared/swf/zoomifyViewer.swf', "zoomify_object_"+jQuery.zoomifyCounter, w*0.9, h*0.9, "6", false, { zoomifyImagePath: o.attr('src').replace(/\.(jpe?g|tiff?|png|gif)/i, '/'), zoomifyNavigatorVisible: opts.showNavigator }, { menu: 'false', wmode: 'transparent' });
			
			jQuery('<div>').css({
				height: 19,
				width: 116,
				position: 'absolute',
				right: 8,
				bottom: ie6 ? -3 : 1,
				background: 'url('+jQuery('link[@rel=home]').attr('href')+'shared/images/close_toolbar.gif) no-repeat top left'
				}).hover(
				function(){jQuery(this).css('backgroundPosition', 'bottom left')},
				function(){jQuery(this).css('backgroundPosition', 'top left')}
				).click(close).appendTo(z);

			if(opts.backOut) {
				window.location.hash = 'zoomed';
				i = window.setInterval(function() { if (window.location.hash != 'zoomed' && window.location.hash != '#zoomed') close() }, 200);
				}
			
			}
		
		function close() {
			if(opts.backOut) {
				window.clearInterval(i);
				if(window.location.hash == 'zoomed' || window.location.hash == '#zoomed') window.location.hash = '#';
				}
			overlay.empty().remove();
			z.empty().remove();
			z = null;
			}
		
		});
	
	};

