if (typeof($.fn.w2dPhotoTable)=='undefined') {
	(function($) {


	$.fn.w2dPhotoTable = function(settings) {
		var config = {	'tidy'	: true,
						'ie8shade'	: true,
						'ie8rotate' : true,
						'leave' : false,
						'elastic' : false
		};
				
		if (settings) $.extend(config, settings);
		
		return this.each(function() {			
			var ptg=$(this);
			var w=parseInt(ptg.css("width"));		
			var h=parseInt(ptg.css("height"));
			if($.browser.msie && document.compatMode=="BackCompat")ptg.css({width :(w+parseInt(ptg.css("borderWidth"))*2)+"px",height :(h+parseInt(ptg.css("borderWidth"))*2)+"px"});
			var pics=ptg.children('.w2dphototable_div');
			var tidybtn=ptg.children('.w2dphototable_tidy');			
			var noClick;		
			
			pics.each(function(){
				if(config.tidy==true) $(this).css({left : w/2-50, top : h/2-50});
				if($.browser.msie && parseInt($.browser.version)<9) {
					if (config.ie8shade||config.ie8rotate) {
						var f = (config.ie8rotate)? 'progid:DXImageTransform.Microsoft.Matrix(sizingMethod=\'auto expand\', '+MforIE8()+') ' :''
						f+= (config.ie8shade)? 'progid:DXImageTransform.Microsoft.Shadow(color=\'#333333\', Direction=135, Strength=4)' : '';
						$(this).css({filter : f, backgroundImage : 'none', backgroundColor : '#eeeeee', padding : '5px 5px 18px 5px'});
					}	
				}
				var contain = (config.leave)? 'document' : 'parent';
				$(this).draggable({containment: contain,stack : ".w2dphototable_div",addClasses : false,start: function(event, ui){noClick=true;},stop: function(event, ui){setTimeout(function(){noClick=false;},200);}});
			});			
			if (config.tidy==true) {
				pics.last().mouseenter(function(){$(this).unbind("mouseenter");w2dphototable_spread(true);});
				tidybtn.click(function(){
					$(this).fadeOut('slow');
					pics.unbind("mouseenter");
					w2dphototable_tidyUp();
				});		
			}
			else w2dphototable_spread(false);					
		
			pics.children("a").each(function() {
				$(this).bind("click", function(e) {if(noClick||$(this).attr('href')==''){e.stopImmediatePropagation();e.preventDefault();}});
			});
			overlop=(config.elastic)?0:0.2;
			trans=(config.elastic)?'elastic':'fade';
			si=(!config.elastic&&$.browser.msie && parseInt($.browser.version)<9)?100:600;
			so=(!config.elastic&&$.browser.msie && parseInt($.browser.version)<9)?0:600;
			pics.children("a.w2dpt_fb").fancybox({
				'titlePosition'	: 'inside',
				'cyclic' : true,
				'speedIn' : si,
				'speedOut' : so,
				'overlayColor' : '#000000',
				'transitionIn' : trans,
				'transitionOut' : trans,
				'overlayOpacity' : overlop
			});
			
			function w2dphototable_tidyUp() {
				var topone;var topz=0;
				pics.each(function(){	
					if (isNaN(parseInt($(this).css('z-index')))==false && parseInt($(this).css('z-index'))>topz) {topz=parseInt($(this).css('z-index'));topone=$(this).index();}
					$(this).animate({top: h/2-50, left : w/2-50}, 500,'swing');
				});
				if (topz==0) topone=pics.length-1;
				pics.eq(topone).mouseenter(function() { 	
					$(this).unbind("mouseenter");
					w2dphototable_spread(true);
				});
			}

			function w2dphototable_spread(anim){
				if (anim==true) tidybtn.css({left : (w-tidybtn.outerWidth()-7),top : h-tidybtn.outerHeight()-7}).fadeIn('slow');
				pics.each(function(){	
					if (anim==true) $(this).animate({top: Math.random()*h*0.8-50, left: Math.random()*w*0.8-50}, 500,'swing');
					else $(this).css({top: Math.random()*h*0.8-50, left: Math.random()*w*0.8-50});
				});	
			}
			function MforIE8(){
				radian = (Math.random()*90-45) * Math.PI/180;
        		cos = Math.cos(radian);
				sin = Math.sin(radian);
				return "M11=" + cos + ",M12=" + -sin + ",M21=" + sin + ",M22=" + cos;
			}
		});
	};
})(jQuery);
}


