var Notifier = {
	/** идет ли онлайн-трансляция */
	online: false,
	/** ссылка */
	link: null,
	/** язык */
	lang: 'rus',
	/** Инициализация */
	init: function(){
		Notifier.setLang();
		if (Notifier.isCookie()){
			// трансляция идет - показываем ссылку
			Notifier.online = true;
			Notifier.create();
			Notifier.show(true);
		}
		$(document).everyTime(1000,Notifier.check);	
	},
	
	/** проверяем статус онлайн-трансляции */
	check: function(){
		$.ajax({
			url: 'ajax.php',
			type: 'post',
			dataType: 'json',
			data: {type: 'notifier'},
			success: function(data){
				if (data.status){
					if (Notifier.online === false){
						Notifier.create();
						Notifier.show();
					}
				} else {
					if (Notifier.online === true){
						Notifier.destroy();
					}
				}
			}
		});
	},
	/** трансляция началась - создать кнопку */
	create: function(){
		Notifier.link = $('<a />',{
			id: 'online-broadcasting-link',
			href: '/'+Notifier.lang+'/online/broadcast'
		})
		.css({
			position: 'fixed',
			display: 'block',
			width: 55,
			height: 75,
			right: 0,
			top: 110,
			background: 'url(img/online_button.png) left top no-repeat',
			zIndex: 999,
			cursor: 'pointer',
			textDecoration: 'none'
		})
		.prepend(
			$('<span />')
			.css({
				color: '#d85303',
				font: 'italic 14px arial, sans-serif',
				textDecoration: 'none',
				position: 'absolute',
				top: 30,
				left: 65,
				display: 'block',
				width: 143,
			})
			.text(Notifier.lang == 'rus' ? 'Онлайн-трансляция' : 'Online broadcast')
		)
		.hide()
		.prependTo('body')
		.bind({
			mouseenter: function(){
				$(this).stop().animate({width: 208},500);
			},
			mouseleave: function(){
				$(this).stop().animate({width: 55},500);
			}
		});
		Notifier.online = true;
		Notifier.setCookie();
	},
	/** показываем ссылку. immediately - если надо показать мгновенно */
	show: function(immediately){
		var immediately = immediately || false;
		var time = immediately ? 0 : 500;
		Notifier.link.fadeIn(time);
	},
	/** трансляция завершилась - убрать кнопку */
	destroy: function(){
		Notifier.link.fadeOut(500,function(){
			$(this).remove();
		});
		Notifier.online = false;
		Notifier.removeCookie();
	},
	/** определение и установка языка */
	setLang: function(){
		var href = window.location.href;
		if (href.indexOf('/eng')+1){
			Notifier.lang = 'eng';
		} else {
			Notifier.lang = 'rus';
		}
	},
	/** проверка Cookie */
	isCookie: function(){
		return $.cookie('notifier');
	},
	/** устанавливаем Cookie */
	setCookie: function(){
		$.cookie("notifier",true,{path: "/"});   
	},
	/** удаляем Cookie */
	removeCookie: function(){
		$.cookie("notifier",null,{path: "/"});   
	}
}

$(function(){
	if (!(window.location.href.indexOf('online/broadcast')+1)){
		Notifier.init();
	} 
});

