function TV(){

	this.site = 0;

	this.days = new Array();
	this.program = new Array();
	this.date = null;
	this.date_s = null;
	this.time = null;
	this.date_pro = null;
	this.type = 0;
	this.announce = 0;
	this.ie6 = false;
	this.nextUpdate = null;

	this.editable = 0;

	this.load = function(){
		var obj = null;
		var tv_page = false;
		this.ie6 = ($.browser.msie&&$.browser.version=='6.0') ? true : false;
		$('#context div.pro ul li:first').each(function(){
			obj = this;
		});
		$('#tv-program').each(function(){
			obj = this;
			tv_page = true;
		});
		if (!obj) return false;
		$.post('/tv-program/program',{ajax:1, site:this.site, date:this.date_pro, admin:this.editable},function(data){
			//$('div.content').text(data);
			var a = eval(data);
			TV.date = a.date;
			if (!TV.date_pro) TV.date_pro = TV.date;
			TV.time = a.time;
			TV.date_s = a.date_s;
			a = a.program;
			for(i=0; i<a.length; i++){
				TV.days[i] = [a[i][0], a[i][1], a[i][2]];
				TV.program[a[i][0]] = new Array();
				for(j=0; j<a[i][3].length; j++){
					TV.program[a[i][0]][j] = a[i][3][j];
				}
			}
			TV.showWeek();
			TV.showTvDate();
			TV.showDate();
			var c_date = $("div.content div.day select");
			if (c_date){
				c_date.each(function(){
					for (i=this.options.length-1; i>=0; i--) this.options[i] = null;
					this.options[0] = new Option('Выбрать другой день',0,true);
					for (i=0; i<TV.days.length; i++){
						this.options[i+1] = new Option(TV.days[i][2], TV.days[i][0]);
					}
				});
			}
			TV.build();
			var i = 0;
			$('#context div.pro ul a').each(function(){
				if (i<TV.days.length) $(this).attr('href','/watch/tv-program/'+TV.days[i][0]);
				++i;
			});
			$('#context div.pro ul li a').click(function(){
				var str = $(this).attr('href');
				TV.changeTvDate(str.replace('/watch/tv-program/',''));
				return tv_page ? false : true;
			});
		});
		return true;
	}

	this.build = function(update){
		if (!update) update = false;
		var html = '';
		var obj = $("#tv-program");
		if (!obj) return false;
		if (!update) obj.hide();
		var length = 0;
		var pro = this.program[this.date_pro];
		if (!pro) return false;
		for(i=0; i<pro.length; i++){
			if (this.announce>0 && pro[i][3]==0) continue;
			if (this.type>0 && pro[i][2]!=this.type) continue;
			length++;
		}
		var p = 0;
		var admin = this.site?'tv5-sport':'watch';
		this.nextUpdate = null;
		for(i=0; i<pro.length; i++){
			if (this.announce>0 && pro[i][3]==0) continue;
			if (this.type>0 && pro[i][2]!=this.type) continue;
			html += '<div class="item">';
			var j= pro.length>i+1 ? i+1 : i;
			var t1 = parseInt(pro[j][1].replace(/:/,''));
			var t2 = parseInt(this.time.replace(/:/,''));
			var is_past = pro[j][7] < this.date || (pro[j][7] == this.date && t1 < t2);
			if (is_past){
				html += '<div class="past">';
			} else if (this.nextUpdate==null){
				this.nextUpdate = t1;
			}
			html += '<span class="icon'+pro[i][2]+'"></span>';
			if (this.editable){
				html += '<span class="timepro"><a href="/admin/'+admin+'/tv-program/edit/'+pro[i][0]+'">'+pro[i][1]+'</a></span>';
				if (pro[i][3])
					html += '<a class="announce" href="javascript:TV.showAnnounce(\''+pro[i][3]+'_'+pro[i][0]+'\')">&nbsp;</a>';
				else
					html += '<span class="announce">&nbsp;</span>';
				html += '<a href="javascript:TV.showAnnounce(\''+pro[i][3]+'_'+pro[i][0]+'\')">'+pro[i][4]+'</a>';
				if (!this.site) html += '<a class="'+(pro[i][6]?'image':'image-off')+'" href="/admin/watch/tv-program/image/'+pro[i][0]+'"></a>';
				if (pro[i][5]) html += '<span class="pro">'+pro[i][5]+'</span>';
			} else if (pro[i][3]){
				html += '<span class="timepro"><a href="javascript:TV.showAnnounce(\''+pro[i][3]+'_'+pro[i][0]+'\')">'+pro[i][1]+'</a></span>';
				html += '<a class="announce" href="javascript:TV.showAnnounce(\''+pro[i][3]+'_'+pro[i][0]+'\')">&nbsp;</a>';
				html += '<a class="link" href="javascript:TV.showAnnounce(\''+pro[i][3]+'_'+pro[i][0]+'\')">'+pro[i][4]+'</a>';
			} else {
				html += '<span class="timepro">'+pro[i][1]+'</span>';
				html += '<span class="announce">&nbsp;</span>';
				html += '<span class="text">'+pro[i][4]+'</span>';
			}
			if (is_past){
				html += '</div>';
			}
			html += '</div><div class="clear"></div>';
			if (pro[i][3]){
				html += '<div id="announce-'+pro[i][3]+'_'+pro[i][0]+'" class="hidden"></div>';
			}
			p++;
			if (length>p) html += '<div class="line"></div>';
		}
		obj.html(html);
		if (!update) obj.show();
	}

	this.changeType = function(type){
		var arr = ['all','child','info','science','movie','announce'];
		var obj = $("ul.tv-cat a."+arr[type]);
		if (!obj) return false;
		$("ul.tv-cat li").each(function(){
			$(this).removeClass('s');
		});
		$(obj).parent().addClass('s');
		if (type==5){
			this.type = 0;
			this.announce = 1;
		} else {
			this.type = type;
			this.announce = 0;
		}
		this.build();
	}

	this.changeTvDate = function(d){
		for (var date in this.program){
			if (d==date){
				TV.date_pro = date;
				TV.showWeek();
				TV.showTvDate();
				TV.build();
				break;
			}
		}
	}

	this.showTvDate = function(){
		for (i=0; i<this.days.length; i++){
			if (this.days[i][0]==this.date_pro){
				$("div.content table.caption span.date").text(this.days[i][2]);
				break;
			}
		}
	}

	this.update = function(){
		$.post('/tv-program/date',{ajax:1},function(data){
			var a = eval(data);
			if (a.date!=TV.date){
				TV.date = a.date;
				TV.showWeek();
			}
			if (TV.time!=a.time){
				TV.time = a.time;
				var t1 = parseInt(TV.time.replace(/:/,''));
				if (TV.nextUpdate<=t1) TV.build(1);
			}
			TV.date_s = a.date_s;
			TV.showDate();
		});
	}

	this.showWeek = function(){
		obj = $("#context div.pro ul li:first");
		if (obj){
			for(i=0; i<this.days.length; i++){
				var date = this.days[i][0];
				obj = obj.next();
				obj.removeClass('p');
				obj.removeClass('c');
				obj.removeClass('h');
				if (this.date_pro==date) {
					obj.addClass('c');
				} else if (this.date>date){
					obj.addClass('p');
				} else if (i>4){
					obj.addClass('h');
				}
			}
		}
		return false;
	}

	this.showDate = function(){
		$("span.time").text(this.time);
		for (i=0; i<this.days.length; i++){
			if (this.days[i][0]==this.date_pro){
				$("span.date").text(this.days[i][2]);
				break;
			}
		}
		$("span#time").text(this.time);
		$("span#date").text(this.date_s);
	}

	this.showAnnounce = function(id){
		var obj = $('div#announce-'+id);
		if (obj.html()==''){
			$.post('/tv-program/announce/'+id,{ajax:1, ie:this.ie6},function(data){
				obj.html(data).show('slow');
			});
		} else if (obj.css('display')=='none'){
			obj.show('slow');
		} else {
			obj.hide('slow');
		}
	}

	this.debug = function(){
		var str = '';
		for(var date in this.program){
			str += date+"=> ["+this.program[date].length+"]\n";
			for (i=0; i<this.program[date].length; i++){
				str += "\t"+this.program[date][i][1]+", ";
			}
			str += "\n";
		}
		alert(str);
	}

}

var TV = new TV();