/* Constants
*/
var UNDEF='undefined';
var C = {
	THMB:'#thumbs',
	THMBC:'#thumbcnt',
	THMC:'#thumbcnt>ul.thmb',
	SCRL:'#scroll-left',
	SCRR:'#scroll-right',
	CLOS:'#close',
	IMGS:'#images',
	HEAD:'#header',
	FOOT:'#footer',
	UPBTN:'#up',
	PRVBTN:'#prev',
	NXTBTN:'#next'
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,'');
};
String.prototype.cleanupHTML = function() {
	return this.replace(/<br>/g,'\n').replace(/\&amp;/g,'&').replace(/\&lt;/g,'<').replace(/\&gt;/g,'>').replace(/\&(m|n)dash;/g,'-');
};
String.prototype.appendSep = function(s,sep) {
	if(typeof sep=='undefined') sep=' &middot; ';
	return (this.length?(this+sep):'')+s;
};

/* Easing by George Smith
*/

jQuery.extend(jQuery.easing, {
	easeOutBack: function (x, t, b, c, d, s) {
		if(s==undefined) s=1.70158;
		return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b;
	}
});

var Imgs = {
	div:null,
	curr:-1,
	width:0,
	height:0,
	left:0,
	ul:null,
	anim:0,
	init:function(){
		Imgs.ul=$('#images>.cont>ul');
		if(images.length>0){
			l=Imgs.getImg(0);
			Imgs.curr=0;
			Imgs.width=l.outerWidth()+parseInt(l.css('margin-right'));
			Imgs.height=l.height();
			Imgs.left=parseInt(Imgs.ul.css('left'));
			Imgs.setImg(1,0);
			Imgs.setImg(2,1);
			Imgs.addCaption(1,0);
			Imgs.preload(2);
			$('#images #left').click(Imgs.prev).bind('dragstart',function(){return false;});
			$('#images #right').click(Imgs.next).bind('dragstart',function(){return false;});
			Thumbs.setactive();
		}
	},
	preload:function(n){
		if(n<(images.length)&&n>=0){
			var img=new Image();
			img.src=images[n].file;
		}	
	},
	addCaption:function(pos,n){
		if(Imgs.curr<0) return;
		var t=images[n].title.trim();
		var c=images[n].comment.trim();
		var m=(typeof images[n].meta==UNDEF)?'':images[n].meta.trim();
		if(t.length||c.length||m.length){
			Imgs.getImg(pos).append('<div class="caption">'+
			((t.length)?('<h3>'+t+'</h3>'):'')+((c.length)?('<p>'+c+'</p>'):'')+((m.length)?('<p class="meta">'+m+'</p>'):'')+'</div>');
			Imgs.ul.find('.caption').fadeIn(1000);
		}
	},
	next:function(){
		Imgs.goto((Imgs.curr+1)%images.length);
	},
	prev:function(){
		Imgs.goto((Imgs.curr==0)?(images.length-1):Imgs.curr-1);
	},
	getImg:function(pos){
		return Imgs.ul.find('li').eq(pos);
	},
	setImg:function(pos,n){
		if(n<0||n>=images.length) return;
		var l=Imgs.getImg(pos);
		l.empty().append('<img src="'+images[n].file+'" alt="'+images[n].file+'" />');
		l.find('img').css({marginTop:Math.round((Imgs.height-images[n].height)*4/5+0.5)});
		if(images[n].link.length)
			l.find('img').addClass('original').click(function(){
				window.open(images[n].link,'Original','width=840,height=600,scrollbars=yes,resizable=yes,menubar=no,toolbar=no,directories=no,status=no,copyhistory=no');});
	},
	stopRight:function(){
		Imgs.getImg(3).find('.caption').stop(true,false);
		Imgs.getImg(3).remove();
		Imgs.anim=0;
	},
	moveRight:function(n){
		Imgs.ul.prepend('<li>&nbsp;</li>').css({left:Imgs.left-Imgs.width});;
		if(n<(Imgs.curr-1)){Imgs.setImg(1,n);Imgs.setImg(2,n+1);}
		Imgs.setImg(0,n-1);
		Imgs.addCaption(1,n);
		Imgs.ul.animate({left:Imgs.left+'px'},1000,'easeOutBack',Imgs.stopRight);
		Imgs.preload(n-2);
	},		
	stopLeft:function(){
		Imgs.getImg(0).find('.caption').stop(true,false);
		Imgs.getImg(0).remove();
		Imgs.ul.css({left:Imgs.left});
		Imgs.anim=0;
	},
	moveLeft:function(n){
		Imgs.ul.append('<li>&nbsp;</li>');
		if(n>(Imgs.curr+1)){Imgs.setImg(2,n);Imgs.setImg(1,n-1);}
		Imgs.setImg(3,n+1);
		Imgs.addCaption(2,n);
		//alert(Imgs.ul.css('left')+'\n'+(Imgs.left-Imgs.width));
		Imgs.ul.animate({left:(Imgs.left-Imgs.width)+'px'},1000,'easeOutBack',Imgs.stopLeft);
		Imgs.preload(n+2);
	},
	stopMove:function(){
		if(Imgs.anim==0) return;
		Imgs.ul.stop(true,false);
		if(Imgs.anim<0) Imgs.stopRight();
		else Imgs.stopLeft();
	},
	goto:function(n){
		if(n==Imgs.curr) return;
		Imgs.ul.find('.caption').stop(true,false).fadeOut(250,function(){$(this).remove();});
		Imgs.stopMove();
		Imgs.anim=n-Imgs.curr;
		if(Imgs.anim<0) Imgs.moveRight(n);
		else Imgs.moveLeft(n);
		Imgs.curr=n;
		Thumbs.setactive();
	}	
};

var Thumbs = {
	div:null,
	thc:null,
	thm:null,
	cnt:0,
	tw:0,
	th:0,
	lw:0,
	init:function(){
		this.load();
		this.lw=this.thm.find('li:first').outerWidth();
		this.cnt=this.thm.find('li').length;
		this.tw=this.cnt*this.lw;
		this.thm.find('li.image>a').each(function(i){
			Hints.add($(this),'<span>'+(i+1)+'</span> '+(images[i].title.length?('<b>'+images[i].title+'</b> '):'')+(images[i].comment.length?'<small>'+images[i].comment+'</small>':''));
			$(this).click(function(){Imgs.goto(i);return false});
		});
		this.thm.find('li.folder,li.folderi').each(function(i){
			Hints.add($(this),'<b>'+folders[i].name+'</b>'+'<small>'+folders[i].desc+'<br />images:'+folders[i].imgs+'<br/>last mod:'+folders[i].lastmod+'</small>');
		});
		$(C.SCRR).click(function() {
			Thumbs.scrollright();
			return false;
		});
		$(C.SCRL).click(function() {
			Thumbs.scrollleft();
			return false;
		});
	},
	scrollleft:function(){
		var sw=Thumbs.thc.width();
		if(sw<Thumbs.tw)
			Thumbs.thm.stop(true,false).animate({left:Math.min(parseInt(Thumbs.thm.css('left'))+sw,0)+'px'},2000,'easeOutBack');
	},
	scrollright:function(){
		var sw=Thumbs.thc.width();
		if(sw<Thumbs.tw)
			Thumbs.thm.stop(true,false).animate({left:Math.max(parseInt(Thumbs.thm.css('left'))-sw,sw-Thumbs.tw)+'px'},2000,'easeOutBack');
	},
	setactive:function(){
		var n=images[Imgs.curr].rel;
		var sw=Thumbs.thc.width();
		var lw=n*Thumbs.lw;
		var lo=-parseInt(Thumbs.thm.css('left'));
		var rt=((n<(Thumbs.cnt-1))?2:1)*Thumbs.lw;
		if((lw-lo)>(sw-rt))
			Thumbs.thm.stop(true,false).animate({left:Math.max(-lw+sw-rt,sw-Thumbs.tw)+'px'},1000,'easeOutBack');
		else if((lw-lo)<Thumbs.lw) 
			Thumbs.thm.stop(true,false).animate({left:Math.min(-lw+Thumbs.lw,0)+'px'},1000,'easeOutBack');
		this.thm.find('li.active').each(function(){$(this).removeClass('active');});
		$(this.thm.find('li').get(n)).addClass('active');
	},
	load:function(){
		var st=Thumbs.thc.find('li.image').each(function(i){
			$(this).css({backgroundImage:('url('+escape(images[i].thumb)+')')});
		});
	}
};

var Hints = {
	init:function(){
		$('a.showhint,div.showhint,ul.showhint>li').each(function() {Hints.add($(this));});
	},
	add:function(to,txt){
		if(typeof txt==UNDEF) {
			txt=to.attr('title');
			to.removeAttr('title');
		}
		if(txt.length){
			to.data('hint',txt).hover(function(){
				$('body').append('<p id="hint">'+to.data('hint')+'</p>');
				var h=$('#hint');
				var o=to.offset();
				var t=o.top-h.outerHeight()-10;
				var l=Math.round(o.left+(to.outerWidth()-h.outerWidth())/2);
				if(t<0){
					t=o.top+to.outerHeight()+10;
					if((t+to.outerHeight())>$(window).height())
						t=$(window).height()-h.outerHeight();
				}
				if((l+h.outerWidth())>$(window).width())
					if((l=$(window).width()-h.outerWidth())<0) l=0;
				h.css({left:l,top:t}).fadeIn();
			},function(){$('#hint').remove()});
		}
	}
};

var Help = {
	init:function(){
		$('.showhelp').click(Help.show);
		$('#help .close a').click(Help.hide);
	},
	show:function(){
		$('#help').fadeIn(500);
	},
	hide:function(){
		if($('#help').is(':visible'))
			$('#help').fadeOut(500);
	}
};

$(document).ready(function(){
	if(userName.length){
		$('#breadcrumb').prepend('<a href="'+homePath+userName+'/" title="Back to '+userName+'\'s homepage" class="user showhint">'+userName+'</a><span>&nbsp;</span>');
	}
	Hints.init();
	Help.init();

	Thumbs.div=$(C.THMB);
	Thumbs.thm=$(C.THMC);
	Thumbs.thc=$(C.THMBC);
	Imgs.div=$(C.IMGS);
	
	if(images.length){
		Imgs.init();
		Thumbs.init();
	}
	$(document).keydown(function(e){
		if(typeof _jaWidgetFocus!=UNDEF&&_jaWidgetFocus) return true;
		var k=e?e.keyCode:window.event.keyCode; //alert(k);
  		switch(k) {
  			case 38: window.location=(typeof parentIndexPage=='undefined' || !parentIndexPage.length)?'../':parentIndexPage; break;
			case 39: Imgs.next(); break;
			case 37: Imgs.prev(); break;
			case 112: Help.show(); break;
			case 27: Help.hide(); break;
			default: return true;
		}
		return false;
	});
});
