/**
 * Slideshow widget
 **/
 
(function() {
 
 	// Element of slideshow container DIV
 	var g_ssEl;
 	
 	var g_fPhotoEl; // Photo area
 	var g_titleEl; // Image title
 	var g_attrEl; // Image attribution
 	
 	var g_fContentEl;
 	
 	var g_images;
 	var g_config;
 	var g_posEl;
 	var g_captionEl;
 	
 	// Slide show slide interval
 	var g_interval = 7000;
 	
 	// Time to pause auto-forwarding after user interaction
 	var g_activityPause = 10000;
 	
 	// Id of setTimeout() object used 
 	var g_delayedActionId;
 	
 	var g_autoMode = true;
 	 	
 	/**
 	 * Start timers to execute appropriate delayed action depending on mode
 	 */
 	function setTimers () {
		if (g_autoMode) {
			g_delayedActionId = setTimeout ( function () {
				animateTransition(currentIndex+1);
				setTimers();
			}, g_interval);
		} else {
			g_delayedActionId = setTimeout ( function () {
				g_autoMode=true;
				setTimers();
			}, g_activityPause)
		}
	}
	
 	/**
 	 * Initialize slideshow. Create DOM structure.
 	 */
 	YAHOO.widget.IASlideShow = function (sElementId, images, config) {
 		g_ssEl = document.getElementById(sElementId);
 		g_images = images;
 		g_config = config;
 		
 		// Create internal structure
 		g_fPhotoEl = document.createElement("div");
 		g_fPhotoEl.className="feature-photo";
 		g_ssEl.appendChild (g_fPhotoEl);
 		
 		g_titleEl = document.createElement("span");
 		g_titleEl.id="imageTitle"; // id'ed for styling only
 		g_titleEl.innerHTML = g_config.title;
 		g_fPhotoEl.appendChild(g_titleEl);
 		
 		g_attrEl = document.createElement("div");
 		g_attrEl.id = "fphoto_attr"; // id'ed for styling only
 		g_attrEl.className = "feature-photo-copyright";
 		g_fPhotoEl.appendChild(g_attrEl);
 		
 		
 		g_fContentEl = document.createElement("div");
 		g_fContentEl.className="feature-content";
 		g_ssEl.appendChild (g_fContentEl);
 		
 		var ssNavEl = document.createElement("div");
 		ssNavEl.className="float-photo-nav";
 		
 		g_posEl = document.createElement("span"); // "x of y"
 		g_posEl.style.marginRight="5px";
 		ssNavEl.appendChild(g_posEl);
 		
 		var prevBtnEl = document.createElement("img");
 		prevBtnEl.src="http://www.irelandadventures.com/images/nav-photo-l.gif";
 		YAHOO.util.Event.addListener(prevBtnEl,"click", onPrevBtnClick);
 		ssNavEl.appendChild(prevBtnEl);
 		
 		var prevBtnEl = document.createElement("img");
 		prevBtnEl.src="http://www.irelandadventures.com/images/nav-photo-r.gif";
 		YAHOO.util.Event.addListener(prevBtnEl,"click", onNextBtnClick);
 		ssNavEl.appendChild(prevBtnEl);
 		
 		g_fContentEl.appendChild(ssNavEl);
 		
 		g_captionEl = document.createElement("div");
 		g_captionEl.id="fphoto_caption"; // for styling
 		g_fContentEl.appendChild(g_captionEl);
 		
 		// Display first image
 		setImage (0);
 		setTimers();
 		
 	}
 	
 	/**
 	 * Transition from current image to image at index 'toIndex'
 	 */
	function animateTransition (toIndex) {
		var anim = new YAHOO.util.Anim(g_fPhotoEl, {opacity:{from:1,to:0}}, 0.5);
		anim.onComplete.subscribe (function (s,e) { 
			setImage(currentIndex+1);
			var anim2 = new YAHOO.util.Anim(g_fPhotoEl, {opacity:{from:0,to:1}}, 0.5);
			anim2.animate();
		});
		anim.animate();
	}
	
	function setImage (index) {
		
		if (YAHOO.lang.isUndefined(g_images) || images.length==0) {
			return;
		}
		
		index = index % images.length;
	
		var image = images[index];

		// Set image title color according to intensity of top of image
		g_titleEl.style.color = image.mean_intensity>128 ? "#0A3557":"white";
	
		if (image.image_copyright) {
			YAHOO.util.Dom.setInnerHTML (g_attrEl , image.image_copyright);
		} else {
			YAHOO.util.Dom.setInnerHTML (g_attrEl , "");
		}
	
		// Make caption HTML
		var html="";
		if (image.image_type == "yp") {
			html += "<h1><a href=\"yp-" 
			+ image.yp_id
			+ ".html\" title=\"Go to item associated with this image\">"
			+ image.yp_name
			+ "</a></h1>";
	
			if (image.yp_address) {
				html += image.yp_address + "<br />";
			}
		}
	
		if (image.image_type == "article") {
			html += "<h1><a href=\"article-" 
			+ image.article_id
			+ ".html\" title=\"Go to article associated with this image\">"
			+ image.article_title
			+ "</a></h1>";
			if (image.article_subtitle) {
				html += image.article_subtitle + "<br />";
			}
		}
	
		html += (image.image_caption ? image.image_caption : "<br />")
	
		YAHOO.util.Dom.setInnerHTML (g_captionEl,html);
		
		g_posEl.innerHTML= (index+1) + " of " + images.length;
	
		// Location of image depends on type (yp | article)
		var photoUrl;
		if ( image.image_type == "article" ) {
			photoUrl = ROOT_LEVEL + "/news/image/296h/" + image.image_id + ".jpg";
		} else {
			photoUrl = ROOT_LEVEL + "/image/296h/" + image.image_id + ".jpg";
		}
		g_fPhotoEl.style.backgroundImage="url(" + photoUrl + ")";
		currentIndex = index;
	
		// If onSetImage handler call it
		if (g_config.onSetImage) {
			g_config.onSetImage (image);
		}
	}
	
	/**
	 * Start auto advancing slideshow, called using setTimeout()
	 */
	function startAutoMode () {
		g_autoMode=true;
		dispImageAnimate(currentIndex);
	}
	
	/**
	 * Handle click on slideshow
	 */
	function onSSClick () {
		var image = images[currentIndex];
		if (image.image_type == "yp") {
			// yp html file is usually in same dir, but in some cases, eg in tour pages it is not
			var newloc="";
			if (image.path_to_yp) {
				newloc += image.path_to_yp;
			}
			newloc += "yp-" + images[currentIndex].yp_id + ".html";
			window.location=newloc;
		}
		if (image.image_type == "article") {
			window.location="article-" + images[currentIndex].article_id + ".html";
		}
	}
	
	/**
	 * Next button pressed, advance one slide, stop auto mode
	 */
	function onNextBtnClick () {
		clearTimeout (g_delayedActionId);
		g_autoMode = false;
		setImage (currentIndex + 1);
		setTimers();
	}
	/**
	 * Prev button pressed, back one slide, stop auto mode
	 */
	function onPrevBtnClick () {
		clearTimeout (g_delayedActionId);
		g_autoMode = false;
		setImage (currentIndex - 1);
		setTimers();
	}

})();