/*
# MTS Studios ltd 2010 - copyrights
#
# Display v.1 / 2010
#
# 
#
*/
var Disp;
var ctrls = false;
var ar = true;
function initDisplay( ar, ctrls )
{
if(document.getElementById('display'))
	Disp = new _Disp(document.getElementById('display'),ar,ctrls);
else
	return;
	
		var s = '';
		for(i = 0; i < Disp.getSlCont().childNodes.length; i++)
		{
			if(Disp.getSlCont().childNodes[i].id)
			{
				s = Disp.getSlCont().childNodes[i].id.match(/slide/gi);
				if(s == 'slide')
				{
					Disp.insertSlide(Disp.getSlCont().childNodes[i]);
				}
			}
			
			
			
			
		}
		
		function _Disp ( disp, ar, ctrls )
		{
			this.display = disp;
                        var autorot = ar;
                        var ctrls = ctrls;
                        var controls = new Array();
			var sl_cont = document.getElementById('slides_cont');
			var arr1 = document.getElementById('arr1');
			var arr2 = document.getElementById('arr2');
			var slides = new Array();
			var pos_map = new Array();
			var time = 5000; //ms
			var t;
                        var t_scr;
			var frame = 0;
                        var cur_frame = 0;
			var timer;
                        if(autorot)
                            timer = setTimeout(function(){rotate();}, time);

                        if(ctrls)
                            initCtrls();
			
			arr1.onclick = moveRight;
			arr2.onclick = moveLeft;
			this.display.onmouseover = showArrs;
			this.display.onmouseout = hideArrs;
			
			function showArrs()
			{
				
					arr1.style.display = 'block';
					arr2.style.display = 'block';
				
			}
			function hideArrs()
			{
				
					arr1.style.display = 'none';
					arr2.style.display = 'none';
					
			}
			
			function moveRight()
			{
				if(t)
				{
//					if(frame < pos_map.length-2)
//					{
//						clearTimeout(timer);
//						t.stop();
//						//alert(frame);
//						curL = sl_cont.offsetLeft;
//						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame-1] ,1,'px');
//						t.start();
//						t.onMotionFinished = function () {t=null}
//                                                if(controls.length != 0)
//                                                {
//                                                controls[frame-2].className = 'sel_thumb';
//                                                controls[frame-1].className = 'thumb';
//                                                document.getElementById('middle_pan').innerHTML = controls[frame-2].title;
//                                                }
//                                                if(autorot)
//						timer = setTimeout(function(){rotate();}, time);
//					}
//					else
//					{
//						clearTimeout(timer);
//						t.stop();
//
//						//frame = 0;
//						curL = sl_cont.offsetLeft;
//						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , pos_map[frame-1] ,1,'px');
//						t.start();
//						t.onMotionFinished = function () {frame++;t=null}
//                                                if(controls.length != 0)
//                                                {
//                                                controls[frame-2].className = 'sel_thumb';
//                                                controls[frame-1].className = 'thumb';
//                                                document.getElementById('middle_pan').innerHTML = controls[frame-2].title;
//                                                }
//                                                if(autorot)
//						timer = setTimeout(function(){rotate();}, time);
//					}
				}
				if(!t)
				{
					if(frame < pos_map.length-2)
					{
						
						//alert(frame);
						clearTimeout(timer);
						curL = sl_cont.offsetLeft;
						if(frame == 0)
						{
                                                    frame = pos_map.length-2;
                                                    t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame] ,1,'px');
                                                    t.start();
                                                    t.onMotionFinished = function () {t=null}
                                                    if(controls.length != 0)
                                                    {
                                                    controls[frame].className = 'sel_thumb';
                                                    cur_frame = frame;
                                                    controls[0].className = 'thumb';
                                                    document.getElementById('project_title').innerHTML = controls[frame].title;
                                                    if(controls[frame].getElementsByTagName('a')[0])
                                                    {
                                                        link = document.createElement('a');
                                                        link.href = controls[frame].getElementsByTagName('a')[0].href;
                                                        link.target = controls[frame].getElementsByTagName('a')[0].target;
                                                        link.innerHTML = controls[frame].getElementsByTagName('a')[0].innerHTML;
                                                        document.getElementById('project_launch').innerHTML = '';
                                                        document.getElementById('project_launch').appendChild(link);
                                                        document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                                    }
                                                    else
                                                        document.getElementById('project_launch').innerHTML = '';
                                                    }
						}
						else
						{
                                                    frame--;
                                                    t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame] ,1,'px');
                                                    t.start();
                                                    t.onMotionFinished = function () {t=null}
                                                    if(controls.length != 0)
                                                    {
                                                    controls[frame].className = 'sel_thumb';
                                                    cur_frame = frame;
                                                    controls[frame+1].className = 'thumb';
                                                    document.getElementById('project_title').innerHTML = controls[frame].title;
                                                    if(controls[frame].getElementsByTagName('a')[0])
                                                    {
                                                        link = document.createElement('a');
                                                        link.href = controls[frame].getElementsByTagName('a')[0].href;
                                                        link.target = controls[frame].getElementsByTagName('a')[0].target;
                                                        link.innerHTML = controls[frame].getElementsByTagName('a')[0].innerHTML;
                                                        document.getElementById('project_launch').innerHTML = '';
                                                        document.getElementById('project_launch').appendChild(link);
                                                        document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                                    }
                                                    else
                                                        document.getElementById('project_launch').innerHTML = '';
                                                    }
						}
						if(autorot)
						timer = setTimeout(function(){rotate();}, time);
					}
					else
					{
						clearTimeout(timer);
						//alert(frame);
						curL = sl_cont.offsetLeft;
						//alert(curL);
                                                frame--;
						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame] ,1,'px');
						t.start();
						t.onMotionFinished = function () {t=null}
                                                if(controls.length != 0)
                                                {
                                                controls[frame].className = 'sel_thumb';
                                                cur_frame = frame;
                                                controls[frame+1].className = 'thumb';
                                                document.getElementById('project_title').innerHTML = controls[frame].title;
                                                if(controls[frame].getElementsByTagName('a')[0])
                                                    {
                                                        link = document.createElement('a');
                                                        link.href = controls[frame].getElementsByTagName('a')[0].href;
                                                        link.target = controls[frame].getElementsByTagName('a')[0].target;
                                                        link.innerHTML = controls[frame].getElementsByTagName('a')[0].innerHTML;
                                                        document.getElementById('project_launch').innerHTML = '';
                                                        document.getElementById('project_launch').appendChild(link);
                                                        document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                                    }
                                                    else
                                                        document.getElementById('project_launch').innerHTML = '';
                                                }
                                                if(autorot)
						timer = setTimeout(function(){rotate();}, time);
					}	
				}
			}
			function moveLeft()
			{
				if(t)
				{
//					if(frame < pos_map.length-2)
//					{
//						clearTimeout(timer);
//						t.stop();
//						//alert(frame);
//						curL = sl_cont.offsetLeft;
//						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame] ,1,'px');
//						t.start();
//						t.onMotionFinished = function () {frame++;t=null}
//                                                if(controls.length != 0)
//                                                {
//                                                controls[frame].className = 'sel_thumb';
//                                                controls[frame-1].className = 'thumb';
//                                                document.getElementById('middle_pan').innerHTML = controls[frame].title;
//                                                }
//                                                if(autorot)
//						timer = setTimeout(function(){rotate();}, time);
//					}
//					else
//					{
//						clearTimeout(timer);
//						t.stop();
//						//alert(frame);
//						curL = sl_cont.offsetLeft;
//						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , pos_map[0] ,1,'px');
//						t.start();
//						t.onMotionFinished = function () {frame=1;t=null}
//                                                if(controls.length != 0)
//                                                {
//                                                controls[0].className = 'sel_thumb';
//                                                controls[frame-1].className = 'thumb';
//                                                document.getElementById('middle_pan').innerHTML = controls[0].title;
//                                                }
//                                                if(autorot)
//						timer = setTimeout(function(){rotate();}, time);
//					}
				}
				if(!t)
				{
					if(frame < pos_map.length-2)
					{
						clearTimeout(timer);
						//alert(frame);
						curL = sl_cont.offsetLeft;
                                                frame++;
						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , -pos_map[frame] ,1,'px');
						t.start();
						t.onMotionFinished = function () {t=null}
                                                if(controls.length != 0)
                                                {
                                                controls[frame-1].className = 'thumb';
                                                controls[frame].className = 'sel_thumb';
                                                cur_frame = frame;
                                                document.getElementById('project_title').innerHTML = controls[frame].title;
                                                if(controls[frame].getElementsByTagName('a')[0])
                                                    {
                                                        link = document.createElement('a');
                                                        link.href = controls[frame].getElementsByTagName('a')[0].href;
                                                        link.target = controls[frame].getElementsByTagName('a')[0].target;
                                                        link.innerHTML = controls[frame].getElementsByTagName('a')[0].innerHTML;
                                                        document.getElementById('project_launch').innerHTML = '';
                                                        document.getElementById('project_launch').appendChild(link);
                                                        document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                                    }
                                                    else
                                                        document.getElementById('project_launch').innerHTML = '';
                                                }
                                                if(autorot)
						timer = setTimeout(function(){rotate();}, time);
					}
					else
					{
						clearTimeout(timer);
						//alert(frame);
						curL = sl_cont.offsetLeft;
                                                frame = 0;
						t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, curL , pos_map[frame] ,1,'px');
						t.start();
						t.onMotionFinished = function () {t=null}
                                                if(controls.length != 0)
                                                {
                                                    controls[0].className = 'sel_thumb';
                                                    cur_frame = 0;
                                                    controls[controls.length-1].className = 'thumb';
                                                    document.getElementById('project_title').innerHTML = controls[0].title;
                                                    if(controls[0].getElementsByTagName('a')[0])
                                                    {
                                                        link = document.createElement('a');
                                                        link.href = controls[0].getElementsByTagName('a')[0].href;
                                                        link.target = controls[0].getElementsByTagName('a')[0].target;
                                                        link.innerHTML = controls[0].getElementsByTagName('a')[0].innerHTML;
                                                        document.getElementById('project_launch').innerHTML = '';
                                                        document.getElementById('project_launch').appendChild(link);
                                                        document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                                    }
                                                    else
                                                        document.getElementById('project_launch').innerHTML = '';
                                                }
                                                
                                                if(autorot)
						timer = setTimeout(function(){rotate();}, time);
					}	
				}	
			}
			
			this.insertSlide = function (obj)
			{
				if(pos_map.length > 0)
				{
					pos_map[pos_map.length] = pos_map[pos_map.length-1] + obj.offsetWidth;
					sl_cont.style.width = pos_map[pos_map.length-1]+'px';
					
				}
				else
				{
					
					pos_map[0] = 0;
					pos_map[1] = obj.offsetWidth;
					sl_cont.style.width = pos_map[1];
					
				}
				slide = new _Slide(obj);
				slides.push(slide);
				//alert(pos_map.length);
			}
                        ////-!
                        function initCtrls()
                        {
                            if(document.getElementById('d_controls'))
                            {
                                d_ctrls = document.getElementById('d_controls');
                                //alert(d_ctrls.childNodes.length);
                                for(i = 0; i < d_ctrls.childNodes.length; i++)
                                {
                                    if(d_ctrls.childNodes[i].className == 'thumb')
                                    {
                                        d_ctrls.childNodes[i].onclick = dGoTo;
                                        controls.push(d_ctrls.childNodes[i]);
                                        //alert(d_ctrls.childNodes[i].id);
                                    }
                                }
                                controls[0].className = 'sel_thumb';
                                //alert(controls.length);
                            }
                        }
                        
                        function dGoTo(e)
                        {
                            if(!e)
                                var e = window.event;
                            if (e.target) targ = e.currentTarget;
                            if (e.srcElement)
                            {
                                targ = findParent(e.srcElement,'thumb');
                                if(!targ)
                                {
                                    targ = findParent(e.srcElement,'sel_thumb');
                                }
                            }
                            //alert(targ.id);
                            
                            if(!t)
                            {
                                if(cur_frame != null)
                                {
                                    controls[cur_frame].className = 'thumb';
                                }
                                else
                                {
                                    cur_frame = 0;
                                    controls[cur_frame].className = 'thumb';
                                }
                                targ.className = 'sel_thumb';
                                id_arr = targ.id.split('_');
                                frame = parseInt(id_arr[1]);
                                //alert(id_arr[1]);
                                t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, -pos_map[cur_frame] , -pos_map[frame] ,1,'px');
                                t.start();
                                t.onMotionFinished = function () {t=null}
                                cur_frame = frame;
                                document.getElementById('project_title').innerHTML = targ.title;
                                if(targ.getElementsByTagName('a')[0])
                                {
                                    link = document.createElement('a');
                                    link.href = targ.getElementsByTagName('a')[0].href;
                                    link.target = targ.getElementsByTagName('a')[0].target;
                                    link.innerHTML = targ.getElementsByTagName('a')[0].innerHTML;
                                    document.getElementById('project_launch').innerHTML = '';
                                    document.getElementById('project_launch').appendChild(link);
                                    document.getElementById('project_launch').getElementsByTagName('a')[0].style.display = 'block';
                                }
                                else
                                    document.getElementById('project_launch').innerHTML = '';

                                if(window.pageYOffset)
                                    curScrY = window.pageYOffset
                                else
                                    curScrY = document.documentElement.scrollTop;

                                if(curScrY != 184)
                                    $('html, body').animate({scrollTop:184}, 'slow');
                                //alert(scrollY);
                            }
                            //alert(pos_map[cur_frame]+' -> '+pos_map[frame]);
                            
                            //alert(frame);
                            
                            
                            
                        }
			////-!
			this.getSlCont = function()
			{
				return sl_cont;	
			}
			
			
			function rotate()
			{
				if(frame < pos_map.length-2)
				{
                                        frame++;
					t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, -pos_map[frame-1] , -pos_map[frame] ,1,'px');
					t.start();
					t.onMotionFinished = function () {t=null}
                                        if(autorot)
					timer = setTimeout(function(){rotate();}, time);
				}
				else
				{
					frame = 0;
					t = new Tween(sl_cont.style,'left',Tween.regularEaseInOut, -pos_map[pos_map.length-2] , pos_map[frame] ,1,'px');
					t.start();
					t.onMotionFinished = function () {t=null}
                                        if(autorot)
					timer = setTimeout(function(){rotate();}, time);
				}
				
			}
			
			
			
		}
		
		function _Slide ( obj )
		{
			var slide = obj;
			this.width = slide.offsetWidth;
			
		}

                function findParent( obj, pClassName )
                {
                    var pObj = obj.parentNode;
                    
                    var count = 1;
                    var deep = 4;
                    while(pObj.className != pClassName)
                    {
                        pObj = pObj.parentNode;
                        count++;
                        if(count == deep)
                            break;
                    }
                    
                    if(pObj.className == pClassName)
                        return pObj;
                    else
                        return null;
                }
}
