(function() {
	var photoViewer = window.photoViewer = function(spec) {
		var that = {};

		that.photos = [];
		that.pager = function() {
			return;
		};

		// initialize
		(function() {
			$.ajax({
				adataType: 'xml',
				url: spec.xmlPath,
				success: function(xml) {
					$(xml).find('img').each(function(i) {
						that.photos.push({src: $(this).attr('src'), desc: $(this).attr('description')});
					});
					that.layout(0);
					that.pager();
				}
			});
		})();

		return that;
	};

	var newDesigns = window.newDesigns = function(spec) {
		var that = photoViewer(spec);

		that.layout = function() {
			var tags = '';
			tags += '<li><a href="/gallery/?name=' + that.photos[0].src.split('.')[0] + '_l.' + that.photos[0].src.split('.')[1] + '"><img src="/gallery/img/56.jpg" width="168" height="112" alt="" /></a></li>';
			tags += '<li class="even"><a href="/gallery/?name=' + that.photos[1].src.split('.')[0] + '_l.' + that.photos[1].src.split('.')[1] + '"><img src="/gallery/img/57.jpg" width="168" height="112" alt="" /></a></li>';
			tags += '<li class="btm"><a href="/gallery/?name=img/52_l.jpg""><img src="/gallery/img/52.jpg" width="168" height="112" alt="" /></a></li>';
			tags += '<li class="even btm"><a href="/gallery/?name=' + that.photos[3].src.split('.')[0] + '_l.' + that.photos[3].src.split('.')[1] + '"><img src="/gallery/img/59.jpg" width="168" height="112" alt="" /></a></li>';
			var photoList = $('#newDesign ul');
			photoList.append(tags);
		};

		return that;
	};

	var gallery = window.gallery = function(spec) {

		var num = spec.num || 12;
		var that = photoViewer(spec);

		that.layout = function(pageNum) {
			var tags = '';
			var start = pageNum * num;
			var end = start + Number(num);
			end = (that.photos.length >= end)? end: that.photos.length;

			for(var i = start; i < end; i++) {
				tags += '<li><a href="' + that.photos[i].src.split('.')[0] + '_l.' + that.photos[i].src.split('.')[1] + '" class="thickbox" rel="gallery" title="' + that.photos[i].desc + '"><img src="' + that.photos[i].src + '" /></a></li>';
			};

			var photoList = $('#photoList ul');

			photoList.fadeOut('slow', function() {
				photoList.empty();
				photoList.append(tags);
				photoList.fadeIn('slow');
				tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
			});
		};

		that.pager = function() {
			var tags = '';
			var total = Math.floor(that.photos.length / num);
			total += (that.photos.length % num)? 1: 0;

			for(var i = 0; i < total; i++) {
				tags += '<span>' + Number(i + 1) + '</span>';
			}

			$('#pager').append(tags).children().click(function () {
				that.layout($(this).text() - 1);
			});
		};

	};
})();
