var TINY=new Array();
var timeoutArray = new Array();
var intervalArray = new Array();

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

TINY.slideshow=function(n){
	//console.log("init variable");
	this.infoSpeed=this.imgSpeed=this.speed=10;
	this.thumbOpacity=this.navHover=30;
	this.maxThumbNum=6;
	this.navOpacity=25;
	this.scrollSpeed=5;
	this.letterbox='#DFD';
	this.n=n;	
	this.c=0;
	this.a=[];
};

TINY.slideshow.prototype={
	init:function(s,z,b,f,q){		// init("slideshow","image","imgprev","imgnext","imglink")	
		//console.log("init function");
		s=jsGet(s);				//slideshow
		var m=jsGetWithTag('li',s), i=0, w=0;
		this.l=m.length;		//list length
		this.q=jsGet(q);		//imglink
		this.z=jsGet(z);		//image
		this.r=jsGet(this.info);
		this.o=parseInt(TINY.style.val(z,'width'));
		
		// thumb slide left and right buttons
		if(this.thumbs){
			var u=jsGet(this.left), r=jsGet(this.right);
			if (jsGetWithTag('img', jsGet('slideshow')).length > this.maxThumbNum) {
				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 + ')');
				u.style.display = 'block';
				r.style.display = 'block';
				var sa = jsGet('slidearea');
				sa.style.marginLeft = '5px';
				sa.style.width = '456px';
			}
			else {
				/*u.style.display = 'none';
				r.style.display = 'none';
				var sa = jsGet('slidearea');
				sa.style.marginLeft = '0px';
				sa.style.width = '512px';*/				
			}
			this.p=jsGet(this.thumbs)
		}
		
		// for each item in the list
		for(i; i<this.l; i++){
			this.a[i]={};
			var h=m[i], a=this.a[i];
			a.t=jsGetWithTag('h3',h)[0].innerHTML;
			a.d=jsGetWithTag('p',h)[0].innerHTML;
			a.l=jsGetWithTag('a',h)[0]?jsGetWithTag('a',h)[0].href:'';
			a.p=jsGetWithTag('span',h)[0].innerHTML;			
			if(this.thumbs){
				var g=jsGetWithTag('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;
					//alert(this.spacing);
				}
				this.p.style.width=w+2+'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)');
			}
		}
		// imgprev, imgnext
		if(b&&f){
			this.b=jsGet(b);
			this.f=jsGet(f);
			this.b.style.opacity=this.f.style.opacity=this.navOpacity/100;
			this.b.style.filter=this.f.style.filter='alpha(opacity='+this.navOpacity+')';
			this.b.onmouseover=this.f.onmouseover=new Function('TINY.alpha.set(this,'+this.navHover+',5)');
			this.b.onmouseout=this.f.onmouseout=new Function('TINY.alpha.set(this,'+this.navOpacity+',5)');
			this.b.onclick=new Function(this.n+'.mv(-1,1)');
			this.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){		// index of thumbs, 1
		clearTimeout(this.lt);
		if(c){
			clearTimeout(this.at)
		}
		this.c=t;
		this.is(t,c)
	},
	is:function(s,c){		// index of thumbs, 1	the "c" parameter is for auto
		if(this.info){
			
				TINY.height.set(this.r,1,this.infoSpeed/2,-1)
			
		}
		
		var type_array = this.a[s].p.split(".");		
		var type = type_array[type_array.length-1];				
		var source = this.a[s].p;		
		//alert("source: "+source);
		
		switch(type) {
			case "jpg":
			case "jpeg":
			case "gif":
			case "bmp":						
				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=source;
				
				//console.log(i.src);
				break;
			case "swf":
			case "swf?aId=":
				/*var i = new SWFObject(source, "movie", "400", "300", "9", "#FF6600");	
				if(s!=0)
					i.addParam("wmode", "transparent");
				i.write(this.z.id);*/
				//console.log("this.z.id: %s", this.z.id);
				var obj = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="100%" height="100%">';
				obj += '<param name="movie" value="'+source+'">';
				obj += '<param name="quality" value="high">';
				if(this.a[s].l=='http://www.myccfamily.com/2009/playFlash'){
					// flash enable
					obj += '<embed src="'+source+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="100%" height="100%"></embed>';
				} else {
					obj += '<param name="wmode" value="transparent">';
					obj += '<embed wmode="transparent" src="'+source+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="100%" height="100%"></embed>';
				}
				obj += '</object>';
				jsGet(this.z.id).innerHTML = obj;
				
				/*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
				}
				
				/*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);
				if(s!=0){
					var n=new Function(this.n+'.nf('+s+')');
					this.lt=setTimeout(n,this.imgSpeed*100);
					timeoutArray.push(this.lt);
				}
				
				// Timeout for Next Slide
				if(!c){
					this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed*1000);
					timeoutArray.push(this.at);
				}
							
				// Setting imgLink Event
				if(this.a[s].l!=''){
					this.q.onclick=new Function('window.location="'+this.a[s].l+'"');
					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'
				}
							
				break;
		}
		
		if(this.thumbs){		// set border of thumb active
			var a=jsGetWithTag('img',this.p), l=a.length, x=0;
			for(x;x<l;x++){
				//a[x].style.borderColor=x!=s?this.active:'';
				a[x].onmouseover = x!=s ? new Function('TINY.alpha.set(this,100,5)') : '';
				a[x].onmouseout = x!=s ? new Function('TINY.alpha.set(this,'+this.thumbOpacity+',5)') : '';
				a[x].style.opacity = x!=s ? this.thumbOpacity/100 : 1;
				a[x].style.filter = x!=s ? 'alpha(opacity = '+this.thumbOpacity+')' : 'alpha(opacity = 100)'; 
			}
		}
	},
	le:function(s,c){
		// remove all img
		var cell = this.z;
		if (cell.hasChildNodes()) {
			while (cell.childNodes.length >= 1) {
				cell.removeChild(cell.firstChild);
			}
		}

		this.z.appendChild(this.i);		// image tag append img
	
		// alert("this.o: "+this.o+" this.i.offsetWidth: "+this.i.offsetWidth);
		/*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);
		timeoutArray.push(this.lt);
		if(!c){
			this.at=setTimeout(new Function(this.n+'.mv(1,0)'),this.speed*1000)
			timeoutArray.push(this.at);
		}
		
		/*if(this.a[s].l!=''){
			this.q.onclick=new Function('window.location="'+this.a[s].l+'"');
			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'
		}*/
		
		// Setting imgLink Event
		if(this.a[s].l!=''){
			this.q.onclick=new Function('window.location="'+this.a[s].l+'"');
			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=jsGetWithTag('img',this.z);
		if(m.length>2){
			this.z.removeChild(m[0])
		}*/
	},
	nf:function(s){
		if(this.info){
			s=this.a[s];
			jsGetWithTag('h3',this.r)[0].innerHTML=s.t;
			jsGetWithTag('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){		// this.thumbs,  -1, this.scrollSpeed
			e=typeof e=='object'?e:jsGet(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); intervalArray.push(e.si);
		},
		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:jsGet(e); clearInterval(e.si)}
	}
}();

TINY.height=function(){			
	return{
		set:function(e,h,s,d){		// this.info, 1, this.infoSpeed/2, -1)
			e=typeof e=='object'?e:jsGet(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); intervalArray.push(e.si);
		},
		tw:function(e,h,ho,hd,s){
			var oh=e.offsetHeight-ho;
			if(oh==h){
				clearInterval(e.si);
			}else{
				if(oh!=h){		
					var eh = oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px';
					//e.style.height=eh;
					$("#information").height(eh);					
				}
			}
			//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:jsGet(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); intervalArray.push(e.ai);
		},
		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:jsGet(e); return e.currentStyle?e.currentStyle[p]:document.defaultView.getComputedStyle(e,null).getPropertyValue(p)}}}();

function tvBoxClearAllTimeout() {
	for (var i=0; i<timeoutArray.length; i++) {
		if (timeoutArray[i])
			clearTimeout(timeoutArray[i]);
	}
}
function tvBoxClearAllInterval() {
	for (var i=0; i<intervalArray.length; i++) {
		if (intervalArray[i])
			clearInterval(intervalArray[i]);
	}	
}
