﻿///<reference path="~/scripts/jquery.intellisense.js" />
///<reference path="~/scripts/true_core.js" />

//<![CDATA[

if (!window.Legacy) {
    Type.registerNamespace("Legacy");
}

if (!window.Legacy.Gallery) {
	
// ---------------------------------------------------------------------
// Legacy.Gallery
// ---------------------------------------------------------------------
	Legacy.Gallery = function(p_el){
		Legacy.Gallery.constructBase(this, [p_el]);
		this.categories = {};
		this.photo = $('.js-photo');
		var self = this;
		this.jq.find("li").each(function(p_i){
			var t = $("a:first", this);
			var n = t.html();
			self.categories[n] = {name: n, currentIndex: 0, images: null, element: this};
			if(!self.currentCategory && t.parent().hasClass("on")){
				self.setCategory(t.get(0));
			}
		}).end();
	};

	Legacy.Gallery.prototype = {
		dispose: function() {
			this.categories = [];
			this.currentCategory = null;
			Legacy.Gallery.callBaseMethod(this, 'dispose');
		},
		handle_click: function(e) {
			var t = e.src;
			while (t && !t.href) {
				t = t.parentNode;
			}
			if (t) {
				this[t.href.split("#")[1]](t);
			}
			e.preventDefault();
			e.stopPropagation();
		},
		setCategory: function(p_target) {
			this._setCategory(this.categories[$(p_target).html()]);
			$(p_target.parentNode).addClass("on");
		},
		clearCategory: function() {
			this.currentCategory = null;
			this.jq.find("li.on").removeClass("on").end();
			$('.js-category').hide();
		},
		previous: function() {
			if (this.currentCategory && this.currentCategory.images !== null && this.currentCategory.currentIndex > 0) {
				this.currentCategory.currentIndex--;
				this._setImage();
				this._updateNavState();
			}
		},
		next: function() {
			if (this.currentCategory && this.currentCategory.images !== null && this.currentCategory.currentIndex < this.currentCategory.images.length - 1) {
				this.currentCategory.currentIndex++;
				this._setImage();
				this._updateNavState();
			}
		},
		_setImage: function() {
			if (this.currentCategory && this.currentCategory.images !== null) {
				var image = this.currentCategory.images[this.currentCategory.currentIndex];
				this.photo.css('background-image', 'url(' + image.Location + ')');
			}
		},
		_updateNavState: function() {
			if (this.currentCategory && this.currentCategory.images !== null) {
				if (this.currentCategory.currentIndex == 0) {
					$("a.previous", this.currentCategory.element).addClass("disabled");
					$("a.next", this.currentCategory.element).removeClass("disabled");
				}
				else if (this.currentCategory.currentIndex === this.currentCategory.images.length - 1) {
					$("a.previous", this.currentCategory.element).removeClass("disabled");
					$("a.next", this.currentCategory.element).addClass("disabled");
				}
				else {
					$("a.previous", this.currentCategory.element).removeClass("disabled");
					$("a.next", this.currentCategory.element).removeClass("disabled")
				}
				$("strong", this.currentCategory.element).html(this.currentCategory.currentIndex + 1 + ' of ' + this.currentCategory.images.length);
			}
			else {
				$("a.previous", this.currentCategory.element).addClass("disabled");
				$("a.next", this.currentCategory.element).addClass("disabled")
			}
		},
		_setCategory: function(p_category) {
			this.clearCategory();
			this.currentCategory = p_category;

			var selector = '.js-' + this.currentCategory.name.toLowerCase().replace(/\s*/g, '');
			selector = selector.replace(/&amp;/g, '-')
			$(selector).fadeIn();

			if (this.currentCategory && this.currentCategory.images === null) {
				this._getCategoryData();
			}
			else {
				this._setImage();
				this._updateNavState();
			}
		},
		_getCategoryData: function() {
			var self = this;
			var categoryName = this.currentCategory.name.replace(/&amp;/g, '&');
			$.ajax({
				type: "POST",
				url: (window.LegacyWeb_ServicePath) ? window.LegacyWeb_ServicePath + "GalleryService.asmx/GetCategory" : "GalleryService.asmx/GetCategory",
				data: (window.LegacyWeb_GalleryName) ? "{'galleryName': '" + window.LegacyWeb_GalleryName + "', 'categoryName': '" + categoryName + "'}" : "{'galleryName': 'default', 'categoryName': '" + categoryName + "'}",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(msg) {
					var c = self.categories[msg.d.Name];
					if (c) {
						c.images = msg.d.Images;
						c.description = msg.d.Description;
						self._setImage();
						self._updateNavState();
					}
				}
			});
		}
	}
	
	True.EventDispatcher.decorate(Legacy.Gallery.prototype);
	
	Legacy.Gallery.register("Legacy.Gallery", True.Control);	
}

//]]>