
function LoopCount(min, max) {
	this.count = min;
	this.min = min;
	this.max = max;
	this.range = null;
	this.auto = false;
}

LoopCount.prototype.toHash = function(auto, d) {
	if (auto) {
		var c = "auto";
	} else {
		if (d) {
			c = this.peek(d);		
		} else {
			c = this.count;
		}
	}
	
	if (this.range == null) {
		return "#" + c;
	} else {
		return "#" + c + "-" + this.range[0] + "-" + this.range[1];
	}
}

LoopCount.prototype.setRange = function(range) {
	if (range == null) {
		this.range = null;
	} else {
		var min = range[0];
		var max = range[1];
		if (min < this.min) {
			min = this.min;
		}
		if (this.max < max) {
			max = this.max;
		}
		this.range = [min, max];
	}
	this.setCount(this.count);
}

LoopCount.prototype.include = function(n) {
	if (this.range) {
		return this.range[0] <= n && n <= this.range[1];
	} else {
		return this.min <= n && n <= this.max;
	}
}

LoopCount.prototype.move = function(d) {
	this.count = this.peek(d);
	return this.count;
}

LoopCount.prototype.peek = function(d) {
	var c = this.count + d;
	var r = this.currentRange();
	if (c < r[0]) {
		c = r[1];
	} else if (r[1] < c) {
		c = r[0];
	}
	return c;
}

LoopCount.prototype.current = function() {
	return this.count;
}

LoopCount.prototype.currentRange = function() {
	if (this.range) {
		return this.range;
	} else {
		return [this.min, this.max];
	}
}

LoopCount.prototype.setCount = function(n) {
	if (this.include(n)) {
		this.count = n;
	} else {
		var r = this.currentRange();
		this.count = r[0];
	}
}

LoopCount.prototype.parseHash = function() {
	var h = location.hash;
	if ((result = h.match(/^#(auto|\d+)(?:-(\d+)-(\d+))?/)) == null) {
		return false;
	}
	if (result[2]) {
		var min = new Number(result[2]);
		var max = new Number(result[3]);
		this.setRange([min, max]);
	} else {
		this.setRange(null);
	}

	if (result[1] == "auto") {
		this.auto = true;
	} else {
		this.auto = false;
		this.setCount(new Number(result[1]));
	}
}



					
					

function Slide(imageCount, autoWait) {
	this.elements = {};
	
	this.elements.image = document.getElementById("mainPhoto");
	this.elements.imageLink = document.getElementById("imageLink");
	this.elements.back = document.getElementById("back");
	this.elements.next = document.getElementById("next");
	this.elements.counter = document.getElementById("count");
	this.elements.auto = document.getElementById("autoButton");
	
	this.elements.title = document.getElementById("title");
	this.elements.content = document.getElementById("contentBody");
	this.elements.buynow = document.getElementById("buynow");
	this.elements.dictionary = document.getElementById("dictionary");

	this.autoWait = autoWait;
	this.lastMove = new Date().getTime();
	
	this.count = new LoopCount(1, imageCount);

	this.updateCount();
	this.show();
}

Slide.prototype.show = function() {
	var c = this.count.current();
	this.elements.image.src = "./img/" + c + ".jpg";
	this.elements.counter.innerHTML = c;
	this.elements.next.href = this.count.toHash(false, 1);
	this.elements.back.href = this.count.toHash(false, -1);
	
	if (this.count.auto) {
		this.elements.imageLink.href = this.count.toHash(false);
	} else {
		this.elements.imageLink.href = this.count.toHash(false, 1);
	}
	
	var parseUrl =  "./xml/" + c + ".xml";
	var parseHttp = new JKL.ParseXML( parseUrl );
	var parseData = parseHttp.parse();
	this.elements.title.innerHTML = parseData.item.title;
	this.elements.content.innerHTML = parseData.item.body;
	this.elements.buynow.href = parseData.item.storeUrl;
	this.elements.dictionary.href = parseData.item.dictionaryUrl;
}


Slide.prototype.updateCount = function() {
	if (this.hash == location.hash) {
		return false;
	}
	this.hash = location.hash;
	
	var currentAuto = this.count.auto;
	this.count.parseHash();
	
	if (currentAuto && ! this.count.auto) { // stop auto
		this.elements.auto.innerHTML = "<img src=\"./img/play.gif\" onmouseover=\"this.src='./img/play_on.gif'\" onmouseout=\"this.src='./img/play.gif'\" />";
		this.elements.auto.href = this.count.toHash(true);
	} else if (! this.count.auto) {
		this.elements.auto.innerHTML = "<img src=\"./img/play.gif\" onmouseover=\"this.src='./img/play_on.gif'\" onmouseout=\"this.src='./img/play.gif'\" />";
		this.elements.auto.href = this.count.toHash(true);
	} else {
		this.elements.auto.innerHTML = "<img src=\"./img/stop.gif\" onmouseover=\"this.src='./img/stop_on.gif'\" onmouseout=\"this.src='./img/stop.gif'\" />";
		this.elements.auto.href = this.count.toHash(false);
	}
	return true;
}

Slide.prototype.update = function() {
	this.updateCount();

	if (this.count.auto) {
		var t = new Date().getTime();
		if (this.autoWait < t - this.lastMove) {
			this.lastMove = new Date().getTime();
			this.count.move(1);
			this.show();
		}
	} else {
		this.show();
	}
}


