
// 프리로더
// http://www.psyonline.kr

function preloader(autostart){

	this.parent=null;
	this.pieces=12;
	this.tail=5;
	this.direction='right';
	this.speed=75;
	this.left=25;
	this.top=25;
	this.background='#FFF';
	this.zindex=100;
	this.size={
		circlewidth : 50,
		circleheight :50,
		dotwidth : 5,
		dotheight : 5
	}
	this.opacity={
		min : 10,
		max : 100
	}
	var isie=(navigator.userAgent.toLowerCase().indexOf('msie')!=-1)? true : false;
	if((/msie 9/i).test(navigator.userAgent)) isie=false;
	var create=function(style){
		var newobj;
		if(isie) newobj=document.createElement('<div style="'+style+'" id="loader_">');
		else{
			newobj=document.createElement('div');
			newobj.setAttribute('style',style);
		}
		return newobj;
	}
	var setopacity=function(target,value){
		if(isie) target.style.filter='alpha(opacity='+value+')';
		else target.style.opacity=value/100;
	}
	var obj,items,index=0;
	this.initialize=function(){
		if(obj){
			clearTimeout(obj.timer);
			obj.parentNode.removeChild(obj);
		}
		obj=create('position:absolute;left:'+this.left+'px;top:'+this.top+'px;width:1px;height:1px;z-index:'+this.zindex+';');
		var sample=create('position:absolute;width:'+this.size.dotwidth+'px;height:'+this.size.dotheight+'px;background:'+this.background+';overflow:hidden;');
		for(var temp=null,i=0,max=this.pieces*2; i<max; i+=2){
			temp=sample.cloneNode(true);
			temp.style.left=(Math.round(Math.cos(i*(Math.PI/this.pieces))*Math.ceil((this.size.circlewidth-this.size.dotwidth)/2))-(this.size.dotwidth/2))+'px';
			temp.style.top=(Math.round(Math.sin(i*(Math.PI/this.pieces))*Math.ceil((this.size.circleheight-this.size.dotheight)/2))-(this.size.dotheight/2))+'px';
			setopacity(temp,this.opacity.min);
			obj.appendChild(temp);
		}
		items=obj.childNodes;
		if(this.parent) this.parent.appendChild(obj);
		else document.body.appendChild(obj);
		this.show();
	}
	this.show=function(){
		var range=(this.opacity.max-this.opacity.min)/this.tail;
		var info=[this.opacity.max,this.opacity.min,items.length,this.tail,this.direction,this.speed];
		clearTimeout(obj.timer);
		obj.style.left=this.left+'px';
		obj.style.top=this.top+'px';
		var change=function(){
			for(var i=0; i<info[2]; i++) setopacity(items[i],info[1]);
			for(var i=0,value=0; i<info[3]; i++){
				value=info[0]-(range*i);
				if(info[4]=='left') setopacity(((index+i)>info[2]-1)? items[Math.abs(info[2]-(index+i))] : items[index+i],value);
				else if(info[4]=='right') setopacity(((index-i)<0)? items[info[2]+(index-i)] : items[index-i],value);
			}
			if(info[4]=='left') index=(index==0)? info[2]-1 : index-1;
			else if(info[4]=='right') index=(index==info[2]-1)? 0 : index+1;
			obj.timer=setTimeout(change,info[5]);
		}
		change();
	}
	this.hide=function(){
		obj.style.display='none';
		clearTimeout(obj.timer);
	}
	if(autostart) this.initialize();
}
