$(document).ready(function() {
	var anchor,image;
	var images=[];
	var originaltitle;
	var resizeimage=true;
	var nextimage;

	function URLForIndex(index)
	{
		return comicurl+images[index];
	}

	function AnchorNameForIndex(index)
	{
		return "page-"+(index+1);
	}

	function IndexForAnchorName(aname)
	{
		var matches=/page-([0-9]+)/.exec(aname);
		if(matches) return parseInt(matches[1])-1;
		else return 0;
	}

	function MakeBrowseControl(label,num,next,func)
	{
		var el;

		if(next!=num && next>=0 && next<images.length)
		{
			el=document.createElement("a");

			el.href="#"+AnchorNameForIndex(next);

			$(el).click(function(event) {
				PickImage(next);

				document.location="#"+AnchorNameForIndex(next);
				event.preventDefault();
			});
		}
		else
		{
			el=document.createElement("span");
		}

		$(el).addClass("control");
		$(el).text(label);

		return el;
	}

	function MakeResizeControl(num,state)
	{
		var el=document.createElement("a");
		el.href="#"+AnchorNameForIndex(num);

		$(el).click(function(event) {
			resizeimage=state;

			ImageDidLoad();

			$("#controls").remove();
			$("#reader").append(RemakeControls(num));

			document.location="#"+AnchorNameForIndex(num);
			event.preventDefault();
		});

		$(el).addClass("control");

		if(state) $(el).text("Fit page on screen");
		else $(el).text("Show full size");

		return el;
	}

	function RemakeControls(num)
	{
		var controls=document.createElement("div");
		controls.id="controls";
		$(controls)
			.append(MakeBrowseControl("<< Last",num,images.length-1))
			.append(" ")
			.append(MakeBrowseControl("< Next",num,num+1))
			.append(" ")
			.append(MakeResizeControl(num,!resizeimage))
			.append(" ")
			.append(MakeBrowseControl("Previous >",num,num-1))
			.append(" ")
			.append(MakeBrowseControl("First >>",num,0));
		return controls;
	}

	function PickImage(num)
	{
		image.src=URLForIndex(num);
		document.title=originaltitle+" (Page "+(num+1)+")";

		if(num+1<images.length)
		{
			nextimage=new Image();
			nextimage.src=URLForIndex(num+1);
		}

		anchor.name=AnchorNameForIndex(num);

		$("#controls").remove();
		$("#reader").append(RemakeControls(num));
	}

	function ImageDidLoad()
	{
		var height=$(window).height();
		image.style.height="";
		if(resizeimage&&image.height>height) image.style.height=height+"px";
	}


	$.get(comicurl,null,function(data,status) {
		var regex=/\shref=(["'])(.*?\.(?:jpg|png|gif))\1/mig;
		var matches;
		while(matches=regex.exec(data)) images.push(matches[2]);

		if(images.length>0)
		{
			var currimage=0;

			var matches=/#(.*)/.exec(document.location);
			if(matches) currimage=IndexForAnchorName(matches[1]);
			else currimage=0;

			if(currimage<0) currimage=0;
			if(currimage>=images.length) currimage=images.length-1;

			originaltitle=document.title;

			anchor=document.createElement("a");

			image=document.createElement("img");
			$(image).load(ImageDidLoad);

			var controls=RemakeControls(currimage);

			$("#reader").append(anchor).append(image).append(controls);

			PickImage(currimage);

			$(image).click(function() {
				$("#controls a:contains(Next)").click();
			});
		}
	},"html");
});

