(function($) {

	// init
	$(function() {

		var $Imgs = $('.details-list>img,#page>.item>.details>img:first');
//		trace("$Imgs: "+$Imgs.length); // TRACE ### $Imgs
		$Imgs.each(function(i,el){
			var $Img = $(el);
//			trace(i+" "+$Img.attr('src')); // TRACE ### $Img
//			trace(i+" "+$Img.width()+" "+$Img.height()); // TRACE ### i
			var setImageSize = function setImageSize(e) {
//				trace("setImageSize: "+$Img.width()+" "+$Img.height()); // TRACE ### setImageSize
				var fAsp = $Img.height()/$Img.width();
				$Img.height(150);
				$Img.width(150/fAsp);
			}
			if (($Img.width()+$Img.height())===0)	$Img.load(setImageSize);
			else									setImageSize();
		});
		$('div#wrapper>div#container>div#page>div.item>div.details-list>img').each(function(i,el){
			var $Img = $(el);
			var setSurround = function setSurround(e) {
				var iW = 220;
				var iH = 150;
				var fA = iW/iH;
				var iOW = $Img.get(0).naturalWidth||$Img.width();
				var iOH = $Img.get(0).naturalHeight||$Img.height();
				var fOA = iOW/iOH;
//document.write('iOW:',iOW); // TRACE ### iOW				

//		return;
				$Img.css({
					 padding: '0'
					,margin: '0 auto'
					,position: 'relative'
					,width:		(fOA>fA?iW:iH*fOA)+'px'
					,height:	(fOA>fA?iW/fOA:iH)+'px'
//				}).width(fOA>fA?iW:iH*fOA).height(fOA>fA?iW/fOA:iH).wrap('<div></div>').parent().css({
				}).wrap('<div></div>').parent().css({
					 background: '#eee'
					,width: iW+'px'
					,height: iH+'px'
					,textAlign: 'center'
					,position: 'absolute'
					,left: '0'
					,margin: '5px'
				}).parent().css({
					 height: '160px'
				});
			}
		
			if (($Img.width()+$Img.height())===0)	$Img.load(setSurround);
			else									setSurround();
		});
		
		
		//
		var $ItemImg = $('#page>.item>.details>img:first');
		if ($ItemImg.length) $ItemImg.wrap('<a/>').parent().attr('href',$ItemImg.attr('src')).lightBox();

//		$ItemImg.wrap('<a/>').css({border:'1px solid red'});
//		$ItemImg.wrapInner('<a/>').css({border:'1px solid red'});
//		$ItemImg.lightBox();

//		$('div.item').css({display:'block',height:'300px'});
//		$('div.details').css({display:'block'});
//		$('div.item>div.details').css({display:'block',height:'300px'});

//		$('div.item>div.details').css({display:'block',height:($('div.item>div.details').height()+20)+'px'});
		$('<br class="clear" />').insertAfter($('div.item'));
		$('.clear').css('clear','both');

	});


	// trace
	function trace(o,v) {
		if (window.console&&window.console.log) {
			if (typeof(o)=="string")	window.console.log(o);
			else						for (var prop in o) window.console.log(prop+":\t"+String(o[prop]).split("\n")[0]);
		}
	}

})(jQuery);
