var rating_arr = new Array();
var s_rait = 0;
var s_count = 0;
var other_id = 0;
var curUrl = '';
var checkUrl = location.href;
var countFlag = 0;
var imgDir = '/img/center/format';

if (checkUrl.indexOf("hardrock.myradio.com.ua") != -1) {
	curUrl = location.href;
}

if (checkUrl.indexOf("la-mur.myradio.com.ua") != -1) {
	curUrl = location.href;
}

function init() {
	rating_arr = $$('span.newStars a img');

	rating_arr.each(function(item, index) {
				// alert('Ok');
				item.ratingOn = false;

				item.addEvent('mouseover', function(e) {
							var event = new Event(e);
							showStars(index);
						});

				item.addEvent('mouseout', function(e) {
							var event = new Event(e);
							hideStars();
							showRaiting((songRait + s_rait)
									/ (countRait + s_count));
						});

				item.addEvent('click', function(e) {
							var event = new Event(e);

							setRating(index);
						});

			});

}

function showStars(index) {
	var i;

	if (type == 2) {
		var star_url1 = '/img/center/clip-star-1.gif';
		var star_url0 = '/img/center/clip-star-0.gif';
	} else {
		var star_url1 = '/img/center/format/golos_2.gif';
		var star_url0 = '/img/center/format/golos_0.gif';
	}

	var kook = getCookie('rait_' + type + '_' + songIdRait);

	if (kook != 1) {
		for (i = 0; i < rating_arr.length; i++) {
			// alert('Ok1');
			var item = rating_arr[i];
			if (i <= index) {
				if (!item.ratingOn)
					item.src = star_url1;
				// item.ratingOn = true;
			} else {
				// item.ratingOn = false
				item.src = star_url0;
			}
		}
	}

}

function hideStars() {
	var i;

	if (type == 2) {
		var star_url1 = '/img/center/clip-star-1.gif';
		var star_url0 = '/img/center/clip-star-0.gif';
	} else {
		var star_url1 = '/img/center/format/golos_2.gif';
		var star_url0 = '/img/center/format/golos_0.gif';
	}

	for (i = 0; i < rating_arr.length; i++) {
		var item = rating_arr[i];
		item.src = star_url0;

	}
}

function likesong(songId, type, negative){
    if (songId){
        var kook = Cookie.read('rait_' + songIdRait);
        if (!kook){
            $('voteActionContainer').setText('Спасибо за то, что оценили эту песню');
            var votes = $('formatVoteCount').get('text').toInt() + 1;
            if (negative) var rate = $('formatRating').get('text').toInt() - 1;
            else var rate = $('formatRating').get('text').toInt() + 1;
            $('formatRating').setText(rate);
            $('formatVoteCount').setText(votes);
            var url = 'ajax2.php?' + Object.toQueryString({
                                            target : 'format',
                                            event : 'rating',
                                            songId : songId,
                                            negative : negative,
                                            new_vote : 1,
                                            type : type,
                                            formatId : formatMegaId
                                    });
            var my_ajax = new Ajax(url, {
                                    method : 'get'
            });
            my_ajax.request();
        }else{
            $('voteActionContainer').setText('Вы уже оценивали эту песню');
            return 0;
        }
    }
}


function setRating(rating) {

	var kook = getCookie('rait_' + type + '_' + songIdRait);

	if (kook != 1) {
		s_rait = rating;
		s_count = 1;
		var url = curUrl + 'ajax.php?' + Object.toQueryString({
					target : 'format',
					event : 'rating',
					ourRating : rating,
					type : type,
					songIdRaiting : songIdRait,
					formatId : formatMegaId
				});
		var my_ajax = new Ajax(url, {
					method : 'get',
					onComplete : resp_setRating.bind(this)
				});
		my_ajax.request();
	}
}

function resp_setRating(response) {
	var result = Json.evaluate(response);

	if (result.formatRating == 0 && result.formatVoteCount == 0) {
		$('board').setText('Вы уже голосовали');
		return 0;
	}
	$('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, 1);

	showRaiting(index - 1);
}

function showRaiting(raiting) {
	var i = 0;

	if (type == 2) {
		var star_url1 = '/img/center/clip-star-1.gif';
		var star_url0 = '/img/center/clip-star-0.gif';
	} else {
		var star_url1 = '/img/center/format/golos_2.gif';
		var star_url0 = '/img/center/format/golos_0.gif';
	}

	raiting = Math.round(raiting);
	for (i = 0; i <= raiting; i++) {

		var item = rating_arr[i];
		if (i <= raiting) {
			item.src = star_url1;
		} else {
			item.src = star_url0;
		}
	}
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = 0;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return (setStr);
}

function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays == null || nDays == 0)
		nDays = 1;
	expire.setTime(today.getTime() + 3600000 * 24 * nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires="
			+ expire.toGMTString();
}

var flagSpan = 0;

function setRatingMax(id, flag) {
	var kook = getCookie('rait_1_' + id);
	if (kook != 1) {
		var url = curUrl + 'ajax.php?' + Object.toQueryString({
					target : 'format',
					event : 'rating',
					ourRating : 1,
					formatId : formatMegaId,
					songIdRaiting : id,
					type : 1
				});

		var my_ajax = new Ajax(url, {
					method : 'get',
					onComplete : resp_setRatingMax.bind(this)
				});

		// setCookie('rait_1_'+ id, 1, 14);
		other_id = id;
		flagSpan = flag;
		$('sound_' + id).title = 'summ+2';
		my_ajax.request();

	}
}

function resp_setRatingMax(response) {

	var result = Json.evaluate(response);
	var str = result.formatRating;
	var txt = '';

	txt = '<img src="/img/center/voice-bg-11.gif';
	
	txt += '" ';
	txt += ' onmouseout="UnTip();" onmouseover="';
	txt += "Tip('<b>Результаты голосования</b><br>Голосов ";
	txt += str;
	txt += "')";
	txt += '">';
	$('star_' + other_id).innerHTML = txt;
}

function getIp() {
	var ip = new java.net.InetAddress.getLocalHost();
	var ipStr = new java.lang.String(ip);

	ip = ipStr.substring(ipStr.indexOf("/") + 1)
	return ip;
}

if (navigator.cookieEnabled == 0) {
	var cookieEnable = 0;
} else {
	var cookieEnable = 1;
}

function hideStarsN() {
	var i;
	var ratingStars = $$('span.newStars a img');
	var star_url0 = imgDir + '/golos-0.gif';

	for (i = 0; i < ratingStars.length; i++) {
		var item = ratingStars[i];
		item.src = star_url0;
	}
}

function showRaitingN(raiting) {

	if ((getCookie('rait_' + type + '_' + songIdRait) == 1) && (protect == 0)) {
		return false;
	}

	protect = 0;

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

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

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

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

function setRatingN(rating) {

	var kook = getCookie('rait_' + type + '_' + songIdRait);

	if (kook != 1) {
		s_rait = rating;
		s_count = 1;
		var url = curUrl + 'ajax.php?' + Object.toQueryString({
					target : 'format',
					event : 'rating',
					ourRating : rating,
					type : type,
					songIdRaiting : songIdRait,
					formatId : formatMegaId
				});
		var my_ajax = new Ajax(url, {
					method : 'get',
					onComplete : resp_setRatingN.bind(this)
				});
		my_ajax.request();
	}
}

function resp_setRatingN(response) {
	var result = Json.evaluate(response);

	if (result.formatRating == 0 && result.formatVoteCount == 0) {
		$('board').setText('Вы уже голосовали');
		return 0;
	}

	$('formatRating').setText(result.formatRating);
	$('formatVoteCount').setText(result.formatVoteCount);

	$('board').setText('Спасибо :)');

	var index = result.formatRating / result.formatVoteCount;
	songRait = index;
	showRaitingN(index);
}

function initN() {
	rating_arr = $$('span.newStars a img');

	rating_arr.each(function(item, index) {
				item.addEvent('mouseover', function(e) {

							var i = 0;
							var event = new Event(e);
							var show = item.id;
							var s1 = show.substr(show.length - 2, 2);
							if (s1.substr(0, 1) == '-') {
								i = parseInt(s1.substr(1, 1));
								i *= -1;
								i += 3;
							} else {
								i = parseInt(s1.substr(1, 1));
								i += 2;
							}
							var ar = new Array('-3', '-2', '-1', '+1', '+2',
									'+3');

							if (getCookie('rait_' + type + '_' + songIdRait) != 1) {
								Tip(ar[i], DELAY, 100);
							}

						});

				item.addEvent('mouseout', function(e) {
							UnTip();
						});
			});

}

