$(document).ready ( function () {

$('.plg-picbox').each ( function () {
	var t = $(this)
	var id = /\d+$/.exec(this.className)
	var items = t.children()
	var w = t.width()
	var h = t.attr('imgheight')//t.height()
	var fw = t.attr('fullwidth')
	var maxTextHeight = 0
	
	if ( fw ) items.css ( { display: 'inline-block', height: h+'px', overflow: 'hidden' } )
		else items.css ( { display: 'inline-block', width: w+'px', height: h+'px', overflow: 'hidden' } )
	items.each ( function () {
		var img = $(this).find('img')
		var textHeight = $(this).find('.pic-text').height()
		if ( textHeight > maxTextHeight ) maxTextHeight = textHeight
		if ( ! fw ) {
			img.bind ( 'load', function () {
				if ( w/h > img.width()/img.height() ) img.css ( { marginTop: h-img.height()+'px', width: w+'px' } )
					else img.css ( { marginLeft: w-img.width()+'px', height: h+'px' } )
			} )
		} else img.css ( { marginRight: '10px', height: h+'px' } )
	} )
	items.height ( parseInt(h) + parseInt(maxTextHeight) + 5 )

	t.css ( 'visibility', 'visible' )

	$('.picbox-'+id+'-left').bind ( 'click', function () {
		var curPic
		items.each ( function () {
			if ( parseInt($(this).css('marginLeft')) < 0 ) curPic = $(this)
		} )
		if ( ! curPic ) return
		curPic.animate ( { marginLeft: 0 } )
	} )

	if ( fw ) {
		$('.picbox-'+id+'-right').bind ( 'click', function () {
			var curPic
			var width = -10 // ширина оставшихся слайдов
			items.each ( function () {
				var t = $(this)
				if ( parseInt(t.css('marginLeft')) < 0 ) return
				width += t.width()
				if ( ! curPic ) curPic = t
			} )
			if ( width < w ) return
			var offset = -curPic.width()
			if ( width + offset < w ) offset = w - width
			curPic.animate ( { marginLeft: offset } )
		} )
	} else {
		$('.picbox-'+id+'-right').bind ( 'click', function () {
			var curPic
			items.each ( function () {
				if ( parseInt($(this).css('marginLeft')) < 0 ) return
				curPic = $(this)
				return false
			} )
			if ( curPic.index() == items.length-1 ) return
			curPic.animate ( { marginLeft: -w } )
		} )
	}
} )

} )