// Arekore-Request
// 1: Require and import libraries. (JavaScript-file, StyleSheet-file, and ...)
// 2: Add class for inserted-table at the time of the cache of a search-engine.
	// (c) snow-materia "http://sm.useyan.com/"


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	setting
// add class attribute to the first table at the cached page ?
var req_cache = 'cache';

// base-URL for import files.
var base = 'http://coconutsmilk.com/';
if (navigator.userAgent.indexOf("Mac") >= 0) base = base.replace(/~/, '%7E'); // mac?

var request = [
	// Script, Link-element
	{
		parent_id  : '',	// inserted parent element-id.
		parent_tag : 'HEAD',	// if not parent-id, inserted parent element-tag-name.
		position : -1,	// inserted position. [-1:last, 0:first, 1:second, 2:third, ...]
		timing   : 0,	// timing of causing window-event. [0:soon, 1:onload]
		tree : [	// element-tree
		// SCRIPT
/*
			// Arekore-Trick-OUT
			{'SCRIPT' : {
				'charset' : 'UTF-8',
				'src' : base + 'common/js/ArekoreTrick.js'
			}},
*/
			// Arekore-LinkList
			{'SCRIPT' : {
				'charset' : 'UTF-8',
				'src' : base + 'common/js/ArekoreLink.js'
			}},
/*
			// Arekore-TextArea
			{'SCRIPT' : {
				'charset' : 'UTF-8',
				'src' : base + 'common/js/ArekoreArea.js'
			}},
*/
			// CSS-Change-Script
			{'SCRIPT' : {
				'charset' : 'UTF-8',
				'src' : base + 'common/js/ccs.js'
			}},
/*
			// Arekore-TextArea
			{'LINK' : {
				'rel' : 'stylesheet',
				'type' : 'text/css',
				'media' : 'screen,tv',
				'href' : base + 'common/js/ArekoreArea.css'
			}},
*/
		// LINK
			// About
			{'LINK' : {
				'rel' : 'help',
				'title' : 'About',
				'href' : base + 'about/'
			}},

			// Diary
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'Diary',
				'href' : base + 'diary/'
			}},
			// Works
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'Works',
				'href' : base + 'works/'
			}},
			// Story
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'Story',
				'href' : base + 'story'
			}},
			// Others
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'Others',
				'href' : base + 'others/'
			}},
			// BBS
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'BBS',
				'href' : base + 'pbbs/'
			}},
			// Links
			{'LINK' : {
				'rel' : 'chapter',
				'title' : 'Links',
				'href' : base + 'links/'
			}},

			// Favorite Icon
			{'LINK' : {
				'rel' : 'shortcut icon',
				'title' : 'Favorite Icon',
				'type' : 'image/x-icon',
				'href' : base + 'favicon.ico'
			}},

		// META
			// Author
			{'META' : {
				'name' : 'author',
				'content' : 'Coco Asami'
			}},

		0]
	},
	// lastModified
/*
	{
		parent_id  : 'footer',	// inserted parent element-id.
		parent_tag : '',	// if not parent-id, inserted parent element-tag-name.
		position : -1,	// inserted position. [-1:last, 0:first, 1:second, 2:third, ...]
		timing   : 1,	// timing of causing window-event. [0:soon, 1:onload]
		tree : [	// element-tree
			// child[0]
			{'DT' : '\u66f4\u65B0'},	// text (*abbreviation)
			// child[1]
			{'DD' : {
				child : [document.lastModified]	// lastModified
			}},
		0]
	},
*/
	// Web-Clap
	{
		parent_id  : 'footer',	// inserted parent element-id.
		parent_tag : 'h2',	// if not parent-id, inserted parent element-tag-name.
		position : -1,	// inserted position. [-1:last, 0:first, 1:second, 2:third, ...]
		timing   : -1,	// timing of causing window-event. [0:soon, 1:onload]
		scr_syntax : 1,	//  syntax at IE or Opera. [0:`createElemnt`, 1:`write`]
		tree : [
				{'P' : {
					'id' : 'pati',
					child : [
								{'A' : {
									'href' : base + 'clap/index.cgi',
										child : ['WEB-Clap']
								}}
							]
				}}
			]
	},

0];


//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	script
// create objects initiarize (get last-modified)
function req_create_objects_init(ori) {
	var d = document;
	if (!ori || !ori.tree || !ori.tree.length) return false;

	// to
	var to;
	if (ori.parent && typeof(ori.parent) == 'object' && ori.parent.nodeType == 1)
		to = ori.parent;
	if (!to && ori.parent_id)
		to = d.getElementById(ori.parent_id);
	if (!to && ori.parent_tag)
		to = d.getElementsByTagName(ori.parent_tag)[0];
	if (!to) return false;

	// inserted-position ([-1:last, 0:first, 1:second, 2:third, ...])
	var leng = to.childNodes.length;
	var pos  = ori.position - 0 || 0;
	if (leng == 0) pos = -1;
	else if (pos <  0 && -pos >  leng) pos = 0;
	else if (pos <  0 && -pos <= leng) pos += 1 + leng;
	if (pos >= 0 &&  pos >= leng) pos = -1;

	// create-objects
	req_create_objects(to, pos, ori.tree);
	return true;
}
// create object
function req_create_objects(to, pos, from) {
	var d = document;
	// string
	if (typeof(from) != 'object') {
		from += '';
		var text = d.createTextNode(from);
		// insert
		if (pos == -1) to.appendChild(text);
		else to.insertBefore(text, to.childNodes[pos]);
		return;
	}
	// array
	for (var i = 0; i < from.length; i++) {
		var from_i = from[i];
		if (!from_i) continue;
		// string
		if (typeof(from_i) != 'object') {
			// loop
			req_create_objects(to, pos, from_i);
			continue;
		}
		// object
		for (var k in from_i) {
			if (!k || !from_i[k]) continue;
			var obj = d.createElement(k);
			var content = from_i[k];
			// string
			if (typeof(content) != 'object') {
				// insert
				if (pos == -1) to.appendChild(obj);
				else to.insertBefore(obj, to.childNodes[pos]);
				// loop
				req_create_objects(obj, -1, content);
				continue;
			}
			// object
			for (var at in content) {
				if (!at || at == 'child') continue;	// if child
				obj.setAttribute(at, content[at]);
				if (at == 'class' && typeof(obj.className) == 'string')
					obj.className = content[at];
			}
			// insert
			if (pos == -1) to.appendChild(obj);
			else to.insertBefore(obj, to.childNodes[pos]);
			// childNodes
			if (content['child']) {
				// loop
				req_create_objects(obj, -1, content['child']);
			}
		}
	}
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	cache
// add class for inserted-table at the time of the cache of a search-engine.
function req_cache_table(cache) {
	var href = (location.href || '').toLowerCase();
	if (href.indexOf('cache') < 0 || href.indexOf('search') < 0) return false;
	if (!cache) cache = 'cache';
	// first-table
	var table = document.getElementsByTagName('TABLE')[0];
	if (table) {
		var cl  = table.getAttribute('class') || table.className || '';
		var cls = (!cl) ? cache : cl + ' ' + cache;
		table.setAttribute('class', cls);
		if (typeof(table.className) == 'string') table.className = cls;
	}
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	request
// addEvent ?
function req_addEvents(type, func, capt, obj) {
	if (obj.addEventListener) obj.addEventListener(type, func, capt);	// DOM2?
	else if (obj.attachEvent) obj.attachEvent('on'+type, func);	// IE?
	else {	// other
		var prev = obj['on'+type];
		obj['on'+type] = (prev) ? function (e) { prev(e); func(e); } : func;
	}
}
// request-load
function req_loader() {
	var load = 0;
	for (var q = 0; q < request.length; q++) {
		var req = request[q];
		if (!req || typeof(req) != 'object') continue;
		if (req.timing) { load++; req.timing = 0; continue; }	// load
		var ok = req_create_objects_init(req);
		if (ok) delete request[q];	// delete
	}
	if (!load) request = null;
	return load;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	omake
// in object
function each_object(obj) {
	if (typeof(obj) != 'object') return false;
	var key = new Array();
	for (var k in obj) { key[key.length] = k; }
	key = key.sort();
	return key.join('\n');
}


// window x-y coordinate
function req_xy(e) {
	// !ie || ie-strict || ie-transitional
	var d = document;
	// window-size
	this.inner_x = window.innerWidth
		 || d.documentElement.clientWidth
		 || (d.body ? d.body.clientWidth : 0);
	this.inner_y = window.innerHeight
		 || d.documentElement.clientHeight
		 || (d.body ? d.body.clientHeight : 0);
	// scroll-position
	this.scroll_x = window.pageXOffset
		 || d.documentElement.scrollLeft
		 || (d.body ? d.body.scrollLeft : 0);
	this.scroll_y = window.pageYOffset
		 || d.documentElement.scrollTop
		 || (d.body ? d.body.scrollTop : 0);
	// absolute-cursor-position
	this.absolute_x = (e) ? e.pageX || e.clientX + scroll_x : 0;
	this.absolute_y = (e) ? e.pageY || e.clientY + scroll_y : 0;
	// window-cursor-position
	this.window_x = (e) ? e.clientX || e.pageX - scroll_x : 0;
	this.window_y = (e) ? e.clientY || e.pageY - scroll_y : 0;
	// element-cursor-position
	this.element_x = (e) ? e.offsetX || e.layerX : 0;
	this.element_y = (e) ? e.offsetY || e.layerY : 0;

	return this;
}




//	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	-	start ?
// cache-page ?
if (req_cache) req_cache_table(req_cache);
req_cache = null;
base = null;


// soon ?
if (request && request.length) {
	var load = req_loader();	// soon
	if (load) req_addEvents('load', req_loader, false, window);	// onload
}
