function openWindow(e) {
	if (!e) e = event;
	var o = e.srcElement || e.target;
	while (true) {
		if (o.tagName === 'A') break;
		if (!o || o.tagName === 'BODY') return;
		o = o.parentNode;
	}
	window.open(o.href);
	return false;
}

var Gallery = {
	pos: 0,
	total: 0,
	objs: {},
	ini: function() {
		Gallery.total = Gallery.getO('gsmall').children().length;
	},
	getO: function(id) {
		if (Gallery.objs[id]) return Gallery.objs[id];
		return Gallery.objs[id] = $('#'+ id);
	},
	next: function() {
		var oSmall = Gallery.getO('gsmall').children(':nth-child('+ (Gallery.pos + 1) +')');
		oSmall.attr('src', oSmall.attr('src').replace('&roundaa=5x008800x4', '&roundaa=5xffffffx4')).css('border-color', '#fff');

		Gallery.pos++;
		if (Gallery.pos > Gallery.total - 1) Gallery.pos = 0;
		var o = Gallery.getO('gsmall').children(':nth-child('+ (Gallery.pos + 1) +')');
		Gallery.show(o.attr('src'));

		o.attr('src', o.attr('src').replace('&roundaa=5xffffffx4', '&roundaa=5x008800x4')).css('border-color', '#080');
	},
	prev: function() {
		var oSmall = Gallery.getO('gsmall').children(':nth-child('+ (Gallery.pos + 1) +')');
		oSmall.attr('src', oSmall.attr('src').replace('&roundaa=5x008800x4', '&roundaa=5xffffffx4')).css('border-color', '#fff');

		Gallery.pos--;
		if (Gallery.pos < 0) Gallery.pos = Gallery.total - 1;
		var o = Gallery.getO('gsmall').children(':nth-child('+ (Gallery.pos + 1) +')');
		Gallery.show(o.attr('src'));

		o.attr('src', o.attr('src').replace('&roundaa=5xffffffx4', '&roundaa=5x008800x4')).css('border-color', '#080');
	},
	show: function(src, o) {
		var pos = src.indexOf('&');
		if (pos > -1) src = src.substr(0, pos);
		Gallery.getO('gbig').css('background-image', 'url("'+ src +'&resize=466x466")');

		if (!o) {
			Gallery.scrollerAlign();
			return;
		}

		var oSmall = Gallery.getO('gsmall').children(':nth-child('+ (Gallery.pos + 1) +')');
		oSmall.attr('src', oSmall.attr('src').replace('&roundaa=5x008800x4', '&roundaa=5xffffffx4')).css('border-color', '#fff');

		pos = 0;
		while (o) {
			pos++;
			o = o.previousSibling;
		}
		oSmall = Gallery.getO('gsmall').children(':nth-child('+ pos +')');
		oSmall.attr('src', oSmall.attr('src').replace('&roundaa=5xffffffx4', '&roundaa=5x008800x4')).css('border-color', '#080');

		Gallery.pos = pos - 1;
		Gallery.scrollerAlign();
	},
	scoller: {
		image: {width: 68, padding: 10},
		viewport: 466
	},
	scrollerAlign: function() {
		var viewport = Gallery.scoller.viewport, image = Gallery.scoller.image.width, imagePadding = Gallery.scoller.image.padding, photosInVp = 6;
		var marginLeft = Gallery.getO('gsmall').css('margin-left');
		marginLeft = parseInt(marginLeft.charAt(0) === '-' ? marginLeft.substr(1) : marginLeft, 10);
		var currentImgOffset = Gallery.pos * (image + imagePadding) - marginLeft;

		var viewportStart = Math.round(marginLeft / (image + imagePadding)), viewportEnd = viewportStart + 6;
		if (Gallery.pos >= viewportStart && Gallery.pos <= viewportEnd - 1) return;

		if (Gallery.pos < viewportStart) {
			marginLeft = Gallery.pos * (image + imagePadding);
		} else {
			marginLeft = (Gallery.pos - photosInVp + 1) * (image + imagePadding);
			if (marginLeft < 0) marginLeft = 0;
		}

		Gallery.getO('gsmall').css('margin-left', '-'+ marginLeft +'px');
	},
	scrollerInt: 0,
	scrollerDelay: 150,
	scrollerNext: {
		mousedown: function() {
			if (Gallery.scrollerInt) clearInterval(Gallery.scrollerInt);
			Gallery.scrollerInt = setInterval(Gallery.scrollerNext.move, Gallery.scrollerDelay);
			Gallery.scrollerNext.move();
		},
		mouseup: function() {
			clearInterval(Gallery.scrollerInt);
		},
		move: function() {
			var marginLeft = Gallery.getO('gsmall').css('margin-left');
			marginLeft = parseInt(marginLeft.charAt(0) === '-' ? marginLeft.substr(1) : marginLeft, 10);
			var image = Gallery.scoller.image.width + Gallery.scoller.image.padding;
			marginLeft += image;
			if (marginLeft >= (Gallery.total * image - Gallery.scoller.viewport)) return;
			Gallery.getO('gsmall').css('margin-left', '-'+ marginLeft +'px');
		}
	},
	scrollerPrev: {
		mousedown: function() {
			if (Gallery.scrollerInt) clearInterval(Gallery.scrollerInt);
			Gallery.scrollerInt = setInterval(Gallery.scrollerPrev.move, Gallery.scrollerDelay);
			Gallery.scrollerPrev.move();
		},
		mouseup: function() {
			clearInterval(Gallery.scrollerInt);
		},
		move: function() {
			var marginLeft = Gallery.getO('gsmall').css('margin-left');
			marginLeft = parseInt(marginLeft.charAt(0) === '-' ? marginLeft.substr(1) : marginLeft, 10);
			marginLeft -= Gallery.scoller.image.width + Gallery.scoller.image.padding;
			if (marginLeft < 0) return;
			Gallery.getO('gsmall').css('margin-left', '-'+ marginLeft +'px');
		}
	}
};

$(document).ready(function() {
	var i = 0, nodes = document.getElementsByTagName('A'), total = nodes.length, domain = location.hostname.toLowerCase();
	if (domain.indexOf('www.') !== -1) domain = domain.replace('://www.', '://');
	for (; i < total; i++) {
		if (nodes[i].href.indexOf('mailto:') !== -1) continue;
		if (nodes[i].href.indexOf(domain) === -1) nodes[i].onclick = openWindow;
	}
});
