var desc0;
var check=true;
var slidey;
var slideSpeed = 1000;
var slideImgWidth = 564;

function change(n,link) {
	var page = $("#pages div[id^='page']:eq("+n+")");
	$("#pages div[id^='page']:visible").hide();
	page.show();
	if(n == 0) {
		$('#description1').html(desc0);
	} else {
		$('#description1').html(page.children("div[id^='description']").hide().html());
	}
	$('#subcontent-left .links ul a.on').removeClass('on');
	$(link).addClass('on');
	return false;
}

function scrollToImg(i,link) {
	//var div = $("#pages div[id^='page']:visible");
	$(link).parent().prevAll('.slider').children('.slidey').stop().animate({
			left : '-' + (i-1)*slideImgWidth + 'px'
		},slideSpeed);
	$(link).siblings('.on').removeClass('on');
	$(link).addClass('on');
	$(link).parent().nextAll('.img-caption').html($(link).attr('title'));
	return false;
}
function scrollPrev() {
	var on = 0;
	var link;
	$("#pages div[id^='page']:visible").children('.imgs').children('a').each(function(i) {
		if($(this).attr('class') == 'on') {
			on = i;
			link = $(this).prev();
		}
	});
	if( on > 1) {
		scrollToImg((on - 1), link);
	}
	return false;
}
function scrollNext(nlink) {
	var on = 0;
	var link;
	$("#pages div[id^='page']:visible").children('.imgs').children('a').each(function(i) {
		if($(this).attr('class') == 'on') {
			on = i;
			link = $(this).next();
		}
	});
	
	if(link.html() != '+') {
		scrollToImg((on + 1), link);
	}
	return false;
}

$(function() {
	//ini.js initial(m)
	$("#pages div[id^='delim']").hide();
	desc0 = $('#description1').html();
	var hsh = 0;
	if(window.location.hash) {
		hsh = parseInt((window.location.hash).substr(2,1));
		if((hsh < 0) || (hsh > $('#subcontent-left .links ul a').length)) { hsh = 0; }
	}
	$('#subcontent-left .links ul a').each(function(i) {
		$(this).click(function() { return change(i,this); });
		if(i==hsh) {
			change(i,this);
		}
	});
	
	//img galleries
	$("#pages div[id^='page']").each(function() {

		$(this).children('img:first').replaceWith('<div class="slider"><div class="slidey"></div></div>');
		slidey = $(this).children('.imgs').each(function() {
			$(this).children('a').each(function(i) {
				if($(this).attr('href') ) {
					$(this).parent().prevAll('.slider').children('.slidey').append('<img src="'+$(this).attr('href')+'" />');
					$(this).click(function() { return scrollToImg(i,this)});
				} else {
					if(i == 0) {
						$(this).click(scrollPrev);
					} else {
						$(this).click(function() {
							return scrollNext(this);
						});						
					}
				}
				$(this).bind('focus',function() { $(this).blur(); }); //artifacting
				
			});
		}).prevAll('.slider').children('.slidey');
		
	});
		
});