/*

GZIP CONTENT DELIVERY
Copyright 2010 Fat Publisher

All rights reserved. Republication, redistribution,
replication or use of Fat Publisher content / scripting
is strictly prohibited without the prior written consent of Fat Publisher.

www.fatpublisher.com.au

Content generated : 07:53:44 am 07 September 2010

*/

/*************************************************

SLIDE SHOW SCRIPT WITH FADING TRANSITIONS
- loads from xml file

*************************************************/

var slideshow = {
	
	id: 0,
	fade_amount: 5,
	fade_timer: 10,
	instances: [],
	debug: [],
	current_instance: 0,
	resize_attached: false,
	pause: 2,
	do_fade: true,
	preload: false,				// sets whether you want to preload all images on initialisation or just as needed - not in use
	loading: false,
	load_queue: [],
	
	new_instance: function()
	{
		var inst = slideshow.instances[slideshow.id] = {
			id: slideshow.id,
			slides: [],
			control_required: false,
			control_id: "img_ctrls",
			img_id: "img_ph",
			imgpath: "",
			xmlfile: "",
			current_index: 0,
			current_pane: 0,
			previous_pane: 0,
			timer: 0,
			tab: 0,
			in_action: false
		};
		slideshow.id ++;
		return inst;
	},

	get_instance: function(id)
	{
		if (id == null) {
			id = 0;
		}
		if (typeof(slideshow.instances[id]) == "undefined") {
			return slideshow.new_instance();
		}
		else {
			return slideshow.instances[id];
		}
	},

	// load xml data to populate data
	load_xml: function(instance)
	{
		slideshow.load_queue.push(instance);
		slideshow.do_load();
	},

	do_load: function()
	{
		if (slideshow.loading) {
			setTimeout(function() { slideshow.do_load(); }, 250);
		}
		else if (slideshow.load_queue.length != 0 && slideshow.load_queue[0]) {
			slideshow.loading = true;
			slideshow.current_instance = slideshow.load_queue[0].id;
			xml_get_request(slideshow.load_queue[0].xmlfile, slideshow.save_data);
			slideshow.load_queue.shift();
		}
	},
	
	save_data: function(obj)
	{
		var instance = slideshow.get_instance(slideshow.current_instance);
		var tabs = obj.responseXML.getElementsByTagName("tab");
		for (var x = 0; x < tabs.length; x ++) {
			if (tabs[x].getAttribute("id") == instance.tab) {
				try {
					var name = xml_decode_chars(obj.responseXML.getElementsByTagName("name")[0].firstChild.data);
				}
				catch (e) {
					var name = "";
				}
				var tab = tabs[x];
				var imgs = tab.getElementsByTagName("image");
				var control_required = (imgs.length > 1);
				for (var w = 0; w < imgs.length; w ++) {
					var src = instance.imgpath + "/" + xml_decode_chars(imgs[w].getElementsByTagName("thumb")[0].firstChild.data);
					try {
						var caption = xml_decode_chars(imgs[w].getElementsByTagName("caption")[0].firstChild.data);
					}
					catch (e) {
						var caption = name;
					}
					try {
						var orig = xml_decode_chars(imgs[w].getElementsByTagName("orig")[0].firstChild.data);
					}
					catch (e) {
						var orig = null;
					}
					slideshow.create_slide(instance, src, caption, orig);
				}
				slideshow.set_slide_positions();
				if (control_required) {
					slideshow.setup_controls(instance);
				}
				else {
					slideshow.loading = false;
				}
				break;
			}
		}
	},
	
	setup_controls: function(instance)
	{
		var ctrls = document.getElementById(instance.control_id);
		if (ctrls) {
			var lnks = ctrls.getElementsByTagName("A");
			lnks[0].onclick = function() {
				this.blur();
				slideshow.slideshow_end(instance);
				slideshow.change_image(-1, instance);
				return false;
			}
			lnks[1].id = "INST:" + instance.id + ":CTRL:PLAY";
			lnks[1].onclick = function() {
				this.blur();
				this.className = (this.className == "play") ? "pause" : "play";
				slideshow.slideshow(instance);
				return false;
			}
			lnks[2].onclick = function() {
				this.blur();
				slideshow.slideshow_end(instance);
				slideshow.change_image(1, instance);
				return false;
			}
			ctrls.style.display = "";
		}
		instance.current_pane = 0;
		slideshow.loading = false;
	},

	get_pane: function(instance, index)
	{
		return instance.slides[index];
	},
	
	change_image: function(offset, instance)
	{
		var img = slideshow.get_pane(instance, instance.current_index);
		img.style.zIndex = 0;
		if (img) {
			instance.previous_pane = img;
			instance.current_index = instance.current_index + offset;
			if (instance.current_index < 0) {
				instance.current_index = instance.slides.length - 1;
			}
			else if (instance.current_index > (instance.slides.length - 1)) {
				instance.current_index = 0;
			}
			instance.current_pane = slideshow.get_pane(instance, instance.current_index);
			if (slideshow.do_fade) {
				instance.current_pane.style.zIndex = instance.slides.length;
				//slideshow.setOpacity(instance.current_pane, 0);
				slideshow.fade_obj(instance.current_pane, "in", instance);
				slideshow.fade_obj(img, "out", instance);
			}
			else {
				slideshow.setOpacity(img, 0);
				slideshow.setOpacity(instance.current_pane, 100);
				if (instance.in_action) {
					instance.timer = setTimeout(function() { slideshow.change_image(1, instance); }, slideshow.pause * 1000);
				}
			}
			//slideshow.preload_next(instance);
		}
	},
	
	// return a styles value through js
	get_computed_style: function(obj, style)
	{
		// mozilla
		if (document.defaultView && document.defaultView.getComputedStyle) {
			try {
				return document.defaultView.getComputedStyle(obj, null).getPropertyValue(style);
			}
			catch (e) {
				return false;
			}
		}
		// internet explorer
		else if (obj.currentStyle) {	
			var temp = style.split(/-/);
			style = "";
			for (var a = 0; a < temp.length; a ++) {
				style += ((a != 0) ? temp[a].substring(0, 1).toUpperCase() : temp[a].substring(0, 1).toLowerCase()) + temp[a].substring(1).toLowerCase();
			}
			return eval("obj.currentStyle." + style);
		}
		return false;	
	},
	
	// creates a floating layer containing the image
	create_slide: function(instance, src, caption, popup)
	{		
		var img = document.getElementById(instance.img_id);
		if (img) {
			if (! slideshow.resize_attached) {
				window.onresize = slideshow.set_slide_positions;
				slideshow.resize_attached = true;
			}
			var div = document.createElement("DIV");
			var slide = document.createElement("IMG");
			slide.id = instance.img_id + "[" + instance.slides.length + "]";
			slide.className = img.className;
			slide.src = src;
			slide.style.position = "absolute";
			slide.style.top = 0;
			slide.alt = slide.title = caption;			
			slide.style.zIndex = 0;
			if (popup) {
				slide.onmouseover = function() {
					this.style.cursor = "pointer";
				}
				slide.onclick = function() {
					slideshow.popup(instance);
				}
			}
			slideshow.setOpacity(slide, 0);
			div.appendChild(slide);
			document.body.appendChild(div);
			instance.slides.push(slide);
		}
	},

	popup: function(instance)
	{
		var pane = slideshow.get_pane(instance, instance.current_index);
		window.open(pane.src.replace(/thumb/, "orig"));
	},

	set_slide_positions: function()
	{
		for (var p = 0; p < slideshow.instances.length; p ++) {
			var inst = slideshow.instances[p];
			var img = document.getElementById(inst.img_id);			
			if (img) {
				var mt = slideshow.get_computed_style(img.parentNode, "margin-top").replace(/px/, "");
				var mr = slideshow.get_computed_style(img.parentNode, "margin-right").replace(/px/, "");
				var left = get_position(img, "Left") - 10;
				var top = get_position(img, "Top") - 8;
				for (var t = 0; t < inst.slides.length; t++) {
					var slide = inst.slides[t];
					slide.style.top = top + "px";
					slide.style.left = left + "px";
					slide.style.width = (img.offsetWidth - 2) + "px";
					slide.style.height = (img.offsetHeight - 2) + "px";
				}
			}
		}
	},

	setOpacity: function(obj, percent)
	{
		if (percent < 0 || percent == 100) {
			return false;
		}
		if (document.all) {
			obj.style.filter = "alpha(opacity=" + percent + ")";
		}
		else if (obj.style.MozOpacity) {
			obj.style.MozOpacity = (percent / 100).toFixed(2);
		}
		else {
			obj.style.opacity = (percent / 100).toFixed(2);
		}
		obj.setAttribute("fade_percentage", percent);
		return true;
	},

	fade_obj: function(obj, direction, instance)
	{
		clearTimeout(obj.getAttribute("fade_timer"));	

		var fade_percent = parseInt(obj.getAttribute("fade_percentage"));

		fade_percent = (direction == "in") ? parseInt(fade_percent + slideshow.fade_amount) : parseInt(fade_percent - slideshow.fade_amount);
		slideshow.setOpacity(obj, fade_percent);

		if (fade_percent >= 0 && fade_percent < 100) {
			obj.setAttribute("fade_timer", setTimeout(function() { 
				slideshow.fade_obj(obj, direction, instance); 
			}, slideshow.fade_timer));
		}
		else{
			if (instance.in_action && direction == "in") {
				instance.timer = setTimeout(function() { slideshow.change_image(1, instance); }, slideshow.pause * 1000);
			}
			//slideshow.setOpacity(instance.previous_pane, 0);
		}
	},

	slideshow: function(instance)
	{
		if (instance.in_action) {
			slideshow.slideshow_end(instance);
		}
		else {
			instance.in_action = true;
			slideshow.change_image(1, instance);
		}
	},

	slideshow_end: function(instance)
	{
		instance.in_action = false;
		clearTimeout(instance.timer);
		document.getElementById("INST:" + instance.id + ":CTRL:PLAY").className = "play";
	}

	/*preload_next: function(instance)
	{
		var y = instance.data[instance.current_index + 1];
		if (y && (typeof(y.tagName) == "undefined" || y.tagName != "IMG")) {
			var x = new Image();
			x.src = y.src;
		}
	}*/
};