/*

xFlow!

Created by Paul Whitrow - www.pwhitrow.com

xFlow! Software License - see xFlow! Software License.html in the xFlow! root directory

*/


/* Configuration variables */
var xflow_startimage = 6;				// Sets the image you wish to start at!
var xflow_focus = 3;					// Sets the numbers of images visible on each side of the focused one
var xflow_xstep = 100;					// default central image spacing!

var xflow_allowed_videos = new Array('flv');


/* Global variables */
var xflow_loading = '', xflow_loading_count = '', xflow_div = '', xflow_imagebank = '', xflow_captions = '', xflow_scrollbar = '', xflow_slider = '', xflow_barleft = '', xflow_barright = '', xflow_gallerymenu = '', xflow_gallerymenulist = '', xflow_slider_width = 0, xflow_caption_id = 0, xflow_new_caption_id = 0, xflow_current = 0, xflow_target = 0, xflow_memtarget = 0, xflow_timer = 0, xflow_array_images = new Array(), xflow_imgarray = new Array(), xflow_new_slider_pos = 0, xflow_dragging = false, xflow_dragobj = null, xflow_dragx = 0, xflow_posx = 0, xflow_newposx = 0, xflow_max = 0, xflow_vieweropen = false, xflow_imageviewer = '', xflow_viewerimageholder = '', xflow_viewermainimage = '', xflow_viewertext = '', xflow_viewervideo = '', xflow_viewerrightcontrol = '', xflow_viewerclosecontrol = '', xflow_viewerleftcontrol = '', xflow_preloadedimgs = new Array(), xflow_loadtimer, xflow_percent = 0, xflow_imageid, xflow_isvideo = false, xflow_browser = '', xflow_slidetextlink = '', scrollbar_width = 0;

/* Start functions */


/* detect IE! */
var IE = false;
if (document.all) 
{
	IE = true;
}

function XFLOW_init()
{
	/* Cache document objects in global variables */
	xflow_loading = XFLOW_getByClassName('loading');
	xflow_loading_count = XFLOW_getByClassName('loading-count', 'p');
	xflow_div = XFLOW_getByClassName('xflow');
	xflow_imagebank = XFLOW_getByClassName('images');
	xflow_scrollbar = XFLOW_getByClassName('scrollbar', 'ul');
	xflow_slider = XFLOW_getByClassName('slider', 'li');
	xflow_slidetextlink = XFLOW_getByClassName('slidetext','li');
	xflow_barleft = XFLOW_getByClassName('barleft', 'li');
	xflow_barright = XFLOW_getByClassName('barright', 'li');
	xflow_captions = XFLOW_getByClassName('captions');
	xflow_gallerymenu = XFLOW_getByClassName('gallerymenu');
	xflow_gallerymenulist = XFLOW_getByClassName('gallerymenulist', 'ul');

	/* get xflow_imageviewer objects */
	xflow_imageviewer = XFLOW_getByClassName('xflow_imageviewer');
	xflow_viewerimageholder = XFLOW_getByClassName('xflow_viewerimageholder');
	xflow_viewermainimage = document.getElementById('xflow_viewermainimage');
	xflow_viewertext = document.getElementById('xflow_viewertext');
	xflow_viewervideo = document.getElementById('xflow_viewervideo');
	xflow_viewerleftcontrol = XFLOW_getByClassName('xflow_viewerleftcontrol');
	xflow_viewerrightcontrol = XFLOW_getByClassName('xflow_viewerrightcontrol');
	xflow_viewerclosecontrol = document.getElementById('xflow_viewerclosecontrol');

	if(xflow_imagebank)
	{
		XFLOW_preLoad();
		XFLOW_setUp(true);
		XFLOW_show(xflow_imagebank);
		XFLOW_show(xflow_scrollbar);
		XFLOW_initMouseWheel();
		XFLOW_initMouseDrag();
		XFLOW_goto(xflow_startimage);
		xflow_captions.style.width	= xflow_scrollbar.offsetWidth + 'px';
		XFLOW_disableSelection(xflow_scrollbar);
		XFLOW_hide(xflow_loading);
	}
	
	XFLOW_menuSet();
}

function XFLOW_disableSelection(target)
{
	if(xflow_slider)
	{
		if (typeof target.onselectstart != 'undefined') /* IE route */
		{
			target.onselectstart = function(){return false}
		}
		else if (typeof target.style.MozUserSelect != 'undefined') /* Firefox route */
		{
			target.style.MozUserSelect = 'none'
		}
		else /* All other route (ie: Opera) */
		{
			target.onmousedown = function(){return false}
			target.style.cursor = 'default'
		}
	}
}

function XFLOW_preLoad()
{
	var preLoadImages = xflow_imagebank.getElementsByTagName('IMG');
	
	for(var i=0; i < preLoadImages.length; i++) 
	{
		xflow_preloadedimgs[i] = new Image();
		xflow_preloadedimgs[i].src = XFLOW_str_replace('/thumbs', '', preLoadImages[i].src);
		XFLOW_checkLoadStatus(i)
	}
}

function XFLOW_checkLoadStatus(i)
{
	var imgbg = xflow_imagebank.getElementsByTagName('IMG')[i].parentNode;

	if(XFLOW_isImgLoaded(xflow_preloadedimgs[i]))
	{
		xflow_percent += 1;

		var p = Math.round((xflow_percent / xflow_preloadedimgs.length) * 100);
		
		xflow_loading_count.innerHTML = p + '%';

		if(XFLOW_hasClass(imgbg, 'loading'));
		{
			XFLOW_removeClass(imgbg, 'loading');
		}

		if((i + 1) >= xflow_preloadedimgs.length)
		{
			clearTimeout(xflow_loadtimer);
			//window.status = 'xFlow Ready.';
		}
	}
	else
	{

		if(!XFLOW_hasClass(imgbg, 'loading'));
		{
			XFLOW_addClass(imgbg, 'loading');
		}

		xflow_loadtimer = setTimeout('XFLOW_checkLoadStatus(' + i + ')', 100)
	}
}

function XFLOW_isImgLoaded(img) 
{
	if (!img.complete) 
	{
        return false;
    }

    if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) 
	{
        return false;
    }

    return true;
}

function XFLOW_getFileExtension(file)
{
	var tmp = file.split('.');
	return (tmp[tmp.length - 1]);
}

function XFLOW_in_array(needle, haystack, argStrict) 
{
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) 
	{
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) 
		{
            found = true;
            break;
        }
    }
 
    return found;
}

function XFLOW_addClass(el, cls)
{
	el.className = el.className + ' ' + cls;
}

function XFLOW_removeClass(el, cls)
{
	el.className = XFLOW_str_replace(cls, '', el.className);
}

function XFLOW_hasClass(el, cls)
{
	if(el.className.indexOf(cls) >= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function XFLOW_getByClassName(classname,el)
{
	if(!el)
	{
		el = 'DIV';
	}

	var divs = document.getElementsByTagName(el);
	for(a = 0; a < divs.length; a++)
	{
		var cn = (divs[a].className);

		if(cn == classname)
		{
			return divs[a];
		}
	}

	return false;
}

function XFLOW_pause(ms) 
{
	var now = new Date();
	var exitTime = now.getTime() + ms;
	while (true) 
	{
		now = new Date();
		if (now.getTime() > exitTime)
		{
			return;
		}
	}
}

function XFLOW_menuSet()
{
	if(!xflow_gallerymenu) return;

	XFLOW_show(xflow_gallerymenu);

	/* is menu hide enabled? */
	if(xflow_gallerymenu.getAttribute('lang'))
	{
		XFLOW_changeOpac(0, xflow_gallerymenulist);

		xflow_gallerymenu.onmouseover = function()
		{
				XFLOW_changeOpac(100, xflow_gallerymenulist);
		}

		xflow_gallerymenu.onmouseout = function()
		{
				XFLOW_changeOpac(0, xflow_gallerymenulist);
		}
	}
}

function XFLOW_goto(n)
{
	n = n -1;
	XFLOW_glideTo(-(xflow_xstep * n), n);
}

function XFLOW_minimiseText(str, n)
{
	if(str.length > n)
	{
		return str.substring(0, n) + '&#8230;[more]';
	}
	else
	{
		return str;
	}
}

/* Main function */
function XFLOW_setUp(onload)
{
	if(typeof xFlowRemoteWidth != 'undefined' && xflow_div)
	{
		xflow_div.style.width = xFlowRemoteWidth + 'px';
	}

	/* Cache global variables, that only change on XFLOW_setUp */
	images_width = xflow_imagebank.offsetWidth;
	images_top = xflow_div.offsetTop;
	images_left = xflow_div.offsetLeft;
	max_xflow_focus = xflow_focus * xflow_xstep;
	size = images_width * 0.5;
	max_height = images_width * 0.5;

	if(xflow_imagebank) 
	{
		xflow_imagebank.style.height = images_width * 0.33 + 'px';  
	}

	xflow_browser = XFLOW_getBrowserInfo();

	scrollbar_width = images_width / 2.5;

	if(xflow_slidetextlink.offsetWidth > scrollbar_width)
	{
		xflow_slidetextlink.innerHTML = XFLOW_minimiseText(xflow_slidetextlink.innerHTML, 15);
	}

	if(xflow_slidetextlink)
	{
		xflow_slidetextlink.style.left = ((scrollbar_width / 2) - (xflow_slidetextlink.offsetWidth / 2)) + 10 + 'px';
	}

	if(xflow_scrollbar.style)
	{
		xflow_scrollbar.style.width = scrollbar_width + 'px';
		/* centralise the scroll bar a little more!  */
		xflow_scrollbar.style.left = 10 + 'px';
	}

	 /* IE fix! */
    if(IE)
	{
		xflow_barleft.style.left = -27 + 'px'
		xflow_barright.style.right = -47 + 'px'
	}

	/* Set slider attributes */
	xflow_slider.onmousedown = function () 
	{ 
		XFLOW_dragStart(this); 
	};

	/* Cache EVERYTHING! */

	XFLOW_loadImagesToArray();

	var i = 0;
	for (var index = 0; index < xflow_max; index++)
	{ 
		var image = xflow_imgarray[index];
		if (image.nodeType == 1)
		{
			xflow_array_images[i] = index;
			
			/* Set image onclick by adding i and x_pos as attributes! */
			image.onclick = function() 
			{ 
				XFLOW_glideTo(this.x_pos, this.i); 
			};

			image.x_pos = (-i * xflow_xstep);
			image.i = i;
			
			/* Add width and height as attributes ONLY once on first load */
			if(onload == true)
			{
				image.w = image.width;
				image.h = image.height;
			    image.defWidth = (image.width / 2);
			}

			image.url = XFLOW_str_replace('thumbs/', '', image.src);

			i++;
		}
	}

	xflow_max = xflow_array_images.length;

	XFLOW_moveTo(xflow_current);
	XFLOW_glideTo(xflow_current, xflow_caption_id);

	if(xflow_gallerymenu)
	{
		xflow_gallerymenu.style.width = xflow_scrollbar.offsetWidth + 'px';
	}

	/* set viewer controls */
	xflow_viewerleftcontrol.onclick = function()
	{
		if(xflow_imageid > 0)
		{
			XFLOW_handle(1);
		}
		this.blur();
	}

	xflow_viewerrightcontrol.onclick = function()
	{
		if(xflow_imageid < index)
		{
			XFLOW_handle(-1);
		}
		this.blur();
	}

	xflow_viewerclosecontrol.onclick = function()
	{
		XFLOW_closeViewer();
	}

	xflow_barleft.onclick = function()
	{
		XFLOW_handle(1);
	}

	xflow_barright.onclick = function()
	{
		XFLOW_handle(-1);
	}

}

function XFLOW_step()
{
	switch (xflow_target < xflow_current-1 || xflow_target > xflow_current+1) 
	{
		case true:
			XFLOW_moveTo(xflow_current + (xflow_target-xflow_current)/3);
			window.setTimeout(XFLOW_step, 50);
			xflow_timer = 1;
			break;

		default:
			xflow_timer = 0;
			break;
	}
}

function XFLOW_glideTo(x, xflow_new_caption_id)
{	
	/* Animate gliding to new x position */

	if(xflow_imgarray.length <= 1)
	{
		return;
	}

	xflow_target = x;
	xflow_memtarget = x;

	/* Display new caption */
	xflow_caption_id = xflow_new_caption_id;
	var caption = xflow_imgarray[xflow_caption_id].alt;
	var description = xflow_imgarray[xflow_caption_id].title;
	if (caption == '') caption = '&nbsp;';

	caption = XFLOW_minimiseText(caption, 20);

	description = XFLOW_minimiseText(description, 30);

	xflow_captions.innerHTML = '<p><strong>' + caption + '</strong></p><p><em>' + XFLOW_convertURL(description) + '</em></p>';

	/* Set scrollbar slider to new position */
	if(!xflow_dragging && xflow_slider)
	{
		xflow_new_slider_pos = Math.round((scrollbar_width * (-(x*100/((xflow_max-1)*xflow_xstep))) / 100));

		xflow_slider.style.left = Math.round(xflow_new_slider_pos - xflow_slider_width) + 'px';

	}

	if (xflow_timer == 0)
	{
		window.setTimeout(XFLOW_step, 50);
		xflow_timer = 1;
	}
	
}

/* This function is called on mouse movement and moves the slider on user action */
function XFLOW_drag(e)
{
	xflow_posx = IE ? window.event.clientX : e.pageX;

	if(xflow_dragobj != null)
	{

		xflow_dragging = true;
		xflow_newposx = (xflow_posx - xflow_dragx) + xflow_slider_width;

		/* Make sure, that the slider is moved in proper relation to previous movements by the XFLOW_glideTo function */
		if(xflow_newposx < ( - xflow_new_slider_pos)) xflow_newposx = - xflow_new_slider_pos;
		if(xflow_newposx > (scrollbar_width - xflow_new_slider_pos)) xflow_newposx = scrollbar_width - xflow_new_slider_pos;
		
		var slider_pos = (xflow_newposx + xflow_new_slider_pos);
		var step_width = slider_pos / ((scrollbar_width) / (xflow_max-1));
		var image_number = Math.round(step_width);
		var new_target = (image_number) * -xflow_xstep;
		var xflow_new_caption_id = image_number;

		xflow_dragobj.style.left = slider_pos + 'px';

		XFLOW_glideTo(new_target, xflow_new_caption_id);
	}
	else
	{
		xflow_dragging = false; 
	}
}

function XFLOW_loadImagesToArray()
{
	if(xflow_imagebank)
	{
		var imgs = xflow_imagebank.getElementsByTagName('IMG')
		xflow_max = imgs.length;
		for (var i = 0; i < xflow_max; i++)
		{ 
			xflow_imgarray[i] = imgs[i];
		}
	}
}

function XFLOW_moveTo(x)
{
	xflow_current = x;
	var zIndex = xflow_max;

	/* loop through images... */
	for (var index = 0; index < xflow_max; index++)
	{ 
		var image = xflow_imgarray[index];
		var current_image = index * -xflow_xstep;
		var imgbg = image.parentNode;


		/* Don't display images that are not focused */
		if ((current_image + max_xflow_focus) < xflow_memtarget || (current_image - max_xflow_focus) > xflow_memtarget)
		{
			image.style.visibility = 'hidden';
			image.style.display = 'none';
			imgbg.style.visibility = 'hidden';
			imgbg.style.display = 'none';
			imgbg.className = 'imagebg';
		}
		else 
		{
			/* are CSS3 classes enabled? */
			if((imgbg.getAttribute('lang')) && (imgbg.getAttribute('lang').indexOf('css3') >= 0))
			{
				if(current_image > xflow_memtarget)
				{
					imgbg.className = 'imagebg imgskewleft';
				}
				else if(current_image < xflow_memtarget)
				{
					imgbg.className = 'imagebg imgskewright';
				}
				else
				{
					imgbg.className = 'imagebg imgcentral';
				}
			}

			var z = Math.sqrt(10000 + x * x) + 100;
			var xs = x / z * size + size;

			/* Still hide images until they are processed, but set display style to block */
			image.style.display = 'block';
			imgbg.style.display = 'block';
		

			/* Process new image height and image width */
			var new_img_h = (image.h / image.w * image.defWidth) / z * size;

			if(isNaN(new_img_h)) continue; /* IE fix! */

			switch ( new_img_h > max_height )
			{
				case false:
					var new_img_w = image.defWidth / z * size;
					break;

				default:
					new_img_h = max_height;
					var new_img_w = image.w * new_img_h / image.h;
					break;
			}

			/* Set new image properties */
			
			if(IE)
			{
				var imgleft = 100+xs - (image.defWidth / 2) / z * (size) + images_left + 'px';
			}else{
				var imgleft = xs - (image.defWidth / 2) / z * size + images_left + 'px';
			}
	
			//var imgleft = xs - (image.defWidth / 2) / z * size + images_left + 'px';
			var new_img_top = (images_width * 0.34 - new_img_h) + images_top + new_img_h;

			if(new_img_w > xflow_div.offsetWidth)
			{
				new_img_w = xflow_div.offsetWidth / 2;
				new_img_h = (image.h / image.w * new_img_w) / z * size;
				imgleft = xs - (new_img_w / 2) / z * size + images_left + 'px';
			}

			imgbg.style.left = imgleft;
			imgbg.style.height = new_img_h + 'px';
			imgbg.style.width = new_img_w + 'px';
			imgbg.style.top = new_img_top + 'px';
			imgbg.style.visibility = 'visible';

			image.style.left = imgleft;
			image.style.height = new_img_h + 'px';
			image.style.width = new_img_w + 'px';
			image.style.top = new_img_top + 'px';
			image.style.visibility = 'visible';
			image.id = index;

			/* Set image layer through zIndex */
			switch ( x < 0 )
			{
				case true:
					zIndex++;
					break;

				default:
					zIndex = zIndex - 1;
					break;
			}
			
			/* Change zIndex and onclick function of the focused image */
			switch ( image.i == xflow_caption_id )
			{
				case false:
					image.onclick = function() 
					{ 
						XFLOW_glideTo(this.x_pos, this.i); 
					}
					break;

				default:
					zIndex = zIndex + 1;

					if(image.url != '')
					{
						/* events when clicking central image */
						image.onclick = function()
						{
							/* check if the description is a URL and go there if clicked */
							if(this.title.substr(0,4) == 'http')
							{
								document.location.href = this.title;
							}
							/* open main viewer if not a URL */
							else
							{
								XFLOW_openViewer(this);
							}
						}
					}
					
					/* set opacities for images either side of main */
					for(a=0; a < xflow_max; a++)
					{
						if((a > index + 3) || (a < index - 3))
						{ 
							XFLOW_changeOpac(15, xflow_imgarray[a]);
						}
						else if((a > index + 2) || (a < index - 2))
						{ 
							XFLOW_changeOpac(25, xflow_imgarray[a]);
						}
						else if((a > index + 1) || (a < index - 1))
						{
							XFLOW_changeOpac(35, xflow_imgarray[a]);
						}
						else if((a > index) || (a < index))
						{
							XFLOW_changeOpac(50, xflow_imgarray[a]);
						}
						else
						{
							// central image
							XFLOW_changeOpac(100, xflow_imgarray[a]);
						}

					}
					break;
			}

			/* make images appear on a curved plain! */
			imgbg.style.top = (new_img_h / 10) + 50 + images_top + 'px';

			imgbg.style.zIndex = zIndex;

		}
		x += xflow_xstep;
	}
}

function XFLOW_basename(file)
{
	var tmp = file.split('/');
	return (tmp[tmp.length - 1]);
}

function XFLOW_isVideo(str)
{
	if(XFLOW_isYouTube(str))
	{
		return str;
	}

	var ext = XFLOW_getFileExtension(str);
	var video = XFLOW_basename(str);

    if(XFLOW_in_array(ext, xflow_allowed_videos))
	{
		return video;
	}
	else
	{
		return false;
	}
}

function XFLOW_isYouTube(str)
{
	if(str.indexOf('youtube') > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function XFLOW_video(file)
{
	var url = file.getAttribute('longdesc');
	var src = file.src;
	var video = XFLOW_isVideo(url);

    if(video)
	{
		xflow_isvideo = true;
		var imgSize = file.className.split(':');
		var width = XFLOW_str_replace('px','',xflow_viewerimageholder.style.width);
		var height = XFLOW_str_replace('px','',xflow_viewerimageholder.style.height);
		var preview = XFLOW_basename(src);

		var s1 = new SWFObject('../../extras/player-viral.swf', 'ply', width, height, '9.0.0');
		s1.addParam('quality', 'best');
		s1.addParam('allowfullscreen', 'true');
		s1.addParam('allownetworking', 'all');
		s1.addParam('allowscriptaccess', 'always');

		if(XFLOW_isYouTube(video))
		{
			s1.addParam('flashvars', 'file=' + url + '&image=' + src);
			s1.addVariable('type','youtube');
		}
		else
		{
			s1.addParam('flashvars', 'file=' + XFLOW_str_replace('thumbs/' + preview, XFLOW_basename(url), src) + '&image=' + src);
		}

		s1.write('xflow_viewervideo');

		xflow_viewerimageholder.style.height = (xflow_viewerimageholder.offsetHeight + xflow_viewertext.offsetHeight) - 12 + 'px';
	}
	else
	{
		xflow_isvideo = false;
		return false;
	}
}

function XFLOW_openViewer(img)
{
	if(!XFLOW_isImgLoaded(xflow_preloadedimgs[parseFloat(img.id)]))
	{
		XFLOW_overlay(true, '<h2>Still preloading this image!</h2><p>Please be patient...</p>');

		var t = setTimeout(function(t)
		{
			XFLOW_overlay(false, false);
			clearTimeout(t);
		},  1000);

		return false;
	}
	else
	{
		XFLOW_updateImageViewer(parseFloat(img.id), false)
	}
}

function XFLOW_updateImageViewer(i, h)
{
	xflow_viewervideo.innerHTML = '';

	img = xflow_imgarray[i];

	XFLOW_setViewerImage(xflow_preloadedimgs[i].src, img.alt, img.title, img.rel, i);

	xflow_imageid = i;

	if(h)
	{
		i = XFLOW_handle(i, true);
	}
}

function XFLOW_setViewerImage(src, alt, txt, rel, i)
{

	if(!XFLOW_isImgLoaded(xflow_viewermainimage))
	{
		var t = setTimeout(function(src, alt, txt, rel, i, t)
		{
			XFLOW_setViewerImage(src, alt, txt, rel, i);
		}, 1);
	}
	else
	{

		xflow_viewermainimage.src = src;
		xflow_viewermainimage.alt = alt;
		
		XFLOW_resizeImage(xflow_imgarray[i]);

		var imgTitle = XFLOW_getByClassName('imagetitle', 'p');
		var imgDesc = XFLOW_getByClassName('imagetext', 'p');

		imgTitle.innerHTML = alt;
		imgDesc.innerHTML = XFLOW_convertURL(txt);

		if(document.getElementById('code_' + i))
		{
			imgDesc.innerHTML += XFLOW_safeHTML(document.getElementById('code_' + i).value);
		}

		if(!xflow_vieweropen)
		{
			XFLOW_viewerDisplay('show');
		}

		xflow_viewerimageholder.style.width = xflow_viewermainimage.offsetWidth + 'px';
		xflow_viewerimageholder.style.height = xflow_viewermainimage.offsetHeight + 'px';

		XFLOW_video(xflow_imgarray[i]);
		
		if(xflow_isvideo)
		{
			xflow_viewertext.style.display = 'block';

			xflow_viewerimageholder.onmouseover = function()
			{
				xflow_viewertext.style.display = 'block';

			}

			xflow_viewerimageholder.onmouseout = function()
			{
				xflow_viewertext.style.display = 'block';
			}
		}
		else
		{

			xflow_viewerimageholder.onmouseover = function()
			{
				xflow_viewertext.style.display = 'block';


			}

			xflow_viewerimageholder.onmouseout = function()
			{
				xflow_viewertext.style.display = 'none';
			}

		}

		XFLOW_setViewerTextSize(200);

		xflow_viewermainimage.onclick = function()
		{
			/* use this to capture a click on the main image! */
		}

		XFLOW_viewerControls(i);
	}
}

function XFLOW_viewerControls(i)
{
	xflow_viewerleftcontrol.style.display = 'block';
	xflow_viewerrightcontrol.style.display = 'block';

	if(i == 0)
	{
		xflow_viewerleftcontrol.style.display = 'none';
	}

	if(i >= (xflow_array_images.length - 1))
	{
		xflow_viewerrightcontrol.style.display = 'none';
	}
}

function XFLOW_setViewerTextSize(val)
{
	xflow_viewertext.style.height = 'auto';

	if(xflow_viewertext.style.overflow)
	{
		xflow_viewertext.style.overflow = 'none';
	}

	if(xflow_viewertext.offsetHeight > val)
	{
		xflow_viewertext.style.height = val + 'px';
		xflow_viewertext.style.overflowY = 'auto';
	}
}

function XFLOW_resizeImage(img)
{
	/* get image dimensions */
	imgSize = img.className.split(':');
	imgWidth = parseFloat(imgSize[0]);
	imgHeight = parseFloat(imgSize[1]);

	/* arbitrary value to get a nice image size! */
	var xBy = 120;

	/* resize the image */
	if((imgHeight + xBy) > xflow_browser['H'])
	{
		xflow_viewermainimage.style.width = 'auto';
		xflow_viewermainimage.style.height = Math.round(xflow_browser['H'] - xBy) + 'px'; 
	}
	else if((imgWidth + xBy) > xflow_browser['W'])
	{
		xflow_viewermainimage.style.width = Math.round(xflow_browser['W'] - xBy) + 'px';
		xflow_viewermainimage.style.height = 'auto'; 
	}
	else
	{
		xflow_viewermainimage.style.width = imgWidth + 'px';
		xflow_viewermainimage.style.height = imgHeight + 'px'; 
	}

}

function XFLOW_getBrowserInfo()
{
	xflow_browser = new Array();

	if( typeof( window.innerHeight ) == 'number' ) 
	{
		xflow_browser['W'] = window.innerWidth;
		xflow_browser['H'] = window.innerHeight;
	} 
	else 
	{
		xflow_browser['W'] = document.documentElement.clientWidth;
		xflow_browser['H'] = document.documentElement.clientHeight;
	}

	return xflow_browser;
}

function XFLOW_viewerDisplay(val)
{
	if(val == 'show')
	{
		xflow_imageviewer.style.display				= 'none';
		xflow_viewerimageholder.style.display		= 'block';
		xflow_viewermainimage.style.display			= 'block';
		xflow_viewerclosecontrol.style.display		= 'block';
		xflow_viewertext.style.display				= 'block';
		xflow_imageviewer.style.display				= 'block';
		xflow_vieweropen = true;
	}
	if(val == 'hide')
	{
		xflow_viewerimageholder.style.display		= 'none';
		xflow_viewermainimage.style.display			= 'none';
		xflow_viewerclosecontrol.style.display		= 'none';
		xflow_viewertext.style.display				= 'none';
		xflow_imageviewer.style.display				= 'none';
		xflow_vieweropen = false;
	}
	if(val == 'loading')
	{
		xflow_viewerimageholder.style.display		= 'none';
		xflow_viewermainimage.style.display			= 'none';
		xflow_viewerclosecontrol.style.display		= 'none';
		xflow_viewertext.style.display				= 'none';
		xflow_imageviewer.style.display				= 'block';
		xflow_vieweropen = true;
	}
}

function XFLOW_closeViewer()
{
	XFLOW_viewerDisplay('hide');
}

/* Change opacity for image (cross browser) */
function XFLOW_changeOpac(opacity, obj) 
{
	var object = obj.style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = 'alpha(opacity=' + opacity + ')';
}

/* Show/hide element functions */
function XFLOW_show(id)
{
	if(typeof id == 'object')
	{
		var el = id;
	}
	else
	{
		var el = XFLOW_getByClassName(id);
	}
	if(el)
	{
		el.style.visibility = 'visible';
	}
}

function XFLOW_hide(id)
{
	if(typeof id == 'object')
	{
		var el = id;
	}
	else
	{
		var el = XFLOW_getByClassName(id);
	}
	if(el)
	{
		el.style.visibility = 'hidden';
		el.style.display = 'none';
	}
}

/* Handle the wheel angle change (delta) of the mouse wheel */
function XFLOW_handle(delta, ret)
{
	var change = false;
	switch (delta > 0)
	{
		case true:
			if(xflow_caption_id >= 1)
			{
				xflow_target = xflow_target + xflow_xstep;
				xflow_new_caption_id = xflow_caption_id - 1;
				change = true;
			}
			break;

		default:
			if(xflow_caption_id < (xflow_max-1))
			{
				xflow_target = xflow_target - xflow_xstep;
				xflow_new_caption_id = xflow_caption_id + 1;
				change = true;
			}
			break;
	}
	
	/* Glide to next (mouse wheel down) / previous (mouse wheel up) image */
	if (change == true)
	{
		XFLOW_glideTo(xflow_target, xflow_new_caption_id);
	}
	
	/* update main viewer if it's open */
	if(xflow_vieweropen)
	{
		XFLOW_updateImageViewer(xflow_new_caption_id, false);
	}

	if(ret) return xflow_new_caption_id;
}

/* Event handler for mouse wheel event, also will capture and move xflow_imageviewer accordingly */
function XFLOW_wheel(event)
{
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta)
	{
		delta = event.wheelDelta / 120;
	}
	else if (event.detail)
	{
		delta = -event.detail / 3;
	}


	if (delta)
	{
		var n = XFLOW_handle(delta, true);

		/* update main viewer if it's open */
		if(xflow_vieweropen)
		{
			XFLOW_updateImageViewer(n, false);
		}
	}
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
}

/* Initialize mouse wheel event listener */
function XFLOW_initMouseWheel()
{
	if(window.addEventListener) xflow_div.addEventListener('DOMMouseScroll', XFLOW_wheel, false);
	xflow_div.onmousewheel = XFLOW_wheel;
}

/* This function is called to drag an object (= slider div) */
function XFLOW_dragStart(element)
{
	xflow_dragobj = element;
	xflow_dragx = xflow_posx - xflow_dragobj.offsetLeft + xflow_new_slider_pos;
}

/* This function is called to stop dragging an object */
function XFLOW_dragStop()
{
	xflow_dragobj = null;
	xflow_dragging = false;
}

/* Initialize mouse event listener */
function XFLOW_initMouseDrag()
{
	document.onmousemove = XFLOW_drag;
	document.onmouseup = XFLOW_dragStop;
}


/* Capture keyboard actions */
document.onkeydown = function(ev) 
{
    ev = ev || window.event;
    kCode = ev.keyCode || ev.which;

	/* Login: ctrl + shift + x */
    if (ev.ctrlKey && ev.shiftKey && kCode == 19 || ev.ctrlKey && ev.shiftKey && kCode == 88) 
	{
		/* grab a string from a .js file path and parse it to get root location, append admin dir and redirect */
		document.location.href = XFLOW_str_replace('js/xflow_global.js', '', document.getElementById('globalscript').src) + 'admin' + (XFLOW_getByClassName('adminlinks','fieldset') ? '?logout=true' : '');
        return false;
    }

	/* Right arrow */
	if(kCode == 39)
	{
		XFLOW_handle(-1);
	}

	/* Left arrow */
	if(kCode == 37)
	{
		XFLOW_handle(1);
	}

	/* Escape */
	if(kCode == 27)
	{
		if (xflow_vieweropen)
		{
			XFLOW_closeViewer();
		}
	}

	/* Enter */
	if(kCode == 13)
	{
		if (xflow_vieweropen)
		{
			XFLOW_closeViewer();
		}
		else
		{
			XFLOW_openViewer(document.getElementById(xflow_new_caption_id));

		}
	}
}

/* Convert textual links to actual urls */
function XFLOW_convertURL(text)
{
	if( !text ) return text;
	text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
	nice = url;
	if( url.match('^https?:\/\/') )
	{
		nice = nice.replace(/^https?:\/\//i,'')
	}
	else
		url = 'http://'+url;
		return '<a href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';
	});
	return text;
}

/* XFLOW_setUp xflow on window resize */
window.onresize = function()
{
	if(XFLOW_getByClassName('xflow'))
	{
		XFLOW_setUp();
	}
}

/* start code */
XFLOW_addLoadEvent(XFLOW_init);




