var lastSongsHandler
var quantity = ""
var time

var stopOnAir = false;


function setUpOnairSongs(all, hideTime, showTime, startUpFlashing, quant)
{
	if (all){
		quantity = "all=1&"
	}
	if(hideTime === undefined){
		hideTime = 100;
	}
	if(showTime === undefined){
		showTime = 200;
	}

	if(startUpFlashing){
		var hh  = shuffleArray(jQuery.makeArray(jQuery("#last_stations_song .onair_color")));
		var time = 0;
		jQuery(hh).css({backgroundColor: "#f5f5f5"}).each(function(i){
			if(quant && Math.floor(i/quant)*quant === i)
			{
				var me = this;
				setTimeout(function(){
					jQuery(me).animate({opacity: 0.5}, showTime/2)
						.animate({opacity: 1}, showTime/2)
						.animate({backgroundColor: "#"+colorsOnAir[Math.floor(Math.random()*colorsOnAir.length)] }, showTime/2)
						.animate({backgroundColor: "#f5f5f5"}, showTime);
				}, time += showTime/2);
			}
		});
	}
	lastSongsHandler = window.setInterval(function(){refreshLastSongs(hideTime, showTime)}, 30000);
	jQuery("#last_stations_song").hover(function(){stopOnAir = true;}, function(){stopOnAir = false;});
}

function refreshLastSongs(hideTime, showTime)
{
	if(!stopOnAir){
		jQuery.get("/onair.content.html?"+ quantity + Math.random(), function(data){
			var reducer = jQuery("#last_stations_song div.reducer")
			var lsBlock = jQuery("#last_stations_song")
			lsBlock.css({height: reducer.innerHeight()})
			var hh  = jQuery.makeArray(jQuery("#last_stations_song .onair_color"))
			shuffleArray(hh)
			var time = 0
			jQuery(hh).each(function(){
				var me = this;
				setTimeout(function(){
					jQuery(me).animate({opacity: 0}, hideTime)
				}, time += hideTime/2)
			})
			jQuery("#stationTime").html(jQuery("#showTime").attr("value"));
			jQuery("#showTime").remove();

			setTimeout(function(){
				reducer.animate({opacity: 0}, showTime, function(){
					reducer.empty().html(data);
					lsBlock.find(".onair_color").css({opacity: 0, backgroundColor: "#f5f5f5"});
					lsBlock.animate({height:  reducer.innerHeight()});
					reducer.css({position: "static", opacity: 1});
					var hh  = shuffleArray(jQuery.makeArray(jQuery("#last_stations_song .onair_color")));
					var time = 0;
					jQuery(hh).each(function(){
						var me = this;
						setTimeout(function(){
							jQuery(me).animate({opacity: 1}, showTime)
								.animate({backgroundColor: "#"+colorsOnAir[Math.floor(Math.random()*colorsOnAir.length)] }, showTime/2)
								.animate({backgroundColor: "#f5f5f5"}, showTime);
						}, time += showTime/2)
					})
				})
			}, hh.length*hideTime/2 + hideTime)
		}) // end of GET
	}
}

function switchOnair(type)
{
	jQuery('#for_mixer').toggle()
	jQuery('#for_songs').toggle()
	if (type == 'songs') {
		refreshLastSongs()
		setUpOnairSongs()
	} else {
		window.clearInterval(lastSongsHandler)
		getMixer()
	}
	jQuery.get('/actions.html?action=toggle_main_page&' + Math.random());
	return false;
}

function getMixer()
{
	jQuery.get('/block.radiomixer', function (data){
		jQuery('#last_stations_song div.reducer').html(data)
	})
}

function datePickerSetup (start_)
{
	var dateP
	jQuery('.widgetCalendar').DatePicker({
		flat: true,
		format: 'Y-m-d',
		date: start_,
		calendars: 1,
		mode: 'single',
		starts: 1,
		prev: jQuery.browser.msie? '&larr;' : '&#9664;' ,
		next: jQuery.browser.msie? '&rarr;' : '&#9654;',
		locale: {
			days: ["Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье"],
			daysShort: ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"],
			daysMin: ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс"],
			months: ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"],
			monthsShort: ["Янв", "Фев", "Март", "Апр", "Май", "Июнь", "Июль", "Авг", "Сен", "Окт", "Нояб", "Дек"],
			weekMin: ''
		},
		onChange: function(formated){
			dateP = formated
		},
		onRender: function(date){
			var d  = new Date()
			return {disabled: (date.valueOf() > d.valueOf())}
		}
	})

	jQuery('.widgetCalendar').css({display: "none", visibility: "visible"})
	jQuery('#station_history').click(function(){
		jQuery('.widgetCalendar').toggle(200)
		return false
	})
	jQuery('input.ok').click(function(){
		if(typeof dateP != 'undefined'){
			var hrf = jQuery('#station_history').attr("href").replace(/\/\d{8}$/, '')
			jQuery('#station_history').attr({href: hrf + '/' + dateP.replace(/-/g, '')})
			var reg = /(\d+)\-(\d+)\-(\d+)/
			var months = runMonth
			var dateParts=reg.exec(dateP) // get month number
			var startDate = parseInt(dateParts[3], 10) + ' ' +months[parseInt(dateParts[2], 10)-1] // create date like "9 мая" from 1945-05-09
			jQuery('#station_history').html(startDate)
		}

		jQuery('.widgetCalendar').hide(200)
	})
	jQuery('span.closer').click(function(){
		jQuery('.widgetCalendar').hide(200)
	})
}

function stationSelectorShow() {
	jQuery('#station_select').show()
}

function stationSelectorHide() {
	jQuery('#station_select').hide()
}

function stationSelect(obj) {
	jQuery('#station_title').html(jQuery(obj).title)
	var reg = /\/\d{8}$/
	var find = reg.exec(jQuery('#station_history').href);
	jQuery('#station_history').href = jQuery(obj).href + ((find == null)?'':find);
	stationSelectorHide();
}

function sortSelect(name) {
	jQuery('#sel_rating').hide();
	jQuery('#sel_title').hide();
	jQuery('#sel_frequency').hide();
	jQuery('#sel_' + name).show();
}

jQuery(function(){
	jQuery('#ndxsel_onair').click(
		function(){
			jQuery('#sol650x280').hide();
			jQuery('div.block_onair').show();
			return false;
		}
	)
	jQuery('#ndxsel_adv').click(
		function(){
			jQuery('div.block_onair').hide();
			jQuery('#sol650x280').show();
			return false;
		}
	)
	var rotateTime = 5000;
	var timerStationNews = window.setTimeout(rotateStationNews, rotateTime);
	function rotateStationNews() {
		var hideTime = 500;
		var newsArr = jQuery.makeArray(jQuery('span.stationNews'));
		var findVisible = false;
		jQuery(newsArr).each(function() {
			if(jQuery(this).is(':visible')) {
				jQuery(this).animate({"opacity": "hide"}, hideTime);
				jQuery(this).hide();
				findVisible = true;
			} else if(findVisible) {
				jQuery(this).animate({"opacity": "show"}, hideTime);
				jQuery(this).show();
				findVisible = false;
			}
		});
		if(findVisible) {
			jQuery(newsArr[0]).animate({"opacity": "show"}, hideTime);
			jQuery(newsArr[0]).show();
		}
		timerStationNews = window.setTimeout(rotateStationNews, rotateTime);
	}
	jQuery('div.block_news_feed').mouseover(
		function(){
			window.clearInterval(timerStationNews);
			return false;
		}
	)
	jQuery('div.block_news_feed').mouseout(
		function(){
			timerStationNews = window.setTimeout(rotateStationNews, rotateTime);
			return false;
		}
	)
});