$(document).ready(function() {
    $("#menu li.level1").hover(
        function(){
            $(this).find("a.level1 span.bg").animate({'padding-top': '0px', 'height': '50px'}, 200);
        },
        function(){
            $(this).find("a.level1 span.bg").animate({'padding-top': '3px', 'height': '47px'}, 200);
        }
    );
        
        
    $("ul.menu-accordion li.level1").click(function(){
        $(this).find('ul.level2').slideToggle("slow");
        $(this).toggleClass('active');
        $(this).find("span.separator").toggleClass('active');
    });
    
    $("#imgs-gallery a, a.lightview").fancybox();
    $("a.window-link, #add-comment").fancybox();
    
    $("#add-comment").fancybox({
    onComplete: function(){
	      $('#file_upload').uploadify({
		  'uploader'  : homeURL+'libs/uploadify/uploadify.swf',
		  'script'    : homeURL+'libs/uploadify/uploadify.php',
		  'cancelImg' : homeURL+'libs/uploadify/cancel.png',
		  'folder'    : '/files/uploads',
		  'auto'      : true,
		  'multi' : true,
		  'buttonText' : 'Add',
		  'onComplete'   : function(event,queueID,fileObj,response,data) {
		      if(response != 'false'){
			    var old_val = $('#uploaded-files').val();
			    $('#uploaded-files').attr('value', old_val+"|"+response);
		      }
		  }
		});           
            },
    });
    
    topGalInit();
    
$('.comments-list .show_full_comment').click(function(){
            if($(this).parent().find('div.full_comment').css('display')=='none'){
                $(this).parent().find('div.full_comment').show();
                $(this).parent().find('div.short_comment').hide();
                $(this).find('span.hide').show();
                $(this).find('span.show').hide();
            } else {
                $(this).parent().find('div.full_comment').hide();
                $(this).parent().find('div.short_comment').show();
                $(this).find('span.hide').hide();
                $(this).find('span.show').show();
            }
            return false;
        });

        $(".add-comment-button").live('click', function(){

            $("#comm-form em").html('');
               var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
               var ok = true;
            if($("#comm-name").val().replace(/\s/g, "") == ""){
                ok=false;
                $("#comm-name").parent().find('em').html("Обязателное поле");
            }
            if($("#comm-email").val().replace(/\s/g, "") == ""){
                ok=false;
                $("#comm-email").parent().find('em').html("Обязателное поле");
            } else if(reg.test($('#comm-email').val()) == false) {
                       ok=false;
                       $("#comm-email").parent().find('em').html("Некорректный email");
            }
            if($("#comm-comment").val().replace(/\s/g, "") == ""){
                ok=false;
                $("#comm-comment").parent().find('em').html("Обязателное поле");
            }

                if(ok){
                            addComment();
                }

        });
  });

  function addComment(){
      var data = $("#comm-form").serialize();
      $.ajax({
              url: homeURL+'guestbook/add_comment/',
            data: data,
            dataType: 'html',
            type: 'post',
            beforeSend: function (XMLHttpRequest) {
                 $('.add-comment-button').hide();
            },
            complete: function() {},
            success: function(resp){
// 	      $("#debug").html(resp);
                    if(resp.error==true){
                        alert("Сообщение не может быть добавлено. Переданны не все данные");
                         $('.add-comment-button').show();
                    } else{
                        window.location.reload();
                    }
            },
            error: function(){
                alert('error');
            },
            cashe: false
      });

  }





function topGalInit(){
    tbCurrentEl = 2;
    tbAllEl = $("#yoo-carousel-1 div.article").size();
    
    $("#yoo-carousel-1 div.article").hide();
    $("#yoo-carousel-1 div.article").next().fadeIn();
    
    setInterval(changeTopGal, 3000);
    return;
}

function changeTopGal(){
    tbCurrentEl = parseInt(tbCurrentEl+1);
    if(tbCurrentEl>tbAllEl) tbCurrentEl = 1;
    
    $("#yoo-carousel-1 div.article:visible").fadeOut('slow');
    $("#yoo-carousel-1 div.article:nth-child("+tbCurrentEl+")").fadeIn('slow');
   return;
}
