$(document).ready(function(){
		  
    $("#navigation .menu li").hover(
    
          function () {
            
            $(this).children('a').addClass('hover');
            $(this).children('.sub-menu').show();
            //$(this).children('.sub-menu').stop().animate({opacity: 1}, 'slow');
          },
          function () {
            $(this).children('a').removeClass('hover');
            $(this).children('.sub-menu').hide();
            //$(this).children('.sub-menu').stop().animate({opacity: 0}, 'slow');
          }       
    );
	
    $('div.gallery').galleryScroll({ step:1 });
	$('div.gallery1').galleryScroll({ step:1 });
	$('div.gallery2').galleryScroll({ step:1 });
	$('div.gallery3').galleryScroll({ step:1 });
	$('div.slider1').galleryScroll({ step:1 });
	$('div.slider2').galleryScroll({ step:1 });
	$('div.slider3').galleryScroll({ step:1 });
    
    $('.alter .box-text:odd').css({ background: '#eeead0'});
    $('.item_trailers_short:odd').css({ background: '#eeead0'});
    $('.shop_item_search:odd').css({ background: '#eeead0'});
    $('.box-reviews:odd').addClass('dark');
    $('.messages:odd').addClass('dark');
    
    $('input[name="star_1"]').rating({
        callback: function(value, link){
            $('#rating_message').html('Загрузка, подождите...');
            $(this).rating('readOnly', true, true);
            $.ajax({              
                type: "POST",
                url: "/engine/modules/rating.php",
                data: {'id': value},
                async: false, success: function(msg){
                    //$("#dialog p").text(msg);
                    //$("#dialog").dialog({autoOpen: true, modal: true});
                    $('#rating_message').html(msg);
                } 
            });
        }
    });

    $('input[name="star_2"]').rating({
        callback: function(value, link){
            $(this).rating('readOnly', true, true);
            $.ajax({              
                type: "POST",
                url: "/engine/modules/rating.php",
                data: {'id': value},
                async: false, success: function(msg){$('body').append(msg);}
            });
        }
    });
    
    $('.sort').click(function(){
        
        $.cookie("sort", $(this).attr("rel"));
        location.reload();
        
    });
    
     $('.ratrew_actives').click(function(){
        
        var date = $(this).attr('rel');
        var link = $(this);
        var pare = $(link).parent('.item_rew');
        
        $.ajax({              
            type: "POST",
            url: "/engine/modules/ratrew.php",
            data: {'id': date},
            async: false, success: function(msg){

                var vote = msg.split(':');
                $(pare).children('.count_yes').html(vote[0]);
                $(pare).children('.count_no').html(vote[1]);
                $(pare).children('.ratrew_actives').removeClass('selvote');
                $(link).addClass('selvote');
              }
        });
        
    });
    
    $('.bcl').click(function(){
        
        $($('.clbt').parent('span')).hide();
        $($(this).next('span')).show();
        
    });
    
    $('.clbt').click(function(){
        
        $($(this).parent('span')).hide();
        
    });
    
    $('.submit_button_search').click(function(){
        
        $("#search_form_top").submit();
        
    });
    
    $('#check_good').click(function(){
        
        $(".text_otzv").css({'border-color':'#97ff98'});
        $("input[name=add_reviews]").attr({'value':'3'});
        
    });
    
    $('#check_bad').click(function(){
        
        $(".text_otzv").css({'border-color':'#ff9d9d'});
        $("input[name=add_reviews]").attr({'value':'1'});
        
    });
    
    $('#check_neutral').click(function(){
        
        $(".text_otzv").css({'border-color':'#e9e6db'});
        $("input[name=add_reviews]").attr({'value':'2'});
        
    });
    
    ////////////////////////////////////
    
    $('#valid_name').click(function(){
        
        if($('input[name=login]').attr('value')){
            
            $(this).next('.msg').text('Загрузка...');
            $(this).next('.msg').load('/engine/modules/valid_name_user.php?name='+$('input[name=login]').attr('value'));
            
        }
        
    });
    
    $('.win_close').live('click', function(){
        
        $("#message").fadeOut();
        
    });
    
    // bbcode
    
    $('.but_bbcode').click(function() {
        
        var button_id = $(this).attr("rev");
        var start = '['+button_id+']';
        var end = '[/'+button_id+']';
        element = $($(this).attr("rel")).get(0);
        insert(start, end, element);
        return false;
    });
	
    
    //Vote
    $('.vote_view_result').click(function(){
        
        var id_vote = $(this).prev('input[name=vote_id]').attr('value');
        var vote_area = $(this).parents('.vote_block');
        
        $(vote_area).html('<div class="load_vote"></div>');
        
        $.ajax({              
            type: "POST",
            url: "/engine/modules/vote.php",
            data: {'id': id_vote, 'flag': 'result'},
            async: false, success: function(msg){
            
                $(vote_area).html(msg);
            
            }
        });
        
    });
    
    $('.vote_go').click(function(){
        
        var id_vote = $(this).next('input[name=vote_id]').attr('value');
        var vote_area = $(this).parents('.vote_block');
        var answer = $(vote_area).find('input[name=vote_val]:checked').attr('value');
         
        $(vote_area).html('<div class="load_vote"></div>');
        
        $.ajax({              
            type: "POST",
            url: "/engine/modules/vote.php",
            data: {'id':id_vote,'flag':'add','answer':answer},
            async: false, success: function(msg){
            
                $(vote_area).html(msg);
            
            }
        });
        
    });
    
  function insert(start, end, element) {
    if (document.selection) {
       sel = document.selection.createRange();
       sel.text = start + sel.text + end;
    } else if (element.selectionStart || element.selectionStart == '0') {
       var startPos = element.selectionStart;
       var endPos = element.selectionEnd;
       element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + end + element.value.substring(endPos, element.value.length);
    } else {
      element.value += start + end;
    }
  }
            
});


