if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
        
    if (from < 0)
      from += len;

    for (; from < len; from++) {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    
    return -1;
  };
}

$(document).ready( function() {
	$('a.openBlank').attr('target', '_blank');
	
	var plural = function(number, pluralForms) {
		if ( number%10 === 1 && number%100 !== 11 ) {
			return pluralForms[0];
		}

		if ( number%10>=2 && number%10<=4 && ( number%100<10 || number%100>=20 ) ) {
			return pluralForms[1];
		}

		return pluralForms[2];
	};
	
	$('img.guys').hover( function() {
		$(this).fadeTo('slow', 1);
	}, function() {
		$(this).fadeTo('slow', 0.6);
	});

	$('#flash-menu').flash({
		swf: '/s/flash/menu.swf',
		height: 353,
		width: 1000
	});

	$('img.manage').live('mouseover', function() {
		img = $(this).attr('src');
		$(this).attr('src', img.replace('out', 'over') );
	}).live('mouseout', function() {
		img = $(this).attr('src');
		$(this).attr('src', img.replace('over', 'out') );
	});

	$(".lightbox").lightbox();

	$('.__poll').submit(function () {
		var checked = $(this).find('input[name="answer"]:checked');
		if ($(checked).length == 0) {
			alert("Вы не выбрали, за что голосовать");
			return false
		}

		var postData = {
			poll_id: $(this).attr("action"),
			answer: $(checked).val()
		};

		$.post("/ajax/backend.vote.php", postData, function (data) {
			if (data && data.errors) {
				alert(data.errors)
			} else {
				alert("Ваш голос будет учтен ага");
				window.location.reload()
			}
		}, 'json' );
		return false
	});

	$("#cform").submit(function () {
		if ($("#name").val() == "") {
			alert("Вы не ввели ваш никнейм");
			return false
		}
		
		if ($("#comment").val() == "") {
			alert("Вы ничего не написали");
			return false
		}
		
		var setData = $('#set').html();
		if (setData.length > 0) {
			var setA = setData.split(',');
			var t, comments, ls = window.localStorage || false;
			
			if (ls !== false) {
				comments = localStorage.getItem('comments') || '[]';
				comments = JSON.parse(comments);
				
				if (comments.length > 0) {
					for (var i=0; i<comments.length; i++) {
						t = comments[i] + '';
						if (setA.indexOf(t) !== -1) {
							alert("Все комментарии с этого IP-адреса заблокированы из-за большого количества мата.\nВы можете прислать ваш комментарий на e-mail 1999@limp-bizkit.ru,\nи мы его обязательно опубликуем на сайте");
							return false;
						}
					}
				}
			}
		}

		var postData = {
			name: $("#name").val(),
			email: $("#email").val(),
			comment: $("#comment").val(),
			news_id: $(this).attr("rel")
		};
		
		$.post("/ajax/backend.post_comment.php", postData, function (data) {
			if (data && data.errors) {
				alert(data.errors)
			} else {
				$("#comment").val('');
				$("#comments").append(data.ok);
				
				var comments, ls = window.localStorage || false;
				if (ls !== false) {
					comments = localStorage.getItem('comments') || '[]';
					comments = JSON.parse(comments);
					
					comments.push(data.id);
					if (comments.length > 100) {
						comments.shift();
					}
					
					localStorage.setItem('comments', JSON.stringify(comments));
				}
			}
		}, 'json');
		
		return false
	});

	$("#subscribe").submit(function () {
		var getData = {
			email: $('#semail').val()
		};

		$.getJSON("/ajax/backend.subscribe.php", getData, function (data) {
			if (data && data.errors) {
				alert(data.errors)
			} else {
				alert(data.ok);
				$("#semail").val("")
			}
		});

		return false
	});
});

