function rand(max){
	var ranNum = Math.floor(Math.random()*max);
	return ranNum;
}
String.prototype.trim = function () {
    return this.replace(/^\s*/, '').replace(/\s*$/, '');
}
function isArray(testObject){
	return testObject && !(testObject.propertyIsEnumerable('length')) && typeof testObject === 'object' && typeof testObject.length === 'number';
}
if (!Array.prototype.indexOf){
	Array.prototype.indexOf = function(elt){
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
		? Math.ceil(from)
		: Math.floor(from);
		if (from < 0)
		from += len;
		for (; from < len; from++){
			if (from in this &&
			this[from] === elt)
			return from;
		}return -1;
	};
}




function getElementsWithAttribute(attribute, tagName, parentElement) {
	var elements = [];
	var children = parentElement.getElementsByTagName(tagName);
	for(var x=0;children.length>x;x++){
		var attributeValue = children[x].getAttribute(attribute);
		if(attributeValue){
			elements.push(children[x]);
		}
	}
	return elements;
}
function ticker(f_prams){
	var ticker = this;
	ticker.els = [];
	ticker.currentIndex = 0;
	if(f_prams!=undefined){
		ticker.parent = f_prams.parent;
		ticker.feed = f_prams.feed;
		ticker.wait = f_prams.wait;
	}
	if(ticker.wait==undefined){ticker.wait=5000};
	ticker.resize = function(){
		ticker.position = getPosition(ticker.parent);
		for(var x=0;ticker.els.length>x;x++){
			ticker.els[x].style.top = ticker.position.y + 'px';
			ticker.els[x].style.left = ticker.position.x + 'px';
		}
	}
	appendEvent('resize',window,function(){setTimeout(ticker.resize,100)},false);
	ticker.resize();
	/* clear parent */
	ticker.parent.style.height = ticker.parent.offsetHeight+'px';
	ticker.parent.innerHTML = '';
	ticker.parent.style.overflow = 'hidden';
	for(var x=0;ticker.feed.length>x;x++){
		var el = document.createElement('div');
		el.style.position = 'absolute';
		el.style.top = ticker.position.y + 'px';
		el.style.left = ticker.position.x + 'px';
		el.setAttribute('blogid',ticker.feed[x][0]);
		el.setAttribute('blogpage',ticker.feed[x][1]);
		el.setAttribute('index',ticker.els.length);
		el.innerHTML = '<a href="http://'+document.domain+ticker.feed[x][1]+'#'+ticker.feed[x][0]+'">'+
		'&nbsp;&bull;&nbsp;'+ticker.feed[x][3]+'&nbsp;'+ticker.feed[x][2]+'</a>';
		el.style.top = eval(ticker.parent.offsetHeight*-1)+'px';
		el.style.display = 'none';
		appendEvent('mouseover',el,function(e){
			clearTimeout(ticker.timer);
			ticker.pauseTicker = true;
			return false;
		},false);
		appendEvent('mouseout',el,function(e){
			ticker.pauseTicker = false;
			clearTimeout(ticker.timer);
			ticker.timer = setTimeout(ticker.updateTicker,ticker.wait);
			return false;
		},false);
		$(el).animate({opacity:'0'},0);
		ticker.els.push(el);
		ticker.parent.appendChild(el);
	}
	ticker.updateTicker = function(){
		if(!ticker.parent||ticker.pauseTicker){return null;};
		if(ticker.currentIndex+1>ticker.els.length-1){
			ticker.currentIndex=0;
			var pindex = ticker.els.length-1;
		}else{
			var pindex = ticker.currentIndex;
			ticker.currentIndex++;
		}
		$(ticker.els[pindex]).stop(false,true).animate({opacity:'0'},'slow','swing');
		ticker.els[ticker.currentIndex].style.display = 'block';
		$(ticker.els[ticker.currentIndex]).animate({opacity:'1'},'slow','swing');
		if(ticker.els.length==1){return null;};
		$(ticker.els[pindex]).animate({opacity:'0'},0);
		ticker.els[pindex].style.display = 'none';
		ticker.timer = setTimeout(ticker.updateTicker,ticker.wait);
	}
	setTimeout(function(){
		ticker.resize();
	},500);
	ticker.currentIndex = ticker.els.length;
	ticker.updateTicker();
}

function gallery(parent,f_prams){
	var transSpeed = null;
	parent.style.MozUserSelect='-moz-none';
	parent.style.overflow = 'hidden';
	var gallery = this;
	
	if(f_prams!=undefined){
		gallery.cw = f_prams.w;
		gallery.ch = f_prams.h;
		gallery.cx = f_prams.x;
		gallery.cy = f_prams.y;
		transSpeed = f_prams.transSpeed;
		gallery.background = f_prams.background;
		gallery.carouselHeight = f_prams.carouselHeight;
		gallery.easing = f_prams.easing;
		gallery.thumbSpace = f_prams.thumbnailSpacing;
		gallery.eventListener = f_prams.eventListener;
		gallery.timerInterval = f_prams.timerInterval;
		gallery.fullImageMarginTop = f_prams.fullImageMarginTop;
		gallery.descriptionProcedure = f_prams.description;
		gallery.loadingImageText = f_prams.loadingImageText;
		gallery.fullImageMargin = f_prams.fullImageMargin;
		gallery.categories = f_prams.categories;
		gallery.animationStyle = f_prams.animationStyle;
		gallery.fullImageBorder = f_prams.fullImageBorder;
		gallery.linkParent = f_prams.linkParent;
		gallery.startImage = f_prams.startImage;
		gallery.buttonTop = f_prams.buttonTop;
		gallery.addButtons = f_prams.addButtons;
		gallery.tags = f_prams.tags;
	}
	if(gallery.tags==undefined){
		gallery.tags = '';
	}
	if(gallery.buttonTop==undefined){
		gallery.buttonTop = 23;
	}
	if(gallery.linkParent==undefined){
		gallery.linkParent = 'refrence';
	}
	if(gallery.fullImageBorder==undefined){
		gallery.fullImageBorder = 'none';
	}
	if(gallery.animationStyle==undefined){
		gallery.animationStyle = 'fade';
	}
	if(gallery.categories==undefined){
		gallery.categories = [];
	}
	if(gallery.cw==undefined){
		gallery.cw = function(){return document.documentElement.clientWidth;};
	}
	if(gallery.ch==undefined){
		gallery.ch = function(){return document.documentElement.clientHeight;};
	}
	if(gallery.cx==undefined){
		gallery.cx = function(){return 0;};
	}
	if(gallery.cy==undefined){
		gallery.cy = function(){return 0;};
	}
	if(gallery.fullImageMargin==undefined){
		gallery.fullImageMargin = 'auto';
	}
	if(gallery.loadingImageText==undefined){
		gallery.loadingImageText = '<h1 style="font-size:17px;">Loading image...</h1>';
	}
	if(gallery.carouselMarginTop==undefined){
		gallery.carouselMarginTop = 44;
	}
	if(gallery.startImage==undefined){
		gallery.startImage = '/img/100PctAlphaBlackDot.png';
	}
	if(gallery.fullImageMarginTop==undefined){
		gallery.fullImageMarginTop = 20;
	}
	if(gallery.timerInterval==undefined){
		gallery.timerInterval = 100;
	}
	if(gallery.eventListener==undefined){
		gallery.eventListener = parent;
	}
	if(gallery.thumbSpace==undefined){
		gallery.thumbSpace = 20;
	}
	if(gallery.easing==undefined){
		gallery.easing='easeOutSine';
	}
	if(gallery.carouselHeight==undefined){
		gallery.carouselHeight = 100;
	}
	if(transSpeed==null){
		transSpeed = 1000;
	}
	if(gallery.background==null){
		gallery.background = 'white';
	}
	gallery.center = cw/2;
	gallery.fullImage = document.createElement('img');
	gallery.fullImage.style.height = '100%';
	gallery.fullImage.onmousedown = function(){return false;};
	gallery.fullImage.src = gallery.startImage;
	gallery.fimageRect = {x:0,y:0,h:0,w:0};
	gallery.fullImage.style.MozUserSelect='-moz-none';
	gallery.fullImageBg = document.createElement('div');
	gallery.fullImageBg.style.position = 'absolute';
	gallery.fullImageBg.style.MozUserSelect='-moz-none';
	gallery.fullImageBg.style.top = gallery.cy()+'px';
	gallery.fullImageBg.style.left = gallery.cx()+'px';
	gallery.fullImageBg.style.width = gallery.cw()+'px';
	gallery.fullImageBg.style.height = gallery.ch()+'px';
	gallery.fullImageBg.style.clip = 'rect(0px '+gallery.cw()+'px '+gallery.ch()+'px 0px)';
	gallery.fullImageBg.style.background = gallery.background;

	gallery.topZ = 0;
	gallery.mouseSpeed = {x:0,y:0};
	gallery.timerMousePos = {x:0,y:0};
	gallery.mousePos = {x:0,y:0};
	gallery.pos = 0;
	gallery.move = '';
	parent.appendChild(gallery.fullImageBg);
	gallery.fullImageBg.appendChild(gallery.fullImage);
	var sqrt = Math.sqrt;
	var log = Math.log;
	var sinh = function(x){return log(x + sqrt(x * x + 1))};
	gallery.leftOffset = 0;
	gallery.images = [];
	gallery.carousel = document.createElement('div');
	gallery.buttons = document.createElement('div');
	parent.style.MozUserSelect='-moz-none';
	gallery.fullImage.ondragstart = function(){return false;};
	gallery.fullImage.onselectstart = function(){return false;};
	parent.onselectstart = function(){return false;};
	gallery.carousel.style.position = 'absolute';
	gallery.carousel.style.top = eval(gallery.ch()-gallery.carouselHeight+gallery.fullImageMarginTop-gallery.carouselMarginTop-2)+'px';
	gallery.buttons.style.top = eval(gallery.ch()-gallery.carouselHeight+gallery.fullImageMarginTop-gallery.carouselMarginTop-gallery.buttonTop)+'px';
	gallery.carousel.style.left = '5px';
	gallery.buttons.style.left = '-3px';
	gallery.carousel.style.height = gallery.carouselHeight+'px';
	gallery.carousel.style.verticalAlign = 'middle';
	gallery.carousel.style.cursor = 'e-resize';
	gallery.carousel.style.background = 'transparent';
	gallery.carousel.style.overflow = 'hidden';
	gallery.carousel.style.zIndex = '5000';
	gallery.carousel.style.width = '10000px';
	
	gallery.buttons.style.width = '800px';
	
	
	
	gallery.lastButton = document.createElement('img');
	gallery.lastButton.style.margin = '0 0 0 5px';
	gallery.lastButton.style.cursor = 'pointer';
	gallery.lastButton.src = '/img/bullet_arrow_last.png';
	appendEvent('click',gallery.lastButton,function(e){
		var moveto = gallery.images[gallery.images.length-1].getAttribute('leftOffset');
		gallery.images[gallery.images.length-1].onclick();
		$(gallery.carousel).stop(true,false).animate({left:eval(moveto*-1)+'px'},1000,gallery.easing);
	},false);
	
	
	gallery.previousButton = document.createElement('img');
	gallery.previousButton.style.margin = '0 0 0 5px';
	gallery.previousButton.style.cursor = 'pointer';
	gallery.previousButton.src = '/img/bullet_arrow_prev.png';
	appendEvent('click',gallery.previousButton,function(e){
		if(gallery.currentImageIndex-1<0){return false;};
		gallery.images[gallery.currentImageIndex-1].onclick();
		var moveto = gallery.images[gallery.currentImageIndex-1].getAttribute('leftOffset');
		$(gallery.carousel).stop(true,false).animate({left:eval(moveto*-1)+'px'},1000,gallery.easing);
	},false);
	
	gallery.nextButton = document.createElement('img');
	gallery.nextButton.style.margin = '0 0 0 5px';
	gallery.nextButton.style.cursor = 'pointer';
	gallery.nextButton.src = '/img/bullet_arrow_next.png';
	appendEvent('click',gallery.nextButton,function(e){
		if(gallery.currentImageIndex+1>gallery.images.length-1){return false;};
		gallery.images[gallery.currentImageIndex+1].onclick();
		var moveto = gallery.images[gallery.currentImageIndex+1].getAttribute('leftOffset');
		$(gallery.carousel).stop(true,false).animate({left:eval(moveto*-1)+'px'},1000,gallery.easing);
	},false);
	
	
	gallery.firstButton = document.createElement('img');
	gallery.firstButton.style.display = 'inline-block';
	gallery.firstButton.style.margin = '0 0 0 5px';
	gallery.firstButton.style.cursor = 'pointer';
	gallery.firstButton.src = '/img/bullet_arrow_first.png';
	appendEvent('click',gallery.firstButton,function(e){
		var moveto = gallery.images[0].getAttribute('leftOffset');
		gallery.images[0].onclick();
		$(gallery.carousel).stop(true,false).animate({left:eval(moveto*-1)+'px'},1000,gallery.easing);
	},false);


	gallery.buttons.style.position = 'absolute';
	
	gallery.buttons.appendChild(gallery.firstButton);
	gallery.buttons.appendChild(gallery.previousButton);
	gallery.buttons.appendChild(gallery.nextButton);
	gallery.buttons.appendChild(gallery.lastButton);
	
	if(addButtons!=undefined){
		for(var x=0;addButtons.length>x;x++){
			gallery.buttons.appendChild(gallery.addButtons[0]);
		}
	}
	
	
	
	$(gallery.carousel).animate({opacity:'.85'},0);
	
	/* acceleration timer */
	gallery.timer = function(){
		/* calculate timer events from previous iteration */
		gallery.mouseSpeed.x = Math.abs(gallery.mousePos.x-gallery.timerMousePos.x);
		gallery.mouseSpeed.y = Math.abs(gallery.mousePos.y-gallery.timerMousePos.y);
		if(gallery.pos>gallery.carousel.offsetLeft+10){
			gallery.move = 'left';
		}else if(gallery.pos<gallery.carousel.offsetLeft-10){
			gallery.move = 'right';
		}else{
			setTimeout(function(){gallery.move = '';},100);
		}
		/* update the timer */
		gallery.timerMousePos.x = gallery.mousePos.x;
		gallery.timerMousePos.y = gallery.mousePos.y;
		gallery.pos = gallery.carousel.offsetLeft;
		setTimeout(function(){
			gallery.timer()
		},gallery.timerInterval);
	}
	appendEvent('mousemove',gallery.eventListener,function(e){
		var e = e || window.event;
		var mouse = mouseCoords(e);
		gallery.mousePos = {x:mouse.x,y:mouse.y};
		if(gallery.movingCarousel){
			var moveto = eval(mouse.x-gallery.mouseOffset.x);
			if(moveto>gallery.cx()-20){moveto=gallery.cx()-20};
			if(moveto<(gallery.carousel.offsetWidth*-1)+gallery.cw()){moveto=(gallery.carousel.offsetWidth*-1)+gallery.cw()};
			if(moveto>gallery.carousel.offsetWidth-gallery.cw()){return null};
			gallery.carousel.style.left = moveto+'px';
		}
	},false);
	
	appendEvent('mousedown',gallery.eventListener,function(e){
		$(gallery.carousel).stop(true,false)
		var e = e || window.event;
		gallery.mouseOffset = mouseCoords(e);
		gallery.movingCarousel = true;
		gallery.mouseOffset.x-=gallery.carousel.offsetLeft;
		return false;
	},false);
	
	appendEvent('mouseup',gallery.eventListener,function(e){
		if(gallery.move=='right'){
			var moveto = gallery.carousel.offsetLeft+(gallery.mouseSpeed.x*4);
		}else if(gallery.move=='left'){
			var moveto = gallery.carousel.offsetLeft-(gallery.mouseSpeed.x*4);
		}
		if(moveto>(gallery.cx()+20)){moveto=gallery.cx()+20};
		if(moveto<(gallery.carousel.offsetWidth*-1)+gallery.cw()-gallery.cx()){moveto=(gallery.carousel.offsetWidth*-1)+gallery.cw()-gallery.cx()};
		gallery.movingCarousel = false;
		if(gallery.move=='right'){
			$(gallery.carousel).stop(true,false).animate({left:moveto+'px'},1000-gallery.mouseSpeed.x,gallery.easing);
		}else if(gallery.move=='left'){
			$(gallery.carousel).stop(true,false).animate({left:moveto+'px'},1000-gallery.mouseSpeed.x,gallery.easing);
		}		
	},false);
	
	
	gallery.hasTags = function(image_tags){
		if(image_tags==undefined){return false};
		if(image_tags.length==0||gallery.tags.length==0){
			return false;
		}
		var itags = image_tags.split(',');
		var tags = gallery.tags.split(',');
		for(var z=0;tags.length>z;z++){
			for(var y=0;itags.length>y;y++){
				if(String(itags[y]).toLowerCase().trim()==String(tags[z]).toLowerCase().trim()){
					return true;
				}
			}
		}
		return false;
	}
	
	gallery.resize = function(){
		gallery.fullImageBg.style.top = gallery.cy()+'px';
		gallery.fullImageBg.style.left = gallery.cx()+'px';
		gallery.fullImageBg.style.width = gallery.cw()+'px';
		gallery.fullImageBg.style.height = gallery.ch()+'px';
		/* 		bind buttons to base of widigit */
		if(gallery.fimageRect.h==0){
			gallery.carousel.style.top = eval(gallery.ch()-gallery.carouselHeight+gallery.fullImageMarginTop-gallery.carouselMarginTop-2)+'px';
			gallery.buttons.style.top = eval(gallery.ch()-gallery.carouselHeight+gallery.fullImageMarginTop-gallery.carouselMarginTop-gallery.buttonTop)+'px';
		}else{
		/*
			or 
				Bind buttons to the base of the image
		*/
		gallery.carousel.style.top = eval(gallery.fimageRect.h+gallery.carouselMarginTop-2)+'px';
		gallery.buttons.style.top = eval(gallery.fimageRect.h+gallery.buttonTop)+'px';
		}

		if(parent.tagName!='body'){
			gallery.fullImageBg.style.clip = 'rect(0px '+gallery.cw()+'px '+gallery.ch()+'px 0px)';
		}
		parent.style.height = gallery.ch()+'px';
		parent.style.width = gallery.cw()+'px';
		gallery.center = gallery.cw()/2;
		gallery.description(gallery.descContent);
		
	}
	
	
	
	//appendEvent('resize',window,gallery.resize,false);
	gallery.fullImageBg.appendChild(gallery.carousel);
	var ref = document.getElementById(gallery.linkParent);
	if(ref){ref.innerHTML = '';};
	
	/* animate procedures */
	
	
	
	
	/* end of animate procedures */
	for(var x=0;imgs.length>x;x++){
		if((gallery.categories.length==0||gallery.categories.indexOf(imgs[x][0])>-1)){
			if(gallery.hasTags(imgs[x][8])||(gallery.tags.length==0)){
				//console.log('tags: '+imgs[x][8]+' found in '+gallery.tags);
				var img = new Image();
				var lnk = document.createElement('a');
				img.setAttribute('index',gallery.images.length);
				lnk.setAttribute('index',gallery.images.length);
				
				gallery.images.push(img);
				if(imgs[x][6]==f_prams.imageId){
					var loadImage = img;
				}
				var link = String(imgs[x][2]);
				img.src = imgs[x][1]+'t.jpg';
				img.setAttribute('full',imgs[x][1]+'f.jpg');
				img.setAttribute('desc',imgs[x][3]);
				thumbAspect = parseInt(imgs[x][5])/parseInt(imgs[x][4]);
				img.style.border = 'none';
				img.onmousedown = function(){return false;};
				
				img.onclick = function(){
					if(gallery.move!=''){return false};
					if(gallery.currentImageIndex==parseInt(this.getAttribute('index'))){return false;};
					var thumb = this;
					gallery.description(gallery.loadingImageText);
					thumb.pluse = function(){
						$(thumb).animate({opacity:'.8'},'fast','swing',function(){
							$(thumb).animate({opacity:'1'},'fast','swing',function(){
								if(!thumb.doneloading){
									setTimeout(function(){
										thumb.pluse();
									},0);
								}
							});
						});
					}
					thumb.pluse();

					
					var fimg = new Image();
					fimg.setAttribute('index',this.getAttribute('index'));
					fimg.src = this.getAttribute('full');
					fimg.ondragstart = function(){return false;};
					var imgDesc = this.getAttribute('desc');
					fimg.style.margin = gallery.fullImageMargin;
					fimg.style.border = gallery.fullImageBorder;
					fimg.style.MozUserSelect='-moz-none';
					fimg.style.position = 'absolute';
					fimg.onmousedown = function(){return false;};
					appendEvent('load',fimg,function(){
						gallery.currentImageIndex = parseInt(fimg.getAttribute('index'));
						/* animation exchange procedure */
						var aspect = fimg.width/fimg.height;
						var cheight = eval(gallery.ch()-gallery.carouselHeight-gallery.fullImageMarginTop-gallery.carouselMarginTop-2);
						var cwidth = eval(cheight*aspect);
						var centerPos = Math.round(gallery.center-(cwidth/2));
						/* if the width is just a bit too much */
						if(cwidth>gallery.cw()){
							var aspect = fimg.height/fimg.width;
							var cwidth = gallery.cw()-10;
							var cheight = cwidth*aspect;
							var centerPos = Math.round(gallery.center-(cwidth/2));
						}
						thumb.doneloading = true;
						if(gallery.animationStyle=='zoom'){
							var firstChild = gallery.fullImageBg.firstChild;
							gallery.fullImageBg.insertBefore(fimg,firstChild);
							gallery.topZ++;
							fimg.style.zIndex = gallery.topZ;
							fimg.style.left = centerPos+'px';
							fimg.style.top = gallery.fullImageMarginTop+'px';
							$(fimg).animate({opacity:'0',height:'1%',width:'1%'},0,'swing');
							$(fimg).animate({opacity:'1',height:cheight+'px',width:cwidth+'px'},transSpeed,'swing',function(){
								gallery.description(imgDesc);
								$(firstChild).animate({opacity:'0',width:'1px',height:'1px'},transSpeed,'swing',function(){
									
								});
							});
						}else if(gallery.animationStyle=='fade'){
							var firstChild = gallery.fullImageBg.firstChild;
							gallery.fullImageBg.insertBefore(fimg,firstChild);
							gallery.topZ++;
							//fimg.style.zIndex = gallery.topZ-1;
							fimg.style.left = centerPos+'px';
							fimg.style.top = gallery.fullImageMarginTop+'px';
							$(fimg).animate({opacity:'0',height:cheight+'px',width:cwidth+'px'},0,'swing');
							$(fimg).animate({opacity:'1'},2000,'swing');
							$(firstChild).animate({opacity:'0'},3000,'easeOutSine',function(){
								this.parentNode.removeChild(this);
							});
							$(fimg).animate({opacity:'1'},transSpeed,'swing',function(){
								gallery.description(imgDesc);
							});
							
						}else if(gallery.animationStyle=='page'){
							var firstChild = gallery.fullImageBg.firstChild;
							gallery.fullImageBg.insertBefore(fimg,firstChild);
							gallery.topZ++;
							fimg.style.zIndex = gallery.topZ-1;
							fimg.style.left = centerPos+'px';
							fimg.style.top = gallery.fullImageMarginTop+'px';
							$(fimg).animate({opacity:'0',height:0+'px',width:0+'px'},0,'swing');
							$(fimg).animate({opacity:'1',top:eval(gallery.fullImageMarginTop)+'px',left:centerPos+'px',height:cheight+'px',width:cwidth+'px'},'fast','swing');
							$(firstChild).animate({left:'+='+eval(cwidth+rand(25))+'px'},2000,'swing',function(){
									fimg.style.zIndex = gallery.topZ;
									$(firstChild).animate({left:'-='+eval(cwidth+rand(25))+'px',top:'-='+rand(15)+'px'},'slow','swing',function(){
										$(firstChild).animate({top:rand(800),left:rand(800),opacity:'0',height:'1px',width:'1px'},'slow','linear',function(){
											this.parentNode.removeChild(this);
										});
										
									});
							});
							$(fimg).animate({opacity:'1',height:cheight+'px',width:cwidth+'px'},transSpeed,'swing',function(){
								gallery.description(imgDesc);
							});
						}else if(gallery.animationStyle=='fallaway'){
							var firstChild = gallery.fullImageBg.firstChild;
							gallery.fullImageBg.insertBefore(fimg,firstChild);
							gallery.topZ++;
							//fimg.style.zIndex = gallery.topZ-1;
							fimg.style.left = centerPos+'px';
							fimg.style.top = gallery.fullImageMarginTop+'px';
							$(fimg).animate({opacity:'0',top:eval(gallery.fullImageMarginTop)+'px',left:centerPos+'px',height:cheight+'px',width:cwidth+'px'},0,'swing');
							$(fimg).animate({opacity:'1'},2000,'swing');
							$(firstChild).animate({top:eval((cwidth/2)*2)+'px',opacity:'0'},3000,'easeOutSine',function(){
								this.parentNode.removeChild(this);
							});
							$(fimg).animate({opacity:'1',height:cheight+'px',width:cwidth+'px'},transSpeed,'swing',function(){
								gallery.description(imgDesc);
							});
						}
						appendEvent('resize',window,function(){
							var aspect = fimg.width/fimg.height;
							var cheight = eval(gallery.ch()-gallery.carouselHeight-gallery.fullImageMarginTop-gallery.carouselMarginTop-2);
							var cwidth = eval(cheight*aspect);
							var centerPos = Math.round(gallery.center-(cwidth/2));
							/* if the width is just a bit too much */
							if(cwidth>gallery.cw()){
								var aspect = fimg.height/fimg.width;
								var cwidth = gallery.cw()-10;
								var cheight = cwidth*aspect;
								var centerPos = Math.round(gallery.center-(cwidth/2));
							}
							fimg.style.left = centerPos+'px';
							$(fimg).animate({height:cheight+'px',width:cwidth+'px'},0,'swing');
							gallery.fimageRect = {x:fimg.offsetLeft,y:fimg.offsetTop,h:cheight,w:cwidth};
							gallery.resize();
						},false);
						
						gallery.fimageRect = {x:fimg.offsetLeft,y:fimg.offsetTop,h:cheight,w:cwidth};
						gallery.resize();
					},false);
					
				};
				lnk.onclick = function(e){
					gallery.images[this.getAttribute('index')].onclick();
				}
				img.alt = imgs[x][3];
				img.style.position = 'absolute';
				img.style.border = 'none';
				img.ondragstart = function(){return false;};
				img.style.left = gallery.leftOffset+'px';
				img.style.height = eval(gallery.carouselHeight-4)+'px';
				img.style.MozUserSelect='-moz-none';
				img.style.width = Math.round(eval(thumbAspect*gallery.carouselHeight))+'px';
				$(img).load(function(){
					return null;
				});
				gallery.carousel.appendChild(img);
				if(ref){
					lnk.innerHTML = imgs[x][7];
					lnk.style.cursor = 'pointer';
					ref.appendChild(lnk);
					ref.appendChild(document.createElement('br'));
				}
				img.setAttribute('leftOffset',gallery.leftOffset);
				gallery.leftOffset+=Math.round(eval(thumbAspect*gallery.carouselHeight))+gallery.thumbSpace;
			}
		};
	}

	gallery.defaultDesc = function(desc){
		if(!gallery.desc){
			gallery.desc = document.createElement('div');
			gallery.desc.style.position = 'absolute';
			gallery.desc.style.zIndex = '5000';
			gallery.desc.style.top = eval(gallery.cy()+10)+'px';
			gallery.desc.style.left = eval(gallery.cx()+10)+'px';
			gallery.desc.style.border = 'solid 3px black';
			gallery.desc.style.background = '#f0f0f0';
			$(gallery.desc).animate({opacity:'0'},0,'linear',function(){
				parent.appendChild(gallery.desc);
			});
		}
		$(gallery.desc).stop(true,false).animate({opacity:'0'},'slow','linear',function(){
			if(desc==undefined){return false};
			if(desc.length==0){return false};
			$(gallery.desc).animate({opacity:'.90'},'slow','linear');
			gallery.desc.style.padding = '10px';
			gallery.desc.innerHTML = desc;
		});
	}
	if(gallery.descriptionProcedure==undefined){
		gallery.descriptionProcedure = gallery.defaultDesc;
	}	
	gallery.description = function(desc){
		gallery.descContent = desc;
		gallery.descriptionProcedure.apply(gallery,arguments);
	}
	gallery.resize();
	/* set the parent width to be the sum of the children */
	gallery.carousel.style.width = gallery.leftOffset+'px';
	/* start mouse acceleration timer */
	gallery.timer();
	/* open the image that was requested when the page loaded (if any) */
	
	if(gallery.images.length>0){
		gallery.fullImageBg.appendChild(gallery.buttons);
		setTimeout(function(){
			if(loadImage){
				loadImage.onclick();
			}else{
				gallery.images[0].onclick();
			}
		},0);
	}else{
		gallery.parent.innerHTML = 'No images in selected gallery';
	}
}
function rotateimages(){
	var rimages = getElementsWithAttribute('rotator','img',document.body);
	for(var x=0;rimages.length>x;x++){
		setupRotator(rimages[x]);
	}
};
function setupRotator(objImage){
	objImage.images = [];
	objImage.speed = objImage.getAttribute('speed') || 'fast';
	objImage.effect = objImage.getAttribute('effect') || 'fade';
	objImage.startIn = objImage.getAttribute('startin') || 0;
	objImage.wait = objImage.getAttribute('wait') || 500;
	objImage.cat = objImage.getAttribute('rotator');
	objImage.rotatorIndex = 0;
	objImage.rotatorZindex = 1;
	for(var x=0;imgs.length>x;x++){
		if(imgs[x][0]==objImage.cat){
			var img = new Image;
			img.style.cursor = 'pointer';
			img.className = 'rotatorimage';
			var link = String(imgs[x][2]);
			img.src = imgs[x][1]+'r.jpg';
			img.setAttribute('imageid',imgs[x][6]);
			img.onclick = function(){
				if(objImage.parentNode.tagName!='A'){
					window.location = '/gallery.asp?imageId='+this.getAttribute('imageid');
				}else{
					if(objImage.parentNode.href.length>1){
						window.location = objImage.parentNode.href;
					}
				};
			return null};
			img.alt = imgs[x][3];
			img.style.position = 'absolute';
			$(img).animate({'opacity':'0'},0,'swing',function(){
				document.body.appendChild(img);
			});
			objImage.images.push(img);
		}
	}
	objImage.updateImgPos = function(e){
		var pos = getPosition(objImage);
		for(var x=0;objImage.images.length>x;x++){
			objImage.images[x].style.top = pos.y+'px';
			objImage.images[x].style.left = pos.x+'px';
		}
	}
	/* find the position of the element just before starting */
	objImage.updateImgPos();
	appendEvent('resize',window,objImage.updateImgPos,false);
	/* start the rotator after the time specified */
	setTimeout(function(){
		rotate(objImage);
	},parseInt(objImage.startIn));
	return null;
}
function rotate(objImage){
	objImage.updateImgPos();
	if(objImage.rotatorIndex+1>objImage.images.length){
		objImage.rotatorIndex = 0;
	}
	/* place image at the top of the Z stack */
	objImage.images[objImage.rotatorIndex].style.zIndex = objImage.rotatorZindex;
	$(objImage.images[objImage.rotatorIndex]).animate({'opacity':'1'}, parseInt(objImage.speed),'swing',function(){
		/* turn off the original image over and over and over again WE HATE YOU ORIGINAL IMAGE */
		$(objImage).animate({'opacity':'0'});
		/* when the animation is finished shut off OTHER images in this group */
		for(var x=0;objImage.images.length>x;x++){
			if(x!=objImage.rotatorIndex){
				$(objImage.images[x]).animate({'opacity':'0'});
			}
		}
		objImage.rotatorIndex++;
		/* increase the Z stack */
		objImage.rotatorZindex++;
		/* reciprocate */
		setTimeout(function(){
			rotate(objImage);
		},parseInt(objImage.wait));
	});
	return null;
}
function appendEvent(type,listener,appendedFunction,capture){
	if(listener){
		if(listener.addEventListener){
			listener.addEventListener(type,appendedFunction,capture);
		}else if(listener.attachEvent){
			listener.attachEvent('on'+type,appendedFunction);
		}else{
			alert('Can\'t attach event to listener!');
		}
	}else{
		alert('No function defined for event on'+type);
	}
}
function mouseCoords(ev){
	return {
		x:ev.clientX,y:ev.clientY
	};
}
function getPosition(e){
	var eleft = etop = 0;
	try{
		while (e.offsetParent){
			eleft += e.offsetLeft;
			etop += e.offsetTop;
			eleft -= e.scrollLeft;
			etop -= e.scrollTop;
			e = e.offsetParent;
		}
	}catch(err){
		
	}
	if(window.navigator.userAgent.indexOf('MSIE 7.0')>-1||window.navigator.userAgent.indexOf('MSIE 6.0')>-1){eleft+=20;};
	return {x:eleft, y:etop};

}