dojo.ready(function() {
	// rewrite embedded videos to add the 'play' overlay
	dojo.query("#page .embeddedMediaScreenshot > a").forEach(
		function(item, index, array){
			// work out the dimensions of the thumbnail
			imgWidth = item.lastChild.width;
			imgHeight = item.lastChild.height;

			dojo.create('span', {className: 'overlay', style: 'width: ' + imgWidth + 'px; height: ' + imgHeight + 'px;'}, item);
		}
	);

	var agentPopups = dojo.query("#page .agentDialog");
	if (agentPopups.length > 0) {
		agentLightbox = new dijit.Dialog({
			title: "&nbsp;",
			style: "width: 420px; height: 320px;",
			draggable: false,
			titleBar: false,
			className: "agentLightbox",
			duration: 500
		});

		agentPopups.forEach(
			function(item, index, array){
				dojo.connect(item, 'onclick', function(event) {
					agentLightbox.show();
					agentLightbox.attr("content", '<iframe src="' + item.href + '?sw=true" width="400" height="300" style="border: 0;"></iframe>');
					agentLightbox.closeText.innerHTML = 'close';

					dojo.stopEvent(event);
				});
			}
		);
	}

});

dojo.provide('lums.AlumniQuoteSlideshow');
dojo.declare('lums.AlumniQuoteSlideshow', [dojox.image.SlideShow], {

	postCreate: function(){
		this.inherited(arguments);

		// build the placeholder quote
		this.caption = dojo.create('div', {className: 'slideShowCaption', style: 'position: absolute;'});
		this.quote = dojo.create('blockquote', {}, this.caption);
		this.cite = dojo.create('cite', {}, this.caption);
		dojo.style(this.caption, 'opacity', 0);

		dojo.connect(this.caption, 'onclick', dojo.hitch(this, function(event){
			linkElement = this.largeNode.firstChild.firstChild;
			if (linkElement) {
				href = linkElement.href;
				if (href) {
					window.location = href;
				}
			}
		}));

		this.outerNode.appendChild(this.caption);

		// add the IE rounded corner image
		if (dojo.isIE < 9) {
			ieThing = dojo.create('img', {src: '/templateimages/ie-gutter-box-top.gif', className: 'ie-gutter-corner'});
			this.outerNode.appendChild(ieThing);
		}
	},

	showImage: function(index, /* Function? */callback){	
		if (this.crossFade) {
			var _this = this;
			var current = this.largeNode.getElementsByTagName("div");
			this.imageIndex = index;

			// ensure the image is loaded
			if (!_this.images[index]) {
				_this._loadImage(index);
			}

			if (current && current.length > 0) {
				// add it with opacity 0
				dojo.style(_this.images[index], "opacity", 0);
				dojo.style(_this.images[index], "position", "absolute");
				_this.largeNode.appendChild(_this.images[index]);

				// cross fade existing image -> new
				var crossFadeParams = {
					nodes: [_this.largeNode.firstChild,_this.largeNode.lastChild],
					duration: _this.crossFadeDuration,
					onBegin: function() {
						//dojo.style(_this.caption, 'opacity', '0');
					},
					onEnd: function() {	
						while (_this.largeNode.children.length > 1) {
							_this.largeNode.removeChild(_this.largeNode.firstChild);
						}
						_this._currentImage = _this.images[index]._img;
						_this._fitSize();
					}
				};

				var captionFadeParams = {
					node: _this.caption,
					duration: _this.crossFadeDuration
				};

				var anim = dojo.fx.combine([
					dojox.fx.crossFade(crossFadeParams),
					dojo.fadeOut(captionFadeParams)
				]);

				dojo.connect(anim, 'onEnd', function(){
					dojo.fadeIn({
						node: _this.caption,
						delay: 2000,
						duration: 1500
					}).play();

					dojo.attr(_this.quote, 'innerHTML', '<p>' + dojo.attr(_this.images[index]._img, 'data-quote') + '</p>');
					dojo.attr(_this.cite, 'innerHTML', dojo.attr(_this.images[index]._img, 'data-cite'));
				});

				anim.play();

			} else {
				// remove placeholder
				while (_this.largeNode.firstChild) {
					_this.largeNode.removeChild(_this.largeNode.firstChild);
				}

				// add image
				dojo.style(_this.images[index], "opacity", 1);
				dojo.style(_this.images[index], "position", "absolute");

				_this.largeNode.appendChild(_this.images[index]);
				_this._currentImage = _this.images[index]._img;
				_this._fitSize();

				// FIXME - for some reason _fitSize is getting a height of 0
				// here so we hardcode it to the height of the slideshow
				if (_this.innerWrapper.style.height != _this.imageHeight + "px") {
					dojo.style(_this.innerWrapper, "height", _this.imageHeight + "px");
				}

				// update the quote
				dojo.attr(_this.quote, 'innerHTML', '<p>' + dojo.attr(_this.images[index]._img, 'data-quote') + '</p>');
				dojo.attr(_this.cite, 'innerHTML', dojo.attr(_this.images[index]._img, 'data-cite'));

				// fade in the quote
				dojo.fadeIn({
					node: _this.caption,
					delay: 2000,
					duration: 1500
				}).play();
			}

			callback();

		} else {
			this.inherited(arguments);
		}
	},
	
	_loadImage: function(index, callbackFn){
		// summary:
		//		Load image at specified index
		// description:
		//		This function loads the image at position 'index' into the
		//		internal cache of images.  This does not cause the image to be displayed.
		// index:
		//		The position in the data store to load an image from.
		// callbackFn:
		//		An optional function to execute when the image has finished loading.

		if(this.images[index] || !this._request) {
			return;
		}
		
		var pageStart = index - (index % (this._request.count || this.pageSize));

		this._request.start = pageStart;

		this._request.onComplete = function(items){
			var diff = index - pageStart;
			
			if(items && items.length > diff){
				loadIt(items[diff]);
			}else{ /* Squelch - console.log("Got an empty set of items"); */ }
		}

		var _this = this;
		var store = this.imageStore;
		var loadIt = function(item){
			var url = _this.imageStore.getValue(item, _this.imageLargeAttr);
			
			var img = new Image();	// when creating img with "createElement" IE doesnt has width and height, so use the Image object
			var div = dojo.create("div", {
				id: _this.id + "_imageDiv" + index
			});
			div._img = img;

			var link = _this.imageStore.getValue(item,_this.linkAttr);
			if(!link || _this.noLink){
				div.appendChild(img);
			}else{
				var a = dojo.create("a", {
					"href": link // target blank here removed
				}, div);
				a.appendChild(img);
			}

			dojo.connect(img, "onload", function(){
				if(store != _this.imageStore){
					// If the store has changed, ignore this load event.
					return;
				}
				_this._fitImage(img);
				dojo.attr(div, {"width": _this.imageWidth, "height": _this.imageHeight});
				
				// make a short timeout to prevent IE6/7 stack overflow at line 0 ~ still occuring though for first image
				dojo.publish(_this.getLoadTopicName(), [index]);

				setTimeout(function(){_this._loadNextImage();}, 1);
				if(callbackFn){ callbackFn(); }
			});
			_this.hiddenNode.appendChild(div);

			var titleDiv = dojo.create("div", {
				className: "slideShowTitle"
			}, div);

			_this.images[index] = div;
			dojo.attr(img, "src", url);
			
			var title = _this.imageStore.getValue(item, _this.titleAttr);
			if(title){ dojo.attr(img, "title", title); }

			// 
			var quote = _this.imageStore.getValue(item, 'quote');
			var cite = _this.imageStore.getValue(item, 'cite');
			if (quote || cite) {
				dojo.attr(img, 'data-quote', quote);
				dojo.attr(img, 'data-cite', cite);
				
			} else {
				// added to get caption
				var caption = _this.imageStore.getValue(item, 'caption');
				if (caption) {
					// try and split it 
					captionMatch = caption.match(/"(.+?)"\n+(.+)/m);
					if (captionMatch) {
						// set attributes on the image that we can use later
						dojo.attr(img, 'data-quote', captionMatch[1]);
						dojo.attr(img, 'data-cite', captionMatch[2]);
					}
				}
			}
		}
		this.imageStore.fetch(this._request);
	}

});

