jQuery(document).ready(function($){

	if( !$('a.photo').length ) return;
	
	var hideFloating = function( ) { $try( function(){ $('#__floating').fadeOut(300,function(){$(this).dispose();}); } ); };
	//var printFloating = function( ) { window.print(); };
	var showFloating = function( $html ) {
		var buttons = $('<div />').attr({'class':'buttons'});
		var container = $('<div />').attr({'class':'container'}).html($html).hide();
		var bg = $('<div />').attr({'class':'bg'}).bind('click',hideFloating).hide();
		$('body').adopt( $('<div />').attr({'id':'__floating','class':'floating'}).adopt( bg, container.adopt(buttons) ) );
		
		//buttons.adopt( new Element( 'div', { 'class':'print', 'text':'печать', 'events': { 'click': printFloating } } ) );
		buttons.adopt( $('<div />').attr({'class':'close'}).text('x').bind('click',hideFloating) );
		bg.fadeIn(1000);
		container.fadeIn(200);
		
		var img = container.find('img');
		img.load(function(){
			var a = {width:img.width(), height:img.height()};
			//console.debug(img.width(),img.height());
			if(a.width < 200 && a.height < 140) {
				img.css('zoom','2.5');
			} else if(a.width < 280 && a.height < 200) {
				img.css('zoom','2');
			} else if(a.width < 380 && a.height < 280) {
				img.css('zoom','1.5');
			} else if(a.width > 600 || a.height > 450) {
				img.css('zoom',0.75);
			} else {
				img.css('zoom',1);
			}
		});
	};

	$('a.photo').each(function(i){
		var el=$(this);
		//el.erase('onclick');
		el.bind( 'click', function(e){
			e.stopPropagation();
			e.preventDefault();
			var _title = el.find('img').attr('alt'); if( !_title || (_title == null) ) _title = el.find('img').attr('title');

			//alert( el.getElement('img').get('tag') );
			showFloating( '<h3>'+_title+'</h3><div class="image-wrapper"><div class="iebug"><img src="'+el.find('img').attr('src').replace('/t','/')+'" /></div></div>' );
		});
	});

});
