/*
	IOSPIRIT EffectGallery
	(C) 2009 by IOSPIRIT GmbH, Felix Schwarz
	All rights reserved.
	
	DO NOT COPY THIS SCRIPT OR USE IT ON YOUR WEBSITE.	
	THIS SCRIPT MAY NOT BE USED AT ANY WEBSITE OTHER THAN THE IOSPIRIT WEBSITE.
*/

function EffectGallery(containerDiv)
{
	containerDiv.style.position = "relative";
	containerDiv.style.overflow = "hidden"; 

	this._elements = new Array();
	this._tagIndexMap = new Array();
	this._containerDiv = containerDiv;
	this._currentIndex = -1;
	this._lastCurrentIndex = -1;

	this._curveFunction = function (progress) { return ((1-Math.sin((Math.PI/2)+(progress*Math.PI)))/2); }
	this._transitionDuration = 500; // in ms, one second == 1000 milliseconds
	
	this._transitionEffect = "none";
	this._transitionCompletionCallback = 0;
	this._transitionTimer = 0;
}

EffectGallery.prototype._containerDiv;
EffectGallery.prototype._currentIndex;
EffectGallery.prototype._lastCurrentIndex;
EffectGallery.prototype._tagIndexMap;
EffectGallery.prototype._elements;

EffectGallery.prototype._transitionEffect;
EffectGallery.prototype._transitionDuration;

EffectGallery.prototype._transitionStartDate;
EffectGallery.prototype._transitionTimer;
EffectGallery.prototype._transitionCompletionCallback;

EffectGallery.prototype.addElement = function(elementDiv, nameTag)
{
	var newIndex = this._elements.length;
	
	this._elements[newIndex] = elementDiv;
	
	elementDiv.style.display = "none";
	elementDiv.style.position = "absolute"; 
	this._containerDiv.appendChild (elementDiv);
	
	this._tagIndexMap[newIndex] = nameTag;

	return (newIndex);
}

EffectGallery.prototype.removeElementAtIndex = function(removeItemAtIndex)
{
	if ((this._elements.length > removeItemAtIndex) && (removeItemAtIndex >= 0))
	{
		this._containerDiv.removeChild (this._elements[removeItemAtIndex]);
	
		this._elements.splice(removeItemAtIndex, 1);
		this._tagIndexMap.splice(removeItemAtIndex, 1);
		
		if (this._currentIndex > removeItemAtIndex)
		{
			this._currentIndex--;
		}

		if (this._lastCurrentIndex > removeItemAtIndex)
		{
			this._lastCurrentIndex--;
		}
	}
}

EffectGallery.prototype.indexOfElementWithNameTag = function(nameTag)
{
	var i;
	
	for (i=0; i<this._tagIndexMap.length; i++)
	{
		if (this._tagIndexMap[i] == nameTag)
		{
			return (i);
		}
	}
	
	return (-1);
}

EffectGallery.prototype.currentIndex = function()
{
	return (this._currentIndex);
}

EffectGallery.prototype.showIndex = function(index, effect, completionCallback)
{
	var thisEffectGallery = this;

	if (index < 0) 			    { index = 0; }
	if (index >= this._elements.length) { index = this._elements.length-1; }

	if (index != this._currentIndex)
	{
		if (!this._transitionTimer)
		{
			this._transitionStartDate = new Date();
			this._transitionEffect = effect;
			this._transitionCompletionCallback = completionCallback;
	
			this._lastCurrentIndex = this._currentIndex;
			this._currentIndex = index;
			
			this._transitionTimer = window.setInterval(function() { thisEffectGallery._calculateTransition(); }, 1);
		}
	}
}

EffectGallery.prototype._calculateTransition = function()
{
	var curTime = new Date(), progress;

	progress = (curTime.getTime() - this._transitionStartDate.getTime()) / this._transitionDuration;
	
	if (progress < 1)
	{
		progress = this._curveFunction(progress);
	
		this.renderElements(this._lastCurrentIndex - ((this._lastCurrentIndex - this._currentIndex) * progress), progress);
	}
	else
	{
		this.renderElements(this._currentIndex, 1);
		
		window.clearInterval(this._transitionTimer);
		this._transitionTimer = 0;
		
		if (this._transitionCompletionCallback)
		{
			this._transitionCompletionCallback(this, this._currentIndex, this._lastCurrentIndex);
		}
	}
}

EffectGallery.prototype.renderElements = function(elementOffset, optionalProgress)
{
	var i, containerWidth, containerHeight, elementDiv;
	
	containerWidth = parseInt(this._containerDiv.offsetWidth);
	containerHeight = parseInt(this._containerDiv.offsetHeight);

	for (i=0; i<this._elements.length; i++)
	{
		var xOffset, yOffset, elementWidth, elementHeight, display=true;
	
		elementDiv = this._elements[i];

		switch (this._transitionEffect)
		{
			case "fade":
				if ((this._currentIndex != i) && (this._lastCurrentIndex != i))
				{
					display = false;
				}
			break;
			
			case "none":
				elementDiv.style.opacity = "1";
				
				if (i != this._currentIndex)
				{
					display = false;
				}
			break;
		}
		
		if (!display)
		{
			elementDiv.style.display = "none";
		}
		else
		{
			elementDiv.style.display = "block";
	
			elementWidth  = parseInt(elementDiv.offsetWidth);
			elementHeight = parseInt(elementDiv.offsetHeight);
			
			switch (this._transitionEffect)
			{
				case "vertical":
					xOffset = 0;
					yOffset = (i * containerHeight) - (elementOffset * containerHeight);
				break;
	
				case "horizontal":
					xOffset = (i * containerWidth) - (elementOffset * containerWidth);
					yOffset = 0;
				break;
				
				default:
					xOffset = yOffset = 0;
				break;
			}
			
			xOffset += ((containerWidth - elementWidth) / 2);
			yOffset += ((containerHeight - elementHeight) / 2);
			
			elementDiv.style.left = xOffset + "px";
			elementDiv.style.top  = yOffset + "px";
	
			switch (this._transitionEffect)
			{
				case "vertical":
					elementDiv.style.opacity = "1";

					if (!(((yOffset >= 0) && (yOffset <= containerHeight)) || (((yOffset+elementHeight) >= 0) && ((yOffset+elementHeight) <= containerHeight))))
					{
						elementDiv.style.display = "none";
					}
				break;

				case "horizontal":
					elementDiv.style.opacity = "1";

					if (!(((xOffset >= 0) && (xOffset <= containerWidth)) || (((xOffset+elementWidth) >= 0) && ((xOffset+elementWidth) <= containerWidth))))
					{
						elementDiv.style.display = "none";
					}
				break;
				
				case "fade":
					if (this._lastCurrentIndex == i)
					{
						elementDiv.style.opacity = 1-optionalProgress;
						
						if (optionalProgress == 1)
						{
							elementDiv.style.display = "none";
						}
					}

					if (this._currentIndex == i)
					{
						elementDiv.style.opacity = optionalProgress;
					}
				break;
			}
		}
	}
}

/*
	IOSPIRIT TwitterStream
	(C) 2009 by IOSPIRIT GmbH, Felix Schwarz
	All rights reserved.
	
	DO NOT COPY THIS SCRIPT OR USE IT ON YOUR WEBSITE.	
	THIS SCRIPT MAY NOT BE USED AT ANY WEBSITE OTHER THAN THE IOSPIRIT WEBSITE.
*/

// Global context
var TwitterStreamCallbackFunctions;

TwitterStreamCallbackFunctions = new Array();

// TwitterStream class
function TwitterStream(userName, maxTweets)
{
	this._userName = userName;
	this._maxTweets = maxTweets;
	this._rawTweets = 0;
	this._rawSingleTweets = new Object();
}

TwitterStream.prototype._userName;
TwitterStream.prototype._maxTweets;
TwitterStream.prototype._rawTweets;
TwitterStream.prototype._rawSingleTweets;

TwitterStream.prototype.loadJSON = function(url, callbackFunction)
{
	var scriptElem, headElem, newCallbackID;
	
	newCallbackID = TwitterStreamCallbackFunctions.length;
	TwitterStreamCallbackFunctions[newCallbackID] = callbackFunction;
	
	scriptElem = document.createElement("script");
	scriptElem.setAttribute("src",  url + "&callback=TwitterStreamCallbackFunctions[" + newCallbackID +"]");
	scriptElem.setAttribute("type", "text/javascript");

	headElem = document.getElementsByTagName("head")[0];
	headElem.appendChild(scriptElem);
}

TwitterStream.prototype.convertTimeStringToText = function(timeString)
{
	var currentDate, tweetDate, elapsedMinutes, humanReadableTime;
	var monthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

	currentDate = new Date();
	currentDate = currentDate.getTime();

	tweetDate = new Date(timeString);

	elapsedMinutes = Math.floor((currentDate - tweetDate.getTime()) / 60000);

	if (isNaN(elapsedMinutes))
	{
		humanReadableTime = "View tweet";
	}
	else
	{
		if (elapsedMinutes > 1440)
		{
			if (elapsedMinutes > 2880)
			{
				humanReadableTime = Math.floor(elapsedMinutes / 1440) + " days ago";
			}
			else
			{
				humanReadableTime = Math.floor(elapsedMinutes / 1440) + " day ago";
			}

			if (elapsedMinutes > 14400)
			{
				humanReadableTime = monthNames[tweetDate.getMonth()] + " " + tweetDate.getDay() + ", " + (tweetDate.getYear() + 1900); // " (" + ((tweetDate.getHours() > 10)?tweetDate.getHours():"0"+tweetDate.getHours()) + ":" + ((tweetDate.getMinutes() > 10)?tweetDate.getMinutes():"0"+tweetDate.getMinutes()) + ")";
			}
		}
		else
		{
			if (elapsedMinutes > 60)
			{
				if (elapsedMinutes > 120)
				{
					humanReadableTime = Math.floor(elapsedMinutes / 60) + " hours ago";
				}
				else
				{
					humanReadableTime = Math.floor(elapsedMinutes / 60) + " hour ago";
				}
			}
			else
			{
				if (elapsedMinutes > 1)
				{
					humanReadableTime = Math.floor(elapsedMinutes) + " minutes ago";
				}
				else
				{
					if (elapsedMinutes == 1)
					{
						humanReadableTime = Math.floor(elapsedMinutes) + " minute ago";
					}
					else
					{
						humanReadableTime = Math.floor((currentDate - tweetDate.getTime()) / 1000) + " seconds ago";
					}
				}
			}
		}
	}
	
	return (humanReadableTime);
}

TwitterStream.prototype.linkifyText = function(inText, linkifyURLs, linkifyUserNames, linkifyHashTags, linkTarget)
{
	outText = inText;
	
	if (linkifyURLs)
	{
		outText = outText.replace(/((https?:\/\/|www\.)[^\s ]+)/g, "<a href=\"$1\"" + (linkTarget ? " target=\"" + linkTarget + "\"" : "")+ ">$1</a>");
	}
	
	if (linkifyUserNames)
	{
		var userNameRegExp = new RegExp("(^|\\s)@(\\w+)");
		userNameRegExp.global = true;
		userNameRegExp.ignoreCase = true;

		outText = outText.replace(userNameRegExp, "$1<a href=\"http://www.twitter.com/$2\"" + (linkTarget ? " target=\"" + linkTarget + "\"" : "")+ " rel=\"nofollow\">@$2</a>");
	}
	
	if (linkifyHashTags)
	{
		var hashTagsRegExp = new RegExp("(^|\\s)#(\\w+)");
		hashTagsRegExp.global = true;
		hashTagsRegExp.ignoreCase = true;

		outText = outText.replace(hashTagsRegExp, "$1<a href=\"http://search.twitter.com/search?q=%23$2\"" + (linkTarget ? " target=\"" + linkTarget + "\"" : "")+ " rel=\"nofollow\">#$2</a>");
	}
	
	return (outText);
}

TwitterStream.prototype.loadTweets = function(callbackFunction)
{
	var thisStream = this;

	this.loadJSON("http://twitter.com/statuses/user_timeline.json?screen_name=" + this._userName + "&count=" + this._maxTweets,
		      function(tweetResult) { thisStream._tweetsLoaded(tweetResult, callbackFunction); });
}

TwitterStream.prototype.loadFavoredTweets = function(callbackFunction)
{
	var thisStream = this;

	this.loadJSON("http://twitter.com/favorites/" + this._userName + ".json?",
		      function(tweetResult) { thisStream._tweetsLoaded(tweetResult, callbackFunction); });
}

TwitterStream.prototype.loadTweet = function(tweetId, callbackFunction)
{
	var thisStream = this;
	
	if (this._rawTweets)
	{
		callbackFunction(this);
	}
	else
	{
		this.loadJSON("http://twitter.com/statuses/show/" + tweetId + ".json?",
			      function(tweetResult) { thisStream._tweetLoaded(tweetId, tweetResult, callbackFunction); });
	}
}

TwitterStream.prototype._tweetsLoaded = function(tweetResult, callbackFunction)
{
	this._rawTweets = tweetResult;
	
	if (callbackFunction)
	{
		callbackFunction(this);
	}
}

TwitterStream.prototype._tweetLoaded = function(tweetId, tweetResult, callbackFunction)
{
	this._tweets = tweetResult;
	
	this._rawSingleTweets[tweetId] = tweetResult;
	
	if (callbackFunction)
	{
		callbackFunction(this, tweetId);
	}
}

TwitterStream.prototype.getRawTweets = function()
{
	return (this._rawTweets);
}

TwitterStream.prototype.getTweets = function()
{
	var i, resultArr = new Array();

	for (i=0;i<this._rawTweets.length;i++)
	{
		resultArr[i] = this.parseTweet(this._rawTweets[i]);
	}

	return (resultArr);
}

TwitterStream.prototype.getRawTweet = function(tweetId)
{
	return (this._rawSingleTweets[tweetId]);
}

TwitterStream.prototype.parseTweet = function(tweetData)
{
	var parsedTweet = new Object();
	
	parsedTweet['date']		= this.convertTimeStringToText(tweetData['created_at']);
	parsedTweet['userName']		= tweetData['user']['screen_name'];
	parsedTweet['avatarImageURL']	= tweetData['user']['profile_image_url'];
	parsedTweet['message']		= tweetData['text'];
	parsedTweet['messageLinkified']	= this.linkifyText(tweetData['text'], true, true, true, "_blank");
	parsedTweet['tweetLink']	= "http://twitter.com/" + tweetData['user']['screen_name'] + "/status/" + tweetData['id'];
	parsedTweet['userLink']		= "http://twitter.com/" + tweetData['user']['screen_name'];

	return (parsedTweet);
}


/*
	IOSPIRIT TwitterFeedList
	(C) 2009 by IOSPIRIT GmbH, Felix Schwarz
	All rights reserved.
	
	DO NOT COPY THIS SCRIPT OR USE IT ON YOUR WEBSITE.	
	THIS SCRIPT MAY NOT BE USED AT ANY WEBSITE OTHER THAN THE IOSPIRIT WEBSITE.
*/

// Global context
if (window.addEventListener)
{
	window.addEventListener("load", setupTwitterFeedLists, false);
}
else
{
	window.attachEvent("onload", setupTwitterFeedLists);
}

function setupTwitterFeedLists()
{
	var twitterFeedContainers, i;

	twitterFeedContainers = document.getElementsByClassName("twitterfeed");

	for (i=0; i<twitterFeedContainers.length; i++)
	{
		var twitterFeed;
		
		twitterFeed = new TwitterFeedList(twitterFeedContainers[i]);
	}
}

// TwitterFeedList class
function TwitterFeedList(feedContainer)
{
	var scriptElem, headElem;
	var thisFeed = this;

	this._feedContainer = feedContainer;
	this._userName = feedContainer.getAttribute('twitterUserName');
	this._itemCount = 5;
	this._followUsText = "";
	this._showAvatar = 0;
	this._showTinyTwitterIcon = "http://twitter-badges.s3.amazonaws.com/t_small-b.png";
	this._loadFavoredTweets = 0;
	
	if (feedContainer.getAttribute('twitterItemCount'))
	{
		this._itemCount = parseInt(feedContainer.getAttribute('twitterItemCount'));
	}

	if (feedContainer.getAttribute('twitterFollowUsText'))
	{
		this._followUsText = feedContainer.getAttribute('twitterFollowUsText'); 
	}

	if (feedContainer.getAttribute('twitterShowAvatar'))
	{
		this._showAvatar = parseInt(feedContainer.getAttribute('twitterShowAvatar'));
	}

	if (feedContainer.getAttribute('twitterShowTwitterIcon'))
	{
		this._showTinyTwitterIcon = feedContainer.getAttribute('twitterShowTinyTwitterIcon'); 
	}

	if (feedContainer.getAttribute('twitterLoadFavoredTweets'))
	{
		this._loadFavoredTweets = parseInt(feedContainer.getAttribute('twitterLoadFavoredTweets'));
	}
	
	this._twitterStream = new TwitterStream(this._userName, ((this._itemCount<10)?10:this._itemCount));
	
	if (this._loadFavoredTweets)
	{
		this._twitterStream.loadFavoredTweets(function(stream) { thisFeed.displayTweetsFromStream(stream); } );
	}
	else
	{
		this._twitterStream.loadTweets(function(stream) { thisFeed.displayTweetsFromStream(stream); } );
	}
}

TwitterFeedList.prototype._twitterStream;

TwitterFeedList.prototype._feedContainer;
TwitterFeedList.prototype._itemCount;
TwitterFeedList.prototype._userName;
TwitterFeedList.prototype._followUsText;
TwitterFeedList.prototype._showAvatar;
TwitterFeedList.prototype._showTinyTwitterIcon;
TwitterFeedList.prototype._loadFavoredTweets;

TwitterFeedList.prototype.displayTweetsFromStream = function(stream)
{
	var compiledHTML, tweets, i, maxTweetCount, tweetCount;

	compiledHTML = "";
	maxTweetCount = this._itemCount;
	tweetCount = 0;

	tweets = stream.getTweets();
	
	if (maxTweetCount > tweets.length)
	{
		maxTweetCount = tweets.length;
	}
	
	for (i=0; ((i<tweets.length) && (tweetCount < maxTweetCount)); i++)
	{
		if (tweets[i]['message'].substring(0,1) != "@")
		{
			compiledHTML = compiledHTML + "<li><a href=\"" + tweets[i]['userLink'] + "\" target=\"_blank\">" + (this._showAvatar ? ("<img src=\"" + tweets[i]['avatarImageURL'] + "\" class=\"avatar\">") : (this._showTinyTwitterIcon ? ("<img src=\"" + this._showTinyTwitterIcon + "\" class=\"symbol\">") : "")) + tweets[i]['userName'] + "</a>: " + tweets[i]['messageLinkified'] + " <i><a href=\"" + tweets[i]['tweetLink'] + "\" target=\"_blank\">" + tweets[i]['date'] + "</a></i></li>";
			tweetCount++;
		}
	}

	if (this._followUsText)
	{
		compiledHTML = compiledHTML + "<li class=\"twitterfeedfollowus\"><a href=\"http://twitter.com/" + this._userName + "\">" + this._followUsText + "</a></li>";
	}
	
	if (compiledHTML.length)
	{
		compiledHTML = "<ul>" + compiledHTML + "</ul>";
	}
	

	this._feedContainer.innerHTML = compiledHTML;
}

/*
	IOSPIRIT TweetGallery
	(C) 2009 by IOSPIRIT GmbH, Felix Schwarz
	All rights reserved.
	
	DO NOT COPY THIS SCRIPT OR USE IT ON YOUR WEBSITE.	
	THIS SCRIPT MAY NOT BE USED AT ANY WEBSITE OTHER THAN THE IOSPIRIT WEBSITE.
*/

// Global context
if (window.addEventListener)
{
	window.addEventListener("load", setupTweetGalleries, false);
}
else
{
	window.attachEvent("onload", setupTweetGalleries);
}

function setupTweetGalleries()
{
	var tweetGalleryContainers, i;

	tweetGalleryContainers = document.getElementsByClassName("tweetgallery");

	for (i=0; i<tweetGalleryContainers.length; i++)
	{
		var twitterFeed;
		
		twitterFeed = new TweetGallery(tweetGalleryContainers[i]);
	}
}

// TweetGallery class
function TweetGallery(galleryContainer)
{
	var loadingElement, thisTweetGallery = this;
	
	this._galleryContainer = galleryContainer;

	// Setup EffectGallery
	loadingElement = document.createElement("div");
	loadingElement.setAttribute("class", "loading");
	
	this._effectGallery = new EffectGallery(this._galleryContainer);
	this._effectGallery.addElement(loadingElement,"loading");
	this._effectGallery.showIndex(0, "fade", 0);

	this._transitionDelay = 10000;
	if (galleryContainer.getAttribute('tweetGalleryItemShowTime'))
	{
		this._transitionDelay = parseInt(galleryContainer.getAttribute('tweetGalleryItemShowTime'));
	}

	// Setup TwitterStream
	this._twitterItemCount = 100;
	if (galleryContainer.getAttribute('twitterItemCount'))
	{
		this._twitterItemCount = parseInt(galleryContainer.getAttribute('twitterItemCount'));
	}

	this._twitterLoadFavoredTweets = 0;
	if (galleryContainer.getAttribute('twitterLoadFavoredTweets'))
	{
		this._twitterLoadFavoredTweets = parseInt(galleryContainer.getAttribute('twitterLoadFavoredTweets'));
	}

	this._twitterUserName = galleryContainer.getAttribute('twitterUserName');
	this._twitterStream = new TwitterStream(this._twitterUserName, this._twitterItemCount);

	if (this._twitterLoadFavoredTweets)
	{
		this._twitterStream.loadFavoredTweets(function(stream) { thisTweetGallery.tweetsLoaded(); } );
	}
	else
	{
		this._twitterStream.loadTweets(function(stream) { thisTweetGallery.tweetsLoaded(); } );
	}
	
	this._changeTimer = 0;
	this._transitionEffect = "vertical";
	if (galleryContainer.getAttribute('tweetGalleryTransitionEffect'))
	{
		this._transitionEffect = parseInt(galleryContainer.getAttribute('tweetGalleryTransitionEffect'));
	}
}

TweetGallery.prototype._galleryContainer;
TweetGallery.prototype._effectGallery;
TweetGallery.prototype._transitionDelay;
TweetGallery.prototype._twitterUserName;
TweetGallery.prototype._twitterItemCount;
TweetGallery.prototype._twitterStream;
TweetGallery.prototype._twitterLoadFavoredTweets;
TweetGallery.prototype._tweets;
TweetGallery.prototype._tweetOffset;
TweetGallery.prototype._transitionEffect;
TweetGallery.prototype._changeTimer;

TweetGallery.prototype.tweetsLoaded = function()
{
	var i, tweets, thisTweetGallery = this;

	this._tweets = tweets = this._twitterStream.getTweets();
	this._tweetOffset = 0;
	
	for (i=0;i<tweets.length;i++)
	{
		var tweetElement;

		tweetElement = document.createElement("div");
		tweetElement.setAttribute("class", "tweet");
		tweetElement.innerHTML = "<a href=\"" + tweets[i]['userLink'] + "\" target=\"_blank\"><img src=\"" + tweets[i]['avatarImageURL'] + "\" class=\"avatar\"></a><div class=\"message\">" + tweets[i]['messageLinkified'] + "<i>- <a href=\"" + tweets[i]['userLink'] + "\" target=\"_blank\">" + tweets[i]['userName'] + "</a> (<a href=\"" + tweets[i]['tweetLink'] + "\" target=\"_blank\">" + tweets[i]['date'] + "</a>)</i>" + "</div>";

		this._effectGallery.addElement(tweetElement, "tweet" + i);
	}
	
	this._effectGallery.showIndex(1, "fade", 0);
	
	this._changeTimer = window.setInterval(function() { thisTweetGallery.showNextTweet(); }, this._transitionDelay);
}

TweetGallery.prototype.showNextTweet = function()
{
	var effect = this._transitionEffect;

	this._tweetOffset++;
	
	if (this._tweetOffset >= this._tweets.length)
	{
		this._tweetOffset = 0;
		effect = "fade";
	}
	
	this._effectGallery.showIndex(this._tweetOffset + 1, effect, 0);
}

/*
	IOSPIRIT MiniGallery
	(C) 2009 by IOSPIRIT GmbH, Felix Schwarz
	All rights reserved.
	
	DO NOT COPY THIS SCRIPT OR USE IT ON YOUR WEBSITE.	
	THIS SCRIPT MAY NOT BE USED AT ANY WEBSITE OTHER THAN THE IOSPIRIT WEBSITE.
*/

// Global context
if (window.addEventListener)
{
	window.addEventListener("load", setupMiniGalleries, false);
}
else
{
	window.attachEvent("onload", setupMiniGalleries);
}

function setupMiniGalleries()
{
	var miniGalleryContainers, i;

	miniGalleryContainers = document.getElementsByClassName("minigallery");

	for (i=0; i<miniGalleryContainers.length; i++)
	{
		var miniGallery;
		
		miniGallery = new MiniGallery(miniGalleryContainers[i]);
	}
}

// MiniGallery class 
function MiniGallery(enclosingDiv)
{
	var i, cnt=0, thisMiniGallery = this, showRoomContainer;

	showRoomContainer = document.createElement("div");
	showRoomContainer.setAttribute("class", "showroom");
	enclosingDiv.appendChild(showRoomContainer);

	this._containerDiv = document.createElement("div");
	this._containerDiv.setAttribute("class", "content");
	showRoomContainer.appendChild(this._containerDiv);
	
	this._navigationDiv = document.createElement("div");
	this._navigationDiv.setAttribute("class", "navigation");
	enclosingDiv.appendChild(this._navigationDiv);
	
	if (enclosingDiv.getAttribute('nodeContainerID'))
	{
		this._nodeContainerDiv = document.getElementById(enclosingDiv.getAttribute('nodeContainerID'));
	}
	else
	{
		for (i=0; i<enclosingDiv.childNodes.length; i++)
		{
			try
			{
				if (enclosingDiv.childNodes[i].className == "nodecontainer")
				{
					this._nodeContainerDiv = enclosingDiv.childNodes[i];
				}
			}
			catch(e)
			{
			}
		}
	}
	
	this._effectGallery = new EffectGallery(this._containerDiv);
	this._transitionDelay = 5000;
	this._transitionEffect = "fade";
	
	this._onclickHandler = function(event) { return (thisMiniGallery.handleOnClick(this, event)); };
	
	this._divs = new Array();
	
	for (i=0;i<this._nodeContainerDiv.childNodes.length;i++)
	{
		try
		{
			if (this._nodeContainerDiv.childNodes[i].nodeName.toLowerCase() == "div")
			{
				this._divs.push(this._nodeContainerDiv.childNodes[i]);
			}
		}
		catch(e)
		{
		}
	}

	for (i=0;i<this._divs.length;i++)
	{
		this._effectGallery.addElement(this._divs[i], "mg" + i);
	}

	thisMiniGallery.showIndex(0);

	this._changeTimer = window.setInterval(function(){ thisMiniGallery.showNext(); }, this._transitionDelay);
}

MiniGallery.prototype._containerDiv;
MiniGallery.prototype._nodeContainerDiv;
MiniGallery.prototype._navigationDiv;

MiniGallery.prototype._divs;

MiniGallery.prototype._effectGallery;
MiniGallery.prototype._transitionDelay;
MiniGallery.prototype._transitionEffect;
MiniGallery.prototype._changeTimer;

MiniGallery.prototype._onclickHandler;

MiniGallery.prototype.showNext = function()
{
	var newIndex = this._effectGallery.currentIndex() + 1;

	if (newIndex < 0)
	{
		newIndex = 0;
	}

	if (newIndex >= this._divs.length)
	{
		newIndex = 0;
	}
	
	this.showIndex(newIndex);
}

MiniGallery.prototype.showIndex = function(index)
{
	var thisMiniGallery = this;

	if (this._effectGallery.currentIndex() != index)
	{
		if ((index >= 0) && (index < this._divs.length))
		{
			this._effectGallery.showIndex(index, this._transitionEffect, function(){ thisMiniGallery._renderNavigation(); });
		}
	}
}

MiniGallery.prototype.handleOnClick = function(thisObj, event)
{
	var galleryOffset;

	if (thisObj)
	{
		if (galleryOffset = thisObj.getAttribute("miniGalleryOffset"))
		{
			if (this._changeTimer)
			{
				window.clearInterval(this._changeTimer);
				this._changeTimer = 0;
			}
		
			this.showIndex(galleryOffset);
		}
		
		return (false);
	}
		
	return (true);
}

MiniGallery.prototype._renderNavigation = function()
{
	var thisMiniGallery = this, i, currentIndex = this._effectGallery.currentIndex();

	while (this._navigationDiv.childNodes.length)
	{
		this._navigationDiv.removeChild(this._navigationDiv.childNodes[0]);
	};

	for (i=0;i<this._divs.length;i++)
	{
		var navAElement;
		
		navAElement = document.createElement("a");
		if (i==currentIndex)
		{
			navAElement.setAttribute("class", "selected");
		}
		navAElement.setAttribute("miniGallery", this);
		navAElement.setAttribute("miniGalleryOffset", i);
		
		navAElement.onclick = this._onclickHandler;
		
		this._navigationDiv.appendChild(navAElement);
	}
}

