var carousel = {};

carousel.init = function() {
	carousel.moved = 0;

	var images = $("#gallery-list li");
	$("#gallery-list li:first").addClass("first");
	var l = 0;
	images.each(function() {
		l += $(this).width();
	})
	var cars = - (l-$("#gallery-wrapper").width());
	var crsl = $("#gallery-list");
	var visible = $("#gallery-wrapper").width();
	carousel.visible = visible;
	var i = 0;
	var prev = 'prev';
	var next = 'next';
	carousel.next = $('#next');
	carousel.prev = $('#prev');

	if (l > visible) {
		carousel.next.addClass(next);
		
		carousel.next.click(function() {
			shift = $("#gallery-list li.first").width();
			if (i > cars) {
				i -= shift;
				carousel.move(crsl, i);
				carousel.prev.addClass(prev);
				if (i <= cars) {
					$(this).removeClass(next);
				}
				$("#gallery-list li.first").removeClass("first").next("li").addClass("first");
			}
		});
		
		carousel.prev.click(function() {
			shift = $("#gallery-list li.first").prev("li").width();
			if (i < 0) {
				i += shift;
				carousel.move(crsl, i);
				carousel.next.addClass(next);
				if (i == 0) {
					$(this).removeClass(prev);
				}
				$("#gallery-list li.first").removeClass("first").prev("li").addClass("first");
			}
		});

	}

	/*$("#gallery-list a").click(function() {
		var href = $(this).attr("href");
		var popup = href.match("#(\\w{1,3},\\w{1,3})");
		if (popup && popup[1]) {
			var size = popup[1].split(',');
			var width = (parseInt(size[0]) || '320');
			var height = (parseInt(size[1]) || '240');
			var w = window.open(href, '_blank','width='+width+',height='+height+',resizable=yes');
			w.document.open()
			w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+"\n");
			w.document.write('<html><head>');
			w.document.write('<style>html, body { padding: 0; margin: 0; background: #FFFFFF;  height: 100%;}');
			w.document.write(' img {margin:0; border: none;} </style>');
			w.document.write('</head><body>');
			w.document.write('<img src="' + href + '" width="' + width + '" height="' + height + '" border="0" alt=""></body></html>');
			w.document.close();
			w.focus();
			return false;
		}
	});*/
}


carousel.move = function(crsl, i) {
	crsl.animate({"left": i+"px"}, "slow");
}

carousel.init();

