String.prototype.startsWith = function(str) {
	return (this.substr(0, str.length) == str)
}
Function.prototype.override = function(fn) {
	fn.superFunction = this;
	return fn;
};

/* Either call the parameter or return it */
function resolve(q, self, args) {
	return (typeof q == 'function') ? resolve(q.apply(self, args)) : q;
}

/* Array.indexOf since IE doesn't */
function indexOf(a, i) {
	if (a == null || a == undefined || a.constructor != Array)
		return -1;
	if (a.indexOf)
		return a.indexOf(i);
	for ( var n = 0; n < a.length; n++)
		if (a[n] == i)
			return n;
	return -1;
}

/* Run a function from a timer until it returns <1 */
function fork(fn, t) {
	setTimeout( function() {
		if (t = fn(t)) {
			fork(fn, t)
		}
	}, t);
}

/*
 * Iterate across the data parts of object or array (ignore functions and type
 * info)
 */
function iterate(o, fn, self) {
	var result = undefined;
	if (o)
		for (var k in o)
			if (k!='prototype' && !k.startsWith("java$") && (typeof (o[k]) != 'function'))
				if (result = fn.apply(self ? self : this, [ k, o[k] ]))
					return result;
}

/* Copy one objects properties onto another */
function set(o, p) {
	for ( var k in p)
		o[k] = p[k];
	return o;
}

function deepset(o, p) {
	for ( var k in p)
		if (p[k] && typeof p[k] == 'object')
			o[k] = deepset( {}, p[k]);
		else
			o[k] = p[k];
	return o;
}

/* now */
function now() {
	return new Date().getTime();
}

/* Sort the fields in an object */
function sortObject(o, sortOrder) {
	var fields = [];
	iterate(o, function(k, v) {
		fields[fields.length] = [ k, v ]
	});
	fields.sort( function(kv1, kv2) {
		return sortOrder(kv1[1], kv2[1])
	});
	var r = {};
	iterate(fields, function(k, v) {
		r[v[0]] = v[1]
	});
	return r;
}

function LIMG(imgid, atts) {
	var i = IMG( {
		src :limg(imgid),
		inherits :atts
	});
	var tt = msg.exists("tt." + imgid);
	if (tt)
		i.tooltip = tt;
	return i;
}

function SLIMG(size, imgid, atts) {
	var i = IMG( {
		src :limg("standard/" + size + "/" + imgid),
		style$width :size + 'px',
		style$height :size + 'px',
		inherits :atts
	});
	var tt = msg.exists("tt." + imgid);
	if (tt)
		i.tooltip = tt;
	return i;
}

function IBLOCK() {
	return SPAN.apply(null, arguments).attrs( {
		style$display :'inline-block'
	});
}

function BUTTON(attr, elts) {
	attr.className = (attr.className || "") + " button";
	return SPAN( {
		inherits : [ {
			style$display :'inline-block'
		}, BUTTON, attr ]
	}, DIV( {
		className :'buttonInner'
	}, elts));
}
BUTTON.setContent = function(elts) {
	this.firstChild.replace(elts);
}

function MSG(attr) {
	return msg.elts.apply(msg, arguments);
}

function VINPUT(attr) {
	var i = ((attr && !attr.password && attr.multiLine) ? TEXTAREA : INPUT)( {
		originalAttr :attr,
		inherits : [ VINPUT, attr ]
	});
	if (attr && attr.maxLength)
		i.style.width = String(attr.maxLength) + 'em';
	i.initClassName = i.className;
	i.checkEmpty();
	return i;
}

VINPUT.setText = function(t) {
	INPUT.inherit.setText.apply(this, arguments);
	this.checkEmpty();
}

VINPUT.checkEmpty = function() {
	if (this.defaultValue && (!this.value || (this.value == this.defaultValue))) {
		INPUT.inherit.setText.apply(this, [ this.defaultValue ]);
		this.showingDefault = true;
		this.style.color = '#999999';
		if (this.type == 'password' && !tags.isIE)
			this.type = 'text';
	} else { // Not default text
		this.showingDefault = false;
		this.style.color = '';
	}
}

VINPUT.onfocus = function() {
	if (this.showingDefault) {
		this.showingDefault = false;
		this.value = '';
		this.style.color = '';
		if (this.password) {
			if (tags.isIE) {
				// IE can't change the type dynamically.
				var shadow = VINPUT(set(this.originalAttr, {
					type :'password',
					value :'',
					defaultValue :''
				}));
				this.replaceNode(shadow);
				shadow.focus();
			} else {
				this.type = 'password';
			}
		}
	}
}
VINPUT.getText = function() {
	return this.showingDefault ? '' : this.value;
}
VINPUT.onblur = function() {
	this.className = this.initClassName;
	this.checkEmpty();
}
VINPUT.setError = function(fNoFocus) {
	this.className = 'errorField';
	if (!fNoFocus)
		this.focus();
}

function COLS(widths, elts, commonAttrs, colAttrs) {
	iterate(elts, function(k, v) {
		if (v) {
			elts[k] = IBLOCK( {
				className :'column' + k,
				inherits : [ commonAttrs, colAttrs ? colAttrs[k] : null ]
			}, v);
			if (widths[k])
				elts[k].style.width = widths[k] + "px";
		}
	});
	return elts;
}

function CHECKIMG(attr) {
	attr = attr || {};
	attr.species = attr.species || "check";
	return LIMG((attr.checked ? "assets/" + attr.species + "on" : "assets/"
			+ attr.species + "off"), {
		inherits : [ CHECKIMG, {
			style$width :'10px',
			style$height :'10px',
			style$verticalAlign :'middle',
			style$padding :'2px'
		}, attr ]
	});
}
CHECKIMG.setCheck = function(f) {
	this.checked = f;
	this.src = limg(this.checked ? "assets/" + this.species + "on" : "assets/"
			+ this.species + "off");
}
CHECKIMG.setSpecies = function(n) {
	this.species = n;
	this.setCheck(this.checked);
}

function CHECK(attr, elts) {
	attr = attr || {};
	attr.checkImg = CHECKIMG( {
		checked :attr.checked
	});
	if (attr && attr.afterElts)
		return SPAN( {
			inherits : [ CHECK, attr ]
		}, IBLOCK( {
			style$verticalAlign :'middle'
		}, elts), attr.checkImg);
	return SPAN( {
		inherits : [ CHECK, attr ]
	}, attr.checkImg, IBLOCK( {
		style$verticalAlign :'middle'
	}, elts));
}
CHECK.onclick = function() {
	this.setCheck(!this.isChecked());
	if (this.onChange)
		this.onChange(this.isChecked());
}
CHECK.setCheck = function(f) {
	this.checkImg.setCheck(f);
}
CHECK.setSpecies = function(f) {
	this.checkImg.setSpecies(f);
}
CHECK.isChecked = function() {
	return this.checkImg.checked;
}

function RADIOCHECKS(attr, checks) {
	iterate(checks, function(f, v) {
		v.setSpecies("radio_");
		v.onclick = RADIOCHECK_onclick;
	})
	return DIV( {
		checks :checks,
		inherits : [ RADIOCHECKS, attr ]
	}, checks);
}
RADIOCHECK_onclick = function(e) {
	this.parentNode.setCheck(this)
}
RADIOCHECKS.setCheck = function(check) {
	if (!isNaN(check))
		return this.setCheck(this.checks[Number(check)]);
	iterate(this.checks, function(f, v) {
		v.setCheck(v == check)
	})
}
RADIOCHECKS.getCheck = function() {
	return iterate(this.checks, function(f, v) {
		if (v.isChecked())
			return v;
	})
}

function BIGSEND(attr, key) {
	key = key || "send";
	return BUTTON(attr, IBLOCK( {
		className :"sendBtn"
	}, [ IBLOCK( {
		style$height :'100%',
		style$verticalAlign :'middle'
	}), msg(key) ]));
}

function PROPERTYCONTENT(content) {
	var textPage = [];
	var links = [];

	var info = Intl("/" + content + ".properties");
	var n = 1;
	var para = 1;
	while (info.exists(String(n) + ".title")) {
		if (info.exists(String(n) + ".cont")) {
			textPage[textPage.length - 2]
					.append(info.elts(String(n) + ".text"));
			n++;
			continue;
		}
		var link = A( {
			className :"infoLink",
			titleElt :info.elts( {
				className :"infoTitle"
			}, String(n) + ".title", para),
			onclick :PROPERTYCONTENT_linkClick
		}, info.elts(String(n) + ".title", para));
		links.push(link);
		textPage.push(link.titleElt);
		textPage.push(DIV( {
			className :"infoText"
		}, info.elts(String(n) + ".text", para)));
		textPage.push(A( {
			className :'infoTop',
			onclick :PROPERTYCONTENT_scrollTop
		}, msg("to.top")));
		n++;
		para++;
	}
	return [ DIV( {
		className :'infoLinkIndex'
	}, links), DIV(textPage) ];
}
PROPERTYCONTENT_scrollTop = function() {
	window.scrollTo(0, 0);
}
PROPERTYCONTENT_linkClick = function() {
	this.titleElt.scrollIntoView();
	window.scrollBy(0, -66);
}

function PODTITLE() { // Takes an optional attrs, and then same args as
						// "msg()" - i.e. a text key and arguments
	var atts;
	var m;
	if (typeof arguments[0] != 'string') {
		atts = arguments[0];
		m = msg.apply(null, Array.prototype.slice.apply(arguments, [ 1 ]));
	} else {
		atts = {};
		m = msg.apply(null, arguments);
	}
	return DIV( {
		titleText :m,
		inherits :atts,
		className :((atts.onclick) ? 'titleLink' : 'title')
	}, LIMG('assets/title', {
		style$paddingRight :'10px'
	}), m);
}

function PODCOMPONENTTITLE(elts, attr) { // Takes a list of
											// components/elements
	attr = attr || {};
	return DIV( {
		inherits :attr,
		className :((attr.onclick) ? 'titleLink' : 'title')
	}, SPAN( {
		className :'fleft'
	}, LIMG('assets/title', {
		style$paddingRight :'10px'
	})), elts);
}

var thirtyTwoBits = 4294967296;
function USERBITSDATA(p, bitsToLabelMap, noDataMsg, dataMsg){
	var d = [] ;
	var bitMask = p.sp.getSplitUserBitsMask();
	if (bitMask == 0)
		return [msg(noDataMsg)];
	
	if (undefined != dataMsg && dataMsg) d.push(msg(dataMsg));

	var hiBitMask = bitMask.getHiBits();
	var loBitMask = bitMask.getLoBits();
	
	for (var key in bitsToLabelMap) {
		
		if (sixtyFourBitAnd(key, bitMask))
		{
			d.push(bitsToLabelMap[key].label());

			d.push(", ") ;			
		}
	}
	if (d.length <= 1)
		return [msg(noDataMsg)] 
	d.pop() ;
	return d ;
}

function sixtyFourBitAnd(keyAsString, bitMask) {
	var hiAndLoKeys = keyAsString.split("|");
	var hiKey = hiAndLoKeys[0];
	var loKey = hiAndLoKeys[1];
	
	var hiBitMask = bitMask.getHiBits();
	var loBitMask = bitMask.getLoBits();
	
	return ((hiBitMask & hiKey) != 0 || (loBitMask & loKey) != 0 );
}

function USERBADGES(badgeAttr,p) {
	var d = [] ;
	var bitMask = p.sp.getSplitUserBitsMask();
	if (bitMask == 0)
		return badgeAttr && badgeAttr.noBadges;
	
	for (var badgeKey in this.profileBadgesToBadgeMap){
		if (sixtyFourBitAnd(badgeKey, bitMask)) {
				var badge = this.profileBadgesToBadgeMap[badgeKey];
				if (badge != undefined && badge != null) {
					d.push(IMG({tooltip:badge.label(),className:'userBadge',profile:p,badge:badge,onclick:USERBADGES.onclick,inherits:badgeAttr,src:staticSrc('/wyll/badges/36/'+badge.name()+'.gif')})) ;
				}
		}
	}	
	if (d.length < 1)
		return badgeAttr && badgeAttr.noBadges;
	return d ;
}

function FLIRTFACENAME(p) {
	var icons = [] ;

	if (p.u.rd.face_icon_id && gift[p.u.rd.face_icon_id] && gift[p.u.rd.face_icon_id].faceImageUrl())
		icons.push(IMG({
			className :"faceIcon",
			src :gift[p.u.rd.face_icon_id].faceImageUrl()
		})) ;
	
	if (USERBADGES.length>0)
		icons.push(SLIMG(18,'badge',{className:'faceIcon'})) ;
		
	if (icons.length)
		return SPAN(icons, USERNAME(p));
	return USERNAME(p);
}

function USERNAME(p) {
	if(p.java$class=='com.flirtomatic.users.ThumbProfile'){
		var genders =["NONE","MALE", "FEMALE","EITHER"];
		return p.fn + " (" + msg("gender."+genders[p.g]) +", " + p.a +")";
	}else
		return p.u.rd.flirtname + " (" + msg("gender." + p.sp.rd.gender) + ", "	+ p.sp.age + ")";
}

function ICONLABEL(eltImg, eltLabel, ctnType, attr) {
	if (typeof eltLabel == 'string' || typeof eltLabel == 'number')
		eltLabel = SPAN(eltLabel);
	if (!ctnType)
		ctnType = SPAN;
	var il = ctnType( {
		inherits :attr
	}, eltImg, eltLabel);
	il.className += " iconlabel";
	il.firstChild.className += " iconlabelicon";
	il.firstChild.nextSibling.className += " iconlabellabel";
	return il;
}

function RATINGICON(p, big, attr) {
	var imgid = "rating." + Math.floor(p);
	var size = big ? 24 : 16;
	var i = IMG( {
		src :limg(msg("rating.group") + "/" + size + "/" + imgid),
		style$width :size + 'px',
		style$height :size + 'px',
		className :"iconlabelicon",
		inherits :attr
	});
	var tt = msg.exists("tt." + imgid);
	if (tt)
		i.tooltip = tt;
	return i;

}

function RATINGMSG(p) {
	var r = "rating." + Math.floor(p);
	return msg(r);
}

function RATING(p, atts) {
	return DIV( {
		className :"iconlabel",
		inherits :atts
	}, RATINGICON(p.u.rd.avg_rating, atts && atts.bigRatingIcon), SPAN( {
		className :"iconlabellabel"
	}, RATINGMSG(p.u.rd.avg_rating)));
}

function targetFlirt(p) {
	var ar = p.sp.prettyAgeRange();
	if (ar)
		ar = ", " + ar;
	return msg("myprofile.target." + p.sp.rd.target_gender, ar);
}

function GALLERY(gallery, promptElt, attr) {
	if (typeof promptElt == undefined)
		promptElt = null;
	return MULTIGALLERY(gallery, promptElt, 9, {
		style$width :'784px',
		style$marginLeft :'20px',
		inherits :attr
	});
}

function MULTIGALLERY(gallery, promptElt, size, attr) {
	if (typeof promptElt == undefined)
		promptElt = null;
	var photos = DIV( {
		gallery :gallery,
		size :size,
		className :'photoGallery',
		inherits : [ MULTIGALLERY, attr ]
	});
	photos.show(false);
	return DIV(promptElt, photos);
}
MULTIGALLERY.show = function(showAll) {
	this.replace();
	if (this.gallery) {
		iterate(
				this.gallery,
				function(k, v) {
					var photoTooltip = v.row.caption;
					if (v.row.user_set_status == 'PRIVATE'
							&& (v.localURL() == limg("assets/bs_photo") || v.localURL() == limg("assets/av_photo"))) {
						photoTooltip = msg("myprofile.backstage.photos.bs.tooltip");
						// if(v.isAdult()){
						// try{
						// if(!wyll.me().u.isAgeVerified())
						// photoTooltip=msg("myprofile.backstage.photos.av.tooltip");
						// }catch(ex){photoTooltip=msg("myprofile.backstage.photos.av.tooltip");}
						// }
					}
					this.append(SPAN( {
						className :'thumbContainer',
						style$display :'inline'
					}, IMG( {
						style$opacity :(v.needsApproval() ? 0.4 : 1.0),
						onclick : function() {
							if (window.PROFILE) {
								if (this.parentNode.parentNode.editMode)
									PROFILE.photoEditPopup(v);
								else
									PROFILE.photoPopup(v);
							}
						},
						src :v.localURL(),
						className :'thumbnailimg',
						onmouseover : function() {
							this.className = 'thumbnailimg2'
						},
						onmouseout : function() {
							this.className = 'thumbnailimg'
						},
						tooltip :photoTooltip,
						tooltipYoffset :'bottom'
					})));
					if (this.childNodes.length == this.size && !showAll)
						return true;
				}, this);
		if (this.gallery.length > this.size)
			this.append(DIV( {
				className :'seeAll',
				onclick : function() {
					this.parentNode.showMore(!showAll)
				}
			}, A((showAll) ? msg('see.fewer') : msg('see.all'))));
	}
}
MULTIGALLERY.showMore = function(showAll) {
	this.show(showAll);
}
MULTIGALLERY.click = function() {
	if (this.parentNode.parentNode.editMode)
		PROFILE.photoEditPopup(v);
	else
		PROFILE.photoPopup(v);
}
if (window.Photo) {
	Photo.prototype.localURL = function() {
		var u = this.getPublicURL();
		if (u)
			return u;
		if (this.isAdult())
			return limg("assets/bs_photo");
		if (this.row.user_set_status == 'PRIVATE')
			return limg("assets/bs_photo");
		return limg("assets/no_photo");
	}
}

function openPopup(elt, owner) {
	var d = DIV( {
		className :'modeless',
		inherits :Draggable
	}, ANIMATE(fadeIn, elt));

	owner = owner || tags.ID("content");
	owner.appendChild(d);

	var p = click.getDocXY();
	d.style.left = String((p.x & ~1) / 2 - (d.clientWidth / 2)) + 'px';
	d.style.top = String((p.y & ~1) / 2 - (d.clientHeight / 2)) + 'px';
	return d;
}

function ACCESSORIESICON(gift, clickFn, captionElt, atts) {
	return SPAN( {
		className :'accessoryContainer'
	}, DIV( {
		className :'accessoryimgcontainer',
		onmouseover :clickFn ? function() {
			this.className = 'accessoryimgcontainerSelected'
		} : null,
		onmouseout :clickFn ? function() {
			this.className = 'accessoryimgcontainer'
		} : null
	}, IMG( {
		onclick :clickFn,
		className :'accessoryimg',
		src :gift.iconImageUrl(),
		inherits :atts
	})), captionElt);
}

function ACCESSORIES(prf, noClicks, atts) {
	var acc = prf.accMap || (prf.getAccessories && prf.getAccessories().accMap)
			|| {};// Horrendous piece of cheese
	//
	var accArr = [];
	iterate(acc, function(k, v) {
		accArr.push(k);
	});// create map array
	accArr.sort( function(k1, k2) {
		return k2 - k1;
	});

	var a = DIV( {
		acc :acc,
		accArr :accArr,
		size :20,
		noClicks :noClicks,
		p :null,
		className :'accessories',
		inherits : [ ACCESSORIES, atts ]
	});
	a.show(false);
	return a;
}

ACCESSORIES.show = function(showAll) {
	this.replace();
	if (this.acc) {
		var idx = 0;
		iterate(this.accArr, function(k, v) {
			var gift = window.gift[v];
			if (gift != undefined) {
				this.append(ACCESSORIESICON(gift, this.noClicks ? null
						: function() {
							this.parentNode.parentNode.parentNode.popup(
									this.accArr, this.idx)
						}, DIV(this.acc[v]), {
					idx :idx,
					accArr :this.accArr
				}));
				if (this.childNodes.length == this.size && !showAll)
					return true;
				idx++;
			}
		}, this);
		if (this.accArr.length > this.size)
			this.append(DIV( {
				className :'seeAll',
				onclick : function() {
					this.parentNode.clickMore(!showAll)
				}
			}, A((showAll) ? msg('see.fewer') : msg('see.all'))));
	}
}

ACCESSORIES.clickMore = function(showAll) {
	this.show(showAll);
}

ACCESSORIES.popup = function(accArr, idx) {
	var k = accArr[idx];
	var gift = window.gift[k];
	var giftName = SPAN(gift.getName());
	var mainImage = IMG( {
		src :gift.mediumImageUrl(),
		height :'136',
		width :'173'
	});

	var tr = TR( {
		idx :idx
	}, TD( {
		tooltipYoffset :'top',
		tooltipXoffset :-110,
		tooltip :GALLERYCAROUSEL.getPosition
	}, ROLLOVERIMG('assets/arrowleft_off', 'assets/arrowleft', {
		style$display :inlineBlock,
		onclick : function() {
			var idx = this.parentNode.parentNode.idx;
			if ((idx) > 0) {
				idx--;
				k = accArr[idx];
				gift = window.gift[k];
				giftName.innerHTML = gift.getName();
				mainImage.src = gift.mediumImageUrl();
				this.parentNode.parentNode.idx = idx;
			}
		}
	})), TD(mainImage), TD( {
		tooltipYoffset :'top',
		tooltipXoffset :-110,
		tooltip :GALLERYCAROUSEL.getPosition
	}, ROLLOVERIMG('assets/arrowright_off', 'assets/arrowright', {
		style$display :inlineBlock,
		onclick : function() {
			var idx = this.parentNode.parentNode.idx;
			if ((idx) < accArr.length - 1) {
				idx++;
				k = accArr[idx];
				gift = window.gift[k];
				giftName.innerHTML = gift.getName();
				mainImage.src = gift.mediumImageUrl();
				this.parentNode.parentNode.idx = idx;
			}
		}
	})));

	var x = POPUP(giftName, TABLE( {
		style$paddingTop :'11px'
	}, TBODY(tr)), {
		style$width :'235px',
		onClose : function() {
			x.remove();
		}
	});

	if (this.p)
		this.p.remove();

	this.p = openPopup(x);
}

function POPUP(title, elts, attr) {
	return DIV( {
		style$width :'500px',
		onClose :openModal.dismiss,
		inherits : [ POPUP, attr ]
	}, CLOSEBOX( {
		style$marginTop :'5px',
		style$marginRight :'5px',
		onclick :POPUP.close_onclick
	}), DIV( {
		className :'profilePopup'
	}, SPAN( {
		className :'title'
	}, title), elts));
}
POPUP.close_onclick = function() {
	this.parentNode.onClose.apply(this.parentNode)
};

function GALLERYCAROUSEL(mainImage, imgCaption, photos, attr) {
	var d = TABLE( {
		mainImage :mainImage,
		imgCaption :imgCaption,
		photos :photos,
		index :0,
		target :0,
		numThumbs :5,
		selectedItem :null,
		ctn :TR(),
		inherits : [ GALLERYCAROUSEL, attr ]
	});

	d.append(TBODY(d.ctn));
	// Initilaise the DIV
	d.ctn.append(TD( {
		tooltipYoffset :'top',
		tooltipXoffset :-110,
		tooltip :GALLERYCAROUSEL.getPosition
	}, d.lbutton = ROLLOVERIMG('assets/arrowleft_off', 'assets/arrowleft', {
		style$display :inlineBlock,
		onclick : function() {
			d.target = d.index - (d.numThumbs - 1)
		}
	})));
	for ( var i = 0; i < d.numThumbs; i++) {
		if (d.photos && i + d.index < d.photos.length) {
			d.ctn.append(GALLERYCAROUSEL.THUMB(d.mainImage, d.imgCaption,
					d.photos[i + d.index]));
		} else {
			d.ctn.append(TD( {
				className :'galleryImageContainer'
			}, DIV( {
				style$width :'40px',
				style$height :'40px'
			})));
		}
	}
	d.ctn.append(TD( {
		tooltipYoffset :'top',
		tooltipXoffset :-110,
		tooltip :GALLERYCAROUSEL.getPosition
	}, d.rbutton = ROLLOVERIMG('assets/arrowright_off', 'assets/arrowright', {
		style$display :inlineBlock,
		onclick : function() {
			d.target = d.index + (d.numThumbs - 1)
		}
	})));
	if (d.photos && d.photos.length > 0) {
		d.setDynamic("animate", GALLERYCAROUSEL.chaseTarget);
	}
	return d;
}

GALLERYCAROUSEL.THUMB = function(mainImage, imgCaption, photo) {
	return TD(
			{
				mainImage :mainImage,
				imgCaption :imgCaption,
				photo :photo,
				className :'galleryImageContainer',
				onclick : function() {
					GALLERYCAROUSEL.select(this.mainImage, this.imgCaption,
							this.photo);
				}
			}, IMG( {
				src :photo.localURL(),
				width :'40',
				height :'40'
			}));
}
GALLERYCAROUSEL.select = function(mainImage, imgCaption, photo) {
	mainImage.src = photo.localURL();
	tags.replace(imgCaption, IBLOCK((photo.row.caption) ? photo.row.caption
			: ''));
}
GALLERYCAROUSEL.chaseTarget = function() {
	if (this.index < this.target) {
		this.stepRight();
	}
	if (this.index > this.target) {
		this.stepLeft();
	}
}

GALLERYCAROUSEL.stepRight = function() {
	if (this.numThumbs + this.index < this.photos.length) {
		this.item(0).remove();
		this.index += 1;
		this.ctn.append(GALLERYCAROUSEL.THUMB(this.mainImage, this.imgCaption,
				this.photos[(this.numThumbs - 1) + this.index]),
				this.rbutton.parentNode);
		if (tags.isOutOfBody(this.selectedItem)) {
			this.selectedItem = null;
			// this.select(this.item(0)) ;
		}
	} else {
		this.target = this.index;
		// this.select(this.item(0)) ;
	}
};

GALLERYCAROUSEL.stepLeft = function() {
	if (this.index > 0) {
		if (this.item((this.numThumbs - 1)))
			this.item((this.numThumbs - 1)).remove();
		this.index -= 1;
		this.ctn.append(GALLERYCAROUSEL.THUMB(this.mainImage, this.imgCaption,
				this.photos[this.index]), this.lbutton.parentNode.nextSibling);
		if (tags.isOutOfBody(this.selectedItem)) {
			this.selectedItem = null;
			// this.select(this.rightMostItem()) ;
		}
	} else {
		this.target = this.index;
		// this.select(this.item(0)) ;
	}
};
GALLERYCAROUSEL.item = function(n) {
	return this.ctn.childNodes[n + 1]; // Skip the lbutton
}
GALLERYCAROUSEL.getPosition = function() {
	var c = this.parentNode.parentNode;
	if (!c.photos || !c.photos.length)
		return null;
	var end = c.index + c.numThumbs;
	if (end > c.photos.length)
		end = c.photos.length;
	return msg("carousel.position", c.index + 1, end, c.photos.length);
}
function ROLLOVERIMG(off, on, attr) {
	return LIMG(off, {
		onmouseover : function() {
			this.src = limg(on)
		},
		onmouseout : function() {
			this.src = limg(off)
		},
		inherits :attr
	});
}

