/**
 * @author ldz
 */
var showImageScrollConfig = {
	array:[],
	divid:'image',
	sleep:5,
	mouse:true,
	auto:true,
	step:1,
	width:'200px',
	height:'200px',
	html:'',	
	flag:'a',	//a:up;b:dw;c:left;d:right
	space:'10px'
}

function getTagNumber(html,tag){	
	var reg = eval('/'+tag+'/ig');
	var result = html.match(reg);
	if (result){
		return (result.length);				
	}else{
		return -1;
	}
}

function showImageScroll(config){	
	for (var prop in showImageScrollConfig){
		if (!config[prop])
		config[prop] = showImageScrollConfig[prop];				
	}
	var array = config.array;
	var divid = config.divid;
	var sleep = config.sleep;
	var mouse = config.mouse;
	var auto = config.auto;
	var step = config.step;
	var width = config.width;
	var height = config.height;
	var html = config.html;
	var flag = config.flag.toLowerCase();
	var space = parseInt(config.space,10);
	var time = null;
	var imgwidth = 0;	
	var imgheight = 0;
	var pos = 0;
	var totalwidth = 0;
	var totalheight = 0;	
	
	var parentdiv = null;	
	var subdiv = null; 
	var _width = 0;
	var _height = 0;	
	var parentdivwidth = 0;	
	var parentdivheight = 0;
	
	var labelwidth=0;
	var labelheight=0;
		
	
	function isIE(){
		return (navigator.appName.indexOf("Explorer") > 0);
	}
	
	function show(){
		parentdiv = document.getElementById(divid);	
		subdiv = parentdiv.getElementsByTagName("div")[0]; 
		_width = parseInt(width,10);
		_height = parseInt(height,10);	

		if (html){
			html = html.replace(/<label>[\s ]*&nbsp;[\s ]*<\/label>/ig,'');
			html = html.replace(/(^\s*)|(\s*$)/g, "");			
			
			var imgnum = getTagNumber(html,'img');
			if (imgnum < 0) return ;
			totalwidth = _width*imgnum + space*imgnum ;
			
			if (isIE()){
				totalheight = _height*imgnum + 1*space*imgnum ;				
			}else{
				totalheight = _height*imgnum + 1*space*imgnum ;								
			}
			
		}else{			
		
			if (array.length <=0 ) return;			
			totalwidth = _width*array.length + space*array.length;
			if (isIE()) {
				totalheight = _height*array.length + space*array.length;
			}else{
				totalheight = _height*array.length + space*array.length;
			}
		}
		
		if ('cd'.indexOf(flag) >=0 ){
			subdiv.style.width= totalwidth+"px";
		}
		
		if (html) {
			subdiv.innerHTML += html;
			for (var i=0;i<subdiv.getElementsByTagName("img").length;i++){
				var element = subdiv.getElementsByTagName("img")[i];
				
				if (flag=='a')
					element.style.marginBottom = space+"px";
					
				if (flag=='b')	
					element.style.marginTop  = space+"px";

				if (flag=='c')	
					element.style.marginRight = space+"px";

				if (flag=='d')	
					element.style.marginLeft = space+"px";
			}
			
		}else {
			for (var i = 0; i < array.length; i++) {
				
				var element = document.createElement("img");
				element.setAttribute("src", array[i]);

				if (width) {
					element.style.width = width;
				}
				if (height) {
					element.style.height = height;
				}
				
				if (flag=='a')
					element.style.marginBottom = space+"px";
					
				if (flag=='b')	
					element.style.marginTop  = space+"px";
					
				if (flag=='c')	
					element.style.marginRight = space+"px";

				if (flag=='d')	
					element.style.marginLeft = space+"px";
					
				subdiv.appendChild(element);
			}
		}
		
		if ('ab'.indexOf(flag) >= 0) {
			auto = (subdiv.offsetHeight <= parentdiv.offsetHeight) ? false : auto;
		}else{
			auto = (subdiv.offsetWidth <= parentdiv.offsetWidth) ? false : auto;
		}
		
		if (auto && ('cd'.indexOf(flag) >=0 )){
			subdiv.style.width = (parseInt(subdiv.style.width) * 2)+"px";
			var tmp = subdiv.innerHTML;
			tmp = tmp.replace(/(^\s*)|(\s*$)/g, "");
			tmp = tmp + tmp;
			subdiv.innerHTML = tmp;
		}
		
		if (auto && ('ab'.indexOf(flag) >=0 ))	{
			if (isIE()){
				subdiv.innerHTML = subdiv.innerHTML+subdiv.innerHTML;				
			}else{
				subdiv.innerHTML = subdiv.innerHTML+subdiv.innerHTML;								
			}
		}
		
		if (auto){
			time = setInterval(function(){
				scroll();						
			},sleep);
		}
		
		if (auto && mouse){
			addEvent(parentdiv,'mouseover',function(){
				if (time) window.clearInterval(time);
			});
			addEvent(parentdiv,'mouseout',function(){
				time = setInterval(function(){
					scroll();						
				},sleep);
			});
		}
	}								
	
	function scroll(){
		if ('ab'.indexOf(flag) >=0 ){
			pos = parentdiv.scrollTop;			
			if ('a' == flag){

				if (pos - (subdiv.offsetHeight) / 2>=0){
					parentdiv.scrollTop -= (subdiv.offsetHeight) / 2;
				}else{
					parentdiv.scrollTop += step;									
				}
/*
				//����һ�ַ���
				if(pos == parentdiv.scrollTop){					
					if (auto)				
					setTimeout(function(){
							parentdiv.appendChild(subdiv.cloneNode(true));		
						},0);
					parentdiv.scrollTop += step;
				}
*/				
			}
			
			if ('b' == flag){
				if(pos ==0){
					parentdiv.scrollTop = (subdiv.offsetHeight) / 2 + 1;
				}else{
					parentdiv.scrollTop -= step ;					
				}
			}
		}else{
			pos = parentdiv.scrollLeft;
			if ('c' == flag){
				if (pos - (subdiv.offsetWidth/2)>=0){
					parentdiv.scrollLeft -= subdiv.offsetWidth / 2;
				}else{
					parentdiv.scrollLeft += step;													
				}
			}

			if ('d' == flag) {
				if (pos== 0){
					parentdiv.scrollLeft = subdiv.offsetWidth/2;				
				}else{
					parentdiv.scrollLeft -= step;					
				}
			}
		}
	}

	function addEvent(obj, type, fn){      
		if (obj.attachEvent) {      
			obj['e'+type+fn] = fn;      
			obj[type+fn] = function() {
				obj['e'+type+fn](window.event);
			}   
			obj.attachEvent('on'+type, obj[type+fn]); 
		} else {
			obj.addEventListener(type, fn, false);      			
		}     
	} 

	this.show = function(){
		addEvent(window,'load',show);
	};

	this.showAgain = function(){
		show();
	};
}

function showImageScrollByAddDel(config){
	for (var prop in showImageScrollConfig){
		if (!config[prop])
		config[prop] = showImageScrollConfig[prop];				
	}
	
	var array = config.array;
	var divid = config.divid;
	var num = config.num;				
	var sleep = config.sleep;
	var mouse = config.mouse;
	var width = config.width;
	var height = config.height;
	
	var pos = 0;
	var time = null;
	
	function show(){					
	
		for (pos; pos<array.length && pos<num; pos++){
			var element = document.createElement("img");
			element.setAttribute("src",array[pos]);
			if (width) {
				element.setAttribute("width", width);
			}
			if (height){
				element.setAttribute("height", height);
			}
		}
		
		
		time = setInterval(function(){
			scroll();						
		},sleep);
		
		if (stop) {
			document.getElementById(parentdivid).onmouseover = function(){
				if (time) 
					window.clearInterval(time);
			}
			
			document.getElementById(parentdivid).onmouseout = function(){
				time = setInterval(function(){
					scroll1();
				}, sleep);
			}
		}
	}								
	
	
	function scroll(){
		if (array.length <= 0) return ;
		if (pos < array.length - 1){
			pos++;
		}else if (pos >= array.length - 1){
			pos = 0;
		}
		if (document.getElementById(divid).firstChild.nextSibling){
			document.getElementById(divid).removeChild(document.getElementById(divid).firstChild.nextSibling);									
		}
		var element = document.createElement("img");
		element.setAttribute("src",array[pos]);
		if (width) {
			element.setAttribute("width", width);
		}
		if (height){
			element.setAttribute("height", height);
		}
		document.getElementById(divid).appendChild(element);
	}
	this.show = show;	
}