if(typeof(DM) == "undefined") { DM={}; }
DM.hcwh = {

	// prevent click madness
	locked: false,

	// current frame
	p: 0,
	
	// width of a frame
	w: 364,

	// animate one frame up or down
	animate:function(dir){

		// requested frame position
		r = this.p + dir;

		// prevent out of range
		if(r < 0) {
			return;
		}

		// confirm next frame
		this.p = r;

		// animate element to new position
		$("#hcwh_list").animate( {left:(this.p * this.w) * -1+"px"}, 500, 'swing', function(){
			DM.hcwh.locked = false;
		} );

	},

	// load the next frame
	loadNext: function(id) {

		if(this.locked) {
			return;
		}
		this.locked = true;

		str = '<li class="list-item">'+$(id).html()+'</li>';
		$("#hcwh_list").append(str);
		this.animate(1);
	},

	// go back by x frames
	goBack: function(cls) {

		if(this.locked) {
			return;
		}
		this.locked = true;

		// extract the number of steps back from supplied classname
		i = cls.replace('back','')
		this.p += (i * -1);
		$("#hcwh_list").animate( {left:(this.p * this.w) * -1+"px"}, 500, 'swing',function(){
			// zap the LIs no longer needed
			while(i > 0) {
				$("#hcwh_list li.list-item").last().remove();
				i --;
			}
			DM.hcwh.locked = false;
		} );
	},

	home: function() {

		if(this.locked) {
			return;
		}
		this.locked = true;

		this.p = 0;

		$("#hcwh_list").animate( {left:0}, 500, 'swing', function(){
			// remove any extras
			$("#hcwh_start").nextAll().remove();
			DM.hcwh.locked = false;
		} );
	}
}

// event handlers
$(function(){

	$("#hcwh_list ul.hchw_another a").live("click",function(e){
		e.preventDefault();
		DM.hcwh.home();
	})

	$("#hcwh_list ul.hchw_links a").live("click",function(e){
		e.preventDefault();
			// love you IE7...
			str = $(this).attr("href").split('#');
			str = '#' + str[1];
		DM.hcwh.loadNext(str);
	})

	$("#hcwh_list ul.hchw_backlinks li").live("click",function(e){
		DM.hcwh.goBack($(this).attr("class"));
	})

})
