var channels = new Array();
var startBound;
var endBound;
var excluded = null;
var shownBlocks = new Array();
var startBoundHour;
var $top = 0;
var HOUR_WIDTH = 256;
var loaded;
var delta = 0;
var currentDate = new Date();
//var setDate = new Date();
var day = 0;//currentDate.getDay() - 1;
var channels_count = 10;
var SCROLL_HEIGHT = 0;
var SCROLL_WIDTH = 7168; //=HOUR_WIDTH*28
var PROGRAM_HEIGHT = 41;
var TOP_CONST = channels_count * PROGRAM_HEIGHT;

(function($) {
$(document).ready(function(){
    buildDayMenu();
    initGrid();
    setTimeBounds(-1);
    getChannels();
    initChannels();
	//initScrol();
    initTimeBar();
    setNowTimeLine();
    setInterval(function(){setNowTimeLine();}, 10000);
	addEventToButtons();

	$('#gridHolder').css('margin-left',(+(deltaHourShift/3600))*HOUR_WIDTH);
	$('#timeBar').css('margin-left',-(+(deltaHourShift/3600))*HOUR_WIDTH);
//initTimeSlider();
});
var gridNix = Math.round(( dateToMidnight( currentDate ) ).getTime()/1000) ;
function initTimeSlider(){
    var $currentTime = new Date();
    //$currentTime.setTime(currentDate);
    var $hour = $currentTime.getHours();
    if ($hour%2 != 0) {
        $hour --;
    }
    $currentTime.setHours($hour, 0, 0, 0);
    var $timeValue = $currentTime/1000;

    //var maxScroll = $("#grid").attr("scrollWidth") - $("#grid").width();
    var maxScroll = SCROLL_WIDTH - $("#grid").width();
    var $timeSliderValue = Math.round(nixToPixel($timeValue)/maxScroll * 1000)/10;

    $('#timeSlider').slider('option', 'value', -$timeSliderValue);
}

function nixToPixel(time)
{
    return (gridNix - time ) / (60*60) * HOUR_WIDTH;
}

function pixelToNix(pixel)
{
    return gridNix + ( (pixel / HOUR_WIDTH) * (60 * 60) );
}

function pixelToChannel(pixel)
{
    var channel = channels[Math.round((pixel - 20) / PROGRAM_HEIGHT)];
    if(channel){
        return channel;
    }
    return '';
}

function dateToMidnight(dDate)
{
    var dDateMid = new Date();
    dDateMid.setTime(dDate.getTime());
    dDateMid.setMinutes(0);
    dDateMid.setSeconds(0);
    dDateMid.setMilliseconds(0);
    dDateMid.setHours(0);
    return dDateMid;
}

function setTimeBounds($px){
    var $currentTime = new Date();
    $currentTime.setTime(currentDate);
    if($px != -1){
        //$currentTime = new Date();
        //$currentTime.setTime(currentDate);
        var $timeValue = Math.round($px/HOUR_WIDTH);
        $currentTime.setHours($timeValue, 0, 0, 0);
    }

    var $hour = $currentTime.getHours();
    if ($hour%2 != 0) {
        $hour --;
    }

    startBoundHour = $hour - 0;

    $currentTime.setHours($hour - 0, 0, 0, 0);
    startBound = $currentTime.getTime()/1000;
    $currentTime.setHours($hour + 2*3, 0, 0, 0);
    endBound = $currentTime.getTime()/1000;
}

function buildGrid(){
	var $startBlock = Math.floor(($top)/TOP_CONST);
    var $middleBlock = ($startBlock + 1);
    var $endBlock = ($startBlock + 2);
    //$selectedChannelsUp = channels.slice($startBlock * 6, $startBlock * 6 + 6);
	var per_block = channels_count / 3;
    var $selectedChannelsUp = sliceGridChannels($startBlock * channels_count, $startBlock * channels_count + channels_count);
    $selectedChannelsUp = $selectedChannelsUp.join("_");

    //$selectedChannelsMiddle = channels.slice(($startBlock * 6 + 6) , ($middleBlock + 1) * 6);
    var $selectedChannelsMiddle = sliceGridChannels(($startBlock * channels_count + channels_count) , ($middleBlock + 1) * channels_count);
    $selectedChannelsMiddle = $selectedChannelsMiddle.join("_");

    //$selectedChannelsDown = channels.slice(($middleBlock * 6 + 6) , ($endBlock + 1) * 6);
    /*var $selectedChannelsDown = sliceGridChannels(($middleBlock * channels_count + channels_count) , ($endBlock + 1) * channels_count);
    $selectedChannelsDown = $selectedChannelsDown.join("_");*/

    var $hour = startBoundHour-(+(deltaHourShift/3600));
    var $limit = $hour + 4;
    var $date = new Date();
    $date.setTime(currentDate);
    //alert("=" + $date.getDate());

    while($hour < $limit){
        //$date.setTime(startBound * 1000);
        $date.setHours($hour, 0, 0, 0);

        var $start = $date/1000 + "_" + $selectedChannelsUp;
        var $middle = $date/1000 + "_" + $selectedChannelsMiddle;
        /*var $end = $date/1000 + "_" + $selectedChannelsDown;*/

		if($selectedChannelsUp && !existShownBlock(shownBlocks, $start)){
            $('#gridHolder').append(drawProgramBlock($hour, $selectedChannelsUp, $startBlock));
            shownBlocks[shownBlocks.length] = $date/1000 + "_" + $selectedChannelsUp;
        }
        if($selectedChannelsMiddle && !existShownBlock(shownBlocks, $middle)){
            $('#gridHolder').append(drawProgramBlock($hour, $selectedChannelsMiddle, $middleBlock));
            shownBlocks[shownBlocks.length] = $date/1000 + "_" + $selectedChannelsMiddle;
        }
        /*if($selectedChannelsDown && !existShownBlock(shownBlocks, $end)){
            $('#gridHolder').append(drawProgramBlock($hour, $selectedChannelsDown, $endBlock));
            shownBlocks[shownBlocks.length] = $date/1000 + "_" + $selectedChannelsDown;
        }*/

        $hour += 2;
    }
}

function existShownBlock(shownBlocks, $start){
    for (var i in shownBlocks){
        if(shownBlocks[i] == $start){
            return true;
        }
    }
    return false;
}

function getExcluded($selectedChannelsArray){
    var excluded = new Array();
    var time;
    if(loaded){
        $.each($selectedChannelsArray, function(i){
            var channelId = $selectedChannelsArray[i];
            if(loaded[channelId]){
                $.each(loaded[channelId], function(n){
                    time = loaded[channelId][n][0];
                    if(startBound < time && time < endBound){
                        excluded[excluded.length] = time + "-" + channelId;
                    }
                });
            }
        /*$.each(loaded, function(i){
                $.each(loaded[i], function(n){
                    time = loaded[i][n][0];
                    if(startBound < time && time < endBound){
                        excluded[excluded.length] = time + "-" + i;
                    }
                });
            });*/
        });
    }
    return excluded;
}

function getData(){	
    var $startBlock = Math.floor(($top)/TOP_CONST);
    var $endBlock = ($startBlock + 2);

    //$selectedChannelsArray = channels.slice($startBlock * 6, $endBlock * 6);
    var $selectedChannelsArray = sliceGridChannels($startBlock * channels_count, $endBlock * channels_count);
    var $selectedChannels = $selectedChannelsArray.join("_");
    var $excludedJoin = null;

    var excluded = getExcluded($selectedChannelsArray);

    if(excluded && excluded!=''){
        $excludedJoin = excluded.join("_");
    }
    var p = {};

    /*p['startBound'] = startBound;
                p['endBound'] = endBound;
                p['selectedChannels'] = $selectedChannels;
                p['excluded'] = excluded;*/
    /*$.post("/imagehandler/getData", p, function(data){
                    if(data.length >0) {
                        //channels = data.split(',');

                    }
                });*/
    $.getJSON(
        "/imagehandler/getData/" + (startBound-(+(deltaHourShift))) + "/" + endBound + "/" + $selectedChannels + "/" + $excludedJoin,
        p,
        function(data){
            if(!loaded){
                loaded = data;
            }else {
                $.each(data, function(i){
                    $.each(data[i], function(n){
                        if(!loaded[i]){
                            loaded[i] = new Object();
                        }
                        loaded[i][n] = data[i][n];
                    });

                });
            }
        }
    )
}

/*
function getDataCallback(data){
    if(!loaded){
        loaded = data;
    }else {
        $.each(data, function(i){
            $.each(data[i], function(n){
                if(loaded[i]){
                    loaded[i][n] = data[i][n];
                }
            });

        });
    }
}*/

function getChannels() {
    $.getJSON("/imagehandler/getChannels", null, function(data){
        channels = data;
        buildGrid();
		getData();
		//buildGridForAllChannels();		
        var $currentTime = new Date();
        $currentTime.setTime(currentDate);
        var $hour = $currentTime.getHours();
        if ($hour%2 != 0) {
            $hour --;
        }
        //$('#gridHolder').css('left',-($hour) * hourWidth + 128);
        //$('#grid').scrollLeft(($hour) * hourWidth);
        $('#gridHolder').css('left',-(($hour + 24 * day) * HOUR_WIDTH));
        $("#timeLine").css("left", -(($hour ) * HOUR_WIDTH));
        initTimeSlider();
    }
    )
}

function setNowTimeLine(){
    var currentTime = new Date();
    //currentTime.setTime(currentDate);

    var hour = currentTime.getHours();
    var mins = currentTime.getMinutes()/60;
//$("#debug").append('<p>hours: '+hour+'; min: '+currentTime.getMinutes()+';</p>');
    var time = (hour + mins)* HOUR_WIDTH/* + currentTime.getHours() * 5*/;
    //var timeLabel = time + 24 * day * hourWidth;
    var timeLabel = time;

    var dLabelDate = new Date();
    var day = dLabelDate.getDay();
    var appDay = currentDate.getDay();

    if(day == appDay){
        //timeLabel = time;
        $("#timeLabel").css("left", timeLabel - 36 - 24);
        $("#timeLabel").css("top", 1);
		$("#timeLabel").css("display", 'block');
		$("#timeBar").css("display", 'block');
    } else {
        $("#timeLabel").css("display", 'none');
		$("#timeBar").css("display", 'none');
    }
    
    var text = 'Сейчас ' + formatTime(currentTime);

    $("#timeLabel").text(text);

    $("#timeBar").css("left", time - 0);
}

function drawProgramBlock($hour, $channels, $top){
    var date = new Date();
    date.setTime(currentDate);
    date.setHours($hour, 0, 0, 0);
    var startTime = date.getTime()/1000;

	date.setTime(currentDate);
    date.setHours($hour + 2, 0, 0, 0);
    var endTime = date.getTime()/1000;
    var timeString = startTime + '_' + endTime;
	
    var content ='<div id="'+startTime+'_'+$channels+'" style="left: ' + ($hour+day*24) * HOUR_WIDTH
                + 'px; top: ' + $top * 41* channels_count+ 'px; width: 512px; height: '
                + 41* channels_count + 'px; background: url(/imagehandler/getProgramBlock/'
                + timeString + '/' + $channels +'.png) no-repeat;" class="tile"/>';

    return content;
}

function getShowInfo(channelId, time){
    var info = new Array();

    if(loaded && loaded[channelId]){
        $.each(loaded[channelId], function(n){
            var startTime = loaded[channelId][n][0];
            var stopTime = startTime + loaded[channelId][n][1];
            if(startTime < time && time < stopTime){
                info[0] = loaded[channelId][n][0];
                info[1] = loaded[channelId][n][1];
                info[2] = loaded[channelId][n][2];
                return false;
            }
        });
    }
    return info;
}

function formatTime(sourceTime){
    var currentTime = new Date(sourceTime);
    var hour = currentTime.getHours();
    var hourPrefix = currentTime.getHours() < 10 ? '0' : '';
    var minPrefix = currentTime.getMinutes() < 10 ? '0' : '';
    var text = hourPrefix + hour + ":"+ minPrefix + currentTime.getMinutes();

    return text;
}

function sliceGridChannels(startBlock, endBlock){
    var i = startBlock;
    var end = endBlock;
    var result = new Array();

    var channelsSize = channels.length;
    if(channelsSize < endBlock){
        end = channelsSize;
    }
    while(i < end){
        if(channels[i]){
            result[result.length] = channels[i][0];
            i++;
        } else {
            break;
        }
    }
    //alert(startBlock + "_" + result.length);
    return result;
}

function initGrid(){
	$('#grid').css('height', 41*channels_count /*+ 30*/ + 'px');
    $("#gridHolder").click(
        function(e){
            var tipX = e.pageX - $("#gridHolder").offset().left + 12;
            var tipY = e.pageY - $("#channels").offset().top;

            var channel = pixelToChannel(tipY - 12);
            var time = pixelToNix(tipX - 8 - 24*HOUR_WIDTH*day);
            var $showInfo = getShowInfo(channel[0], time);

            if($showInfo && $showInfo[0]){
                tipX = -nixToPixel($showInfo[0]) + 24*HOUR_WIDTH*day;
                tipY = Math.floor((tipY/*-12*/)/PROGRAM_HEIGHT) * PROGRAM_HEIGHT;
                var tipWidth = Math.round($showInfo[1]/(60*60) * HOUR_WIDTH) - 5;

                $('#cursor').remove();
                $(this).append(
                    '<div id="cursor">'
                    +'</div>'
                    );
                $('#cursor').fadeIn(0);
                $("#cursor").css({
                    'top': tipY,
                    'left': tipX,
                    'width': tipWidth
                });
            }
        }
        );

    $("#gridHolder").hover(
        function(e) {
            $("#gridHolder").mousemove(function(e) {
                var tipX = e.pageX - $("#gridHolder").offset().left + 12;
                var tipY = e.pageY - $("#channels").offset().top;

                var tipXX = e.pageX - $("#viewer-inner").offset().left + 12 ;
                var tipYY = e.pageY - $("#viewer-inner").offset().top+12;
                $(".toolTipWrapper").css({
                    //'top': tipY,
                    //'left': tipX
                    'top': tipYY,
                    'left': tipXX
                });
                var channel = pixelToChannel(tipY+5/* - 12*/);
                var time = pixelToNix(tipX - 8 - 24*HOUR_WIDTH*day);
				time += delta/1000;
                var $showInfo = getShowInfo(channel[0], time);
                if($showInfo && $showInfo[0]){
                    $('.toolTipUp').html($showInfo[2]);
                    $('.toolTipDown').html(formatTime((+$showInfo[0]+(+(deltaHourShift)))*1000)+ " - " + channel[1]);
                } else {
                    $(".toolTipWrapper").css({
                        'top': -1000,
                        'left': 0
                    });
                }
            }
            );

            //$(this).append(
			$('#viewer-inner').append(
                '<div class="toolTipWrapper">'
                +   '<div class="toolTipTop">'
                +   '</div>'
                +'  <div class="toolTipMid">'
		+'	<div class="toolTipCont">'
		+	    '<div class="toolTipUp">'
                +	    '</div>'
		+	    '<div class="toolTipDown">'
                +	    '</div>'
		+	'</div>'
                +   '</div>'
                +   '<div class="toolTipBtm"></div>'
                +'</div>'
                );
            $('.toolTipWrapper').fadeIn(0);
        },
        function() {
            $('.toolTipWrapper').fadeOut(0);
            $('.toolTipWrapper').remove();
        }
        );
}

function initChannels(){
	$("#channelsWrapper").css('height', 41*channels_count/*+15*/+'px');
    $("#channelsWrapper").hover(
        function(e) {
            $("#channelsWrapper").mousemove(function(e) {
                var tipX = e.pageX - $("#channelsWrapper").offset().left + 22;
                var tipY = e.pageY - $("#channelsWrapper").offset().top + 12;
                var y = e.pageY - $("#channels").offset().top;

                $(".toolTipWrapperChannel").css({
                    'top': tipY,
                    'left': tipX
                });
                var channel = pixelToChannel(y/* - 12*/);
                if(channel != ''){
                    $('.toolTipTopChannel').html(channel[1] + " - " + channel[0]);
                } else {
                    $(".toolTipWrapperChannel").css({
                        'top': -1000,
                        'left': 0
                    });
                }
            }
            );

            $("#viewer").append(
                '<div class="toolTipWrapperChannel">'
                +'<div class="toolTipTopChannel">'
                +'</div>'
                +'<div class="toolTipMidChannel">'
                +'</div>'
                +'</div>'
                );

            $('.toolTipWrapperChannel').fadeIn(0);
        },
        function() {
            $('.toolTipWrapperChannel').fadeOut(0);
            $('.toolTipWrapperChannel').remove();
        }
        );

    var height = 100;/*
    $("#channelSlider").removeAttr('slide').slider({//.removeAttr('slide') added to remove conflict with mootools Fx.slide
        orientation:'vertical',
        range: "min",
        value: height,
        min: 0,
        max: height,
        step: 0.3,
        slide: function(event, ui) {
            //$("#grid").attr("scrollHeight")
            var maxScroll = SCROLL_HEIGHT - $("#grid").height();
            $top = maxScroll-ui.value * (maxScroll / 100);
            //$("#grid").attr({
            //    scrollTop: $top
            //});
            $('#channels').css('top', -$top);
            $('#gridHolder').css('top', -$top + 15);

            buildGrid();
            //$("#channels ").scrollTop(-$top);
        },
        stop: function(event, ui) {
            //var maxScroll = $("#grid").attr("scrollHeight") - $("#grid").height();
            //$top = maxScroll-ui.value * (maxScroll / 100);
            getData();
        }
    });
*/
	//$("#viewer").append('<img src="/imagehandler/buildChannelsBlock" alt=""/>');
    $('#channelsWrapper').load("/imagehandler/buildChannelsBlock", null, function(str){
        $('#timeBar').height($('#channels').height() + 20);
        SCROLL_HEIGHT = $('#channels').height() + 35;
		/*$('#channels').css('background', 'none');
		$('#channels').append('<img src="/imagehandler/getChannelBlock" alt=""/>');*/
    });
}

function initTimeBar(){
    $("#timeSlider").removeAttr('slide').slider({
        orientation:'horizontal',
		animate: true,
        min: 0,
        max: 90,
        step: 1,
        slide: function(event, ui) {
            var maxScroll = SCROLL_WIDTH - $("#grid").width();
            //var maxScroll = $("#grid").attr("scrollWidth") - $("#grid").width();
            var $left = ui.value * (maxScroll / 100);
            $('#gridHolder').css('left',-($left + (SCROLL_WIDTH - 4*256)*day));
            //$("#timeLine").attr({scrollLeft: $left });
            //$("#grid").attr({scrollLeft: ($left)});
            $("#timeLine").css("left", -($left));
			//$top = Math.abs($('#channels').css('top'));
            //$("#amount").val($left);
            setTimeBounds($left);
            buildGrid();
			//buildGridForAllChannels();			
			//$("#timeSlider").css('margin', 0).css('margin-top','-9px');
			//$("#timeSlider").parent().css('position', 'inherit').css('margin',0).css('overflow', 'auto').css('height', 'auto');
        },
        stop: function(event, ui) {
            //var maxScroll = $("#grid").attr("scrollWidth") - $("#grid").width();
            //var maxScroll = SCROLL_WIDTH - $("#grid").width();
            //$left = ui.value * (maxScroll / 100);
            getData();
			//$("#timeSlider").css('margin', 0).css('margin-top','-9px');
			//$("#timeSlider").parent().css('position', 'inherit').css('margin',0).css('overflow', 'auto').css('height', 'auto');
        }
    });
}

function switchDay(dayOfMonth){
	//day = day1;
    currentDate = new Date();
    //currentDate.setDate(dayOfMonth);
	var time = currentDate.getTime();
	delta = +dayOfMonth;
	currentDate.setTime((+time) + (+dayOfMonth));
//покажем программы только для выбранного дня, остальные спрячем
	var toDay = new Date();
	toDay.setTime(currentDate);
	toDay.setHours(0,0,0,0);
	var beginToDay = toDay.getTime()/1000;
	toDay.setHours(24,0,0,0);
	var endToDay = toDay.getTime()/1000;
	$('#gridHolder div.tile').each(
		function (ind, el){
			var id = $(this).attr('id');
            id = id.split('_');
			id = (+id[0]);
			//alert(beginToDay+' > '+id+' > '+endToDay);
			if (id>=beginToDay && id<endToDay){
				$(this).css('display','block');
			}else{
				$(this).css('display','none');
			}
		}
	);

    /*var dLabelDate = new Date();
    var day2 = dLabelDate.getDay();
    var appDay = currentDate.getDay();
    alert(day);*/
    //currentDate.setDate(currentTime.getDate() + day);
    $gridNix = Math.round(( dateToMidnight( currentDate ) ).getTime()/1000) ;

    var $hour = currentDate.getHours();
    if ($hour%2 != 0) {
        $hour --;
    }

    startBoundHour = $hour - 0;

    var $currentTime = new Date();
    $currentTime.setTime(currentDate);

    $currentTime.setHours($hour - 0, 0, 0, 0);
    startBound = $currentTime.getTime()/1000;
    $currentTime.setHours($hour + 2*3, 0, 0, 0);
    endBound = $currentTime.getTime()/1000;
    buildGrid();
    getData();

    $('#gridHolder').css('left',-(($hour+24*day ) * HOUR_WIDTH -0));
    $("#timeLine").css("left", -(($hour) * HOUR_WIDTH - 0));
    
    setNowTimeLine();
    initTimeSlider();
}

function formatMenuDay(time){
    var dLabelDate = new Date();
    dLabelDate.setTime(time);
    return dLabelDate.formatDate( 'l jS' );
}

function menuDay(count){
    var dLabelDate = new Date();

    var day = dLabelDate.getDay();
    if(day > 0){
        count -= day - 1;
    }

    return dLabelDate.setTime( dLabelDate.getTime() + count * (24*60*60*1000) );
}

function buildDayMenu(){
    var time = new Date();
	var dayArr = ['пн','вт','ср','чт','пт','сб','вс'];
	var monthArr = ['января','февраля','марта','апреля','мая','июля','июня','августа','сентября','октября','ноября','декабря'];
	var curDay = currentDate.getDay()-1;
	if (curDay<0){
		curDay = 6;
	}
	$('#timeline-today').html('Сегодня<br/>'+dayArr[curDay]+'. '+currentDate.getDate()+' '+monthArr[currentDate.getMonth()]);
	var curTime = currentDate.getTime();
	$('#timeline-days').html('');
	var zindex = 10;
	var left = 0;
	var classname = 'timeline-day';
	for (var i=0;i<7;i++){
		//time.setTime(curTime + 60*60*24*1000*(i+1));
		//var id = time.getDate();
		var id = 60*60*24*1000*(i-curDay);
		if (curDay == i) {
			classname = 'timeline-day-selected';
			id = 0;
		}else{
			classname = 'timeline-day';
		}
		$('#timeline-days').append(
			"<div id='day_"+id+"' class='"+classname+"' style='z-index:"+(zindex++)+";left:"+(left)+"px;'>"+dayArr[i]+"</div>"//++curDay
		);
		if (curDay>5){
			curDay=-1;
		}
		left+=20;
	}
	$('#timeline-days div.timeline-day,#timeline-days div.timeline-day-selected').click(
        function() {
            var id = $(this).attr('id');
            id = id.substr(4, id.length);
            switchDay(id);
	    $("div.timeline-day-selected").removeClass('timeline-day-selected').addClass("timeline-day");
	    $(this).addClass("timeline-day-selected");
			//buildDayMenu();
        }
    );
    //time.setTime(menuDay(day));

    /*$("#daysMenu").append('<span id="day" class="dropdown">'
        + '<span class="label"></span>'
        + '<span class="arrow">&nbsp;</span>'
        + '<span class="cap">&nbsp;</span></span>'
        );

    $("#daysMenu").after('<ul id="days" class="dropdown">');
    for(var i=0; i<7; i++){
        //time = new Date();
        time.setTime(menuDay(i));

        var id = time.getDate();

        if(currentDate.getDay() == time.getDay()){
            $('#day .label').text(formatMenuDay(time));
        }

        $("#days").append(
            '<li id="day_'
            + id
            + '"><a >'
            + formatMenuDay(time)
            + '</a></li>'
            );
    }

    $('#day').click(
        function() {
            $('#days').css('visibility', 'visible').css('display', 'inline-block');
        }
        );

    $('#days').hover(
        function() {
            $('#days').css('visibility', 'visible').css('display', 'inline-block');
        },
        function() {
            $('#days').css('visibility', 'hidden').css('display', 'none');
        }
        );

    $('#days li').hover(
        function() {
            $('a',this).css({
                'background-color':'#fff',
                'color': '#000'
            });
        },
        function() {
            $('a',this).css({
                'background-color':'#262626',
                'color': '#fff'
            });
        }
        );

    $('#days li').click(
        function() {
            var id = $(this).attr('id');
            id = id.substr(4, id.length);
            $('#days').css('visibility', 'hidden').css('display', 'none');

            var menuDay = new Date();
            menuDay.setDate(id);

            var dLabelDate = new Date();
            var count = dLabelDate.getDay();
            day = menuDay.getDay();

            if(count > 0){
                if(day == 0){
                    day = 7;
                }
                day -= count;
            }

            $('#day .label').text($('a', this).text());
            switchDay(id);
        }
        );*/
}
function addEventToButtons() {
	$('#btn_dwn').click(function(){
		$top = parseInt($('#channels').css('top'))/* - 41*/;
		//alert($top);
		if(-$top + (channels_count+2)*41 > SCROLL_HEIGHT)
			return;		
		$('#channels').css('top', $top - 41);
		$('#gridHolder').css('top', $top - 41);
		$top = -($top-41);
		//$top = parseInt($('#channels').css('height'))+$top;
		buildGrid();
		getData();
	});
	$('#btn_up').click(function(){
		$top = parseInt($('#channels').css('top'))/* - 41*/;
		//alert($top);
		if($top >= 0)
			return;
		$('#channels').css('top', $top + 41);
		$('#gridHolder').css('top', $top + 41);
		$top = -($top + 41);
		//$top = -$top + channels_count*41;
		buildGrid();
		getData();
	});
	$('#timeline-today').click(
		function(){
			switchDay(0);
		}
	);
}
function channel_down(){
	$top = parseInt($('#channels').css('top')) - 41;
	if(-$top+ channels_count*41 > SCROLL_HEIGHT)
		return;
	$('#channels').css('top', $top);
	$('#gridHolder').css('top', $top + 31);
	$top = -$top + channels_count*20;
	buildGrid();
	getData();
}
})(jQuery.noConflict());
