/* Copyright (C) 2002-2011 by Home of the Brave
   Web http://home.of.the.brave.de
   E-Mail info@brave.de */
/* $Revision: 1.1 $ $Date: 2011/03/03 11:13:36 $ */

/*
 * Scale images so they fit into their containers.
 * dependencies: BodyEvents, jQuery
 * owner: Simon.Leidig@brave.de
 *
 */

var ImageScale = {
	Select:			'#content_main img.regular, #content_margin img.regular',
	MinWidth:		60,
	ScaleTime:		300,
	InsertControl:	'before',
	toggle:			function () {
		var a = $(this);
		var d = a.data('ImageScale');
		a.removeClass(d.type[0].css)
		 .addClass	 (d.type[1].css);
		d.image.animate({
			width:	d.type[0].width,
			height: d.type[0].height
		},ImageScale.ScaleTime);
		d.type.reverse();
		return false;
	}
};
BodyEvents.addListener('load',function(){
	$(ImageScale.Select).hide().each(function(){
		var n = $(document.createElement('div'));
		var img = $(this).after(n);
		var mw = Math.max(n.width(),ImageScale.MinWidth);
		n.remove();
		img.show();
		var w = img.width();
		if (w <= mw) return;
		var h = img.height();
		var mh = h * mw / w;
		img.width(mw).height(mh);
		if (ImageScale.InsertControl != 'no') img[ImageScale.InsertControl].call(img,
			$(document.createElement('a')).attr({
				href:	'#',
				'class':'image_scale_toggle image_scale_up'
			}).data('ImageScale',{
				image:	img,
				type:	[{
					css:	'image_scale_up',
					width:	w,
					height:	h
				},{
					css:	'image_scale_down',
					width:	mw,
					height:	mh
				}]
			}).click(ImageScale.toggle)
		);
	});
});

