var TVProgram = {

	MOVE_SETKA : 'move_setka',
	MOVE_MENU : 'move_menu',

    UP : 1,
    DOWN : 2,
    LEFT : 3,
    RIGHT : 4,

    TIME : 0,
    CHANNELS : 1,
    GENRES : 2,

    view : null,

    weekN : 1,
    groupLength : 6,

    height : 7*4,
    blockWidth : 150,
    clientX : 0,
    clientY : 0,
    y : 0, //block number 0..Settings.weeksAvailable*7*24/Settings.blocksCount
    now : 0,
    minY: 0,

    leftScrollTimes : ['6:00', '12:00', '18:00', '0:00'],

    tvPrograms : [],
    tvProgramContainer : null,
    tvCaptions : null,
    tvCaptionsContainer : null,
    
    oldDays: 0,

    initBlocks : function(blockContainer, from){
        for (var blocks=blockContainer.childNodes, j=0, i=0, ilen=blocks.length; i<ilen; i++){        	
            var block = blocks[i]
            if (block.nodeType != 1) {
                i--
                ilen--
                block.parentNode.removeChild(block)
                continue
            }
            if (from + 1) {
                this.view.blocks[from + '' + j] = {}
                this.view.blocks[from + '' + j]['html'] = block
                var label = block.getElementsByTagName('label')[0]
                block.removeChild(label)
                this.view.blocks[from + '' + j++]['label'] = label.firstChild.nodeValue
            }
            else this.view.captions[j++] = block
        }        
    },

    init : function() {

		this.addListener = Mediator.addListener
        this.tvProgramContainer = $('tvProgramContainer')
        this.tvCaptions = $('tvCaptions')
        this.tvCaptionsContainer = $('tvCaptionsContainer')

        BottomMenu.initBottomMenus()
        Anons.init()

        this.view = channelsView
        channelMenu.view = this.view
        channelMenu.init()
        this.view.menu = channelMenu
        this.view.init()

        genresView.menu = genreMenu
        genreMenu.view = genresView
        genresView.init()
        genreMenu.init()
        
        this.height = Settings.weeksAvailable*7*Settings.blocksCount

        for (var lis=this.tvProgramContainer.childNodes, i=0, j=0; i<lis.length; i++){
            if (lis[i].nodeType == 1 && lis[i].tagName.toLowerCase() == 'li') {
                this.tvPrograms[j++] = lis[i].getElementsByTagName('ul')[0]
                lis[i].style.MozUserSelect = 'none'
            }
        }

        this.tvProgramContainer.removeChild(this.tvPrograms[2].parentNode)
        this.tvProgramContainer.insertBefore(this.tvPrograms[2].parentNode, this.tvProgramContainer.firstChild)
        this.tvPrograms.unshift(this.tvPrograms.pop())

        this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight

        if (this.tvPrograms[1].scrollHeight > 0) $('leftScrollThumb').parentNode.style.visibility = 'visible'
        
        this.weekN = Settings.week
        this.y = Settings.block
        this.now = this.y
        this.minY = 0; // this.now - (this.now % 4); // Changed By MORO
        setInterval(function(){
            TVProgram.now++
        }, 3600*1000*24/Settings.blocksCount)

        this.setupButtons()
        this.setupLeftScrollTime()

        if (this.y > this.minY) this.initBlocks(this.tvPrograms[0], this.y - 1)
        this.initBlocks(this.tvPrograms[1], this.y)
        if (this.y + 1 < this.height) {
            this.initBlocks(this.tvPrograms[2], this.y + 1)
        }

        this.initBlocks(this.tvCaptions)
        this.blockWidth = this.view.captions[0].offsetWidth
        
        this.scroll = new ScrollBar($('leftScrollThumb').parentNode, 58)
        
        var f = this.scroll.onmousemove
        this.scroll.onmousemove = function(e, o) {
        	f(e, o)
        	Mediator.notify(ScrollBar.MOVE_SCROLL, o)
    	}
        
		this.setkaScroll = new ScrollBar(this.tvPrograms[1])
		
		this.setkaScroll.move = function(offset){
			var st = this.scrollY + offset
			var ms = this.maxScrollY - this.scrollHeight
        	this.scroll.scrollTop = st > ms ? ms : st
		}
		
		this.setkaScroll.calcMax()
		f = this.setkaScroll.onmousemove
        this.setkaScroll.onmousemove = function(e, o) {
        	f(e, o)
        	Mediator.notify(TVProgram.MOVE_SETKA, o)
    	}
    	
    	this.toggleScroll()
/* ======================================================== */    	
    	this.menuScroll = new ScrollBar($('bottomMenus1'))
    	this.menuScroll.move = function(offset){
			var st = this.scrollY + offset
			var ms = this.maxScrollY - this.scrollHeight
        	this.scroll.scrollTop = st > ms ? ms : st
		}
		
    	this.menuScroll.addListener(BottomMenu.MENU_SHOW, {exec : function(eventSource, params){
    		this.observer.maxScrollY = this.observer.scroll.getElementsByTagName('table')[0].offsetHeight
    	}})
    	
    	f = this.menuScroll.onmousemove
        this.menuScroll.onmousemove = function(e, o) {
        	//$('print').value += 'MENU MOVE \r'
        	f(e, o)
        	Mediator.notify(TVProgram.MOVE_MENU, o)
    	}
    	
    	this.menuScroll.f = this.menuScroll.startMove
    	this.menuScroll.startMove = function(event, dir){
    	    TVProgram.menuScroll.f(event, dir)
    	    var bac = BottomMenu.bacCGP
    	    BottomMenu.changeChannelGroup = BottomMenu.bacCGP
    	    setTimeout(function() {
                BottomMenu.changeChannelGroup = function(){}
            }, 500)
    	}
    	
    	this.scroll.addListener(TVProgram.MOVE_MENU, {exec : function(eventSource, params){
			var mult
			with (eventSource) mult = scroll.scrollTop/(maxScrollY - scroll.offsetHeight)
			this.observer.scroll.scrollTop = this.observer.scrollHeight*(1 - mult)
		}})
/* ======================================================== */    	
    	this.setkaScroll.addListener(ScrollBar.MOVE_SCROLL, {exec : function(eventSource, params){
			var mult = eventSource.scroll.scrollTop/eventSource.scrollHeight
			with (this.observer.scroll){
				var scrollLength = this.observer.maxScrollY - offsetHeight
				scrollTop = scrollLength*(1 - mult)
			}
		}})
		
		this.scroll.addListener(TVProgram.MOVE_SETKA, {exec : function(eventSource, params){
			var mult
			with (eventSource) mult = scroll.scrollTop/(maxScrollY - scroll.offsetHeight)
			this.observer.scroll.scrollTop = this.observer.scrollHeight*(1 - mult)
		}})
		
		this.scroll.addListener(View.CONTENT_CHANGED, {exec : function(eventSource, params){
			if (params && params.block) {
            	var leftBorder = TVProgram.tvPrograms[1].scrollLeft
            	var rightBorder = leftBorder + TVProgram.groupLength * TVProgram.blockWidth
            	if (!params.block.parentNode || params.block.parentNode != TVProgram.tvPrograms[1]) return
        	}
        	
        	TVProgram.setkaScroll.scroll = TVProgram.tvPrograms[1]
			TVProgram.setkaScroll.calcMax()
			var sh = TVProgram.toggleScroll()
			var mult
			with (TVProgram.setkaScroll) {
				var ms = maxScrollY - scrollHeight
				
				if (scroll.scrollTop > ms) scroll.scrollTop = ms
				if (sh) return
				mult = scroll.scrollTop/(maxScrollY - scroll.offsetHeight)
			}
			this.observer.scroll.scrollTop = this.observer.scrollHeight*(1 - mult)
		}})
		
		//this.menuScroll = new ScrollBar($('bottomMenus1'))
		
		this.scroll.addListener(BottomMenu.MENU_SHOW, {exec : function(eventSource, params){
			//$('print').value += 'show\r'
			this.observer.setkaPos = this.observer.scroll.scrollTop
			this.observer.scroll.scrollTop = this.observer.menuPos
			TVProgram.setkaScroll.scroll = $('bottomMenus1')
			TVProgram.setkaScroll.setkaMaxScrollY = TVProgram.setkaScroll.maxScrollY
			TVProgram.setkaScroll.maxScrollY = TVProgram.setkaScroll.scroll.scrollHeight
			TVProgram.toggleScroll()
		}})
		
		this.scroll.addListener(BottomMenu.MENU_HIDE, {exec : function(eventSource, params){
			this.observer.menuPos = this.observer.scroll.scrollTop
			this.observer.scroll.scrollTop = this.observer.setkaPos
			TVProgram.setkaScroll.scroll = TVProgram.tvPrograms[1]
			TVProgram.setkaScroll.menuMaxScrollY = TVProgram.setkaScroll.maxScrollY
			TVProgram.setkaScroll.maxScrollY = TVProgram.setkaScroll.setkaMaxScrollY
			TVProgram.toggleScroll()
		}})
	
		var localBlockN = this.y % Settings.blocksCount
		for (var i=0, divs=$('dayTimeSet').childNodes, ilen=divs.length, count=0; i<ilen; i++){
		    if (divs[i].nodeType==1) {
				if (count == localBlockN) {
				    divs[i].getElementsByTagName('div')[0].className = 'active'
				    break
				}
				count++
		    }
        }
        
        var lsDays = $('2_List').getElementsByTagName('li');
        for (var i = 0; i < lsDays.length; i++) {
        	if (lsDays[i].className.indexOf('before') != -1) {
        		TVProgram.oldDays++;
        	}
        }

        var SetkaEndMoveListener = {
            exec : function(eventSource, params){
                TVProgram.tvProgramContainer.scrollLeft = (TVProgram.view.x > TVProgram.groupLength ? TVProgram.groupLength : TVProgram.view.x) * TVProgram.blockWidth
                //TVProgram.tvProgramContainer.scrollTop = TVProgram.tvPrograms[0].offsetHeight
                //if (TVProgram.tvPrograms[1].scrollTop > TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight)
                //    TVProgram.tvPrograms[1].scrollTop = TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight
//                var thumb = $('leftScrollThumb')
//                var propY = thumb.offsetTop/(thumb.parentNode.offsetHeight - thumb.offsetHeight - 2)

//                TVProgram.tvPrograms[1].scrollTop = (TVProgram.maxScrollY - TVProgram.tvProgramContainer.offsetHeight)*propY
            }
        }

        var CaptionEndMoveListener = {
            exec : function(eventSource, params){
                TVProgram.tvCaptionsContainer.scrollLeft = (TVProgram.view.x > TVProgram.groupLength ? TVProgram.groupLength : TVProgram.view.x) * TVProgram.blockWidth
            }
        }

        var SetkaMoveListener = {
            thumb : $('leftScrollThumb'),
            exec : function(eventSource, params) {
                //var scrollLeft = TVProgram.tvProgramContainer.scrollLeft - params.clientX + TVProgram.clientX
                var minScrollLengthX = TVProgram.view.currBlocksRendered * TVProgram.blockWidth - TVProgram.tvProgramContainer.offsetWidth
                //TVProgram.tvProgramContainer.scrollLeft = (scrollLeft > minScrollLengthX) ? minScrollLengthX : scrollLeft
                //TVProgram.tvProgramContainer.scrollTop -= params.clientY - TVProgram.clientY

//                if (TVProgram.tvPrograms[1].scrollTop - params.clientY + TVProgram.clientY > TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight)
//                    TVProgram.tvPrograms[1].scrollTop = TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight
//                else TVProgram.tvPrograms[1].scrollTop -= params.clientY - TVProgram.clientY
                //if (TVProgram.tvPrograms[1].scrollTop - params.diffY > TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight)
                //    TVProgram.tvPrograms[1].scrollTop = TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight
                //else TVProgram.tvPrograms[1].scrollTop -= params.diffY

                var diff = TVProgram.tvPrograms[1].scrollTop/(TVProgram.maxScrollY - TVProgram.tvPrograms[1].offsetHeight)
                this.thumb.style.top = diff*(this.thumb.parentNode.offsetHeight - this.thumb.offsetHeight)+ 'px'
            }
        }

        var CaptionMoveListener = {
            exec : function(eventSource, params) {
                var scrollLeft = TVProgram.tvCaptionsContainer.scrollLeft - params.clientX + TVProgram.clientX
                var minScrollLengthX = TVProgram.view.currBlocksRendered * TVProgram.blockWidth - TVProgram.tvCaptionsContainer.offsetWidth
                TVProgram.tvCaptionsContainer.scrollLeft = (scrollLeft > minScrollLengthX) ? minScrollLengthX : scrollLeft
            }
        }

        //Mediator.addListener(Mediator.MOVE_SETKA, CaptionMoveListener)

        this.addListener(Mediator.MOVE_SETKA, SetkaMoveListener)

        this.addListener(Mediator.END_MOVE_SETKA, SetkaEndMoveListener)
        this.addListener(Mediator.END_MOVE_SETKA, CaptionEndMoveListener)
        
        //this.setupDay()
		
		$('tvLoaderBlock').parentNode.removeChild($('tvLoaderBlock'))

    },

    setupDayPart : function(button, dayPart){
		var parent = button.parentNode;
		if (parent.className == 'active') return;
		var res = this.moveTo(this.view.x, this.y - (this.y % Settings.blocksCount) + dayPart)

        if (res) {
            this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight
	    	$('leftScrollThumb').style.top = 0
    		this.tvPrograms[1].scrollTop = 0
        	this.setupLeftScrollTime()
	    	for (var i=0, divs=$('dayTimeSet').childNodes, ilen=divs.length; i<ilen; i++){
				if (divs[i].nodeType==1) {
			    	var button = divs[i].getElementsByTagName('div')[0]
			    	if (button.className == 'active') {
			        	button.className = ''
					break
			    	}
				}
	    	}
	    	parent.className = 'active'
            Mediator.notify(Mediator.END_MOVE_SETKA, this)
            Mediator.notify(View.CONTENT_CHANGED)
            this.setupButtons()
            this.setupDay()
        }
    },

    setupButtons : function(){
    	var v = this.view
    	
        var blb = $('bottomLeftButton'), brb = $('bottomRightButton')
        
        /* //stariy variant podmeni stiley
        if (v.x == 0){
            if (blb.className.indexOf('_disabled') == -1) blb.className = blb.className + '_disabled'
        }
        else blb.className = blb.className.replace('_disabled', '')

        if (v.x + this.groupLength > v.width - 1){
            if (brb.className.indexOf('_disabled') == -1) brb.className = brb.className + '_disabled'
        }
        else brb.className = brb.className.replace('_disabled', '')
        */
        
        if (v.x == 0){
            if (blb.className.indexOf('disabled') == -1) blb.className = 'disabled'
        }
        else blb.className = 'enabled'

        if (v.x + this.groupLength > v.width - 1){
            if (brb.className.indexOf('disabled') == -1) brb.className = 'disabled'
        }
        else brb.className = 'enabled'

        var lub = $('leftUpButton'), ldb = $('leftDownButton')
        if (this.y <= this.minY){
            if (lub.className.indexOf('_disabled') == -1) {
                lub.className = lub.className + '_disabled'
            }
        } else {
            lub.className = lub.className.replace('_disabled', '')
        }

        if (this.y >= this.height - 1){
            if (ldb.className.indexOf('_disabled') == -1) {
                ldb.className = ldb.className + '_disabled'
            }
        } else {
            ldb.className = ldb.className.replace('_disabled', '')
        }
        
        //nowButton
        var nowBtn = $('nowButton_2');
        if (this.y == this.now) {
        	if (nowBtn.className.indexOf('active') == -1) {
        		nowBtn.className = 'active';
        	} 
        } else {
        	nowBtn.className = '';
        }
    },

    setupDay : function() {
    	var sel = $('weekDays')
    	var weeks = Settings.weeksAvailable * 7;
    	var index = Math.floor(this.y / Settings.blocksCount) % weeks - Math.floor(this.now / Settings.blocksCount) % weeks
    	sel.selectedIndex = index

    	//$('2_Text').firstChild.innerHTML = $('2_List').getElementsByTagName('li')[index + TVProgram.oldDays].firstChild.innerHTML исходная строка, изменил Флерко
    	
    	$('2_Text').firstChild.innerHTML = $('2_List').getElementsByTagName('li')[index + Settings.day -1].firstChild.innerHTML
    },

    setNow : function() {
        var res = this.moveTo(this.view.x, this.now)
        this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight
        //$('leftScrollThumb').style.top = 0
        this.tvPrograms[1].scrollTop = 0
        this.setupLeftScrollTime()
        if (res) {
			for (var i=0, divs=$('dayTimeSet').childNodes, ilen=divs.length, count=0; i<ilen; i++) {
				if (divs[i].nodeType==1) {
					if (count == this.now%Settings.blocksCount) {
			    		divs[i].getElementsByTagName('div')[0].className = 'active'
					} else divs[i].getElementsByTagName('div')[0].className = ''
					count++
		    	}
	        }
	
	        Mediator.notify(Mediator.END_MOVE_SETKA, this)
	        Mediator.notify(View.CONTENT_CHANGED)
			this.setupButtons()
			this.setupDay()
        }
    },

    scrollPage : function(direction){
        this.view.oldX = this.view.x
        var res = false
        switch (direction){
            case TVProgram.LEFT : {
                res = this.moveTo(this.view.x - this.groupLength, this.y)
                break
            }
            case TVProgram.RIGHT : {
                res = this.moveTo(this.view.x + this.groupLength, this.y)
                break
            }
            case TVProgram.UP : {
                res = this.moveTo(this.view.x, this.y - 1)
                this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight
                $('leftScrollThumb').style.top = 0
                this.tvPrograms[1].scrollTop = 0
                this.setupLeftScrollTime()
                break
            }
            case TVProgram.DOWN : {
                res = this.moveTo(this.view.x, this.y + 1)
                this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight
                $('leftScrollThumb').style.top = 0
                this.tvPrograms[1].scrollTop = 0
                this.setupLeftScrollTime()
                break
            }
        }

        if (res) {
            Mediator.notify(Mediator.END_MOVE_SETKA, this)
            Mediator.notify(View.CONTENT_CHANGED)
            this.setupButtons()
            this.setupDay()
        }
    },

    moveTo : function(x, y){
        var yDirection = 0
        var shift = 0
        var dirY = 0
        var moveHappend = false

        if (y < this.minY || y > this.height - 1) return false

        if (y != this.y) {
            shift = Math.abs(this.y - y)
            if (y > this.y) dirY = 1
            else if (this.y > y) dirY = -1
            shift = (shift > 3) ? 3 : shift
			this.oldY = this.y
            for (var i = 0; i<shift; i++){
                if (dirY > 0){
                    var p = this.tvPrograms.shift()
                    clear(p)
                    var parent = p.parentNode.parentNode
                    parent.removeChild(p.parentNode)
                    parent.appendChild(p.parentNode)
                    this.tvPrograms.push(p)
                } else if (dirY < 0){
                    var p = this.tvPrograms.pop()
                    clear(p)
                    var parent = p.parentNode.parentNode
                    parent.removeChild(p.parentNode)
                    parent.insertBefore(p.parentNode, parent.firstChild)
                    this.tvPrograms.unshift(p)
                }
            }

            this.y = y
        }

        if (x != this.view.x){
            var diff = Math.abs(x - this.view.x)
            var from = 0
            var count = 0

            if (x > this.view.x) {
                var offset = (this.view.x + this.groupLength + diff > this.view.width) ? this.view.width - this.groupLength - this.view.x : diff

                var leftDelsCount = offset + (this.view.x < this.groupLength ? this.view.x : this.groupLength) - this.groupLength
                leftDelsCount = (leftDelsCount < 0) ? 0 : (leftDelsCount > 3 * this.groupLength) ? 3 * this.groupLength : leftDelsCount
//alert(leftDelsCount)
                for (var i = 0; i < leftDelsCount; i++) {
                    for (var j = 0, jlen = 3 - shift; j < jlen; j++) {
                        var ind = j
                        ind = (dirY < 0) ? 2 - j : ind
						if (this.tvPrograms[ind].firstChild) this.tvPrograms[ind].removeChild(this.tvPrograms[ind].firstChild)
//                        this.tvPrograms[ind].removeChild(this.tvPrograms[ind].firstChild)
                    }

                    if (this.tvCaptions.firstChild) this.tvCaptions.removeChild(this.tvCaptions.firstChild)
                    this.view.currBlocksRendered--
                }

                if (offset < 3 * this.groupLength) {
                    from = this.view.x + 2 * this.groupLength
                    count = offset
                } else {
                    from = this.view.x + offset - this.groupLength
                    count = 3 * this.groupLength
                }

                moveHappend = offset
                this.view.x += offset
            } else {
                var offset = (this.view.x - diff < 0) ? this.view.x : diff

                var cacheX = this.view.width - this.view.x - this.groupLength
                cacheX = (cacheX > this.groupLength) ? this.groupLength : cacheX

                var rightDelsCount = offset + cacheX - this.groupLength
                rightDelsCount = (rightDelsCount < 0) ? 0 : (rightDelsCount > 3 * this.groupLength) ? 3 * this.groupLength : rightDelsCount


                for (var i = this.tvPrograms[1].childNodes.length, ilen = i - rightDelsCount; i > ilen; i--) {
                    for (var j = 0, jlen = 3 - shift; j < jlen; j++) {
                        var ind = j
                        ind = (dirY < 0) ? 2 - j : ind
						if (this.tvPrograms[ind].lastChild) this.tvPrograms[ind].removeChild(this.tvPrograms[ind].lastChild)
//                        this.tvPrograms[ind].removeChild(this.tvPrograms[ind].lastChild)
                    }

                    if (this.tvCaptions.lastChild) this.tvCaptions.removeChild(this.tvCaptions.lastChild)
                    this.view.currBlocksRendered--
                }

                if (offset < 3 * this.groupLength) {
                    from = this.view.x - offset - this.groupLength
                    count = offset
                } else {
                    from = this.view.x - offset - this.groupLength
                    count = 3 * this.groupLength
                }

                moveHappend = offset
                this.view.x -= offset
            }
            for (var i = 0, ilen = 3 - shift; from + count > 0 && i < ilen; i++) {
                var ind = (dirY < 0) ? i : i - 1
                this.view.loadBlocks(from, count, ind)
            }
        }
        moveHappend = moveHappend || shift
        if (shift) this.view.oldX = -this.groupLength
        for (var i = 0, ilen = shift; this.y >= 0 && this.y < this.height && i < ilen; i++) {
            var ind = (dirY > 0) ? 1 - i : i - 1

            this.view.loadBlocks(this.view.x - this.groupLength, 3 * this.groupLength, ind)
        }
        if (shift) this.view.oldX = this.view.x
		this.oldY = this.y
//this.weekN = Math.floor(((this.y+1)/4 - 1)/7) + Settings.week
//alert((Math.floor(Math.floor(this.y/4)/7)) +'; '+Settings.week)
		this.weekN = Settings.week + Math.floor(Math.floor(this.y/4)/7)
        return moveHappend
    },

    setupLeftScrollTime : function(){
        var localBlockN = this.y % Settings.blocksCount
        $('leftScrollTimeUp').innerHTML = this.leftScrollTimes[localBlockN]
        $('leftScrollTimeBottom').innerHTML = this.leftScrollTimes[(localBlockN == 3) ? 0 : localBlockN + 1]
    },

    changeDay : function(select){
        select = select || $('weekDays')
        //var index = select.selectedIndex + Math.floor(TVProgram.now / Settings.blocksCount) % 7 // Changed By Moro
        var index = select.selectedIndex*Settings.blocksCount + this.y % Settings.blocksCount;
        
//        alert(index + " :: " + this.y + " :: " + select.selectedIndex + " :: " + Settings.blocksCount + " :: " + TVProgram.now);
//LEO this.weekN = Math.floor(index/7) + Settings.week

        // this.moveTo(this.view.x, index * Settings.blocksCount + this.y % Settings.blocksCount) // Changed by Moro
        this.moveTo(this.view.x, index);

        this.tvProgramContainer.scrollTop = this.tvPrograms[0].offsetHeight
        $('leftScrollThumb').style.top = 0
        this.tvPrograms[1].scrollTop = 0
        this.setupLeftScrollTime()
        this.setupButtons()

        Mediator.notify(Mediator.END_MOVE_SETKA, this)
        Mediator.notify(View.CONTENT_CHANGED)
    },

    changeView : function(type){
        if (this.view.type == type) return
        
        /*
       	divList[0].className += ' on'
        $('button1').className = $('button1').className.replace(' on', '')
        */
            
        /* buttons switch */
        var viewBtnsBlock = document.getElementById("changeviewcontainerID")
		var divList = viewBtnsBlock.getElementsByTagName("div")		
			
        function switchBtns() {        	
			divList[0].getElementsByTagName("div")[0].className = "";
			divList[2].getElementsByTagName("div")[0].className = "";				
        }
        /* END buttons switch */
        
        
        this.tvPrograms[1].scrollTop = 0
        var bcgb = $('bottomChannelsGroupButton')
        switch (type){
            case TVProgram.CHANNELS : {
	        $('chanSelect').style.visibility = 'visible'
                this.view = channelsView
                bcgb.className = bcgb.className.replace('_disabled', '')                
                switchBtns();                
                divList[0].getElementsByTagName("div")[0].className = "active"
                break
            }
            case TVProgram.GENRES : {
                $('chanSelect').style.visibility = 'hidden'
                this.view = genresView
                bcgb.className = bcgb.className += '_disabled'
                switchBtns();                
                divList[2].getElementsByTagName("div")[0].className = "active"
            }
        }
        /*
        if (type == TVProgram.CHANNELS && channelsView.userSetIndex != $('channelSets').selectedIndex){
            channelsView.changeSet()
            this.setupButtons()
            return
        }
        */
        if (type == TVProgram.GENRES && genresView.channelSetN != channelsView.columnSetN) genresView.changeSet()
        
        this.setupButtons()
        clear(this.tvCaptions)
        for (var i=0; i<3; clear(TVProgram.tvPrograms[i++]));

        if (this.y != this.minY) this.view.loadBlocks(this.view.x - this.groupLength, this.groupLength*3, -1)
        this.view.loadBlocks(this.view.x - this.groupLength, this.groupLength*3, 0)
        if (this.y < this.height - 1) this.view.loadBlocks(this.view.x - this.groupLength, this.groupLength*3, 1)
        Mediator.notify(Mediator.END_MOVE_SETKA, this)
        Mediator.notify(View.CONTENT_CHANGED)
    },
    
    toggleScroll : function(){
    	//$('print').value = this.setkaScroll.maxScrollY
		if (this.setkaScroll.maxScrollY - this.tvPrograms[1].offsetHeight <= 0) {
			if (!this.scrollHidden){
           		this.scroll.scroll.style.visibility = 'hidden'
           		$('leftUpButton').style.visibility = 'hidden'
           		$('leftDownButton').style.visibility = 'hidden'
           		//$('_scrollContainer').style.visibility = 'hidden'
           		this.scrollHidden = true
           	}
        } else if (this.scrollHidden) {
            this.scroll.scroll.style.visibility = 'visible'
            $('leftUpButton').style.visibility = 'visible'
            $('leftDownButton').style.visibility = 'visible'
            //$('_scrollContainer').style.visibility = 'visible'
            this.scrollHidden = false
        }
        
        return this.scrollHidden
    },
    
    toString : function() {return 'tv'}
}

var ScrollBar = function(scroll, thumbHeight){
	this.scroll = scroll
	this.scrollHeight = scroll.offsetHeight
	this.addListener = Mediator.addListener
	this.clientY = 0
	
	if (thumbHeight){
		this.scrollHeight -= thumbHeight
		this.theight = thumbHeight
		this.scroll.scrollTop = this.scrollHeight
		this.setkaPos = 0
		this.menuPos = this.scrollHeight
	} else {
	this.menuMaxScrollY = $('bottomMenus1').scrollHeight
	}
	
	this.scrollPageLength = 20
	this.visible = true

	this.top = 0
	for (var node = this.scroll; node && node.nodeType==1; node = node.parentNode){
        this.top += node.offsetTop
    }
    
    this.initMove = function(){
    	var o = this
		o.scrollY = this.scroll.scrollTop
		
		if (document.selection) document.selection.empty()
        else if (window.getSelection) window.getSelection().removeAllRanges()
        
		document.onmouseout = function() {
            o.over = setTimeout(function() {
                o.endMove()
            }, 0)
        }
        document.onmouseover = function() {
            if (o.over) clearTimeout(o.over)
            o.over = null
        }
        document.onmouseup = function() { o.endMove() }
        document.onselectstart = function() { return false }
    }
    
    this.scrollPage = function(event, dir){
    	this.initMove()
    	var o = this
		
    	var offset 
    	if (dir) offset = (dir == TVProgram.UP) ? this.scrollPageLength : -this.scrollPageLength
    	/*
    	else {
    		var y = event.layerY || event.y
    		if (y < this.scrollHeight) o.move(this.scrollPageLength)
    		else
    		if (y > this.scrollHeight + this.theight) o.move(-this.scrollPageLength)
    		Mediator.notify(ScrollBar.MOVE_SCROLL, o)
    		return
    	}
    	*/
    	
		this.timeinterval = setInterval(function(){
    	  	o.move(offset)
    	    o.scrollY = o.scroll.scrollTop
    	    Mediator.notify(ScrollBar.MOVE_SCROLL, o)
        }, 10)
    }
    
    this.onmousemove = function(e, o) {
        o.move(o.clientY - e.clientY)
    }
    
    this.onmousedown = function(event){
    	if (BottomMenu.visible) stopPropagation(event)
    }

	this.startMove = function(event, dir) {
	
		if (this.theight) {
    		var y = event.layerY || event.y
    		if (y < this.scrollHeight || y > this.scrollHeight + this.theight) return
    	}
	
		var o = this
		o.clientY = event.clientY
		this.initMove()
		
        document.onmousemove = function(e) {
            e = window.event || e
            o.onmousemove(e, o)
        }
        
        //document.documentElement.style.MozUserSelect = 'none'
        //if (this.mode == LeftScroll.MENU) stopPropagation(event)
        }
	
	this.move = function(offset){
        this.scroll.scrollTop = this.scrollY + offset
	}
	
	this.endMove = function() {
        document.onmouseup = null
        document.onmouseout = null
        document.onmouseover = null
        document.onselectstart = function(){return true}
        document.onmousemove = null
        clearInterval(this.timeinterval)
        //$('print').value += 'END MOVE + \r'
        //document.documentElement.style.MozUserSelect = ''
    }
	
	this.calcMax = function(){
		this.maxScrollY = 0
		var max
		for (var i=TVProgram.tvProgramContainer.scrollLeft/TVProgram.blockWidth, ilen=i+TVProgram.groupLength; i<ilen; i++){
			if ((max = TVProgram.tvPrograms[1].childNodes[i].offsetHeight) > this.maxScrollY) this.maxScrollY = max
		}
	}
	/*
	this.render = function(){
		var mult
		this.calcMax()
		with (TVProgram.tvPrograms[1]) mult = scrollTop/(scrollHeight - this.maxScrollY)
		this.scroll.scrollTop = this.scrollHeight*(1 - mult)
		
	}
	*/
	this.toString = function() {return 'scroll' + Math.random()}
}

ScrollBar.MOVE_SCROLL = 'move_scroll'

