var TINY = TINY || {};

function $$(i){return document.getElementById(i)}
function $$$(e,p){p=p||document; return p.getElementsByTagName(e)}

TINY.medialist=function(n){
	this.infoSpeed=this.imgSpeed=this.speed=10;
	this.thumbOpacity=this.navHover=70;
	this.navOpacity=25;
	this.scrollSpeed=5;
	this.letterbox='#fff';
	this.textState='on';
	this.allowHide=false;
	this.textToggle=null;
	this.autoCenterThumb=true;
	this.embedwidth;
	this.embedheight;
	this.n=n;
	this.c=0;
	this.a=[];
};

TINY.medialist.prototype={
	init:function(s,z,b,f,embedwidth,embedheight){
		s=$$(s);
		var m=this.children(s), i=0, w=0;
		this.l=m.length;
		this.f=$$(z);
		this.r=$$(this.info);
		this.embedwidth = embedwidth;
		this.embedheight = embedheight;
		this.o=parseInt(TINY.style.val(z,'width'));
		
		if(this.allowHide)
		{
			this.textToggle = document.createElement('a');
			this.textToggle.onclick=new Function(this.n+'.setTextState()')
			this.textToggle.style.display = "none";
			if(this.textState=='off')
			{
				this.textToggle.style.display = "block";
			}
			var showText = document.createTextNode('+');
			this.textToggle.appendChild(showText);
			this.textToggle.id="showInfoToggle";
			this.f.parentNode.appendChild(this.textToggle);
		}
		else
		{
			this.textState='on';
		}
		
		if(this.thumbs){
			var u=$$(this.left), r=$$(this.right);
			u.onmouseover=new Function(this.n + '.autoCenterThumb=false; TINY.scroll.init("'+this.thumbs+'",-1,'+this.scrollSpeed+')');
			u.onmouseout=r.onmouseout=new Function('TINY.scroll.cl("'+this.thumbs+'")');
			r.onmouseover=new Function(this.n + '.autoCenterThumb=false; 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];
			
			var subItems = this.children(h);
			a.d=subItems[1].innerHTML;
			
			a.p=subItems[0]?subItems[0].href:'';
			
			if(this.thumbslist){
				this.p=$$(this.thumbslist);
				//var g=subItems[3];
				var thumbContOut = document.createElement('div');
				thumbContOut.className = "thumbContainerOuter";
				thumbContOut.id = "thumbContainerOuter_" + i;
				var thumbCont = document.createElement('div');
				var thumbContR = document.createElement('div');
				thumbCont.className = "thumbContainer";
				thumbContR.className = "thumbContainerRight";
				
				var s=this.a[i];
				
				thumbContR.innerHTML = s.d;
				
				thumbCont.appendChild(thumbContR);
				thumbContOut.appendChild(thumbCont);
				this.p.appendChild(thumbContOut);
				
				
				thumbCont.style.opacity=this.thumbOpacity/100;
				thumbCont.style.filter='alpha(opacity='+this.thumbOpacity+')';
				thumbCont.onmouseover=new Function('TINY.alpha.set(this,100,5)');
				thumbCont.onmouseout=new Function('if(!this.activeThumb) {TINY.alpha.set(this,'+this.thumbOpacity+',5)}');
				thumbCont.onclick=new Function(this.n+'.autoCenterThumb=true;'+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+',5)');
			b.onmouseout=f.onmouseout=new Function('TINY.alpha.set(this,'+this.navOpacity+',5)');
			b.onclick=new Function(this.n+'.autoCenterThumb=true;'+this.n+'.mv(-1,1)');
			f.onclick=new Function(this.n+'.autoCenterThumb=true;'+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){
		
		//this.i=i;
		
		var so = new SWFObject(this.a[s].p, "vidflash", this.embedwidth, this.embedheight, "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.addParam("allowFullScreen", "true");
		so.write(this.f);

		if(this.thumbslist){
			//var a=$$$('img',this.p), l=a.length, x=0;
			var a=this.children(this.p), l=a.length, x=0;
			for(x;x<l;x++){
				//a[x].style.borderColor=x!=s?'':this.active
				if(x==s)
				{
					a[x].className = "thumbContainerOuterActive";
					a[x].activeThumb = true;
					TINY.alpha.set(a[x],100,5);
					//this.p.scrollTo("#" + a[x].id);
					
				}
				else
				{
					a[x].className = "thumbContainerOuter";
					TINY.alpha.set(a[x],this.thumbOpacity,5);
					a[x].activeThumb = false;
				}
			}
		}
	},
	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)
		}
		
		var m=$$$('img',this.f);
		if(m.length>2){
			this.f.removeChild(m[0])
		}
	},
	nf:function(s){
		
	},
	setTextState:function() {
			if(this.textState == 'on')
			{
				TINY.height.set(this.r,1,this.infoSpeed/2,-1,new Function(this.n+'.textToggle.style.display = "block";'));
				this.textState='off';
			}
			else
			{
				this.textToggle.style.display = "none";
				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,null);
				this.textState='on';
			}
			
	},
	sibling:function( n, elem ) {
			var r = [];
	
			for ( ; n; n = n.nextSibling ) {
				if ( n.nodeType === 1 && n !== elem ) {
					r.push( n );
				}
			}
	
			return r;
		},	
	children:function( elem ) {
			return this.sibling( elem.firstChild );
	},
	center:function(e){
		if(this.autoCenterThumb)
		{
			var ppw = parseInt(e.parentNode.parentNode.offsetWidth);
			var ppc = parseInt(e.parentNode.parentNode.offsetWidth)/2;
			var slidew = parseInt(e.parentNode.offsetWidth);
			var inloc = e.offsetLeft;
			var ec = parseInt(e.offsetWidth)/2;
			var imgc = ppc - ec - inloc;
			if(inloc > ppc && (inloc + e.offsetWidth) < (slidew - ppc))
			{
				e.parentNode.style.left=imgc+'px';
			}
			else if(inloc < ppc)  /*At beginning*/
			{
				e.parentNode.style.left='0px';
			}
			else{ /*At the end*/
				e.parentNode.style.left=ppw - slidew+'px';
			}
		}
	}
};

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,func){
			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); 
			//document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + '<p>' + e.style.height + ' , ' + e.offsetHeight + ' , ho:' + ho + ' , oh:' + oh + ' , h:' + h + ' , hd:' + hd + ' , s:' + s + '<\/p>';
			
			if(hd == 1)
			{
				oh =0;
				ho =0;
				e.style.visibility = "hidden";
			}
			//alert(e.style.height);
			//e.style.height = '10px';
			//document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + '<p>' + e.style.height + ' , ' + e.offsetHeight + ' , ho:' + ho + ' , oh:' + oh + ' , h:' + h + ' , hd:' + hd + ' , s:' + s + '<\/p>';
			if(h>1 || hd != 1){
			e.si=setInterval(function(){TINY.height.tw(e,h,ho,hd,s,func)},20)
			}
			
		},
		tw:function(e,h,ho,hd,s,func){
			var oh=e.offsetHeight-ho;
			
			if(hd == 1 && e.offsetHeight == h)
			{
				oh =0;
				ho =0;
			}
			
			var calc = oh+(Math.ceil(Math.abs(h-oh)/s)*hd);
			//document.body.insertAdjacentHTML('beforeEnd', '<p>' + e.style.height + ' , ' + e.offsetHeight + ' , ho:' + ho + ' , oh:' + oh + ' , h:' + h + ' , hd:' + hd + ' , s:' + s + ' , calc:' + calc + '<\/p>');
			//document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + '<p>' + e.style.height + ' , ' + e.offsetHeight + ' , ho:' + ho + ' , oh:' + oh + ' , h:' + h + ' , hd:' + hd + ' , s:' + s + ' , calc:' + calc + '<\/p>';
			if(oh+1==h || oh==h || e.style.height==""){clearInterval(e.si); if(func != null){func();}}else{if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px';e.style.visibility = "visible";}}
		}
	}
}();
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.alphatext=function(){
	return{
		set:function(e,s,content,target){
			a=0;
			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.alphatext.tw(e,a,d,s,content, target)},20)
		},
		tw:function(e,a,d,s,content, target){
			var o=Math.round(e.style.opacity*100);
			if(o==a){target.innerHTML=content; clearInterval(e.ai); TINY.alpha.set(e,100,s);}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)}}}();

