var TINY={};

var dontScroll = false;
var lastSlide = -1;


function $(i){return document.getElementById(i)}
function $$(e,p){p=p||document; return p.getElementsByTagName(e)}

function gotoPage(p)
{
		window.location=p;
}

function setCursor(o, setVal)
{
	if (setVal == true)
		o.style.cursor='pointer'
	else
		o.style.cursor='normal'
}

TINY.slideshow=function(n){
	this.infoSpeed=this.imgSpeed=this.speed=10;
	this.thumbOpacity=this.navHover=70;
	this.scrollSpeed=3;
	this.n=n;
	this.c=2;
	this.firstRun = true;
	this.resumed = true;
	this.slides=[];			//array of information on each slide
	this.thumbWidth = 100;
};

TINY.slideshow.prototype={
	init:function(s,z,q){
		s=$(s);
		var m=$$('li',s), i=0, w=0;		//array of our slides
		this.l=m.length;				//number of slides 
		this.q=$(q);					//large image link placeholder
		this.f=$(z);					//large image placeholder

		this.o=parseInt(TINY.style.val(z,'width'));
		if(this.sliderArea){
			this.sliderArea=$(this.sliderArea)

			//this.sliderArea.onmouseover = new Function('TINY.slideControl.pause("'+this+'", this.c)');
			//this.sliderArea.onmouseout = new Function('TINY.slideControl.resume("'+this+'")');
			this.sliderArea.onmouseover = this.pauseSlideControl; //new Function(this+'.pauseSlideControl()');
			this.sliderArea.onmouseout = this.resumeSlideControl; //new Function(this+'.resumeSlideControl()');


		}

		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=0;i<this.l;i++){
				this.slides[i]={};
				var h=m[i];
				var a=this.slides[i];
				
				a.t=$$('h3',h)[0].innerHTML;			//title
				//a.d=$$('p',h)[0].innerHTML;
				a.thumb=$$('a',h)[0]?$$('a',h)[0].href:'';  //thumbnail
				a.full=$$('span',h)[0].innerHTML;			//full image
				
				if(this.thumbs){
					var g=$$('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+')';
					//this.sliderArea.onmouseover=new Function('setCursor(this, true)');
					//this.sliderArea.onmouseout=new Function('setCursor(this, false)');
					//g.onmouseout=new Function('TINY.alpha.set(this,'+this.thumbOpacity+',5)');
					//g.style.border='1px solid red';
					//alert(g.id);
					//this.sliderArea.onclick=new Function('alert(\'a\');') //new Function(this.n+'.pr('+i+',1)')
					g.onclick=new Function('gotoPage("' + a.thumb + '")'); //new Function(this.n+'.pr('+i+',1)')
				}   
			}
		
		this.auto ? this.is(2,0) : this.is(2,1);
	},
	

	
	mv:function(d , manual){
		if (!dontScroll) {
			lastSlide = this.c;
			
			var t=this.c+d;

		
			this.c=  t  = t < 0 ? this.l-1 :   t > this.l-1 ? 0 : t;
//alert("move - act position "  + this.c + " manual is " + manual);

			 this.pr(t , manual)
		} else {
			clearTimeout(this.at);
		}
	},
	
	mv2:function(d , manual){
		if (!dontScroll) {
			var t=d;

		
			this.c=  t  = t < 0 ? this.l-1 :   t > this.l-1 ? 0 : t;
//alert("move - act position "  + this.c + " manual is " + manual);

			 this.pr(t , manual)
		} else {
			clearTimeout(this.at);
		}
	},
	
	pr:function(t, manual){

//		clearTimeout(this.lt);

		
		if(manual || dontScroll){
			clearTimeout(this.at)
		}

		if (!dontScroll) {
			this.c = t;
			//alert("pr - act position "  + this.c + " manual is " + manual);
			this.is(t, manual)
		}
	},
	
	
	
	is:function(s, manual){
		if (!dontScroll) {
	
			var i=new Image();
			i.style.opacity=0;
			i.style.filter='alpha(opacity=0)';
			i.style.cursor='pointer';
			//alert(s + "," + manual);
			this.i=i;

			this.i.onload=new Function(this.n + '.le('+s+','+manual+')');
			this.i.onclick=new Function('gotoPage("' + this.slides[s].thumb + '")'); //new Function(this.n+'.pr('+i+',1)')
			this.i.src=this.slides[s].full;
	
			//this.ft=setTimeout(new Function(this.n+'.le('+s+','+manual+')'),500)
			
			//alert('2');
			if(this.thumbs){
				if (!this.firstRun) {
	
					TINY.scroll.init(this.thumbs,1,this.scrollSpeed)
	
	
					
					
				}
				this.firstRun = false;
				
			}
		}
	},
	le:function(s, manual){
		
		if (!dontScroll) {

			if (this.i) {
				//alert("le");
				
				this.f.appendChild(this.i);
				
				//alert("le(" + s + "," + manual + ")");
		
				TINY.alpha.set(this.i,100,this.imgSpeed);
				
		//		var n=new Function(this.n+'.nf('+s+')');
		//		this.lt=setTimeout(n,this.imgSpeed*100);
		
				clearTimeout(this.at);
				if(!manual && !dontScroll){
					this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed*1000)
						
				}
				//if(this.slides[s].thumb!=''){
				//	this.q.onclick=new Function('window.location="'+this.slides[s].thumb+'"');
		//			this.q.onmouseover=new Function('this.className="'+this.link+'"');
		//			this.q.onmouseout=new Function('this.className=""');
				//	this.q.style.cursor='pointer'
				//}else{
				//	this.q.onclick=this.q.onmouseover=null;
				//	this.q.style.cursor='default'
				//}
		
				var m=$$('img',this.f);
				//alert(m.length);
				if(m.length>1){
					//alert("remove");
					this.f.removeChild(m[0]);
					//alert("removed");
				}
			}
		} else {
				this.i = null;
				
		}
//alert('end of le');
	} ,
	
	pauseSlideControl:function() {
		//alert('pause');
		clearTimeout(this.at);
		clearInterval(this.ai);
		clearInterval(this.ft);
		dontScroll = true;
		resumed = false;
		if (this.c != lastSlide) {
			var m=$$('img',slideshow.f);
			//alert(m.length);
			if(m.length>1){
				//alert("remove");
				slideshow.f.removeChild(m[m.length -1]);
				//alert("removed");
			}
			this.c = lastSlide
		}

	},
	
	resumeSlideControl:function() {
		if (!resumed) {
			dontScroll = false;
			resumed = true;
			this.c = lastSlide;
//  		alert('last slide : ' + slideshow.c);
			this.at=setTimeout(new Function('slideshow.mv(1,0)'),500)  //to prevent sliding when moving between thumbs

			
		}
	}
	
	//,
//	nf:function(s){
//		if(this.info && 1 == 0){
//			s=this.slides[s];
//			$$('h3',this.r)[0].innerHTML=s.t;
//			$$('p',this.r)[0].innerHTML=s.d;
//			this.r.style.height='auto';
//			var h=parseInt(this.r.offsetHeight);
//			this.r.style.height=0;
//			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;
			var l = 100; // d==1 ? parseInt(e.offsetWidth)-parseInt(e.parentNode.offsetWidth):0;

			clearInterval(e.si);
			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(Math.abs(c)==Math.abs(l)){
				clearInterval(e.si);
				//TINY.scroll.cl(e)
				
				var fc = e.firstChild
				e.removeChild(e.firstChild)
				e.style.left=0+'px'
				e.appendChild(fc)
			}else{
					var i=Math.abs(l+c); 
					i=i<s?i:s; 
					var n=c-i*d; 
					//alert(n);
					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.slideControl=function() {
			return {
					pause:function(e, c) {
						//alert('pause');
						clearTimeout(e.at);
						clearInterval(e.ai);
						clearInterval(e.ft);
						dontScroll = true;
						resumed = false;
						
					},
					resume:function(e) {
						if (!resumed) {
							dontScroll = false
							resumed = true
							e.at=setTimeout(new Function('slideshow.mv(1,0)'),1000)  //to prevent sliding when moving between thumbs

							
						}
					}
			}
	}();


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)}}}();