﻿var Flamingo = $(document);
Flamingo = {

	tagList: "",
	filterList: "",
	cookieName: "lovemoney_flamingo_settings" + $.cookies.get("uid"),

	showLoading: function() {
		$('#LoadingMessage').show();
		$('div#FlamingoItems').css("opacity", "0.33");
	},

	hideLoading: function() {
		$('#LoadingMessage').hide();
		$('div#FlamingoItems').css("opacity", "1");
	},

	saveSettings: function() {
		if (Flamingo.tagList.length > 2) {
			data = Flamingo.tagList + "|" + Flamingo.filterList;
			$.cookies.del(Flamingo.cookieName);
			$.cookies.set(Flamingo.cookieName, data, { path: '/', hoursToLive: 168 });
		}
	},

	loadSettings: function() {
		var cookieData = $.cookies.get(Flamingo.cookieName);
		return cookieData ? cookieData : "";
	},

	pageLoad: function() {
		var cookieData = Flamingo.loadSettings();
		var splitCookieData = cookieData.split("|");

		if (splitCookieData.length > 1) {
			if (splitCookieData[0].length > 1) {
				Flamingo.tagList = splitCookieData[0];
				var splitTags = Flamingo.tagList.split(',');
				for (i = 0; i < splitTags.length; i++) {
					$('ul#UserTags input').each(function() {
						if ($(this).attr('value') == splitTags[i]) {
							$(this).attr('checked', 'checked');
						}
					});
				}
			}
			if (splitCookieData[1].length > 1) {
				Flamingo.filterList = splitCookieData[1];
				var splitFilter = Flamingo.filterList.split(',');
				for (i = 0; i < splitFilter.length; i++) {
					$('ul#ContentTypes input').each(function() {
						if ($(this).parent('span').attr('title') == splitFilter[i]) {
							$(this).attr('checked', 'checked');
						}
					});
				}
			}
		}
		else if (cookieData == null || cookieData == '' || cookieData == '|' || cookieData.Length == 0) {
			// no cookie data - set all check boxes to checked (we check or it may be somebody has turned it all off)!
			$('ul#UserTags input').each(function() {
				$(this).attr('checked', 'checked');
			});
			$('ul#ContentTypes input').each(function() {
				$(this).attr('checked', 'checked');
			});
		}

		Flamingo.milk();
	},

	resetNipples: function() {
		// If there are flamingo related checkboxes nearby, reset the tag and filter lists based on them
		if ($('ul#UserTags li, ul#ContentTypes li').length > 0) {
			// get checked tags
			$('ul#UserTags li, ul#ContentTypes li').each(function() {
				$(this).removeClass('Checked');
			});

			checkedTags = $('ul#UserTags input[type="checkbox"]:checked');
			checkedTypes = $('ul#ContentTypes li input[type="checkbox"]:checked');

			Flamingo.tagList = "";
			Flamingo.filterList = "";

			checkedTags.each(function() {
				$(this).parent().addClass('Checked');
				Flamingo.tagList += $(this).attr('value') + ",";
			});

			checkedTypes.each(function() {
				$(this).parent().parent().addClass('Checked');
				Flamingo.filterList += $(this).parent().attr('title') + ",";
			});
		}
	},

	milk: function() {
		Flamingo.resetNipples();

		var count = 36;
		if ($('input.FlamingoCount').length > 0) {
			count = $('input.FlamingoCount').val();
		}

		tagListForAjax = encodeURIComponent(Flamingo.tagList);
		filterListForAjax = encodeURIComponent(Flamingo.filterList);

		Flamingo.saveSettings();
		// set milkUrl
		var milkUrl = "/controls/tagging/flamingoaction.aspx?count=" + count + "&tags=" + tagListForAjax + "&filters=" + filterListForAjax;

		$.ajax({
			cache: false,
			type: "GET",
			url: milkUrl,
			beforeSend: function() {
				Flamingo.showLoading();
			},
			success: function(msg) {
				if ($('div#FlamingoItems').length > 0) {
					Flamingo.drawContent(msg);
				}
			},
			fail: function(msg) {
				Flamingo.hideLoading();
			}
		});
	},

	drawContent: function(ajax) {
		$('div#FlamingoItems').hide();
		$('div#FlamingoItems div.flamingoPage').remove();

		//pagination
		perPage = 10;
		itemsInAjax = $(ajax).children("li").size();
		numOfPages = Math.ceil(itemsInAjax / perPage);

		if ($('.SidebarFlamingo').size() > 0) {
			$('div#FlamingoItems').html(ajax);
		}
		else {
			// create dom elements for pages
			i = 0;
			for (i = 0; i < numOfPages; i++) {
				// create the page container
				var thisPageId = "flamingoPage" + i;
				var thisPageSelector = "div#" + thisPageId;

				var nextPageSelector = "div#flamingoPage" + (i + 1);
				var prevPageSelector = "div#flamingoPage" + (i - 1);

				$('div#FlamingoItems').append('<div class="flamingoPage" id="flamingoPage' + i + '"><ul class="ImgList BlobList CompactBlobList"></ul></div>');
				// get the correct number of items
				$(ajax).children("li").each(function(j) {
					// get start and end positions
					start = i * perPage;
					end = (i * perPage) + perPage
					// draw list items where within the start/end values
					if (j >= start && j < end) {
						$(thisPageSelector + ' .BlobList').append($(this));
					}
				});
				// hide all but first page
				if (i != 0) {
					$(thisPageSelector).hide();
				}

				// draw page links
				if (i != numOfPages - 1) {
					nextLink = $(thisPageSelector).append('<a class="Pagination Next" rel="' + nextPageSelector + '" href="#">Next</a>');
					nextLink.children('a.Next').bind('click', function() {
						selector = ($(this).attr('rel'));
						$('div#FlamingoItems div').hide();
						$(selector).show();
						return false;
					});
				}
				if (i != 0) {
					prevLink = $(thisPageSelector).append('<a class="Pagination Prev" rel="' + prevPageSelector + '" href="#">Previous</a>');
					prevLink.children('a.Prev').bind('click', function() {
						selector = ($(this).attr('rel'));
						$('div#FlamingoItems div').hide();
						$(selector).show();
						return false;
					});
				}
			}
		}

		if (itemsInAjax > 0) {
			$('.Flamingo .NothingBox').hide();
		}
		else {
			$('.Flamingo .NothingBox').show();
			$('div#FlamingoItems').show();
		}

		$('ul#ContentTypes li b').remove();
		$('div#FlamingoItems').show();
		Flamingo.hideLoading();
	}
}

$(document).ready(function() {
	$('ul#UserTags li, ul#ContentTypes li').each(function() {
		$(this).bind('click', function() {
			if ($(this).children('input').size() > 0) {
				$(this).children('input:checkbox')[0].click();
			}
			else if ($(this).children('span').children('input').size() > 0) {
				$(this).children('span').children('input')[0].click();
			}
			Flamingo.milk();
		});
	});
	
	$('.FlamingoFilters input.Checkbox, .FlamingoFilters .Checkbox input').hide();

	Flamingo.pageLoad();
});
