Rounder=function(imgPath,roundRules){
	var thiss=this;
	thiss.imgPath=imgPath||'';
	thiss.rules=roundRules||{};
	thiss.used=[];
	thiss.methods=[];
	thiss.clones={};
	var arrayFind=function(arr, find){
		var found=0;
		for(var i=0;i<arr.length;i++){
			if(this[i]==find){found++;}
		}
		return found;
	}
	thiss.make=function(elem,method){
		if(!elem||!method){return false;}
		if(arrayFind(thiss.used, elem)){return false;}
		var clone=null;
		if(!arrayFind(thiss.methods, method)){
		var rules=thiss.rules[method];
		if(!rules){return false;}
		var mask={
			t:$make('div','','t'),
			m:$make('div','','m'),
			b:$make('div','','b'),
			l:$make('div','','l'),
			c:$make('div','','c'),
			r:$make('div','','r')
			}
		mask.m.style.top=mask.t.style.height=rules.top.height+'px';
		mask.m.style.bottom=mask.b.style.height=rules.bottom.height+'px';
		mask.l.style.width=mask.c.style.left=rules.left.width+'px';
		mask.c.style.marginRight=rules.left.width+rules.right.width+'px';
		mask.r.style.width=rules.right.width+'px';
		mask.t.appendChild(mask.tl=mask.l.cloneNode(false));
		mask.t.appendChild(mask.tc=mask.c.cloneNode(false));
		mask.t.appendChild(mask.tr=mask.r.cloneNode(false));
		mask.m.appendChild(mask.ml=mask.l.cloneNode(false));
		mask.m.appendChild(mask.mc=mask.c.cloneNode(false));
		mask.m.appendChild(mask.mr=mask.r.cloneNode(false));
		mask.b.appendChild(mask.bl=mask.l.cloneNode(false));
		mask.b.appendChild(mask.bc=mask.c.cloneNode(false));
		mask.b.appendChild(mask.br=mask.r.cloneNode(false));
		mask.tl.style.height=mask.tr.style.height=rules.top.height+'px';	//for buggy IE 6
		mask.bl.style.height=mask.br.style.height=rules.bottom.height+'px';	//too
		/// png correction in IE prior 6
		var incorrectBrowser=navigator.userAgent.match(/msie\s+[5-6]/i);
		var rePng=/\.png$/i;
		/// top
		if(incorrectBrowser&&rules.top.leftSrc.match(rePng)){mask.tl.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.top.leftSrc+',sizingMethod=scale)';}
		else{mask.tl.style.backgroundImage='url('+imgPath+rules.top.leftSrc+')';}
		if(incorrectBrowser&&rules.top.centerSrc.match(rePng)){mask.tc.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.top.centerSrc+',sizingMethod=scale)';}
		else{mask.tc.style.backgroundImage='url('+imgPath+rules.top.centerSrc+')';}
		if(incorrectBrowser&&rules.top.rightSrc.match(rePng)){mask.tr.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.top.rightSrc+',sizingMethod=scale)';}
		else{mask.tr.style.backgroundImage='url('+imgPath+rules.top.rightSrc+')';}
		/// middle
		if(incorrectBrowser&&rules.middle.leftSrc.match(rePng)){mask.ml.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.middle.leftSrc+',sizingMethod=scale)';}
		else{mask.ml.style.backgroundImage='url('+imgPath+rules.middle.leftSrc+')';}
		if(incorrectBrowser&&rules.middle.centerSrc.match(rePng)){mask.mc.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.middle.centerSrc+',sizingMethod=scale)';}
		else{mask.mc.style.backgroundImage='url('+imgPath+rules.middle.centerSrc+')';}
		if(incorrectBrowser&&rules.middle.rightSrc.match(rePng)){mask.mr.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.middle.rightSrc+',sizingMethod=scale)';}
		else{mask.mr.style.backgroundImage='url('+imgPath+rules.middle.rightSrc+')';}
		/// bottom
		if(incorrectBrowser&&rules.bottom.leftSrc.match(rePng)){mask.bl.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.bottom.leftSrc+',sizingMethod=scale)';}
		else{mask.bl.style.backgroundImage='url('+imgPath+rules.bottom.leftSrc+')';}
		if(incorrectBrowser&&rules.bottom.centerSrc.match(rePng)){mask.bc.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.bottom.centerSrc+',sizingMethod=scale)';}
		else{mask.bc.style.backgroundImage='url('+imgPath+rules.bottom.centerSrc+')';}
		if(incorrectBrowser&&rules.bottom.rightSrc.match(rePng)){mask.br.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+imgPath+rules.bottom.rightSrc+',sizingMethod=scale)';}
		else{mask.br.style.backgroundImage='url('+imgPath+rules.bottom.rightSrc+')';}
		
		clone=$make('div','','mask');
		clone.appendChild(mask.t);
		clone.appendChild(mask.m);
		clone.appendChild(mask.b);
		thiss.clones[method]=thiss.clone;
		}
		else{
		clone=thiss.clones[method];
		}
		elem.style.background='none';
		elem.style.border='none';
		var content=null;
		for(var j=0;j<elem.childNodes.length;j++){
			if(new RegExp('\\bcontent\\b').test(elem.childNodes[j].className)){
				content=elem.childNodes[j];
			}
		}
		if(content&&clone){
			elem.insertBefore(clone,content);
			if(rules.inner){
				content.style.width = 'auto';
				content.style.height = 'auto';
				content.style.top = rules.top.height + 'px';
				content.style.right = rules.right.width + 'px';
				content.style.bottom = rules.bottom.height + 'px';
				content.style.left = rules.left.width + 'px';
				if(navigator.userAgent.match(/msie\s+[5-7]/i)){
					content.style.setExpression('height', 'previousSibling.childNodes[1].offsetHeight + \'px\'');
					content.style.setExpression('width', 'previousSibling.childNodes[1].childNodes[1].offsetWidth + \'px\'');
				}
			}
		}
		thiss.used.push(elem);
	}
	thiss.capture=function(area){
		if(!area){area=document;}
		var divs=area.getElementsByTagName('div');
		for(var i=0;i<divs.length;i++){
			if(divs[i].className.indexOf('round_')==0){
				thiss.make(divs[i],divs[i].className.split('_')[1]);
			}
		}
	}
}

var roundRules={
	smalltop:{
		top:{
			height:3,
			leftSrc:'smalltop/tl.png',
			rightSrc:'smalltop/tr.png',
			centerSrc:'smalltop/tc.png'
		},
		middle:{
			leftSrc:'smalltop/ml.png',
			rightSrc:'smalltop/mr.png',
			centerSrc:''
		},
		bottom:{
			height:0,
			leftSrc:'',
			rightSrc:'',
			centerSrc:''
		},
		left:{
			width:5
		},
		right:{
			width:5
		}
	},
	main:{
		top:{
			height:10,
			leftSrc:'main/tl.png',
			rightSrc:'main/tr.png',
			centerSrc:'main/tc.png'
		},
		middle:{
			leftSrc:'main/ml.png',
			rightSrc:'main/mr.png',
			centerSrc:''
		},
		bottom:{
			height:9,
			leftSrc:'main/bl.png',
			rightSrc:'main/br.png',
			centerSrc:'main/bc.png'
		},
		left:{
			width:10
		},
		right:{
			width:12
		},
		inner: true
	},
	big:{
		top:{
			height:16,
			leftSrc:'big/tl.png',
			rightSrc:'big/tr.png',
			centerSrc:'big/tc.png'
		},
		middle:{
			leftSrc:'big/ml.png',
			rightSrc:'big/mr.png',
			centerSrc:''
		},
		bottom:{
			height:16,
			leftSrc:'big/bl.png',
			rightSrc:'big/br.png',
			centerSrc:'big/bc.png'
		},
		left:{
			width:16
		},
		right:{
			width:16
		},
		inner: true
	}
};

var rounder=new Rounder('/res/img/rounder/', roundRules);
