
if(typeof itdr=="undefined")var itdr={};
if(typeof itdr.classes=="undefined")itdr.classes={};

if(typeof itdr.classes.BlogShow=="undefined")itdr.classes.BlogShow=new function() {
	// -------------------------------------------------------
	//	BEGIN PRIVATE MEMBERS
	// -------------------------------------------------------
	var current=-1;
	var xhttp = ajax();
	var objects = new Array();
	var index = -1;
	var loading = false;
	var zeno_index = -1;
	var zeno_objects = new Object();
	var zeno_timer;
	/*
		for animation, we're using zeno's paradox
		essentially halving the delta on each "frame".
		we're gonna use ain interval of 33 milliseconds,
		which equates roughly to 30 fps
		*/
	function zeno(element, property, end, units) {
		if (!element.id) {
			++zeno_index;
			element.id = "zeno-object-" + zeno_index;
		}
		var zeno_id = element.id + "-" + property;
		if (!zeno_objects[zeno_id]) {
			zeno_objects[zeno_id] = new Object();
			zeno_objects[zeno_id].obj = element;
			zeno_objects[zeno_id].prop = property;
			zeno_objects[zeno_id].proxy = parseFloat(element.style[property]);
			zeno_objects[zeno_id].units = units;
		}
		zeno_objects[zeno_id].end = end;
		clearInterval(zeno_timer);
		zeno_timer = setInterval(zeno_exec, 33);
		zeno_exec();
	};
	function zeno_exec() {
		var active = 0;
		for(var zeno_id in zeno_objects) {
			++active;
			var zeno_object = zeno_objects[zeno_id];
			zeno_object.proxy += (zeno_object.end - zeno_object.proxy)/10;
			var value = Math.round(zeno_object.proxy);
			zeno_object.obj.style[zeno_object.prop] = value + zeno_object.units;
			if (value == zeno_object.end) delete zeno_objects[zeno_id];
		}
		if (active==0) clearInterval(zeno_timer);
	};
	/*
		loads the next blogshow in sequence
		only if it's not currenlt loading another doc, or if we're done loading
		*/
	function loadNext() {
		if (loading) return false;
		loading = true;
		++index;
		if (index==objects.length) return false;
		var lookup = objects[index];
		var uri = lookup.data + "?" + (new Date().getTime());
		xhttp.open("GET", uri, true);
		xhttp.onreadystatechange=function() {
			if (xhttp.readyState==4) onData();
		};
		xhttp.send(null);
	};
	/*
		does the prep-work after a successful ajax load request
		*/
	function onData() {
		loading = false;
		// create a base uri by popping off the filename
		var lookup = objects[index];
		var uri = lookup.data.split("/");
		uri.pop();
		// normalize values and convert to integers where applicable
		lookup.base_path = uri.join("/");
		lookup.show_width = parseInt(lookup.show_width);
		lookup.show_height = parseInt(lookup.show_height);
		lookup.thumbnail_width = parseInt(lookup.thumbnail_width);
		lookup.thumbnail_height = parseInt(lookup.thumbnail_height);
		lookup.show_inner_width = lookup.thumbnail_alignment=="right" ? lookup.show_width - lookup.thumbnail_width : lookup.show_width;
		lookup.show_inner_height = lookup.thumbnail_alignment=="bottom" ? lookup.show_height - lookup.thumbnail_height : lookup.show_height;
		// ok, the response text is the .dat file -- each line is one table entry
		var lines = xhttp.responseText.split("\n");
		var images = new Array();
		for(var i=0; i<lines.length; ++i) {
			var line = lines[i];
			var columns = line.split("\t");
			var filename = columns[0];
			var dimensions = columns[2].split("x");
			var img = new Object();
			var width = parseInt(dimensions[0]);
			var height = parseInt(dimensions[1]);
			img.src = lookup.base_path + "/" + filename;
			img.thumb = lookup.static_uri + "plugins/BlogShow/php/resample.php?e=1&w=" + lookup.thumbnail_width + "&h=" + lookup.thumbnail_height + "&u=" + lookup.base_path + "/" + filename;
			if (lookup.ssr=="1") img.src = lookup.static_uri + "plugins/BlogShow/php/resample.php?e=" + lookup.autofit + "&w=" + lookup.show_inner_width + "&h=" + lookup.show_inner_height + "&u=" + lookup.base_path + "/" + filename;
			img.width = lookup.show_inner_width;
			img.height = lookup.show_inner_height;
			if (lookup.autofit=="0") {
				var actual = width/height;
				var visible = img.width/img.height;
				if (actual>=visible) {
					img.height = (img.width*height)/width;
				} else {
					img.width = (img.height*width)/height;
				}
				if(img.width>=width||img.height>=height) {
					img.width = width;
					img.height = height;
				}
			}
			img.width = Math.floor(img.width);
			img.height = Math.floor(img.height);
			images.push(img);
		}
		lookup.image_array = images;
		display();
		setTimeout(loadNext, 33);
	};
	/*
		draws all the hml content for the blogshow diplay
		*/
	function display() {
		var lookup = objects[index];
		var container = document.getElementById("blogshow-runtime-" + index);
		var is_right = lookup.thumbnail_alignment=="right";
		var inner_height = lookup.show_inner_height;
		var inner_width = lookup.show_inner_width;
		var thumbnail_height = lookup.thumbnail_height;
		var thumbnail_width = lookup.thumbnail_width;
		var images = lookup.image_array;
		var total = images.length;
		var thumbnail_xhtml = '<div style="padding:0;margin:0;position:absolute;top:'
			+ (is_right?0:inner_height) + 'px;left:'
			+ (is_right?inner_width:0) + 'px;height:'
			+ (is_right?total*thumbnail_height:thumbnail_height) + 'px;width:'
			+ (is_right?thumbnail_width:total*thumbnail_width) + 'px" '
			+ 'onmouseover="itdr.classes.BlogShow.attach(this, ' + index + ');" '
			+ 'onmouseout="itdr.classes.BlogShow.detach(this);">';
		for (var i=0; i<total; ++i) {
			var img = images[i];
			thumbnail_xhtml +='<div style="cursor:pointer;overflow:hidden;padding:0;margin:0;position:absolute;top:'
				+ (is_right?i*thumbnail_height:0) + 'px;left:'
				+ (is_right?0:i*thumbnail_width) + 'px;width:'
				+ thumbnail_width + 'px;height:'
				+ thumbnail_height + 'px" '
				+ 'onmouseover="itdr.classes.BlogShow.thumbover(this, ' + index + ', ' + i + ');" '
				+ 'onmouseout="itdr.classes.BlogShow.thumbout(this, ' + index + ', ' + i + ');" '
				+ 'onclick="itdr.classes.BlogShow.click(this, ' + index + ', ' + i + ');" >';
			thumbnail_xhtml +='<img style="position:absolute;top:0px;left:0px;display:block;padding:0;margin:0;border:none" src="'
				+ img.thumb + '" width="'
				+ thumbnail_width + '" height="'
				+ thumbnail_height + '" />';
			thumbnail_xhtml +='</div>';
		}
		thumbnail_xhtml += '</div>';
		var xhtml = '<div style="padding:0;margin:0;position:relative;overflow:hidden;background-color:'
			+ lookup.background_color + ';width:'
			+ lookup.show_width + 'px;height:'
			+ lookup.show_height + 'px">';
		xhtml += '<div style="padding:0;margin:0;overflow:hidden;position:absolute;top:0;left:0;width:'
			+ inner_width + 'px;height:'
			+ inner_height + 'px">';
		xhtml += '<img style="position:absolute;margin:0;padding:0;border:none;display:block" src="" width="" height="" />';
		xhtml += '</div>' + thumbnail_xhtml + '</div>';
		container.innerHTML = xhtml;
		setSelected(container.firstChild.lastChild.firstChild, index, 0);
	};
	/*
		thumbnail functions
		*/
	function thumbnailOn (element, width, height, color) {
		if (!element) return false;
		element.style.border = "10px solid " + color;
		element.style.width = (width-20) + "px";
		element.style.height = (height-20) + "px";
		var stroke = element.firstChild;
		stroke.style.top="-10px";
		stroke.style.left="-10px";
	};
	function thumbnailOff (element, width, height) {
		if (!element) return false;
		element.style.borderStyle = "none";
		element.style.width = width + "px";
		element.style.height = height + "px";
		var stroke = element.firstChild;
		stroke.style.top = "0px";
		stroke.style.left = "0px";
	};
	function setSelected (element, show_index, image_index) {
		var lookup = objects[show_index];
		var container = document.getElementById("blogshow-runtime-" + show_index);
		var img_container = container.firstChild.firstChild.firstChild;
		var img = lookup.image_array[image_index];
		img_container.src = img.src;
		img_container.setAttribute("width", img.width);
		img_container.setAttribute("height", img.height);
		img_container.style.left = Math.round((lookup.show_inner_width-img.width)/2)+"px";
		img_container.style.top = Math.round((lookup.show_inner_height-img.height)/2)+"px";
		thumbnailOff(lookup.current, lookup.thumbnail_width, lookup.thumbnail_height);
		lookup.current = element;
		thumbnailOn(lookup.current, lookup.thumbnail_width, lookup.thumbnail_height, lookup.thumbnail_highlight_color);
	};
	/*
		the mouse-actuated funcations that are powered by zigo
		*/
	function scrollfunc (evnt) {
		if (current==-1) return false;
		var lookup = objects[current];
		var is_right = lookup.thumbnail_alignment=="right";
		var coord1 = getElementCoord(this.parentNode);
		var coord2 = getCursorCoord(evnt);
		var f = is_right ? coord2[1]-coord1[1] : coord2[0]-coord1[0];
		var g = is_right ? this.parentNode.offsetHeight : this.parentNode.offsetWidth;
		var h = is_right ? this.offsetHeight : this.offsetWidth;
		var i = h-g;
		var j = f/g;
		var k = i*j;
		zeno(this, (is_right?"top":"left"), Math.round(k)*-1, "px");
	};
	function getCursorCoord (evnt) {
		var x = 0;
		var y = 0;
		if (!evnt) var evnt = window.event;
		if (evnt.pageX||evnt.pageY) {
			x = evnt.pageX;
			y = evnt.pageY;
		} else if (evnt.clientX||evnt.clientY) {
			x = evnt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			y = evnt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return [x, y];
	};
	function getElementCoord (element) {
		var x = 0;
		var y = 0;
		if (element.offsetParent) {
			do {
				x += element.offsetLeft;
				y += element.offsetTop;
			} while (element = element.offsetParent)
		}
		return [x, y];
	};
	/*
		get an instance of the borwser xmlhttp obect
		test for various implementations in various browsers
		*/
	function ajax () {
		var result = false;
		if (window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
				result = new XMLHttpRequest();
			} catch(e) {
				result = false;
			}
		} else if (window.ActiveXObject) {
			try {
				result = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					result = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					result = false;
				}
			}
		}
		if (result) {
			return result;
		}
	};
	/*
		blogshow's swf requires version 8 of the flash player.  if the user does not have flash 8
		don't bother them with all the update hockus-pocus, it will just confuse them.
		instead, we'll deliver an html/ajax version that they can't tell apart anyhow
		*/
	function hasFlash() {
		var major_required = 8;
		var minor_required = 0;
		var major_detected = 0;
		var minor_detected = 0;
		var version;
		var mso;
		if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
			version = navigator.plugins["Shockwave Flash"].description;
			if (typeof version!="undefined") {
				version = version.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
				major_detected = parseInt(version.replace(/^(.*)\..*$/, "$1"), 10);
				minor_detected = /r/.test(version) ? parseInt(version.replace(/^.*r(.*)$/, "$1"), 10) : 0;
			}
		} else if (window.ActiveXObject) {
			try {
				var mso = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			} catch(e) {
				try {
					var mso = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					major_detected = 6;
					minor_detected = 0;
					mso.AllowScriptAccess = "always";
				} catch (e) {
					if (major_detected==6) return false;
				}
				try {
					var mso = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (typeof mso=="object") {
				var version = mso.GetVariable("$version");
				if(typeof version!="undefined") {
					version = version.replace(/^\S+\s+(.*)$/, "$1").split(",");
					major_detected = parseInt(version[0], 10);
					minor_detected = parseInt(version[2], 10);
				}
			}
		}
		return major_detected>major_required || ( major_detected==major_required && minor_detected>=minor_required ) ? true : false;
	};
	/*
		parse a name-value pair delimited by ampersands or semicolons
		will safely convert +'s into spades as well
		*/
	function parse (query) {
		var lookup = new Object();
		if (!query) return lookup;
		var pairs = query.split(/[;&]/);
		for (var i=0; i<pairs.length; ++i) {
			var pair = pairs[i].split("=");
			if (!pair||pair.length!=2) continue;
			var name = unescape(pair[0]);
			var value = unescape(pair[1]).replace(/\+/g,' '); // convert +'s into spaces
			lookup[name] = value;
		}
		return lookup;
	};
	// -------------------------------------------------------
	//	BEGIN PUBLIC MEMBERS
	// -------------------------------------------------------
	this.thumbover = function (element, show_index) {
		var lookup = objects[show_index];
		if (lookup.current==element) return false;
		thumbnailOn(element, lookup.thumbnail_width, lookup.thumbnail_height, lookup.thumbnail_border_color);
	};
	this.thumbout = function (element, show_index) {
		var lookup = objects[show_index];
		if (lookup.current==element) return false;
		thumbnailOff(element, lookup.thumbnail_width, lookup.thumbnail_height);
	};
	this.click = function(element, show_index, image_index) {
		setSelected(element, show_index, image_index);
	};
	this.attach=function(element, show_index) {
		current = show_index;
		if (!element.onmousemove) element.onmousemove = scrollfunc;
	};
	this.detach = function(element) {
		current = -1;
		if (element.onmousemove) element.onmousemove="";
	};
	/*
		this is the main public function that runs each time the javascript is embedded
		it takes in the query string from the last js file
		and parses it.
		then, if flash > 8 is available, it embeds flash, otherwise, delivers the
		html version
		*/
	this.create = function() {
		var scripts = document.getElementsByTagName('script');
		var last = scripts[scripts.length-1];
		var query = last.src.replace(/^[^\?]+\??/, '');
		var lookup = parse(query);
		var is_ie = /*@cc_on!@*/ false;
		objects.push(lookup);
		if (hasFlash()) {
			var swf_uri = lookup.static_uri + "plugins/BlogShow/swf/blogshow.swf";
			var xhtml = '<div class="BlogShow" id="blogshow-runtime-' + (objects.length-1) + '">';
			xhtml += '<object style="outline:none"';
			if (is_ie) xhtml += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
			else xhtml += 'type="application/x-shockwave-flash" data="' + swf_uri + '"';
			xhtml += ' width="' + lookup.show_width + '" height="' + lookup.show_height + '">';
			if (is_ie) xhtml += '<param name="movie" value="' + swf_uri + '" />';
			if (lookup.background_color=="transparent") xhtml += '<param name="wmode" value="transparent" />';
			else xhtml += '<param name="bgcolor" value="' + lookup.background_color + '" />';
			xhtml += '<param name="flashvars" value="' + query + '" />';
			xhtml += '<param name="allowFullScreen" value="true" />';
			xhtml += '</object>';
			xhtml += '</div>';
			document.writeln(xhtml);
		} else {
			var xhtml = '<div class="BlogShow" id="blogshow-runtime-' + (objects.length-1) + '"><div style="width:' + lookup.show_width + 'px;height:' + lookup.show_height + 'px"></div></div>';
			document.writeln(xhtml);
			loadNext();
		}
	};
};
itdr.classes.BlogShow.create();
