var cChannel = function(){
    this.path = '/?sectionid=11'; // путь для запроса
    this.section_id = 0;
    this.orderby = 'date';
    this.orderrange = 'rate';
    this.timerange = 'all';
    this.page = 1;
    this.inner = 0;
    this.channelid = 0;
    this.search = '';
    this.exclude_search = '';
    this.user_id = 0;
    this.archive = 0;
    this.all = 0;

    this.contentblock = 'contentintetv';

    this.init = function(){}

    this.load = function(itemid, a){
		var conteinerid=this.contentblock;
		show_AjaxLoader(conteinerid);
//		this.timerange = 'year'; 

        if(itemid == null) itemid=this.channelid;
        else this.channelid = itemid;
        this.ajaxSend(
            this.path,
            (a==undefined ? '' : 'a='+a+'&') + 'section_id=' + this.section_id + '&item_id=' + itemid + '&page=' + this.page + '&orderby=' + this.orderby + '&timerange=' + this.timerange + '&inner=' + this.inner + (this.search ? '&q=' + this.search : '') + (this.orderrange ? '&orderrange=' + this.orderrange : '') + (this.exclude_search ? '&exclude=' + this.exclude_search : '') + (this.user_id ? '&user_id='+this.user_id : '') + (this.archive ? '&archive='+this.archive : '') + (this.all ? '&all='+this.all : ''),
            function(text, obj){
            	hide_AjaxLoader(obj);
                gid(obj).innerHTML = text;
                reload_script(conteinerid);
            },
            this.contentblock
        );
    }
    
    this.ajaxSend = function(url,data,listener, contentblock) {
        var agent = null;
        var method = (data ? data : null) ? 'POST' : 'GET';
        try{agent=new XMLHttpRequest();}catch(e){try{agent=new ActiveXObject('Msxml2.XMLHTTP.3.0');}catch(e){try{agent=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{agent=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){alert('AJAX MESSAGE: initialization error')}}}}
        agent.open(method, url, true);
        agent.setRequestHeader('Connection', 'close');
        agent.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        agent.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
        agent.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        agent.onreadystatechange = function() {
            if(agent.readyState==4) {
                if(agent.status==200) {
                    responseText = agent.responseText;
                    listener(agent.responseText, contentblock);
                } else {
                    alert('AJAX MESSAGE: '+agent.status+' '+agent.statusText)
                }
            }
        }
        agent.send(data);
        return false;
    }

////////favorites////////////
    this.add_favorite_video = function(videoid,sectionid,obj){
      	var x=0;
    	if(obj.className=='link') {
    		// добавляем в избранное
    		this.ajaxSend(
	            this.path,
	            '&add_favorite_video='+videoid+'&section_id='+sectionid,
	            function(responseText){
	            	x=eval('(' + responseText + ')');
	            	show_user_tooltip(x['text'],1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');
	            	if(!x['error']) {
	            		$(obj).find('img').attr('src','/images/icons/favorites-check.png');
	            		$(obj).attr({
	            			title:'Удалить из избранного',
	            			className:'link-check'
	            		});
	            		window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            	}
	            }
	        );
    	}else {
    		this.ajaxSend(
	            this.path,
	            '&del_favorite_video='+videoid+'&section_id='+sectionid,
	            function(responseText){
	            	show_user_tooltip(responseText,1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');

    				$(obj).find('img').attr('src','/images/icons/favorites.png');
            		$(obj).attr({
            			title:'Добавить в избранное',
            			className:'link'
            		});
	            	window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            }
	        );
    	}
    }

    this.add_favorite_channel = function(channelid,sectionid,obj){
        var x=0;
    	if(obj.className=='link') {
    		// добавляем в избранное
    		ajaxSend(
	            this.path,
	            'add_favorite_channel='+channelid+'&section_id='+sectionid,
	            function(responseText){
	            	x=eval('(' + responseText + ')');
	            	show_user_tooltip(x['text'],1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');
	            	if(!x['error']) {
	            		$(obj).find('img').attr('src','/images/icons/favorites-check.png');
	            		$(obj).attr({
	            			title:'Удалить из избранного',
	            			className:'link-check'
	            		});
	            		window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            	}
	            }
	        );
    	}
    	else {
    		this.ajaxSend(
	            this.path,
	            '&del_favorite_channel='+channelid+'&section_id='+sectionid,
	            function(responseText){
	            	show_user_tooltip(responseText,1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');

	            	$(obj).find('img').attr('src','/images/icons/favorites.png');
            		$(obj).attr({
            			title:'Добавить в избранное',
            			className:'link'
            		});
	            	window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            }
	        );
    	}
    }
    this.add_favorite = function(type,id,sectionid,obj){
    	var x=0;
    	if(obj.className=='link') {
    		// добавляем в избранное
    		this.ajaxSend(
	            this.path,
	            '&add_favorite_'+type+'='+id+'&section_id='+sectionid,
	            function(responseText){
	            	x=eval('(' + responseText + ')');
	            	show_user_tooltip(x['text'],1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');
	            	if(!x['error']) {
	            		$(obj).find('img').attr('src','/images/icons/favorites-check.png');
	            		$(obj).attr({
	            			title:'Удалить из избранного',
	            			className:'link-check'
	            		});
	            		window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            	}
	            }
	        );
    	}else {
    		this.ajaxSend(
	            this.path,
	            '&del_favorite_'+type+'='+id+'&section_id='+sectionid,
	            function(responseText){
	            	show_user_tooltip(responseText,1,'favorite-tooltip','favorite-tooltip-content','simple-tooltip','center','center');
    				$(obj).find('img').attr('src','/images/icons/favorites.png');
            		$(obj).attr({
            			title:'Добавить в избранное',
            			className:'link'
            		});
	            	window.setTimeout("close_tooltip2('favorite-tooltip','1')", 1500);
	            }
	        );
    	}
    }
/////////////////////////////
}

var channels = new cChannel();    channels.init();      // каналы
var myvideo = new cChannel();    myvideo.init();    // мое видео
var translation = new cChannel(); translation.init(); // трансляции
var new_ = new cChannel(); new_.init(); // новинки

var favorites = new cChannel(); favorites.init(); // новинки
var mycontests = new cChannel(); mycontests.init(); // новинки






//var channelid = 0;
//
//var cChannel = function(){
//    this.section_id = 0;
//    this.path = '/private_channels/';
//    this.action = '';
//    this.searchQuery = '';
//    this.contentBlock = 'contentintetv';
//    this.page = 0;
//    this.orderBy = 'date';
//    this.timeRange = 'year';
//    this.inner = 0;
//    this.year = 2010;
//
//    this.setYear = function(act){this.year=act;}
//    this.setAction = function(act){this.action=act;}
//    this.setSectionId = function(id){this.section_id=id;}
//    this.setContentBlock = function(cb){this.contentBlock=cb;}
//    this.getContentBlock = function(){return this.contentBlock;}
//    this.setInner = function(){this.inner = !this.inner;}
//    this.setPath = function(ph){this.path = ph;}
//    this.setPage = function(pg){this.page = pg;}
//    this.setOrderBy = function(by){this.orderBy = by;}
//    this.setTimeRange = function(tr){this.timeRange = tr;}
//    this.setSearchQuery = function(sq){this.action='search'; this.searchQuery = sq;}
//
//    // загружаем канал
//    this.load = function(item_id){
//        channelid = item_id;
//
//        ajaxSend(
//            this.path,
//                'section_id=' + this.section_id + '&item_id=' + item_id + '&page=' + this.page + '&orderby=' + this.orderBy + '&timerange=' + this.timeRange + '&inner=' + this.inner + '&a=' + this.action + '&year=' + this.year + '&q=' + encodeURIComponent(this.searchQuery),
//            writeAnswer
//        );
//    }
//
//    function writeAnswer(text){
//        if(!text) alert('Error show page');
//            gid(channel.getContentBlock()).innerHTML = text;
//    }
//}
//var channel = new cChannel();
//
