function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rev") == "external") anchor.target = "_blank";
 	}
}

window.onload = function() {
	externalLinks();
}

$(document).ready(function() {
	$('.hoverable').hover(
		function() {
			$(this).addClass('hovered');
		}, function() {
			$(this).removeClass('hovered');
		}
	);
	var scrollablesindices = new Array();
	var i=0;
	var idx=0;
	$('#sidebar .scrollable .items img').each( function (i) {
		scrollablesindices[i] = this.id.substring(12, this.id.length);
		i++;
	});
	$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx]);
	$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx+1]);
	var scr = $("div.scrollable").scrollable({
		size: 4,
		api: true,
		speed: 80,
		vertical: true
	});
	
	if(document.location.hash!='') {
   	tabSelect = document.location.hash.substr(7,document.location.hash.length);
   	for (i=0; i<scrollablesindices.length;i++) {
			if (scrollablesindices[i]==tabSelect) {
				idx = i;
				scr.seekTo(idx);
				if (scrollablesindices[idx+1]) {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx-1]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx+1]);
				} else {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx-1]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx]);
				}
				break;
			}
		}
		if (scrollablesindices[idx]) {
			var item = $('#galleryitem-'+scrollablesindices[idx]);
			var imagepath = item.attr('src');
			imagepath = imagepath.replace('thumb', 'fullsize');
			var alttext = item.attr('alt');
			$('#galleryfullimage').css('display', 'none');	
			$('#galleryfullimage').attr('src', imagepath);	
			$('#galleryfullimage').attr('alt', alttext);	
			$('#galleryfullimage').attr('title', alttext);	
			$('#galleryfullimage').css('display', 'block');	
		}
	}

	$('.gallerythumbitem', '#sidebar').click( function() {
		var item = $('#'+this.id);
		var imagepath = item.attr('src');
		imagepath = imagepath.replace('thumb', 'fullsize');
		var alttext = item.attr('alt');
		$('#galleryfullimage').css('display', 'none');	
		$('#galleryfullimage').attr('src', imagepath);	
		$('#galleryfullimage').attr('alt', alttext);	
		$('#galleryfullimage').attr('title', alttext);	
		$('#galleryfullimage').css('display', 'block');	
	});
	$('#photoswitch-next, #photoswitch-prev').click( function () {
		if (this.id=='photoswitch-next') {
			scr.next();
			idx++;
		} else {
			scr.prev();
			idx--;
		}
		if (idx<0) idx=0;
		if (idx>scrollablesindices.length-1) idx--;
		if (scrollablesindices[idx]) {
			if (this.id=='photoswitch-next') {
				if (scrollablesindices[idx+1]) {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx-1]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx+1]);
				} else {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx-1]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx]);
				}
			} else {
				if (scrollablesindices[idx-1]) {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx-1]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx+1]);
				} else {
					$('#photoswitch-prev').attr('href', '#photo-'+scrollablesindices[idx]);
					$('#photoswitch-next').attr('href', '#photo-'+scrollablesindices[idx+1]);
				}
			}
			var item = $('#galleryitem-'+scrollablesindices[idx]);
			var imagepath = item.attr('src');
			imagepath = imagepath.replace('thumb', 'fullsize');
			var alttext = item.attr('alt');
			$('#galleryfullimage').css('display', 'none');	
			$('#galleryfullimage').attr('src', imagepath);	
			$('#galleryfullimage').attr('alt', alttext);	
			$('#galleryfullimage').attr('title', alttext);
			$('#galleryfullimage').css('display', 'block');	
		}
	});
});

