﻿///<reference path="~/scripts/jquery.intellisense.js" />
///<reference path="~/scripts/true_core.js" />

//<![CDATA[

if (!window.CS) {
	Type.registerNamespace("CS");
}

if (!window.CS.Gallery) {

// ---------------------------------------------------------------------
// CS.Gallery
// ---------------------------------------------------------------------
	CS.Gallery = function(p_el) {
		CS.Gallery.constructBase(this, [p_el]);
		this.thumbItems = [];
		this.image = $('.js-galery-image-holder img');
		var self = this;
		// get our li elements
		this.jq.find("li").each(function(p_i) {
			self.thumbItems[p_i] = this;
		}).click(function(p_e) { // handle clicks
			self._onThumbClick(p_e);
			p_e.preventDefault();
			p_e.stopPropagation();
		}).end();
	};

	CS.Gallery.prototype = {
		dispose: function() {
			this.thumbLinks = [];
			CS.Gallery.callBaseMethod(this, 'dispose');
		},
		_onThumbClick: function(p_e) {
			var t = p_e.target;
			while (t && t.nodeName != "A") {
				t = t.parentNode;
			}
			if (t) {
				this._setImage(t.href.split("#")[1]);
			}
		},
		_setImage: function(p_idx) {
			var image = true_gallery_images[p_idx];
			this.image.attr({
				'src': image.Url,
				'title': image.Title,
				'alt': image.Alt
			});
			for (var i = 0; i < this.thumbItems.length; i++) {
				if (i == p_idx) {
					$(this.thumbItems[i]).addClass('on');
				}
				else {
					$(this.thumbItems[i]).removeClass('on');
				}
			}
		}
	}

	True.EventDispatcher.decorate(CS.Gallery.prototype);

	CS.Gallery.register("CS.Gallery", True.Control);
}

//]]>