function PennySlideShow ($preferences) {
	this.$img_group = $($preferences["img_group"]);
	this.$img_group.style.display='none';
	this.$img_list = $$('li',this.$img_group);
	this.$img_length = this.$img_list.length;
	this.$ready_index = 0;
	
	this.a=[];
	this.$imgs=[];
//	alert(this.$img_length);
	for (var $index=0; $index<this.$img_length; $index++) {
		this.a[$index] = {};
		var h = this.$img_list[$index];
		var a = this.a[$index];
		
		this.$imgs[$index] = $$('img',h)[0];
//		alert($index+":"+this.$imgs[$index].src);
	}
	
	this.$circle_width = $preferences["circle_width"];
	this.$circle_height = $preferences["circle_height"];
	this.$circle_inner_width = $preferences["circle_inner_width"]?$preferences["circle_inner_width"]:this.$circle_width;
	this.$circle_inner_height = $preferences["circle_inner_height"]?$preferences["circle_inner_height"]:this.$circle_height;
	this.$x_or_y = $preferences["x_or_y"];
	
	this.$img_width = this.$circle_width;
	this.$img_height = this.$circle_width;
	
	this.$button_width = this.$circle_width;
	this.$button_height = 30;
	
	this.$img_circle = $($preferences["img_circle"]);
	
	Pennyutils.setCssStyle(this.$img_circle, "width:"+this.$circle_width+"px;height:"+this.$circle_height+"px;overflow:hidden;display:block;");
	
	this.$img_inner_circle = document.createElement("div");
	
	Pennyutils.setCssStyle(this.$img_inner_circle, "width:"+this.$circle_inner_width+"px;height:"+this.$circle_inner_height+"px;position:relative;overflow:hidden;left:"+((this.$circle_width-this.$circle_inner_width)/2)+"px;top:"+((this.$circle_height-this.$circle_inner_height)/2)+"px;");
	
	// Left button.
	this.$button_opacity = 50;
	var $_this = this;
	this.$prev_button = document.createElement("div");
	Pennyutils.setCssStyle(this.$prev_button,
		"width:"+this.$button_width+"px;height:"+this.$button_height+"px;position:absolute;cursor:pointer;z-index:150;border:0px solid #00cccc;"
		);
	Pennyutils.setClassName(this.$prev_button, "img_top_button");
	
	this.$img_circle.appendChild(this.$prev_button);
	
	this.$prev_button.style.opacity=this.$button_opacity/100;
	this.$prev_button.style.filter='alpha(opacity='+this.$button_opacity+')';
	Pennyutils.events.addEventHandler(this.$prev_button, "mouseover", 
		function () {
			$_this.setOpacity($_this.$prev_button, 80, 5);
			$_this.slidePhoto(true);
		});
	Pennyutils.events.addEventHandler(this.$prev_button, "mouseout", 
		function () {
			$_this.setOpacity($_this.$prev_button, 40, 5);
			clearInterval($_this.$fix_img_group.$interval_id);
		});
//	Pennyutils.events.addEventHandler(this.$prev_button, "click", 
//		function () {
//			$_this.slidePhoto(true);
//		});
	
	// Right button.
	this.$next_button = document.createElement("div");
	
	Pennyutils.setCssStyle(this.$next_button,
		"width:"+this.$button_width+"px;height:"+this.$button_height+"px;position:absolute;cursor:pointer;z-index:150;border:0px solid #cc00cc;margin-top:"+(this.$circle_height-this.$button_height)+"px;"
		);
	Pennyutils.setClassName(this.$next_button, "img_bottom_button");
	
	
	this.$img_circle.appendChild(this.$next_button);
	
	this.$next_button.style.opacity=this.$button_opacity/100;
	this.$next_button.style.filter='alpha(opacity='+this.$button_opacity+')';
	Pennyutils.events.addEventHandler(this.$next_button, "mouseover", 
		function () {
			$_this.setOpacity($_this.$next_button, 80, 5);
			$_this.slidePhoto(false);
		});
	Pennyutils.events.addEventHandler(this.$next_button, "mouseout", 
		function () {
			$_this.setOpacity($_this.$next_button, 40, 5);
			clearInterval($_this.$fix_img_group.$interval_id);
		});
	
	// Images group.
	this.$img_show = $preferences["img_show"];
	this.$method = $preferences["method"]?$preferences["method"]:this.createMainPhoto;
	this.$fix_img_group = document.createElement("div");
	this.$fix_img_group_width = 0;
	this.$fix_img_group_height = 0;
	
	this.$fix_img_group_1 = document.createElement("div");
	this.$fix_img_group_1_width = 0;
	this.$fix_img_group_1_height = 0;
	
	var $size;
	this.$images=[];
	for (var $index=0; $index<this.$img_length; $index++){
		this.$show_img = document.createElement("img");
		
		var $image = new Image();
		$image.onload=function () {
			$_this.$images[$index] = $image;
			
			$_this.setPhotos();
		};
		Pennyutils.setCssStyle($image, "cursor:pointer;margin:1px;");
		$image.src = this.$imgs[$index].src;
		$image.alt = this.$imgs[$index].alt;
		$image.title = this.$imgs[$index].title;
		
		Pennyutils.events.addEventHandler($image, "click", 
			  function () {
				  $_this.$method(arguments[0]);
			  });
		
		this.$images[$index] = $image;
	}
	
	this.showMainPhoto(this.$imgs[0].src);
}

PennySlideShow.prototype.setPhotos = function () {
	this.$ready_index++;
	if (this.$ready_index>=this.$img_length) {
		this.$fix_img_group_top=0;
		for (var $index=0; $index<this.$img_length; $index++){
			var $temp_image = this.$images[$index];
			var $size = Pennyutils.resizeImage($temp_image, this.$img_width, this.$img_height, 2);
//			this.$fix_img_group_height += $size.height*2;
			this.$fix_img_group.appendChild($temp_image);
//			if ($index<(this.$img_length-1)) {
				this.$fix_img_group_height+= $size.height;
				this.$fix_img_group_top+= $size.height;
//			}
		}
		
		this.$fix_img_group_width = this.$circle_width;
		
		Pennyutils.setCssStyle(this.$fix_img_group, "width:"+this.$fix_img_group_width+"px;height:"+this.$fix_img_group_height+"px;left:0px;position:relative;z-index:25;display:block;");
		
		this.$img_inner_circle.appendChild(this.$fix_img_group);
		
		this.$img_circle.appendChild(this.$img_inner_circle);
	}
}

PennySlideShow.prototype.createMainPhoto = function () {
	var $events = Pennyutils.events.getEvent();
	var $element = $events.target;
	this.showMainPhoto($element.src);
}

PennySlideShow.prototype.showMainPhoto = function () {
	if (arguments[0]) {
	} else {
		return;
	}
	
	var $img_show = $(this.$img_show);
	if ($img_show) {
	} else {
		return;
	}
	
	var $image = new Image();
	$image.onload = function () {
//		alert($image.src+","+$img_show.style.width);
		$size = Pennyutils.resizeImage($image, parseInt($img_show.style.width), parseInt($img_show.style.height), 0);
		
		var left = ((parseInt($img_show.style.width) - $size.width)/2);
		var top = ((parseInt($img_show.style.height) - $size.height)/2);
		
		var $length = $img_show.childNodes.length;
		for (var $index=$length-1; $index>=0; $index--) {
			$img_show.removeChild($img_show.childNodes[$index]);
		}
		
		$img_show.appendChild($image);
	}
	$image.src = arguments[0];
}

PennySlideShow.prototype.slidePhoto = function () {
	if (this.$fix_img_group_width<this.$circle_width) {
		return;
	}
	
	var $p_or_n = arguments[0];
	$element = this.$fix_img_group;
	clearInterval($element.$interval_id);
	var $borderline = 0;
	
	$borderline = this.$fix_img_group_height-this.$circle_height;
	
	$borderline *= -1;
	
	var $_this = this;
	$element.$interval_id=setInterval(function(){
		if ($p_or_n) {
//			$_this.$img_inner_circle.scrollTop-=10;
			if ($_this.$img_inner_circle.scrollTop<=0) {
				$_this.$img_inner_circle.scrollTop=$_this.$fix_img_group_top;
			}else{
				$_this.$img_inner_circle.scrollTop-=10;
			}
		} else {
			if (($_this.$fix_img_group_height/2)-$_this.$img_inner_circle.scrollTop<=0) {
				$_this.$img_inner_circle.scrollTop=-10;
			}else{
				$_this.$img_inner_circle.scrollTop+=10;
			}
		}
		
//		window.status=$_this.$fix_img_group_top+"-"+$_this.$img_inner_circle.offsetTop+"-"+$_this.$img_circle.scrollTop+"-"+$_this.$img_inner_circle.scrollTop+"-"+($_this.$fix_img_group_height/2);
	}, 20);
}

PennySlideShow.prototype.changeImage = function($step, $walk){
	this.$img_index += $step;
	this.$img_index = this.$img_index<0?0:this.$img_index>this.$img_length-1?this.$img_length-1:this.$img_index;
	this.showImage(this.$img_index, $walk);
}

PennySlideShow.prototype.showImage = function($img_index, $walk){
	var $image = new Image();
	$image.style.opacity = 0;
	$image.style.filter = 'alpha(opacity=0)';
	$image.onload=new Function("PennySlideShow.setOpacity(this, 20, 5)");
	this.$show_img.src.onload=new Function("PennySlideShow.setOpacity(this, 20, 5)");
	this.$show_img.src = this.$imgs[$img_index].src;
	Pennyutils.resizeImage(this.$show_img, this.$img_width, this.$img_height);
	this.$show_img.style.left = (this.$img_width - this.$show_img.width) / 2;
	this.$show_img.style.top = (this.$img_height - this.$show_img.height) / 2;
}

PennySlideShow.prototype.setOpacity = function ($element, $alpha, $target) {
	$element = 'object'==typeof $element?$element:$($element);
	var $opacity = $element.style.opacity||this.getStyleProperty($element, 'opacity');
	var $destine=$alpha>$opacity*100?1:-1;
	$element.style.opacity = $opacity;
	clearInterval($element.$interval_id);
	var $_this = this;
	$element.$interval_id=setInterval(function(){$_this.tween($element, $alpha, $destine, $target)}, 20);
}

PennySlideShow.prototype.tween = function ($element, $alpha, $destine, $target) {
	var $opacity = Math.round($element.style.opacity*100);0
	if($opacity==$alpha){
		clearInterval($element.$interval_id);
	} else {
		var $now_opacity = $opacity + Math.ceil(Math.abs($alpha - $opacity) / $target) * $destine;
		$element.style.opacity = $now_opacity / 100;
		$element.style.filter = 'alpha(opacity='+$now_opacity+')';
	}
}

PennySlideShow.prototype.getStyle = function ($elements_tag_name, $property_value) {
	$elements_tag_name = 'object'==typeof $elements_tag_name?$elements_tag_name:$($elements_tag_name);
	return $elements_tag_name.currentStyle?$elements_tag_name.currentStyle[$property_value]:document.defaultView.getComputedStyle($elements_tag_name,null).getPropertyValue($property_value);
}
