/********************************************************************
* main_runner														*
********************************************************************/


function main_runner(speed){
	main_runner.storage = new Array();
	main_runner.speed = speed;
}

main_runner.add = function(f){
	main_runner.storage[main_runner.storage.length] = f;
	if(main_runner.storage.length == 1) this.timerID = window.setInterval("main_runner.control()",main_runner.speed);
}

var count = 0;

main_runner.remove = function(f){
	for(var i=0;this.storage[i];i++){
		if(f == this.storage[i]){
			this.storage = this.storage.slice(0,i).concat(this.storage.slice(i+1));
			//break;
		}
	}
	if(this.storage.length == 0){
		window.clearInterval(this.timerID);
		this.timerID=null;
		dislay_message("msg","");
	}
	
}

main_runner.control = function(){
	var now = new Date().getTime();
	dislay_message("msg",this.storage.length + " : " +this.storage[0] + " : " + now);
	for(var i=0; this.storage[i]; i++){
		if(typeof this.storage[i] == "function") {
			this.storage[i]();
		} else {
			eval(this.storage[i]);
		}
	}
	
}
/********************************************************************
* Commun															*
********************************************************************/

function dislay_message(id,msg){
	//document.getElementById(id).innerHTML = msg;
}


/* Return Click dont follow link */
function apCR() {   
	return false; 
}

/********************************************************************
* AP Usefull function												*
********************************************************************/

function in_array(the_needle, the_haystack){
	var the_hay = the_haystack.toString();
	if(the_hay == ''){
		return false;
	}
	var the_pattern = new RegExp(the_needle, 'g');
	var matched = the_pattern.test(the_haystack);
	return matched;
}

function print_r(theObj){
	if(theObj.constructor == Array ||theObj.constructor == Object) {
		document.write("<ul>")
		for(var p in theObj){
			if(theObj[p].constructor == Array||theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>");
				print_r(theObj[p]);
				document.write("</ul>");
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
    	document.write("</ul>")
	}
}

/********************************************************************
* AP style	groupe setings												*
********************************************************************/


function ap(){}

ap.css = function(){}

ap.css.setStyleByID = function(i,p,v){ /* e: obj.el -> html Element; p: css Property; v: css Value */
	var n = document.getElementById(i);
	n.style[p] = v;
}

ap.css.setStyle = function(e,p,v){ /* e: obj.el -> html Element; p: css Property; v: css Value */
	e.el.style[p] = v;
}

ap.css.getStyleById = function (i, p) { /* i: id; p: css Property */
	var n = document.getElementById(i);
	var s = eval("n.style." + p);
	if((s != "") && (s != null)) { // inline
		return s; 
	}
	if(n.currentStyle) { // currentStyle
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) {
			return s;
		}
	}
	var sheets = document.styleSheets;
	if(sheets.length > 0) { // styleSheets
		for(var x = 0; x < sheets.length; x++) {
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					if(((z[p] != "") && (z[p] != null)) || (rules[y].selectorText == i)) {
						return z[p];
					}
				}
			}
		}
	}
	return null;
}

ap.css.setStyleByTag = function(t, p, v) { /* t: tag; p: css Property; v: css Value */
	var elements = document.getElementsByTagName(t);
	for(var i = 0; i < elements.length; i++) {
		elements.item(i).style[p] = v;
	}
}

ap.css.toogleClassByID = function(id, c1, c2){ /* id; class 1; class 2; */
	if (el = document.getElementById(id)) {
		if (el.className == c1) el.className = c2;
			else el.className = c1;
			return true;
	}
	return false;
}

ap.css.changeClassByID = function(id, c){ /* id; class 1; */
	if (el = document.getElementById(id)) {
		el.className= c;
		return true;
	}
	return false;
}

ap.css.replaceClassByClass = function(c1,c2){ /* class 1; class 2;*/
	ela = this.getElementsByClassName(c1);
	for (var i = 0; i < ela.length; i++) {
		ela[i].className= c2;
	}
}

ap.css.addClassById = function(id, c){ /* id; class 1; */
	if (el = document.getElementById(id)) {
	
		arr = el.className.split(' ');
		
		if (in_array(arr, c)) return true;
		
		if (el.className=='') el.className =  c;
		else el.className +=  ' '+c;
		return true;
	}
	return false;
}

ap.css.removeClassById = function(id, c){ /* id; class 1; */

	if (el = document.getElementById(id)) {
		
		arr = el.className.split(' ');

		nclass ='';
		for (var p in arr ) {
		
			if (arr[p]!=c) {
			
				if (nclass!= '') nclass+=' ';
				nclass += arr[p];
			}
		}
		
		
		el.className =  nclass;
	
		return true;
	}
	return false;
}



ap.css.getElementsByClassName = function(c) {
	var child = document.body.getElementsByTagName('*');
	var ela = new Array(); // element array
	for (var i = 0; i < child.length; i++) { 
		if (child[i].className.match(new RegExp("(^|\\s)" + c + "(\\s|$)"))) {
			ela.push(child[i]);
		}	
	}
	return ela;
}

/********************************************************************
* Scroll															*
********************************************************************/

function apScroll(el,cont,dir,s){ // id, container, direction, (angle)
	this.id = el.id;
	this.index = 0;
	this.el = document.getElementById(this.id);
	this.cont = cont;
	this.dir = dir;
	this.s = (s) ? s : 5;
	
	this.setStyleElement = function(e){
		this.styleElement = e;
	}
	
	this.callBack = false;
	
	this.addCallBack = function(f){
		this.callBack = f;
	}
	
	this.initObj = apScroll.initObj;
	if (!apScroll.storage) apScroll.storage = new Array();
	this.doScroll = apScroll.doScroll;
	apScroll.remove(this.id);
	apScroll.add(this);
	apScroll.init();
	return this;
}


apScroll.init = function(){
	for(var i=0; apScroll.storage.length > i;i++){
		var thisObj = apScroll.storage[i];
		if(thisObj) thisObj.initObj();
	}
	
	main_runner.remove(apScroll.control);
	main_runner.add(apScroll.control);

}

apScroll.initObj = function(){
	this.el = document.getElementById(this.id);
}

apScroll.add = function(o){
	apScroll.storage[apScroll.storage.length] = o;
}

apScroll.remove = function(id){
	for(var i=0;this.storage[i];i++){
		if(id == this.storage[i].id){
			this.storage = this.storage.slice(0,i).concat(this.storage.slice(i+1));
		}
	}
	if(this.storage.length == 0){
		main_runner.remove(apScroll.control);
	}
}

apScroll.control = function(){
	var function_name = "apScroll.control";
	for(var i=0; apScroll.storage.length > i;i++){
		var thisObj = apScroll.storage[i];
		if(thisObj) thisObj.doScroll();
	}
}

apScroll.doScroll = function(){
	var dis = (this.dir != 'up') ? - this.s : this.s;
	
	var btPos = this.el.offsetHeight + parseInt(this.el.style.top.replace('px',''));
	var tpPos = parseInt(this.el.style.top.replace('px',''));
	
	if ( btPos > this.cont.offsetHeight && this.dir == 'up') {
		this.el.style.top = (parseInt(this.el.style.top.replace('px','')) - dis) + 'px';
	} else if ( tpPos < 0 && this.dir != 'up') {
		this.el.style.top = (parseInt(this.el.style.top.replace('px','')) - dis) + 'px';
	} else if (this.callBack) {
		this.callBack(this.id);
		apScroll.remove(this.id);
	} else {
		
		apScroll.remove(this.id);
	}
	
}