// $E Release H.D$
// $D http://wiki.newsonline.tc.nca.bbc.co.uk/wiki/moin.cgi/HieuLuuDanh/DhtmlSlideshowV2

/**
 * @fileOverview DhtmlSlideshow Helper
 * @author BBC / Hieu Luu Danh &lt;hieu.danh (at) bbc.co.uk&gt;
 * @version 1.1.0-rls
 */


(function () {
	
	function createObject(strName) {
		var nameParts = strName.split("."),
		i = 0,
		len = nameParts.length,
		obj = window;
		
		for (; i < len; i++){
			if (obj[nameParts[i]] === undefined) {
				obj[nameParts[i]] = {};
			}
			
			obj = obj[nameParts[i]];
		}
	}
	
	createObject("bbc.fmtj.apps");
	
	bbc.fmtj.apps.createObject = createObject;
	
})();

bbc.fmtj.apps.createObject("bbc.fmtj.csd.DhtmlSlideshow");

bbc.fmtj.csd.DhtmlSlideshow = {
	Master: function Master(glow) {
		var that = {};
		
		if (typeof(glow) == 'undefined') {
			throw "Glow not included!";
		}
		bbc.fmtj.csd.DhtmlSlideshow.glow = glow; // for other not Master()
		
		// Attributes
		var dirv, // directives
			slides, // collection of entries (slides)
			c_slide = 1, // current slide, defaults 1
			max; // max number of slides
		
		// Constants
		var D = {
			enclosure: "div.dslideshow-enclosure",
			entries: "div.dslideshow-entries",
			controls: "div.dslideshow-controls",
			controls_enabled: "div.dslideshow-controls-enabled",
			locator: "span.dslideshow-controls-locator",
			back: "span.dslideshow-controls-btn-back",
			next: "span.dslideshow-controls-btn-next",
			back_off: "span.dslideshow-controls-btn-back-off",
			next_off: "span.dslideshow-controls-btn-next-off",
			prefix: {
				enclosure: "dslideshow-enclosure-"
			}
		};
		
		function receiveDirectives(passedDirv) { // [Public] inject()
			dirv = passedDirv;
			if (typeof(dirv.namespace) == 'undefined' ||
				typeof(dirv.size) == 'undefined' ||
				typeof(dirv.total) == 'undefined') { throw "Directive problem"; }
			_init(); // pass on to init
		}
		that.inject = receiveDirectives;
		
		function _init() { // [Private]
			// set namespace appelation
			var ns = "div#ss-" + dirv.namespace + " ";
			// set css size context for enclosure
			// FIX: This should be set manually to avoid problems when JS is turned off
			glow.dom.get(D.enclosure + "#ss-" + dirv.namespace).addClass(D.prefix.enclosure + dirv.size);
			// declare global variables
			slides = glow.dom.get(ns + D.entries).children();
			max = dirv.total;
			// set init state, showing slide01 and hiding all others
			showSlide(1);
			// show controls
			glow.dom.get(ns + D.controls).addClass(D.controls_enabled.split(".")[1]);
			// set css size context for locator
			glow.dom.get(ns + D.locator).addClass(D.locator.split(".")[1] + "-" + dirv.size);
			// set max slide
			max = dirv.total;
			/* button states */
			// back button : off when slide = 1 (at init | triggered by signal), active all other times
			// next button : off when slide = max (triggered by signal), active all other times
			var back_btn = glow.dom.get(ns + D.controls + " " + D.back);
			var next_btn = glow.dom.get(ns + D.controls + " " + D.next);
			glow.events.addListener(
				back_btn,
				"click",
				clickTrigger,
				back_btn
			);
			glow.events.addListener(
				next_btn,
				"click",
				clickTrigger,
				next_btn
			);
			// back button at init
			announce("init");
			// 1 of max
			glow.dom.get(ns + D.locator).text(
			glow.lang.interpolate("{current} of {total}", {current:"1",total:dirv.total}));
		}
		
		function showSlide(which) { // [Private]
			slides.each(function(i) {
				if ( i == (which - 1) ) {
					glow.dom.get(this).css("display", "block");
				} else {
					glow.dom.get(this).css("display", "none");
				}
			});
		}
		
		function clickTrigger() { // [Private]
			if (this.attr("class").match(/back/)) {
				announce("back");
			} else if (this.attr("class").match(/next/)) {
				announce("next");
			}
		}
		
		function announce(trigger) { // [Private]
			var ns = "div#ss-" + dirv.namespace + " ";
			var back_btn = glow.dom.get(ns + D.controls + " " + D.back);
			var next_btn = glow.dom.get(ns + D.controls + " " + D.next);
			
			// special announce
			if (trigger == "init") {
				back_btn.addClass(D.back_off.split(".")[1]);
				return;
			}
			
			// invalid conditions
			if (
			((trigger == "back") && (c_slide == 1)) ||
			((trigger == "next") && (c_slide == max))
			) { return false; }
			
			// update current slide
			if (trigger == "next") {
				c_slide = c_slide + 1;
			} else if (trigger == "back") {
				c_slide = c_slide - 1;
			}
			
			if (c_slide == max) {
				// blank out next
				next_btn.addClass(D.next_off.split(".")[1]);
			} else if (c_slide == 1) {
				// blank out back
				back_btn.addClass(D.back_off.split(".")[1]);
			}
			if (max == 3 && c_slide == 2) {
				// unblanken next and back
				next_btn.removeClass(D.next_off.split(".")[1]);
				back_btn.removeClass(D.back_off.split(".")[1]);
			} else if (c_slide == (max - 1)) {
				// unblanken next
				next_btn.removeClass(D.next_off.split(".")[1]);
			} else if (c_slide == 2) {
				// unblanken back
				back_btn.removeClass(D.back_off.split(".")[1]);
			}
			
			// do it
			showSlide(c_slide);
			
			// update locator
			glow.dom.get(ns + D.locator).text(
			glow.lang.interpolate("{current} of {total}", {current:c_slide,total:max}));
		}
		
		
		return that;
	},
	glow: {} // stores passed glow
}