var gallery = function(opt) {
  var that = {num_files: 0};

  opt.thumb_width = opt.thumb_width || 70;
  opt.thumb_margin = opt.thumb_margin || 11;

  that.zoom = function(url) {
    ZoomIt($('fullsize_' + opt.gallery_name), url);
  };

  that.show_files = function(r) {
    var gallery_list = $('slideshow_' + opt.gallery_name);
    while (gallery_list.hasChildNodes()) { gallery_list.removeChild(gallery_list.firstChild); }
    var files = $A(r.responseXML.getElementsByTagName('file'));
    that.num_files = files.length;
    if (files.length == 0) {
      that.empty_gallery();
    } else {
      files.sort(function(a,b) {
        try {
          return parseInt(a.attributes.getNamedItem('seq_no').value) - parseInt(b.attributes.getNamedItem('seq_no').value);
        } catch(e) {
          return -1;
        };
      });
      files.each(function(f) {
        var filename = (f.hasChildNodes() ? f.firstChild.nodeValue : '');
        if (filename != '') {
          var thumb = '/images/gallery/' + opt.gallery_name + '/_thumbs/' + filename;
          var preserved = '/images/gallery/' + opt.gallery_name + '/_preserved/' + filename;
          filename = '/images/gallery/' + opt.gallery_name + '/' + filename;
          var title = f.attributes.getNamedItem('title').value;
          gallery_list.insert('<li><h3>' + title + '</h3><span onclick="gallery_' + opt.gallery_name + '.zoom(\'' + preserved + '\')">' + filename + '</span><p></p><img src="' + thumb + '" alt="" /></li>');
        };
      });
      $('slider_' + opt.gallery_name).width = that.num_files * (opt.thumb_width + opt.thumb_margin);
    };
  };

  that.empty_gallery = function() {
      if (opt.readonly == true) {
        $('wrapper_' + opt.gallery_name).hide();
      } else {
        var gallery_list = $('slideshow_' + opt.gallery_name);
        gallery_list.insert('<li><h3>' + '' + '</h3><span>/common/js/gallery/images/empty.jpg</span><p></p>' + 
          '<img src="/common/js/gallery/images/thumb_empty.jpg" alt="" /></li>');
      }
  };

  that.get_files = function() {
    new Ajax.Request("/images/gallery/" + opt.gallery_name + "/manifest.xml?r=" +  Math.round(Math.random()*10000), {
      method: 'get',
      asynchronous: false,
      onCreate: function() { },
      onSuccess: that.show_files,
      onFailure: function(){ that.empty_gallery(); }
    });
  };

  return that;
};

//-------------------------------------------------------------------
//derived from: http://www.leigeber.com/2008/12/javascript-slideshow/

var TINY={};

//function $(i){return document.getElementById(i)}
function $$tag(e,p){p=p||document; return p.getElementsByTagName(e)}

TINY.slideshow=function(n){
	this.infoSpeed=this.imgSpeed=this.speed=10;
	this.thumbOpacity=this.navHover=70;
	this.navOpacity=25;
	this.scrollSpeed=5;
	this.letterbox='#000';
	this.n=n;
	this.c=0;
	this.a=[]
};

TINY.slideshow.prototype={
	init:function(s,z,b,f,q){
		s=$(s);
		var m=$$tag('li',s), i=0, w=0;
		this.l=m.length;
		this.q=$(q);
		this.f=$(z);
		this.r=$(this.info);
		this.o=parseInt(TINY.style.val(z,'width'));
		if(this.thumbs){
			var u=$(this.left), r=$(this.right);
			u.onmouseover=new Function('TINY.scroll.init("'+this.thumbs+'",-1,'+this.scrollSpeed+')');
			u.onmouseout=r.onmouseout=new Function('TINY.scroll.cl("'+this.thumbs+'")');
			r.onmouseover=new Function('TINY.scroll.init("'+this.thumbs+'",1,'+this.scrollSpeed+')');
			this.p=$(this.thumbs)
		}
		for(i;i<this.l;i++){
			this.a[i]={};
			var h=m[i], a=this.a[i];
			a.t=$$tag('h3',h)[0].innerHTML;
			a.d=$$tag('p',h)[0].innerHTML;
//			a.l=$$tag('a',h)[0]?$$tag('a',h)[0].href:'';
			a.c=$$tag('span',h)[0]?$$tag('span',h)[0].onclick:null;
			a.p=$$tag('span',h)[0].innerHTML;
			if(this.thumbs){
				var g=$$tag('img',h)[0];
				this.p.appendChild(g);
				w+=parseInt(g.offsetWidth);
				if(i!=this.l-1){
					g.style.marginRight=this.spacing+'px';
					w+=this.spacing
				}
				this.p.style.width=w+'px';
				g.style.opacity=this.thumbOpacity/100;
				g.style.filter='alpha(opacity='+this.thumbOpacity+')';
				g.onmouseover=new Function('TINY.alpha.set(this,100,5)');
				g.onmouseout=new Function('TINY.alpha.set(this,'+this.thumbOpacity+',5)');
				g.onclick=new Function(this.n+'.pr('+i+',1)')
			}
		}
		if(b&&f){
			b=$(b);
			f=$(f);
			b.style.opacity=f.style.opacity=this.navOpacity/100;
			b.style.filter=f.style.filter='alpha(opacity='+this.navOpacity+')';
			b.onmouseover=f.onmouseover=new Function('TINY.alpha.set(this,'+this.navHover+',18)');
			b.onmouseout=f.onmouseout=new Function('TINY.alpha.set(this,'+this.navOpacity+',18)');
			b.onclick=new Function(this.n+'.mv(-1,1)');
			f.onclick=new Function(this.n+'.mv(1,1)')
		}
		this.auto?this.is(0,0):this.is(0,1)
	},
	mv:function(d,c){
		var t=this.c+d;
		this.c=t=t<0?this.l-1:t>this.l-1?0:t;
		this.pr(t,c)
	},
	pr:function(t,c){
		clearTimeout(this.lt);
		if(c){
			clearTimeout(this.at)
		}
		this.c=t;
		this.is(t,c)
	},
	is:function(s,c){
		if(this.info){
			TINY.height.set(this.r,1,this.infoSpeed/2,-1)
		}
		var i=new Image();
		i.style.opacity=0;
		i.style.filter='alpha(opacity=0)';
		this.i=i;
		i.onload=new Function(this.n+'.le('+s+','+c+')');
		i.src=this.a[s].p;
		if(this.thumbs){
			var a=$$tag('img',this.p), l=a.length, x=0;
			for(x;x<l;x++){
				a[x].style.borderColor=x!=s?'':this.active
			}
		}
	},
	le:function(s,c){
		this.f.appendChild(this.i);
		var w=this.o-parseInt(this.i.offsetWidth);
		if(w>0){
			var l=Math.floor(w/2);
			this.i.style.borderLeft=l+'px solid '+this.letterbox;
			this.i.style.borderRight=(w-l)+'px solid '+this.letterbox
		}
		TINY.alpha.set(this.i,100,this.imgSpeed);
		var n=new Function(this.n+'.nf('+s+')');
		this.lt=setTimeout(n,this.imgSpeed*100);
		if(!c){
			this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed*1000)
		}
		if(this.a[s].c!=null){
			this.q.onclick=this.a[s].c;
			this.q.onmouseover=new Function('this.className="gallery_imglink gallery_linkhover"');
			this.q.onmouseout=new Function('this.className="gallery_imglink gallery_linkexists"');
			//on ie, need to "show" this so add a class and style it
			this.q.className = 'gallery_imglink gallery_linkexists';
			this.q.style.cursor='pointer'
		}else{
			this.q.onclick=this.q.onmouseover=this.q.onmouseout=null;
			this.q.className = 'gallery_imglink';
			this.q.style.cursor='default'
		}
		var m=$$tag('img',this.f);
		if(m.length>2){
			this.f.removeChild(m[0])
		}
	},
	nf:function(s){
		if(this.info){
			s=this.a[s];
			$$tag('h3',this.r)[0].innerHTML=s.t;
			$$tag('p',this.r)[0].innerHTML=s.d;
			this.r.style.height='auto';
			var h=parseInt(this.r.offsetHeight);
			this.r.style.height='0px';
			this.r.style.display='none';
			if ((s.d+s.t) == '') {
				h = 0;
			} else TINY.height.set(this.r,h,this.infoSpeed,0);
		}
	}
};

TINY.scroll=function(){
	return{
		init:function(e,d,s){
			e=typeof e=='object'?e:$(e); var p=e.style.left||TINY.style.val(e,'left'); e.style.left=p;
			var l=d==1?parseInt(e.offsetWidth)-parseInt(e.parentNode.offsetWidth):0; e.si=setInterval(function(){TINY.scroll.mv(e,l,d,s)},10)
		},
		mv:function(e,l,d,s){
			var c=parseInt(e.style.left); if(c==l){TINY.scroll.cl(e)}else{var i=Math.abs(l+c); i=i<s?i:s; var n=c-i*d; e.style.left=n+'px'}
		},
		cl:function(e){e=typeof e=='object'?e:$(e); clearInterval(e.si)}
	}
}();

TINY.height=function(){
	return{
		set:function(e,h,s,d){
			e=$(e);
			var oh=(parseInt(e.style.height)||0);
			var ho=oh - (parseInt(e.style.height||TINY.style.val(e,'height'))||0);
			var hd=oh-ho>h?-1:1; clearInterval(e.si); e.si=setInterval(function(){TINY.height.tw(e,h,ho,hd,s)},20)
		},
		
		tw:function(e,h,ho,hd,s){
			var oh=(parseInt(e.style.height)||0)-ho;
			if(oh==h) {
				clearInterval(e.si);
			}else{
				try {
					e.style.height = oh+(Math.ceil(Math.abs(h-oh)/s)*hd) + 'px';
				} catch(ex) {
					//hack for IE quirks mode
					e.style.height = '0px';
					clearInterval(e.si);
				};
				e.style.display = (parseInt(e.style.height) == 0) ? 'none' : 'block';
			}
		}
	}
}();

TINY.alpha=function(){
	return{
		set:function(e,a,s){
			e=typeof e=='object'?e:$(e); var o=e.style.opacity||TINY.style.val(e,'opacity'),
			d=a>o*100?1:-1; e.style.opacity=o; clearInterval(e.ai); e.ai=setInterval(function(){TINY.alpha.tw(e,a,d,s)},20)
		},
		tw:function(e,a,d,s){
			var o=Math.round(e.style.opacity*100);
			if(o==a){clearInterval(e.ai)}else{var n=o+Math.ceil(Math.abs(a-o)/s)*d; e.style.opacity=n/100; e.style.filter='alpha(opacity='+n+')'}
		}
	}
}();

TINY.style=function(){return{val:function(e,p){e=typeof e=='object'?e:$(e); return e.currentStyle?e.currentStyle[p]:document.defaultView.getComputedStyle(e,null).getPropertyValue(p)}}}();
