var playerFlag = 0;
var protect = 0;
var imgDir = 'http://img.myradio.com.ua/img/small_player';

var img_add = 'http://img.myradio.com.uaimg/center/forPlayer/addtoplaylist.gif';
var img_added = 'http://img.myradio.com.uaimg/center/forPlayer/track_added.gif';

function nowPlaying() {
	var url = Ria_Myradio_GlobalVars.base_href + 'ajax.php?' + Object.toQueryString({
				target : 'format',
				event : 'nowPlaying',
				format_id : formatMegaId
			});
	var my_ajax = new Ajax(url, {
				method : 'get',
				onComplete : resp_nowPlaying.bind(this)
			});
	my_ajax.request();
}

function resp_nowPlaying(response) {
	var result = Json.evaluate(response);
	if (oldPosition != result) {
		oldPosition = result;
		whatIsPlay();
	}
}

function whatIsPlay() {
	var url = Ria_Myradio_GlobalVars.base_href + 'ajax.php?' + Object.toQueryString({
				target : 'format',
				event : 'whatIsPlay',
				format_id : formatMegaId
			});
	var my_ajax = new Ajax(url, {
				method : 'get',
				onComplete : resp_whatIsPlay.bind(this)
			});
	my_ajax.request();
}

function resp_whatIsPlay(response) {

	var result = Json.evaluate(response);
	var songRait = result.song_rait;
	var countRait = result.count_rait;

	songIdRait = result.file_id;
	var stars_kol = result.stars_kol;
	var song = result.good_name;
	var group = result.group_name;
	var group_id = parseInt(result.group_id);
        var shop = result.shop;

	$('formatRating').setText(songRait);
	$('formatVoteCount').setText(countRait);
	$('song_name').setText(song);
	if (group_id > 0) {
		$('group_name').innerHTML = '<a class="my_alias" href="group/'
				+ group_id + '">' + group + '</a>';
	} else {
		$('group_name').setText(group);
	}
        if ($('voteActionContainer')){
            if ($('voteActionContainer').getAttribute('songid')){
                if ($('voteActionContainer').getAttribute('songid')!=songIdRait){
                    $('voteActionContainer').empty();
                    insertLikeAnchors(songIdRait);
                    $('voteActionContainer').setAttribute('songid', songIdRait);
                }
            }
        }
	if ($('board')) $('board').setText('');
        if ($('unlikeSongAnchor')) $('unlikeSongAnchor').setAttribute('songid',songIdRait);
        if ($('likeSongAnchor')) $('likeSongAnchor').setAttribute('songid',songIdRait);
	protect = 1;
	showRaitingN(stars_kol);
	refresh_list(formatMegaId);
}

function insertLikeAnchors(songIdRait){
    var likeanchor = new Element('a', {
                                        'class':'likeit likeit_selected','title':'+1 балл этой песне',
                                        'href':'javascript:','onclick':'likesong('+songIdRait+',1)',
                                        'id':'likeSongAnchor','songid':songIdRait});

    likeanchor.inject($('voteActionContainer'));
    likeanchor.grab(new Element('img',{'src':'/img/likeit.png'}));
    likeanchor.appendText('Нравится');
    var unlikeanchor = new Element('a', {
                                        'class':'likeit','title':'-1 балл этой песне',
                                        'href':'javascript:','onclick':'likesong('+songIdRait+',1,1)',
                                        'id':'unlikeSongAnchor','songid':songIdRait});
    unlikeanchor.inject($('voteActionContainer'));
    unlikeanchor.grab(new Element('img',{'src':'/img/unlikeit.png','alt':'Не нравится'}));
}

function whatIsPlayShort() {
	var url = Ria_Myradio_GlobalVars.base_href + 'ajax.php?' + Object.toQueryString({
				target : 'format',
				event : 'whatIsPlayShort',
				format_id : formatMegaId
			});
	var my_ajax = new Ajax(url, {
				method : 'get',
				onComplete : resp_whatIsPlayShort.bind(this)
			});
	my_ajax.request();
}

function resp_whatIsPlayShort(response) {
	var result = Json.evaluate(response);
	songIdRait = result.file_id;
	var song = result.good_name;
	var group = result.group_name;

	$('song_name').setText(song);
	$('group_name').setText(group);
}

function resp_setRating(response) {
	var result = Json.evaluate(response);
	$('formatRating').setText(result.formatRating);
	$('formatVoteCount').setText(result.formatVoteCount);
	if (type == 2) {
		$('board').setText('Спасибо :)');
	} else {
		$('board').setText('Спасибо :)');
	}
	var index = $('formatRating').innerHTML / $('formatVoteCount').innerHTML;

	// setCookie('rait_'+type+'_'+ songIdRait, 1, 14);

	showRaiting(index);
}

function refresh_list(id) {
	new Ajax(Ria_Myradio_GlobalVars.base_href + '?target=main&event=nextSongs&format_id=' + id+(Cookie.read('new_vote')?'&new_vote=1':''), {
				method : 'get',
				update : $('format-list-song')
			}).request().chain(function() {
				MyPlaylist = new Ria_Myradio_Playlist({});
			});

}

function showStarsSmall(index) {
	var i;

	var star_url1 = 'http://img.myradio.com.ua/img/format/golos_2.gif';
	var star_url0 = 'http://img.myradio.com.ua/img/format/golos_0.gif';

	for (i = 0; i < rating_arr.length; i++) {
		var item = rating_arr[i];
		if (i <= index) {
			if (!item.ratingOn)
				item.src = star_url1;
		} else {
			item.src = star_url0;
		}
	}
}

function whatIsPlayN() {
	var url = Ria_Myradio_GlobalVars.base_href + 'ajax.php?' + Object.toQueryString({
				target : 'format',
				event : 'whatIsPlay',
				format_id : formatMegaId
			});
	var my_ajax = new Ajax(url, {
				method : 'get',
				onComplete : resp_whatIsPlayN.bind(this)
			});
	my_ajax.request();
}

function resp_whatIsPlayN(response) {

	var result = Json.evaluate(response);
	var songRait = result.song_rait;
	var countRait = result.count_rait;
	var group_id = result.group_id;

	songIdRait = result.file_id;
	var stars_kol = result.stars_kol;
	var song = result.good_name;
	var group = result.group_name;
	raiting = songRait;

	if (playerFlag == 1) {

		$('formatRating').setText(songRait);
		$('formatVoteCount').setText(countRait);

		hideStarsN();
		if (raiting < (-3)) {
			raiting = -3;
		}

		if (raiting > 3) {
			raiting = 3;
		}

		for (var i = -1; i >= raiting; i--) {
			document.images['rating_' + i].src = imgDir + '/golos' + (i)
					+ '.gif';
		}

		for (var i = 1; i <= raiting; i++) {
			document.images['rating_' + i].src = imgDir + '/golos_' + (i)
					+ '.gif';
		}
	}

	$('song_name').setText(song);
	$('group_name').setText(group);

	/*
	 * if(group_id>0){ $('group_name').set({ href: 'group/'+group_id, text:
	 * group}); } else{ $('group_name').setText(group);
	 *  }
	 */
	refresh_listN(formatMegaId);
}

function nowPlayingN() {
	var url = Ria_Myradio_GlobalVars.base_href + 'ajax.php?' + Object.toQueryString({
				target : 'format',
				event : 'nowPlaying',
				format_id : formatMegaId
			});
	var my_ajax = new Ajax(url, {
				method : 'get',
				onComplete : resp_nowPlayingN.bind(this)
			});
	my_ajax.request();
}

function resp_nowPlayingN(response) {
	var result = Json.evaluate(response);
	if (oldPosition != result) {
		oldPosition = result;
		if (is_news == 1) {
			whatIsPlayShort();
		} else {
			whatIsPlayN();
		}
	}

}

function refresh_listN(id) {
	new Ajax(Ria_Myradio_GlobalVars.base_href + '?target=main&event=nextSongs&player=1&format_id='
					+ id, {
				method : 'get',
				update : $('format-list-song')
			}).request();
}
