$(document).ready(function(){

    $("#menu img.m").hover(
      function () {
      	var src = $(this).attr('src').replace('.', '_over.');
        $(this).attr('src', src);
      }, 
      function () {
      	var src = $(this).attr('src').replace('_over', '');
        $(this).attr('src', src);
      }
    );

    $("#vote").hover(
      function () {
        $(this).toggleClass("cursor");
      }, 
      function () {
        $(this).toggleClass("cursor");
      }
    );

	var wait = 20;
	
	$("#vote").click(function(){
				
	    $("#vote").fadeOut('slow', function () {

			$("#timer").text('Подождите ' + wait + ' сек');
	    	$("#timer").attr('style', 'display: block;');	
			$("#timer").everyTime(1000, function(i) {
		
				if ((wait - i) >= 1) {

					$(this).text('Подождите ' + (wait - i) + ' сек');
				}
					else
				if ((wait - i) == 0) {
					
			    	$("#timer").attr('style', 'display: none;');	
//	    			jQuery.getJSON(base_href + 'vote.php', {'check_request' : 1}, function(json) {
	    			
//	    				$("#check_img").attr('src', base_href + 'img.php?type=' + json.type);	
	    				$("#check_img").attr('src', base_href + 'img.php?type=' + $("#id").attr('value'));	
//	    				alert($("#id").attr('value'));
	    				$("#check").attr('style', 'display: block;');
//	    			});
	    			$("#alert").fadeIn('slow');
				}
			});

	    	$("#vote").attr('style', 'display: none;');	
	    });
	});

	$("#check_ok").click(function(){
	
    	$("#check_ok").attr('style', 'display: none;');	
	    jQuery.getJSON(base_href + 'vote.php', {'id' : $("#id").attr('value'), 'check' : $("#check_value").attr('value')}, function(json) {
	
	    	$("#check").attr('style', 'display: none;');	
	    	$("#alert").attr('style', 'display: none;');
			if (json.check == false) {
				
		    	$("#vote_text").html('Введенный код неверен.<br>Обновите страницу.');
		    	$("#vote_text").attr('style', 'display: block;');
			}
				else	    	
			if (json.state == true) {
				
			    $("#votes").text(json.votes);
		    	$("#vote_text").text('Ваш голос учтен.');
		    	$("#vote_text").attr('style', 'display: block;');
			}
				else {
					
		    	$("#vote_text").text('Ошибка. Голос не был учтен.');
		    	$("#vote_text").attr('style', 'display: block; color: #900;');
			}
		});
	});
});