var Ds = {};

Ds.hidePickup = function() {
	$("#pickup-close").hide();
	$("#pickup-open").show();
	$.cookie('pickup-hidden', 1, {
		expires: 7,
		path: "/"
	});
}

Ds.showPickup = function() {
	$("#pickup-open").hide();
	$("#pickup-close").show();
	$.cookie('pickup-hidden', -1, {
		expires: 7,
		path: "/"
	});
}

Ds.getPickup = function() {
	$.get("/home/pickup", function(html) {
		$("#pickup").html(html);

		var cookie = $.cookie('pickup-hidden');
		if (cookie == 1) {
			Ds.hidePickup();
		} else {
			Ds.showPickup();
		};
	});
};

Ds.getTopShopInfo = function() {
	$('div.shops-info').each(function(index) {
		var $this = $(this),
			shopId = $(this).attr('shop-id');
		$.get('/shops/info/' + shopId + '/_blank', function(html) {
			$this.html(html);
		});
	});
};

Ds.loadShopPosts = function() {
	var $e = $("#items"),
		$f = $("#wrapper"),
		opts = {offset: "100%"},
		next = $f.attr("src"),
		loading = '<div class="loading"></div>';

	$e.empty();
	$f.waypoint("destroy");

	$f.waypoint(function(event, direction) {
		if (direction === 'down' && next.length > 0) {
			$e.find("div.loading").detach();
			$e.append(loading);

			$.get(next + '#items', function(data) {
				$e.find("div.loading").fadeOut();
				$e.append(data);

				var _next = $e.find("a.next:last").attr("href");
				if (next === _next) {
					next = false;
				} else {
					next = _next;
				};

				if (next) {
					$f.waypoint(opts);
				} else {
					$f.waypoint("destroy");
					$e.find("div.loading").detach();
				};
			});
		};
	}, opts);
};

Ds.load = function() {
	$("div[data-load]").each(function(index) {
		var $this = $(this),
			url = $(this).attr('data-load');
		$.get(url, function(html) {
			$this.html(html);
		});
	});
};

$(document).ready(function() {
	Ds.getPickup();
	Ds.load();
});
