$(document).ready(function() {

// switch stream url
	$('#switch_stream_url').find('a').each(function() {
		if ($(this).attr("id") != undefined)
			$(this).css('display','inline');
		else
			$(this).css('display','none');
		
	});
	
	$('#switch_stream_url').css('display','inline');

	$('#switch_stream_url').click(function (e) {
		clicked = $(e.target);
		var ident = clicked.attr("id");
		$('#radio_player').attr('SRC', ident);
		return false;
	});

// top10
	var speed = "fast";
	var toplink = $("a.toplink");
	var top10 = $("#top10");
	top10.hide();
	top10.fadeIn(speed)
	top10.html($("#rating").html());

	toplink.click(function (e) {
		clicked = $(e.target);
		var text = $(clicked.attr("href")).html();
		if (text != top10.html()) {
			top10.fadeOut(speed, function () {
				top10.html(text);
			});
			top10.fadeIn(speed);
		}
		return false;
	});

// favorites functions
	var favorites = $("#favorites");

	function addFavorites() {
		thisId = $("#idStation").text() + ";" + $("#idName").text();
		ids = new Array();
		n = 1;
		block = false;
		ids[n] = $.cookie("id"+n);
		while (ids[n] != null) {
			if (ids[n] == thisId)
				block = true;
			n++;
			ids[n] = $.cookie("id"+n);
		}
		if (!block) {
			$.cookie("id"+n, thisId, {expires: 1200, path: "/"});
			getFavorites();
		}
	}

	function getFavorites() {
		ids = new Array();
		n = 1;
		idsFavorites = "";
		ids[n] = $.cookie("id"+n);
		while (ids[n] != null) {
			idParams = ids[n].split(";");
			idsFavorites += "<a id=\"fav\" href=\"http://fmhit.ru/online-radio/" + idParams[0] + "\">" + idParams[1] + "</a> <a class=\"del\" id=\"" + n + "\" href=\"#\">x</a><br>\n";
			n++;
			ids[n] = $.cookie("id"+n);
		}

		favorites.fadeOut(200, function () {
			if (idsFavorites != "") {
				favorites.html(idsFavorites);
				$("#favorites a.del").css("display", "none");
				$('#favorites a[href^="http://"]').css('text-decoration', 'underline');
			}
			else
				favorites.html("Добавь в избранное");
		});
		favorites.fadeIn(200);
	}

	$("#addFavorites").click(function () {
		addFavorites();
		return false;
	});

	favorites.click(function (e) {
		clicked = $(e.target);
		ident = new Number(clicked.attr("id"));
		if (/\d+/.test(ident) & ident!=0) {
			$.cookie("id"+ident, null, {expires: 1200, path: "/"});
			n = ident+1;
			cook = $.cookie("id"+n);
			while (cook != null) {
				m = n-1;
				$.cookie("id"+m, null, {expires: 1200, path: "/"});
				$.cookie("id"+m, cook, {expires: 1200, path: "/"});
				$.cookie("id"+n, null, {expires: 1200, path: "/"});
				n++;
				cook = $.cookie("id"+n);
			}
			getFavorites();
			return false;
		}
	});

	favorites.mouseover(function () {
		$("a.del").css("display", "inline");
	});

	favorites.mouseout(function () {
		$("a.del").css("display", "none");
	});

	favorites.css("display", "none");
	$("#addFavorites").html("<img src=\"http://fmhit.ru/images/addfavorites.png\" alt=\"Добавить радиостанцию в избранное\" border=\"0\">");
	getFavorites();

// send rating
	var idStation = $("#idStation").text();
	$("#rate").click(function (e) {
		clicked = $(e.target);
		var rate = clicked.attr("id");
		$.get("http://fmhit.ru/rating.php", {id: idStation, rate: rate}, function (song) {
			$("#rate").html(song);
		});
		return false;
	});

//send comments for radio stations
	var idStation = $("#idStation").text();
	var yourname = $("#yourname");
	var yourcomment = $("#yourcomment");
	var sendcomment = $("#sendcomment");
	yourname.val("Имя");
	yourcomment.val("Сообщение");
	sendcomment.removeAttr("disabled");
	sendcomment.click(function () {
		name = yourname.val();
		comm = yourcomment.val();
		if (yourname.val() != "Имя" && yourcomment.val() != "Сообщение") {
			$.get("http://fmhit.ru/sendcomm.php", {id: idStation, name: name, comm: comm}, function (comments) {
				$("#showcommentspanel").css("height", "auto");
				$("#showcomment").html(comments);
			});
		}
		return false;
	});

	yourname.click(function () {
		if (yourname.val() == "Имя")
			yourname.val("");
	});

	yourname.blur(function () {
		if (yourname.val() == "")
			yourname.val("Имя");
	});

	yourcomment.click(function () {
		if (yourcomment.val() == "Сообщение")
			yourcomment.val("");
	});

	yourcomment.blur(function () {
		if (yourcomment.val() == "")
			yourcomment.val("Сообщение");
	});

// send messages administrator
	var idStation = $("#idStation").text();
	$("#adminmessage").click(function () {
		var info = window.prompt("Напишите любую информацию об этой радиостанции:\n(не работает радио, предложить другой url потока и пр.)", "");
		if (info.length > 5) {
			$.get("http://fmhit.ru/sendinfo.php", {id: idStation, info: info}, function () {
			alert('Собщение получено администратором, спасибо!');
			});
		}
		else
			alert('Собщение должно быть не менее 5 символов!');
		return false;
	});

//send comments for news
	var news_params = $("#news_params").text();
	var params = news_params.split(";");
	var sendcommentnews = $("#sendcommentnews");
	sendcommentnews.removeAttr("disabled");
	sendcommentnews.click(function () {
		name = yourname.val();
		comm = yourcomment.val();
		if (yourname.val() != "Имя" && yourcomment.val() != "Сообщение") {
			$.get("http://fmhit.ru/sendcomm_news.php", {y: params[0], m: params[1], d: params[2], news: params[3], name: name, comm: comm}, function (comments) {
				$("#showcommentspanel").css("height", "auto");
				$("#showcomments").html(comments);
			});
		}
		return false;
	});

// hide/show Enter Name string
	search1 = $("#search1");
	search1.val("Введите название");
	search1.click(function () {
		if (search1.val() == "Введите название")
			search1.val("");
	});
	search1.blur(function () {
		if (search1.val() == "")
			search1.val("Введите название");
	});

// hide/show description
	hide_desc = true;
	$("#hide_desc").click(function () {
		if (hide_desc) {
			$("#description").slideUp("slow");
			$("#hide_desc").text("Показать описание");
			hide_desc = false;
		}
		else {
			$("#description").slideDown("slow");
			$("#hide_desc").text("Скрыть описание");
			hide_desc = true;
		}
		return false;
	});

// _blank links
	var url = window.location;
	
	if (/\/online-radio\/\d+/.test(url))
		$('a').not('a[href="javascript:MyOtziv.mo_show_box();"], #adihet').attr('target', '_blank');
});
