var bp_common_server = bp_server;
var bp_common = function(){
	// locals
	var VERSION = 20070223;
	var eleCancel = null;
	var eleCancelImg = bp_common_server+"img/btn_off_b.gif";
	var eleCancelSize = [192,41];
	var aWindowSize = [];
	var aScrollSize = [];
	var aScrollPosition = [];
	var aWindowSizeLast = [];
	var aScrollSizeLast = [];
	var aScrollPositionLast = [];
	var effectCancelAppear = null;
	var effectCancelDisapear = null;
	// return null if not found
	var revivalEmbedSelect = function(arr){
		var len = arr.length;
		if( len == 0 ){ return; }
		for( var i=0; i<len; i++ ){
			var ele = arr[i];
			if( ele.getAttribute('rel')=='bpdeleted' ){
				//ele.setAttribute('rel')='';
				//ele.style.display='block';
				ele.style.visibility='visible';
			}
		}
	};
	var deleteEmbedSelect = function(arr){
		var len = arr.length;
		if( len == 0 ){ return; }
		for( var j=0; j<len; j++ ){
			var ele = arr[j];
			if( ele.style.visibility!='hidden' && (ele.id.search(/bp/)==-1) ){
				ele.setAttribute('rel','bpdeleted');
				ele.style.visibility='hidden';
			}
		}
	};
	// whole page size, including hidden area by scroll bars
	var calcScrollSize = function(){
		//return [document.documentElement.scrollWidth,document.documentElement.scrollHeight];
		return [document.body.scrollWidth,document.body.scrollHeight];
	};
	// visible area size, without hidden area by scroll bars
	var calcWindowSize = function(){
		//var width =  ((window.khtml||window.opera) ? window.innerWidth	: (document.documentElement.clientWidth ||document.body.clientWidth));
		//var height = ((window.khtml||window.opera) ? window.innerHeight : (document.documentElement.clientHeight||document.body.clientHeight));
		//var windowWidth, windowHeight;
		var width = (window.innerWidth) ? window.innerWidth :
						(document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
		var height= (window.innerHeight) ? window.innerHeight :
						(document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
		return [width,height];
	};
	var calcScrollPosition = function(){
		var arr = [];
		arr[0] = document.body.scrollLeft || document.documentElement.scrollLeft;
		arr[1] = document.body.scrollTop || document.documentElement.scrollTop;
		return arr;
	};
	var checkDisplay = function(t){
		aWindowSizeLast = aWindowSize;
		aScrollSizeLast = aScrollSize;
		aScrollPositionLast = aScrollPosition;
		aWindowSize = calcWindowSize();
		aScrollSize = calcScrollSize();
		aScrollPosition = calcScrollPosition();
		if( t!="FIRST" &&
			 ( aWindowSize[0]			!=aWindowSizeLast[0]
			|| aWindowSize[1]			!=aWindowSizeLast[1]
			|| aScrollSize[0]			!=aScrollSizeLast[0]
			|| aScrollSize[1]			!=aScrollSizeLast[1]
			|| aScrollPosition[0]	!=aScrollPositionLast[0]
			|| aScrollPosition[1]	!=aScrollPositionLast[1] ) ){
			fireEvents("DisplayArea","change");
		}
	};
	var fireEvents = function(element, name){
		if( !listeners[element] ){ return; }
		if( !listeners[element][name] ){ return; }
		var len=listeners[element][name].length;
		for( var i=0; i<len; i++ ){
			if( !listeners[element][name][i] ){ continue; }
			listeners[element][name][i]();
		}
	};
	var listeners = {};
	var createHttpRequest = function(){
		if(window.ActiveXObject){
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					return null;
				}
			}
		} else if(window.XMLHttpRequest){
			return new XMLHttpRequest();
		} else {
			return null
		}
	};

	//var cache={};

	// constructor funcs

	// globals
	return {
		gEleCancel : null,
		load : function(){
			checkDisplay("FIRST");
			bp_Interval.set(checkDisplay.bpc_bind(this),30);
		},
		el : function(id){
			//return cache[id] || (cache[id]=document.getElementById(id));
			return document.getElementById(id);
		},
		getElementsByClassName : function(c,p){
			var el = p.getElementsByTagName('*');
			var ar = [];
			for( var i in el ){
				if( el[i].className && el[i].className.match(c) ){
					ar.push(el[i]);
				}
			}
			return ar;
		},
		play : function(){
			checkDisplay();
			if( !bp_isWithPriorParts ){
				if( !eleCancel ){
					eleCancel = new bp_elementFactory(document.getElementsByTagName("body")[0],
					 [ {"tag" : "a",
							"attr" :{"href":"javascript:void(0)"},
							"style":{"width"	:eleCancelSize[0]+"px",
											 "height" :eleCancelSize[1]+"px",
											 "left" 	:"0px",
											 "top"		:"0px",
											 "zIndex" :"10000"
											},
							"children":[ {"tag" 	: "img",
														"attr"	:{"src":eleCancelImg},
														"style" :{"display":"block",
																		 "width"	:eleCancelSize[0]+"px",
																		 "height" :eleCancelSize[1]+"px"
																		 }
												 } ]
						 }
					 ]);
					bp_common.gEleCancel = eleCancel;
					bp_common.addListener( eleCancel, 'click', bp_common.stop, false );
				}
				var effectCancelAppear = new bp_Effect(eleCancelSize[0],eleCancelSize[1],eleCancel,"SLIDEDOWN","LEFTTOP",2);
				if( effectCancelDisapear ){
					effectCancelDisapear.end();
				}
				effectCancelAppear.start();
				bp_common.addListener("DisplayArea","change",bp_common.updateCancelBtn,false);
			}
			bp_scheduler.play();
		},
		stop : function(){
			if( !bp_isWithPriorParts ){
				var effectCancelDisapear = new bp_Effect(eleCancelSize[0],eleCancelSize[1],eleCancel,"SLIDEUP","LEFTTOP",-2);
				effectCancelDisapear.aftermove = function(){
					bp_common.removeListener("DisplayArea","change",bp_common.updateCancelBtn,false);
					eleCancel.style.display="none";
				};
				if( effectCancelAppear ){
					effectCancelAppear.end();
				}
				effectCancelDisapear.start();
			}
			if( typeof(SEditor)!="undefined" ){
				SEditor.previewCancel();
			}
			bp_scheduler.pause();
			bp_scheduler.reset();
		},
		updateCancelBtn : function(){
			if( eleCancel ){
				var s = eleCancel.style;
				s.left = aScrollPosition[0]+"px";
				s.top =  aScrollPosition[1]+"px";
			}
		},
		//usage: bp_addRule(".LeftNum0","background-image:url(img/number/0.gif)");
		addRule: function( selector, property, sheetindex, ruleindex ){
			if( !document.styleSheets ){ return; }
			if( sheetindex == undefined ) sheetindex = 0;
			var sheet = document.styleSheets[ sheetindex ];
			if( sheet.addRule ) { //IE
				if( ruleindex == undefined ) ruleindex = sheet.rules.length;
				sheet.addRule( selector, "{" + property + "}", ruleindex );
				return ruleindex;
			}
			else if( sheet.insertRule ) { //Mozilla
				if( ruleindex == undefined ) ruleindex = sheet.cssRules.length;
				return sheet.insertRule( selector + "{" + property + "}", ruleindex );
			}
			return null;
		},
		getColor : function(r,g,b){
			var hr = r.toString(16);
			hr = (r<16) ? '0'+hr : hr;
			var hg = g.toString(16);
			hg = (g<16) ? '0'+hg : hg;
			var hb = b.toString(16);
			hb = (b<16) ? '0'+hb : hb;
			return '#'+hr+hg+hb;
		},
		// overwrite properties with same name, otherwise add (different from prototype's
		extend: function(d,s){
			for( var key in s ){
				if( typeof s[key] == "object" ){
					if( d[key]==null ){
						if( s[key].constructor == Array ){
							d[key] = new Array;
						}else{
							d[key] = new Object;
						}
					}
					bp_common.extend(d[key],s[key]);
				}else{
					d[key] = s[key];
				}
			}
		},
		getWindowSize : function(){
			if( !aWindowSize || aWindowSize.length==0 ){ checkDisplay(); }
			return aWindowSize;
		},
		getScrollSize : function(){
			if( !aScrollSize || aScrollSize.length==0 ){ checkDisplay(); }
			var ret=[];
			ret[0] = (aWindowSize[0]>aScrollSize[0]) ? aWindowSize[0] : aScrollSize[0];
			ret[1] = (aWindowSize[1]>aScrollSize[1]) ? aWindowSize[1] : aScrollSize[1];
			return ret;
		},
		hideScrollBars : function(){
			//document.body.style.overflow  = "hidden";
			document.body.style.overflowX = "hidden";
			//document.body.style.overflowY = "hidden";
		},
		resetScrollBars : function(){
			document.body.style.overflow = "";
		},
		getScrollPosition : function(){
			if( !aScrollPosition || aScrollPosition.length==0 ){ checkDisplay(); }
			return aScrollPosition;
		},
		// get img size fits visible area
		getImgSize : function( imgsize ){
			var ret = [];
			var imgrate = imgsize[1]/imgsize[0];
			var displayrate = aWindowSize[1]/aWindowSize[0];
			if( displayrate > imgrate ){
				ret[0] = aWindowSize[0];
				ret[1] = parseInt(ret[0]*imgrate);
			}else{
				ret[1] = aWindowSize[1];
				ret[0] = parseInt(ret[1]/imgrate);
			}
			return ret;
		},
		revivalObstacles : function(){
			var objs = document.getElementsByTagName('object');
			revivalEmbedSelect(objs);
			var eles = document.getElementsByTagName('embed');
			revivalEmbedSelect(eles);
			var selects = document.getElementsByTagName('select');
			revivalEmbedSelect(selects);
			var iframes = document.getElementsByTagName('iframe');
			revivalEmbedSelect(iframes);
		},
		deleteObstacles : function(){
			var objs = document.getElementsByTagName('object');
			deleteEmbedSelect(objs);
			var eles = document.getElementsByTagName('embed');
			deleteEmbedSelect(eles);
			var selects = document.getElementsByTagName('select');
			deleteEmbedSelect(selects);
			var iframes = document.getElementsByTagName('iframe');
			deleteEmbedSelect(iframes);
		},
		getMouseX: function(e){
			if(window.opera)
				return (e.clientX+document.body.scrollLeft);
			else if(document.all)
				return (document.body.scrollLeft || document.documentElement.scrollLeft)+event.clientX
			else if(document.layers||document.getElementById)
				return e.pageX
		},
		getMouseY: function(e){
			if(window.opera)
				return (e.clientY+document.body.scrollTop);
			else if(document.all)
				return (document.body.scrollTop || document.documentElement.scrollTop)+event.clientY
			else if(document.layers||document.getElementById)
				return e.pageY
		},
		cleanupSWFs: function(){
			if(window.opera||!document.all){return;}
			var _2d=document.getElementsByTagName("OBJECT");
			for(var i=0;i<_2d.length;i++){
				_2d[i].style.display="none";
				for(var x in _2d[i]){
					if(typeof _2d[i][x]=="function"){
						_2d[i][x]=function(){};
					}
				}
			}
		},
		prepUnload: function(){
			__flash_unloadHandler=function(){};
			__flash_savedUnloadHandler=function(){};
			if(typeof window.onunload=="function"){
				var _30=window.onunload;
				window.onunload=function(){
					bp_common.cleanupSWFs();
					_30();
				};
			}else if(window.onunload){
				window.onunload=bp_common.cleanupSWFs;
			}
		},
		// usage: bp_common.addListener( window, 'load', bp_common.load, false );
		addListener: function(element, name, observer, useCapture){
			if( !element ){ return; }
			if( element=="DisplayArea" && name=="change" ){
				if( !listeners[element] ){
					listeners[element] = new Object();
				}
				if( !listeners[element][name] ){
					listeners[element][name] = new Array();
				}
				listeners[element][name].push(observer);
				return;
			}
			if (element.addEventListener) {
				element.addEventListener(name, observer, useCapture);
			} else if (element.attachEvent) {
				element.attachEvent('on' + name, observer);
			}
		},
		removeListener: function( element, name, observer, useCapture ){
			if( element=="DisplayArea" && name=="change" ){
				if( !listeners[element] ){ return; }
				if( !listeners[element][name] ){ return; }
				var len = listeners[element][name].length;
				for( var i=0; i<len; i++ ){
					if( listeners[element][name][i]==observer ){
						listeners[element][name][i]=null;
					}
				}
			}
			if (element.removeEventListener) {
				element.removeEventListener(name, observer, useCapture);
			} else if (element.detachEvent) {
				element.detachEvent('on' + name, observer);
			}
		},
		getCookie : function(key){
			var c = document.cookie;
			var d = c.match(key+"=([^ ;]*)");
			return (d) ? d[1] : null;
		},
		// key,value,days
		setCookie : function( k, v, d ){
			day = new Date;
			day.setDate(day.getDate()+d);
			document.cookie = k+"="+v+"; expires="+day.toGMTString()+"; path=/";
		},
		// usage: requestFile( 'p='+p, 'POST', 'http://localhost/collect.pl', true, res );
		requestFile: function( data , method , fileName , async, cb ){
			var h = createHttpRequest()
			h.open( method , fileName , async );
			h.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			h.onreadystatechange = function(){
				if (h.readyState==4){
					cb(h);
				}
			}
			h.send( data );
		},
		correctPNG : function(){ // correctly handle PNG transparency in Win IE 5.5 or higher.
			for(var i=0; i<document.images.length; i++){
				var img = document.images[i]
				var imgName = img.src.toUpperCase()
				if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
				 var imgID = (img.id) ? "id='" + img.id + "' " : ""
				 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				 var imgStyle = "display:inline-block;" + img.style.cssText
				 if (img.align == "left") imgStyle = "float:left;" + imgStyle
				 if (img.align == "right") imgStyle = "float:right;" + imgStyle
				 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				 var strNewHTML = "<span " + imgID + imgClass + imgTitle
				 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
				 img.outerHTML = strNewHTML
				 i = i-1
				}
			}
		},
		asArray : function(obj){
			var l = obj.length;
			var ret = new Array();
			for( var i=0; i<l; i++ ){
				ret.push(obj[i]);
			}
			return ret;
		}
	}
}();


Array.prototype.bpc_each = function(iterator){
	var results = new Array();
	for (var i = 0; i < this.length; i++){
		results.push( iterator(this[i],i) );
	}
	return results;
}
Array.prototype.bpc_filter = function(iterator){
	var results = new Array();
	this.bpc_each( function(v){
		if( iterator(v) ){
			results.push(v);
		}
	});
	return results;
}
Function.prototype.bpc_bind = function(object) {
	var __method = this;
	var __object = object;
	var __arguments = arguments;
	for( var index = 0; index < __arguments.length-1; ++index )
		__arguments[index] = __arguments[index+1];
	__arguments.length = __arguments.length-1;
	return function(){ __method.apply(__object, __arguments); }
}

//usage:
// var bp_sound = new bp_SimpleSound("sound.mp3");
// bp_sound.play();
// better instanciate as fast as u can
var bp_SimpleSound_index=0;
var bp_SimpleSound = function(url,autorepeat){
	this.initialize(url,autorepeat);
};
bp_SimpleSound.prototype = {
	url: '',
	idHead:'bp_SimpleSound',
	id: '',
	index: 0,
	swf:bp_common_server+'SimpleSound.swf',
	el:null,
	autorepeat:false,
	play: function(){
		if( this.el.playSound ){ this.el.playSound(); }
	},
	stop: function(){
		if( this.el.stopSound ){ this.el.stopSound(); }
	},
	// 0-100
	setVol: function(vol){
		if( this.el.setVol ){ this.el.setVol(vol); }
	},
	initialize: function(url,autorepeat){
		this.id = this.idHead+bp_SimpleSound_index;
		this.index = bp_SimpleSound_index;
		this.url = url;
		this.autorepeat = autorepeat;
		bp_SimpleSound_index++;
		var tag = [
		'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="0" height="0" id="' + this.id + '" align="middle">',
		'<param name="allowscriptaccess" value="always" />',
		'<param name="movie" value="'+this.swf+'" />',
		'<param name="quality" value="high" />',
		'<param name="bgcolor" value="#ffffff" />',
		'<embed src="'+this.swf+'" quality="high" bgcolor="#ffffff" width="0" height="0" name="' + this.id + '" align="middle" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />',
		'</object>'
		];
		var txt = tag.join('');
		document.write(txt);
		this.el = navigator.userAgent.match(/MSIE/) ? window[this.id] : document[this.id];
		setTimeout(this.load.bpc_bind(this),100);
	},
	load: function(){
		var ele = this.el;
		if( ele.loadSound ){
			ele.loadSound( this.url, this.autorepeat );
		}else{
			setTimeout(this.load.bpc_bind(this),100);
		}
	}
};


var bp_Interval_interval = 10;
var bp_Interval_timer = null;
var bp_Interval_tid = 0;
var bp_Interval_procs = 0;
var bp_Interval_proc = {};
var bp_Interval_countInit = {};
var bp_Interval_count = {};
var bp_Interval_loop = function(){
	for( var i in bp_Interval_proc ){
		bp_Interval_count[i]--;
		if( bp_Interval_count[i]==0 ){
			bp_Interval_count[i] = bp_Interval_countInit[i];
			bp_Interval_proc[i]();
		}
	}
}
var bp_Interval = function(){
	return{
		// t:[ms]
		set : function( func, t ){
			bp_Interval_tid++;
			bp_Interval_procs++;
			if(typeof func == 'string'){ func = new Function(func); }
			bp_Interval_proc[bp_Interval_tid] = func;
			bp_Interval_countInit[bp_Interval_tid] = parseInt(t/10);
			bp_Interval_count[bp_Interval_tid] = bp_Interval_countInit[bp_Interval_tid];
			if( !bp_Interval_timer ){
				bp_Interval_timer = setInterval(bp_Interval_loop,10);
			}
			return bp_Interval_tid;
		},
		del : function( timerid ){
			if( bp_Interval_proc[timerid] ){
				bp_Interval_proc[timerid]=undefined;
				bp_Interval_countInit[timerid]=undefined;
				bp_Interval_count[timerid]=undefined;
				bp_Interval_procs--;
			}
			if( bp_Interval_procs==0 ){
				clearInterval(bp_Interval_timer);
				bp_Interval_timer=null;
			}
		}
	}
}();

var bp_Effect = function(width,height,ele,effect,targetpos,speed,opt){
	this.initialize(width,height,ele,effect,targetpos,speed,opt);
};
bp_Effect.prototype = {
	size			:null,
	pos				:null,
	timerMove	:null,
	max				:0,
	ele				:null,
	bd				:null,
	effect		:"",
	targetpos	:"",
	option		:null,
	speed			:5,
	opacity		:0,	// 0-100
	initialize: function(width,height,ele,effect,targetpos,speed,opt){
		this.size = new Array();
		this.size[0] = width;
		this.size[1] = height;
		this.pos = new Array();
		this.ele = ele;
		this.effect = effect;
		this.targetpos = targetpos;
		this.speed = speed;
		this.option = new Object();
		this.option = opt;
	},
	start: function(){
		switch(this.effect){
		case "SLIDEDOWN":
			switch(this.targetpos){
			case "CENTER":
				this.pos[0] = parseInt((bp_common.getPageSize()[0]-this.size[0])/2);
				this.pos[1] = -this.size[1];
				this.max = parseInt((bp_common.getPageSize()[1]-this.size[1])/2);
				break;
			case "LEFTTOP":
				this.pos[0] = 0;
				this.pos[1] = -this.size[1];
				this.max = 0;
				break;
			case "OPTIONAL":
				this.pos[0] = this.option.left;
				this.pos[1] = this.option.top;
				this.max = this.option.maxtop;
				break;
			}
			break;
		case "SLIDEUP":
			this.pos[0] = 0;
			this.pos[1] = this.ele.offsetTop-bp_common.getScrollPosition()[1];
			this.max = -this.size[1];
			break;
		case "APPEAR":
		case "DISAPPEAR":
			this.opacity = init;
			var init = (this.option) ?
				((this.option.initopacity!=undefined) ? (this.option.initopacity) : 0 ) : 0;
			var max =  (this.option) ?
				((this.option.targetopacity!=undefined)?(this.option.targetopacity):99.9999):99.9999;
			max = (max==100) ? 99.9999 : max;
			this.opacity = init;
			this.setOpacity( init );
			this.max = max;
			this.ele.style.display='block';
			break;
		}
		this.timerMove = bp_Interval.set(this.move.bpc_bind(this),30);
	},
	isComplete: function(){
		switch(this.effect){
		case "SLIDEDOWN":
			return ( this.pos[1] >= this.max );
		case "SLIDEUP":
			return ( this.pos[1] <= this.max );
		case "APPEAR":
			return ( this.opacity >= this.max );
		case "DISAPPEAR":
			return ( this.opacity <= this.max );
		}
	},
	move: function(){
		switch(this.effect){
		case "SLIDEDOWN":
		case "SLIDEUP":
			this.pos[1] += this.speed;
			var s = this.ele.style;
			if( this.isComplete() ){
				s.top = this.max+bp_common.getScrollPosition()[1]+'px';
				bp_Interval.del(this.timerMove);
				this.timerMove = null;
				this.aftermove();
			}else{
				s.top = this.pos[1]+bp_common.getScrollPosition()[1]+'px';
				s.display='block';
			}
			break;
		case "APPEAR":
		case "DISAPPEAR":
			this.opacity += this.speed;
			var s = this.ele.style;
			if( this.isComplete() ){
				this.setOpacity( this.max );
				bp_Interval.del(this.timerMove);
				this.timerMove = null;
				this.aftermove();
			}else{
				this.setOpacity( this.opacity );
			}
			break;
		}
	},
	aftermove: function(){
	},
	beforeend: function(){
		// usually, rewrite this method after instanciation
	},
	end: function(){
		this.beforeend();
		if( this.timerMove ){
			bp_Interval.del(this.timerMove);
			this.timerMove = null;
		}
		this.afterend();
	},
	afterend: function(){
		// usually, rewrite this method after instanciation
	},
	// op: 0-100
	setOpacity: function(op){
		var el = this.ele;
		el.style.opacity = op/100;
		if( (navigator.userAgent.indexOf('MSIE') != -1) && (!window.opera) ){
			el.style.filter="alpha(opacity=0)";
			el.filters.alpha.Opacity	= op;
		}
	},
	isMoving: function(){
		if( this.timerMove ){
			return 1;
		}else{
			return 0;
		}
	}
};


/**
 * schedular.js :
 *	library to schedule tasks easily
 *
 * Usage and examples :
 *
 * bp_scheduler.init();
 * bp_scheduler.add({
 *	startFunc: "a.a()",
 *	intervalFunc: "a.i()",
 *	endFunc: a.e,
 *	start: 0,
 *	interval: 1000,
 *	end: 4000
 * });
 * bp_scheduler.add({
 *	startFunc: "b.a()",
 *	intervalFunc: "b.i()",
 *	endFunc: "b.e()",
 *	start: 3000,
 *	interval: 500,
 *	end: 5000
 * });
 * bp_scheduler.play();
 * bp_scheduler.pause();
 * bp_scheduler.play();
 *
 * makes this[ms]
 * 0000  1000  2000  3000 			 3500  4000 			 4500  5000
 * a.a() a.i() a.i() a.i();b.a() b.i() a.e();b.i() b.i() b.e()
 *
 * prior added tasks will be fired prior
 * if startFunc defined, intervalFunc will be fired after 1st interval
 *
 *
 * -minimum requirements-
 * bp_scheduler.init();
 * bp_scheduler.add({
 *	intervalFunc: "a.i()",
 *	interval: 1000
 * });
 * bp_scheduler.play();
 *
 * makes this[ms]
 * 0000  1000  2000  3000
 * a.i() a.i() a.i() a.i() ...
 *
 */
var bp_scheduler = {};
bp_scheduler = {
	intervalTime: 10,
	defaultOptions : {
		startFunc: null,
		intervalFunc: null,
		endFunc: null,
		start: 0,
		interval: 20,
		end: null
	},
	startF : [],
	intervalF : [],
	endF : [],
	startT : [],
	startInitT : [],
	intervalT : [],
	intervalInitT : [],
	endT : [],
	endInitT : [],
	schedules : 0,
	timer : null,
	init : function(){
		var len = this.endF.length;
		for( var i=0; i<len; i++ ){
			this.endF[i]();
		}
		this.startF = [];
		this.intervalF = [];
		this.endF = [];
		this.startT = [];
		this.startInitT = [];
		this.endT = [];
		this.endInitT = [];
		this.intervalT = [];
		this.intervalInitT = [];
		this.schedules = 0;
		if( this.timer!=null ){
			clearInterval(this.timer);
		}
		this.timer=null;
	},
	add : function( options ){
		var o = new Object();
		for (var p in this.defaultOptions) {
			var value = options[p];
			if( options[p] ){
				o[p] = options[p];
			}else{
				o[p] = this.defaultOptions[p];
			}
		}

		// judge if err
		if( !o.startFunc && !o.intervalFunc ){
			throw("[bp_scheduler] at least needs startFunc or intervalFunc");
		}
		if( o.startFunc ){
			if( typeof o.startFunc=='string' ){
				o.startFunc = new Function(o.startFunc);
			}
			this.startF[this.schedules] = o.startFunc;
		}
		if( o.intervalFunc && o.interval ){
			if( typeof o.intervalFunc=='string' ){
				o.intervalFunc = new Function(o.intervalFunc);
			}
			this.intervalF[this.schedules] = o.intervalFunc;
			this.intervalInitT[this.schedules] = o.interval/this.intervalTime;
		}
		if( o.endFunc ){
			if( typeof o.endFunc=='string' ){
				o.endFunc = new Function(o.endFunc);
			}
			this.endF[this.schedules] = o.endFunc;
		}
		this.startT[this.schedules] = (o.start!=null) ? (o.start/this.intervalTime) : 0;
		this.startInitT[this.schedules] = this.startT[this.schedules];
		this.endT[this.schedules] = (o.end!=null) ? (o.end/this.intervalTime) : -1;
		if( o.end!=null && this.endT[this.schedules]<this.startT[this.schedules]){
			throw("[bp_scheduler] end time must be bigger than start time");
		}
		this.endInitT[this.schedules] = this.endT[this.schedules];
		this.intervalT[this.schedules] = (this.startT[this.schedules]) ? -1 : 0;
		this.schedules++;
	},
	isReady : function(){
		if ( typeof document.getElementsByTagName != 'undefined'
			&& typeof document.getElementById != 'undefined'
			&& ( document.getElementsByTagName('body')[0] != null
				|| document.body != null ) ){
			return 1;
		}else{
			return 0;
		}
	},
	play : function(){
		this.loop();
		if( this.timer!=null){
			this.pause();
		}
		this.timer = setInterval(this.loop.bpc_bind(this),this.intervalTime);
	},
	pause : function(){
		clearInterval(this.timer);
		this.timer=null;
	},
	reset : function(){
		var len = this.schedules;
		for( var i=0; i<len; i++ ){
			this.endF[i]();
		}
		for( var i=0; i<len; i++ ){
			this.startT[i] = this.startInitT[i];
			this.endT[i]	 = this.endInitT[i];
			this.intervalT[i] = (this.startT[i]) ? -1 : 0;
		}
	},
	loop : function(){
		for( var i=0; i<this.schedules; i++ ){
			if( this.startT[i]==0 ){
				this.startT[i]=-1;
				if( this.startF[i] ){
					this.startF[i]();
					this.intervalT[i] = this.intervalInitT[i];
				}else{
					this.intervalT[i] = 0;
				}
			}
			if( this.endT[i]==0 ){
				this.endT[i]=-1;
				this.intervalT[i]=-1;
				if( this.endF[i] ){
					this.endF[i]();
				}
			}
			if( this.intervalT[i]==0 ){
				this.intervalT[i]=this.intervalInitT[i];
				this.intervalF[i]();
			}
			this.startT[i] =		(this.startT[i] > 0)		? this.startT[i]-1		: this.startT[i];
			this.endT[i] =			(this.endT[i] > 0)			? this.endT[i]-1			: this.endT[i];
			this.intervalT[i] = (this.intervalT[i] > 0) ? this.intervalT[i]-1 : this.intervalT[i];
		}
	}
};

/*---------bp_elementFactory---------
usage:
 new bp_elementFactory(
 document.getElementsByTagName("body")[0],
 [ {"tag" : "div",
		"attr":{"id":"idContainer"},
		"style":{"display":"none",
						 "border" :"none",
						 "width"	:"200px",
						 "height" :"200px",
						 "position":"absolute",
						 "left" 	:"0px",
						 "top"		:"0px"
						},
		"nums":"1",
		"children":[ {"tag" 	: "img",
									"idHead":"eleIdHead",
									"class" :"eleClassName",
									"attr"	:{"src":"blank.jpg"},
									"style" :{"display":"none",
													 "width"	:"100px",
													 "height" :"100px"
													},
									"nums":"2"
							 } ]
	 },
	 {"tag" :"div",
		"attr":{"id":"idText"},
		"style":{"display":"none"},
		"innerHTML":"blahblah"
	 }
 ]);
output:
appends this into body tag
<div id="idContainer" style="display:none; border:none; width:200px; height:200px; position:absolute; left:0px; top:0px;"><img id="eleIdHead0" class="eleClassName" src="blank.jpg" style="display:none; width:100px; height:100px" /><img id="eleIdHead1" class="eleClassName" src="blank.jpg" style="display:none; width:100px; height:100px" /></div><div id="idText" style="display:none;">blahblah</div>
*/
var bp_elementFactory = function(pele,op){
	return this.initialize(pele,op);
};
bp_elementFactory.prototype = {
	defaultoptions : {
		"tag" 	: "div",
		"style" : {"display":"none",
							 "border" :"none",
							 "position":"absolute",
							 "left" 	:"0px",
							 "top"		:"0px" },
		"nums"	: "1" },
	options: null,
	initialize : function(pele,op){
		if( !pele ){ return false; }
		var len = op.length;
		var retele;
		for( var i=0; i<len; i++ ){
			this.options = new Object;
			bp_common.extend( this.options, this.defaultoptions );
			bp_common.extend( this.options, op[i] );
			var ele = document.createElement(this.options["tag"]);
			if( i==0 ){ retele = ele; }
			if( this.options["children"] ){
				new bp_elementFactory(ele,this.options["children"]);
			}
			for( var k in this.options ){
				switch(k){
				case "style":
					var styles = this.options[k];
					if( styles=='nostyle' ){ break; }
					for( var si in styles ){
						if( !ele.style ){ ele.style=new Object(); }
						ele.style[si] = styles[si];
					}
					break;
				case "attr":
					var attrs = this.options[k];
					for( var ai in attrs ){
						ele.setAttribute(ai,attrs[ai]);
					}
					break;
				case "innerHTML":
					ele.innerHTML = this.options[k];
					break;
				default:
					break;
				}
			}
			pele.appendChild(ele);
			var nums = this.options["nums"];
			for( var m=1; m<nums; m++ ){
				var el = ele.cloneNode(true);
				pele.appendChild(el);
			}
		}
		return retele;
	}
};

bp_common.addListener( window, 'load', bp_common.load, false );

/*
 * below code portions, prepUnload, cleanupSWFs are from SWFObject v1.4.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */
if(typeof window.onbeforeunload=="function"){
	var oldBeforeUnload=window.onbeforeunload;
	window.onbeforeunload=function(){
		bp_common.prepUnload();
		oldBeforeUnload();
	};
}else{
	window.onbeforeunload=bp_common.prepUnload;
}
/*
bp_isloaded["bp_common"] = true;
if( bp_load ){
	bp_load.loaded("bp_common");
}
*/
//bp_common.addListener( window, 'load', ____bp.load, false );

