/**
 * fd_imageResizer
 
 * desciption:
 *
 *
 * @author communicatie bureau fourdesign_ Menno Tempelaar
 */
 
 var fd_imageResizer = function (fd, element) {
 
	var self			= this;
 
	this.instances		= [];
 
	var _construct = function () {
		_setup();
		_watch();
	};
	
	var _setup = function () {
		
	};
	
	var _watch = function () {
		if ($.browser.msie && $.browser.version == 7) {
			$(element).find('img').each(function () {
				new fd_imageResizer_item(self, this);
			});
		} else {
			$(element).find('img').each(function () {
				$(this).one('load', function () {
					new fd_imageResizer_item(self, this);
				}).each(function () {
					if (this.complete) $(this).load();
				});
			});
		}
	};
	
	_construct();
	return {
	};
 };
 
 var fd_imageResizer_item = function (fd_imageResizer, object) {
	
	var self			= this;
	var clone, parent, ieInterval;
	
	var _construct = function () {
		if ($.browser.msie && $.browser.version == 7)
			_setup();
		else 
			_start();
	};
	
	var _setup = function () {
		clone 	= $(object).clone();
		parent 	= $(object).parent()[0];
		$(object).remove();
		
		object = $('<img></img>').addClass(clone[0].className);
		$(object).attr('alt', $(clone).attr('alt'));
		$(object).load(function () {
			ieInterval = window.setInterval(function () {
				if ($(object).width() != 0 && $(object).height() != 0) {
					_start();
					clearInterval(ieInterval);
				}
			})	
			
		});
		$(object).attr('src', $(clone).attr('src'));
		$(object).appendTo($(parent));
	};
	
	var _start = function () {
		if ($(object).hasClass('fd_imageResizer_total')) {
			_total();
		}
		
		if ($(object).hasClass('fd_imageResizer_middle')) {
			_middle();
		}
		
		if ($(object).hasClass('fd_imageResizer_full')) {
			_full();
		}
		
		if ($(object).hasClass('fd_imageResizer_center')) {
			_center();
		}
	};
	
	var _total = function () {
		if ($(object).width() > $(object).parent().width() || $(object).height() > $(object).parent().height()) {
			_startReducing();
		}
	};
	
	var _startReducing = function () {		
		var width_diff 	= ($(object).width() - $(object).parent().width());
		var width_perc	= ((width_diff / $(object).width()) * 100);
		var height_diff = ($(object).height() - $(object).parent().height());
		var height_perc	= ((height_diff / $(object).height()) * 100);
		
		if (width_perc >= height_perc) {
			$(object).css({
				width: ($(object).width() - ((width_perc / 100) * $(object).width())) + 'px',
				height: ($(object).height() - ((width_perc / 100) * $(object).height())) + 'px'
			});
		} else {
			$(object).css({
				width: ($(object).width() - ((height_perc / 100) * $(object).width())) + 'px',
				height: ($(object).height() - ((height_perc / 100) * $(object).height())) + 'px'
			});
		}
		
		_middle();
	};
	
	var _full = function () {
		var width_diff 	= ($(object).width() - $(object).parent().width());
		var width_perc	= ((width_diff / $(object).width()) * 100);
		var height_diff = ($(object).height() - $(object).parent().height());
		var height_perc	= ((height_diff / $(object).height()) * 100);
		
		if (width_perc >= height_perc) {
			$(object).css({
				width: ($(object).width() - ((height_perc / 100) * $(object).width())) + 'px',
				height: ($(object).height() - ((height_perc / 100) * $(object).height())) + 'px'
			});
		} else if (height_perc > width_perc) {
			$(object).css({
				width: ($(object).width() - ((width_perc / 100) * $(object).width())) + 'px',
				height: ($(object).height() - ((width_perc / 100) * $(object).height())) + 'px'
			});
		}
	};
	
	var _middle = function () {
		var height_diff = ($(object).parent().height() - $(object).height());
		$(object).css('marginTop', (height_diff / 2) + 'px');
	};
	
	var _center = function () {
		var width_image 	= $(object).width();
		var parent_width	= $(object).parent().width();
		var diff			= (width_image - parent_width) / 2;
		
		$(object).css('marginLeft', '-' + diff + 'px');
	};
	
	_construct();
	return {
	
	};
 };

