
//
// Initialize Variables
//

var locationMatching;
if(locationMatching = location.search.match(/([1-9][0-9]*|-[1-9][0-9]*)/)) {
	locationMatching = parseInt(locationMatching[1]);
	if(locationMatching < firstPage || locationMatching > lastPage)
		initPage = firstPage - (spreadPage && firstPage % 2 == 1 ? startRight : 0);
	else
		initPage = locationMatching;
}
else {
	initPage = firstPage;
	// initPage = firstPage - (spreadPage && firstPage % 2 == 1 ? startRight : 0);
}

IconClickEffect = Effect.Highlight; // Effect.Shake, Effect.Pulsate, Effect.Highlight

//
//	Utility Functions
//

String.prototype.trim = function() {
	var x	=	this;

	x	=	x.replace(/^\s*(.*)/, '$1');
	x	=	x.replace(/(.*?)\s*$/, '$1');

	return x;
}

function NewEl(tag, id) {
	var el = document.createElement(tag);

	if(id)
		el.id = id;

	return el;
}

function NewText(str) {
	return document.createTextNode(str);
}

function NewImg(src, id) {
	var img = NewEl('img', id);
	img.src = src;
	return img;
}

function NewCol(row, id) {
	var td = NewEl('td', id);
	$(row).appendChild(td);
	return td;
}

function AlignCenter(el) {
	var parentWidth = $(el).parentNode.offsetWidth;
	var parentLeft = Math.round((parentWidth - $(el).offsetWidth) / 2);
	$(el).parentNode.style.paddingLeft = parentLeft + 'px';
	$(el).parentNode.style.width = (parentWidth - parentLeft) + 'px';
	return parentLeft;
}

function RenderFade(el) {
	Element.hide(el);
	Effect.Appear(el);
}

//
//	Utility Classes
//

Roller = {
	'Set': function(el, overImg) {
		$(el).overSrc = overImg;
		$(el).onmouseover = this.Over;
		$(el).onmouseout = this.Out;
	},

	'Over': function() {
		if(typeof this.overSrc == 'undefined')
			return;
	
		var over = this.overSrc;
		this.overSrc = this.src;
		this.src = over;
	},

	'Out': function() {
		if(typeof this.overSrc == 'undefined')
			return;
	
		var src = this.overSrc;
		this.overSrc = this.src;
		this.src = src;
	}
};

Setter = {
	'Set': function(el, onImg) {
		$(el).turn = Setter.Turn;
		$(el).onSrc = onImg;
		$(el).turned = false;
		$(el).onclick = function() {
			this.turn(!this.turned);
		};
	},

	'Turn': function(onoff) {
		if(onoff == this.turned)
			return;

		this.turned = onoff;
		Setter.Control(this);
	},

	'Control': function(el) {
		var result = true;
		var tmp;

		if(el.turned) {
			if(typeof el.onTurnOn == 'function')
				result = el.onTurnOn() != false ? true : false;

			if(typeof el.onturnon == 'function')
				result = result && el.onturnon() != false;

			if(!result) {
				el.turned = false;
				return;
			}
		}
		else {
			if(typeof el.onTurnOff == 'function')
				result = el.onTurnOff() != false ? true : false;
	
			if(typeof el.onturnoff == 'function')
				result = result && el.onturnoff() != false;
	
			if(!result) {
				el.turned = true;
				return;
			}
		}

		tmp = el.onSrc;
		el.onSrc = el.src;
		el.src = tmp;
	}
};
