var TINY={};

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

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

TINY.slideshow.prototype={
	init:function(z,s,b,f){
		
		this.f=$(z);
		
		var s=this.f.parentNode;
		var m=$$($$(s, 'ul')[0],'li'), i=0, w=0;
		this.l=m.length;
		//this.q=$(q);
		
		for(i;i<this.l;i++){
			this.a[i]={};
			var h=m[i], a=this.a[i];
			var img = $$(h,'img')[0];
			a.p=img.src;
			a.a=img.alt;
			a.c=$$(h,'span')[0];
		}

		this.img = $$(this.f, 'img')[0];
		
		this.copyPlace = s.childNodes[s.childNodes.length - 2];
		
		var divs  = $$(s, 'div');
		for (var div_id in divs) {
			var el = divs[div_id];
			var el_class = el.className;
			if (el_class == 'panel') {
				this.panel = el;
				this.img.onmouseover = el.onmouseover = new Function(this.n+'.nav(1)');
				this.img.onmouseout = el.onmouseout = new Function(this.n+'.nav(0)');
			} else if (el_class == 'controls') {
				this.controls = el;
			} else if (el_class == 'left-arrow') {
				el.onclick=new Function(this.n+'.mv(-1,1)');
			} else if (el_class == 'right-arrow') {
				el.onclick=new Function(this.n+'.mv(1,1)');
			} else if (el_class == 'play') {
				this.play = el;
				el.onclick=new Function(this.n+'.is("z",0)');
			} else if (el_class == 'stop') {
				this.stop = el;
				el.onclick=new Function(this.n+'.is("z",1)');
			}
		}
		
		var spans = $$(s, 'span');
		for (var span_id in spans) {
			if (spans[span_id].className != 'current')
				continue;
			this.frame = spans[span_id];
			break;
		}
		
		this.auto?this.is(0,0):this.is(0,1);
	},
	nav:function(d){
		//if (d != 0) {
		//	TINY.alpha.set(this.controls, 100, this.imgSpeed);
		//} else {
		this.controls.style.opacity = d;
		this.controls.style.filter='alpha(opacity='+d*100+')';
		//}
	},
	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)
		}
		
		if (c==0) {
			this.stop.style.display='block';
			this.play.style.display='none';
		} else {
			this.stop.style.display='none';
			this.play.style.display='block';
			clearTimeout(this.at);
		}

		if (isNaN(s)) { // we only change play/pause status
			if (!c)
				this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed * 1000)
			return;
		}
		
		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=$$(this.p,'img'), l=a.length, x=0;
			for(x;x<l;x++){
				a[x].style.borderColor=x!=s?'':this.active
			}
		}
	},
	le:function(s,c){
		var m=$$(this.f, 'img')[0];
		
		this.copyPlace.innerHTML = this.a[s].c.innerHTML;
		
		this.frame.innerHTML = s + 1;
		
		this.f.style.backgroundImage = 'url('+m.src+')';
		
		m.style.opacity=0;
		m.style.filter='alpha(opacity=0)';
		m.src = this.i.src;
		
		var title = this.a[s].a;
		m.alt = title;
		m.title = title;
		this.panel.title = title;
		
		TINY.alpha.set(m, 100, this.imgSpeed);
		
		//this.lt=setTimeout(n,this.imgSpeed*100);
		if(!c){
			// play at start
			this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed * 1000)
		}
	}
};

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)},20)
		},
		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=typeof e=='object'?e:$(e); var oh=e.offsetHeight, ho=e.style.height||TINY.style.val(e,'height');
			ho=oh-parseInt(ho); 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=e.offsetHeight-ho;
			if(oh==h){clearInterval(e.si)}else{if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px'}}
		}
	}
}();

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)}}}();
