﻿// add to ap.css.
function getElementsByClassNameInID(id,c) {
	var child = document.getElementById(id).childNodes;
	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;
}

function findPos(obj) {
	var o = new Object();
	o.left = 0;
	o.top = 0;
	if (obj.offsetParent) {
		o.left = obj.offsetLeft
		o.top = obj.offsetTop
		while (obj = obj.offsetParent) {
			o.left += obj.offsetLeft
			o.top += obj.offsetTop
		}
	}
	return o;
}

function setOpacity(o,v)
{
	o.style.opacity = v/10;
	o.style.filter = 'alpha(opacity=' + v*10 + ')';
}

/*
function fadeIn(id,v)
{
	v++;
	if (v < 10) {
		setOpacity(id,v);
		setTimeout('fadeIn('+ id +','+ v +')',1000);
	}
}

function fadeOut(id,v){
	
}
*/


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;
	}
	
}

main_runner.control = function(){
	var now = new Date().getTime();
	//dislay_message(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]);
		}
	}
}

function dislay_message(msg){
	document.getElementById("apDebug_msg").innerHTML = "<hr/>" + msg;
}



function apSlideShow(id){
	if (!main_runner.speed) main_runner(42);
	apSlideShowAction();
	this.oDiv = document.getElementById(id);
	this.oDiv.style.position = "absolute";
	this.oDiv.items = new Array();
	// generate the list
	for (var i = 0; i < this.oDiv.childNodes.length; i++) {
		if (this.oDiv.childNodes[i].nodeType == 1)
		{
			n = this.oDiv.items.length;
			this.oDiv.items[n] = this.oDiv.childNodes[i];
			this.oDiv.items[n].num = n;
			this.oDiv.items[n].style.position = "absolute";
			this.oDiv.items[n].style.display="none";
			this.oDiv.items[n].opacity = 0;
		}
	}
	/*
	for (var val in this.oDiv.childNodes) {
		if (this.oDiv.childNodes[val].nodeType == 1)
		{
			n = this.oDiv.items.length;
			this.oDiv.items[n] = this.oDiv.childNodes[val];
			this.oDiv.items[n].num = n;
			this.oDiv.items[n].style.position = "absolute";
			this.oDiv.items[n].style.display="none";
			this.oDiv.items[n].opacity = 0;
		}
	}*/
	// init
	this.oDiv.index = 0;
	this.oDiv.items[this.oDiv.index].style.display="block";
	this.oDiv.items[this.oDiv.index].opacity = 10;
	
	// 500 / 360
	
	this.window_onresize = function(e) {
		this.oDiv.box = findPos(this.oDiv);
		this.oDiv.box.width = this.oDiv.style.width.substring(0, this.oDiv.style.width.indexOf("px"));
		this.oDiv.box.height = this.oDiv.style.height.substring(0, this.oDiv.style.height.indexOf("px"));
	}
	this.window_onresize();
	
	this.oDiv.onmousemove = function(e) {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		this.mouseX = (posx - this.box.left);
		this.mouseY = (posy - this.box.top);
		if (elt_back = document.getElementById("apSlideShow_tools_back")) {
			elt_back.style.opacity = ( ((1-(this.mouseX/this.box.width))/2) + 0.5 );
			elt_back.style.filter = 'alpha(opacity=' + ( ((1-(this.mouseX/this.box.width))/2) + 0.5 )*100 + ')';
		}
		if (elt_next = document.getElementById("apSlideShow_tools_next")) {
			elt_next.style.opacity = ( ((this.mouseX/this.box.width)/2) + 0.5 );
			elt_next.style.filter = 'alpha(opacity=' + ( ((this.mouseX/this.box.width)/2) + 0.5 )*100 + ')';
		}
	};
	
	this.oDiv.onclick = function() {
		if ((this.mouseX - (this.box.width / 2)) <= 0) {
			this.back();
		} else {
			this.next();
		}
	}
	if (elt_back = document.getElementById("apSlideShow_tools_back")) {
		elt_back.onclick = function() {
			document.getElementById(id).back();
		}
	}
	if (elt_next = document.getElementById("apSlideShow_tools_next")) {
		elt_next.onclick = function() {
			document.getElementById(id).next();
		}
	}
	this.oDiv.next = function(){
		//this.items[this.index].style.display = "block";
		this.items[this.index].action = this.fadeOut;
		apSlideShowAction.add(this.items[this.index]);
		this.index++;
		if (this.index >= this.items.length) this.index = 0;
		this.items[this.index].action = this.fadeIn;
		apSlideShowAction.add(this.items[this.index]);
	}
	this.oDiv.back = function(){
		//this.items[this.index].style.display = "block";
		this.items[this.index].action = this.fadeOut;
		apSlideShowAction.add(this.items[this.index]);
		this.index--;
		if (this.index < 0) this.index = this.items.length-1;
		this.items[this.index].action = this.fadeIn;
		apSlideShowAction.add(this.items[this.index]);
	}
	
	this.oDiv.fadeIn = function(o){
		//dislay_message("fadeIn:" + o.action);
		o.opacity++;
		if (o.opacity > 10) {
			o.opacity = 10;
			apSlideShowAction.remove(o);
			o.style.display = "block";
		} else {
			o.style.opacity = o.opacity/10;
			o.style.filter = 'alpha(opacity=' + o.opacity*10 + ')';
		}
	}
	
	this.oDiv.fadeOut = function(o){
		//dislay_message("fadeOut:" + o.action);
		o.opacity--;
		if (o.opacity < 0) {
			o.opacity = 0;
			apSlideShowAction.remove(o);
			o.style.display = "none";
		} else {
			o.style.opacity = o.opacity/10;
			o.style.filter = 'alpha(opacity=' + o.opacity*10 + ')';
		}
	}
	document.getElementById("apSlideShow_msg").action = this.oDiv.fadeOut;
	document.getElementById("apSlideShow_msg").opacity = 10;
	apSlideShowAction.add(document.getElementById("apSlideShow_msg"));	
}

function apSlideShowAction(){
	if (!apSlideShowAction.storage) apSlideShowAction.storage = new Array();
}

apSlideShowAction.add = function(o){
	apSlideShowAction.storage[apSlideShowAction.storage.length] = o;
	if(apSlideShowAction.storage.length == 1) main_runner.add(apSlideShowAction.control);
}

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

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

apSlideShowAction.msg = function(msg){
	document.getElementById("apSlideShow_msg").innerHTML = msg;
}



function onload_initDelay() {
	
}

function onload_init() {
	initD = setTimeout('onload_initDelay()',1000);
	slideShow = new apSlideShow("apSlideShow_main");
	//alert(document.lastModified);
}	

function onresize_init() {
	slideShow.window_onresize();
}	




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