// Copyright © 2005-2006 Water Street Web Solutions, LLC.  All rights reserved. --
// ----------------------------------------------------------------------------------
// Water Street Web Solutions (WSWS) software and code [both binary and source 
// (if released)] (Software) is intellectual property owned by WSWS and is copyright 
// of WSWS in all countries in the world, and ownership remains with WSWS.
//
// You (Licensee) may use the WSWS Software and install it only in accordance with 
// use and licensing agreements you have signed with WSWS.  Licenses are distributed 
// only by WSWS. Please send an email to info@waterstreetusa.com for more information.
//
// If you do not have a valid signed license agreement, you may not use, copy or
// distribute the Software for any purpose.
//
// This copyright notice may not be removed or altered or it will constitute a
// violation of any license agreements.
// ----------------------------------------------------------------------------------


var enablePopups = true;

if(!mvs)
	var mvs = new Array();

function wsgallery()
{
	this.Init = Init;
	this.SelectImage = SelectImage
	this.NextImage = NextImage;
	this.PrevImage = PrevImage;
	this.OffsetImage = OffsetImage;
	this.WrapIndex = WrapIndex;
	this.DoPopup = DoPopup;
	this.SetCaption = SetCaption;
	this.OnRenameClick = OnRenameClick;
	this.OnUpdateCaptionClick = OnUpdateCaptionClick;
	this.SetCurrentIndex = SetCurrentIndex;
	this.GetCurrentIndex = GetCurrentIndex;
	
	function Init()
	{
		this.SetCurrentIndex(this.GetCurrentIndex());
		this.mainImage = document.getElementById(this.prefix + "Main");
		this.Slide = document.getElementById(this.prefix + "divMain");
		this.SetCaption();
				
		//	if(!(this.mainImage || this.rowPreview))
		//	if(!this.mainImage)
		//		this.failed = true;

		this.SelectImage(this.GetCurrentIndex(), false);
			
		//	if(this.failed)
		//		alert("Initialization of WSGallery failed");		
			
		window.offscreenBuffering = true;
		
	}

	function NextImage()
	{
		this.OffsetImage(1);
	}

	function PrevImage()
	{
		this.OffsetImage(-1);
	}

	function OffsetImage(offset)
	{
		this.SetCurrentIndex(this.WrapIndex(eval(this.GetCurrentIndex() + offset)));
		this.SelectImage(this.GetCurrentIndex(), true);
	}

	function SelectImage(index, transition)
	{
		this.SetCurrentIndex(index);
		var path = this.mainimages[index];
		var fn = this.filenames[index];
		if(this.Slide.filters && transition) this.Slide.filters[0].apply();
		if (this.mainImage)
			this.mainImage.src = path;
		var txtFn = GetObject(this.prefix + "txtFilename");
		if (txtFn)
			txtFn.value = fn;
		this.SetCaption();
		if (this.Slide.filters && transition) this.Slide.filters[0].play();
	}
	function SetCurrentIndex(index)
	{
		GetObject(this.prefix + 'hdnMultiViewCurrentIndex').value = index;
	}
	function GetCurrentIndex()
	{
		return eval(GetObject(this.prefix + 'hdnMultiViewCurrentIndex').value);
	}
	function WrapIndex(index)
	{	
		//this function assumes an out of bounds no greater than the length of the array
		if(index > this.mainimages.length - 1)
			return index - this.mainimages.length;
		
		if(index < 0)
			return this.mainimages.length + index;
		
		return index;
	}

	function DoPopup()
	{
		var currentIndex = this.GetCurrentIndex();
		if (enablePopups)
			enlargeImage(this.images[currentIndex], null);
	}
	
	function SetCaption()
	{
		GetObject(this.prefix + 'lblCaption').innerHTML = this.captions[this.GetCurrentIndex()];
		var txtCaption = GetObject(this.prefix + "txtCaption");
		if (txtCaption)
			txtCaption.value = this.captions[this.GetCurrentIndex()];
	}
	
	function OnRenameClick()
	{
		fn = GetObject(this.prefix + 'txtFilename').value;

		ajax = GetAjaxRequestObject();
		url = document.URL + GetQueryLeadIn() + "wsGalleryCmd=renamefile&currentIndex=" 
			+ this.GetCurrentIndex().toString()
			+ "&fn=" + UrlEncode(fn);
		//alert(url);
		ajax.open("GET",url,false)
		ajax.send(null)
		response = ajax.responseText;
		
		if(response == "True")
		{
			alert('File successfully renamed.  Any changes in image order as a result of the change will appear when you reload or refresh the page.');
			this.filenames[this.GetCurrentIndex()] = fn;
		}
		else
		{		
			alert(response);
			GetObject(this.prefix + 'txtFilename').value = this.filenames[this.GetCurrentIndex()];
		}
	}
	function GetQueryLeadIn()
	{
			if (document.URL.indexOf('?') > -1)
				return "&";
			else
				return "?";
	}
	function OnUpdateCaptionClick()
	{
		caption = GetObject(this.prefix + 'txtCaption').value;
		fn = this.filenames[this.GetCurrentIndex()];

		ajax = GetAjaxRequestObject();		
		url = document.URL + GetQueryLeadIn() + "wsGalleryCmd=updatecaption&fn=" 
			+ UrlEncode(fn)
			+ "&caption=" + UrlEncode(caption);
		ajax.open("GET",url,false)
		ajax.send(null)
		response = ajax.responseText;
		
		if(response == "True")
		{
			alert('Caption successfully updated.');
			this.captions[this.GetCurrentIndex()] = caption;
			GetObject(this.prefix + 'lblCaption').innerHTML = caption;
		}
		else
		{
			alert(response);
		}
	}


}

function GetAjaxRequestObject()
{
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return xmlhttp;
}


/* utility funtions */
function GetObject(obj)
{
			var theObj;
			if (document.all) {
				if (typeof obj=='string') 
				{
					return document.all(obj);
				} 
				else 
				{
					return obj.style;
				}
			}
			if (document.getElementById) 
			{
				if (typeof obj=='string') 
				{
					return document.getElementById(obj);
				} 
				else 
				{
					return obj.style;
				}
			}
			return null;

}

function UrlEncode(input)
{
		if (window.encodeURIComponent) 
				return encodeURIComponent(input); 
		else if (window.escape) 
				return escape(input);
	
		return input;
}
function UrlDecode(input)
{
		if (window.decodeURIComponent) 
				return decodeURIComponent(input); 
		else if (window.unescape) 
				return unescape(input);
	
		return input;
}
