$(document).ready(function(){

/* mailSendForm part */

$("#mailSend").validate({
  
  submitHandler: function(form) {
   	$(form).submit(function(){
         
         $('#name').attr("disabled", true);
         $('#email').attr("disabled", true);
         $('#text').attr("disabled", true);

         $.get('/frontend_ajax_tools.pl', { query: 'messageFormPost', name: $('#name').val(), email: $('#email').val(), text: $('#text').val() },
            function(data){
               //already posted
               if ( data == 700 ) { $('#mailSendForm').html('<p id="result">Вы уже отправляли сообщение сегодня.</p>');
                  var options = { path: '/', expires: 1 };
                  $.cookie('mailSend', 'sent', options);
               }
               //post succes
               if ( data == 300 ) {
                  $('#mailSendForm').html('<p id="result">Ваше сообщение записано.</p>');
                   //set cookie
                   var options = { path: '/', expires: 1 };
                   $.cookie('mailSend', 'sent', options);
               }
               //send error
               if ( data == 800 ) { $('#mailSendForm').html('<p id="result">Произошла техническая ошибка. Сообщение не записано.</p>'); }
         });
         //return true;
        });
  },
  
  rules: {
    name: {
      required: true,
      email: false
    },
    email: {
      required: true,
      email: true
    },
    text: {
      required: true,
      email: false
    }
  },
  messages: {
     name: "это поле обязательно для заполнения",
     email: {
       required: "введите ваш адрес для связи с вами",
       email: "адрес не соответствует формата aaa@bbb.ccc только англ. буквы"
     },
     text: "это поле обязательно для заполнения"
  }
});

/* mailSendForm part end */
   
   /*$('#arhiveSearchCleaner').click(function(event){
        $('#archiveSearch').val('');
   });*/
   
   //$('#archiveSearch').focus(function () {
   //      $('#archiveSearch').css('border-color','#555');
   // });
   
   /*$('#archiveSearchSubmittor').mouseover(function () {
         $(this).css({
           'background' : '#888',
           'color' : '#fff',
           'font-weight' : 'bold',
           'border-color' : '#fff'
         });
    });
   
   $('#archiveSearchSubmittor').mouseout(function () {
         $(this).css({
           'background' : '#fff',
           'color' : '#555',
           'font-weight' : 'normal',
           'border-color' : '#999'
         });
    });

    $('#archiveSearchSubmittor').click(function () {
        $('#archiveSearchResults').css({'display' :'block', 'z-index':'50' });
        
    });
  
  $('#archiveTrigger').click(function () {
    if ($("#archive").is(":hidden")) {
        $("#archive").fadeIn("fast");
      } else {
        $("#archive").fadeOut("fast");
      }
    });
*/   
});


$(window).load(function() {
  $('.slider').nivoSlider({
		effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices:15,
		animSpeed:500,
		pauseTime:3000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
                controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});

});



$('#voteSubmit').click(function(event){
   
        var $poll_id = $('#poll_id').val();
        var $vote = $('input:radio:checked').val();
        
        $.get('/frontend_ajax_tools.pl', { query: 'vote', vote: $vote, poll: $poll_id },
            function(data){
            $('#status').html(data);
                if ( data == '300' ){
                     //$('#status').html('Ваш голос учтен.');
                     $.get('/frontend_ajax_tools.pl', { query: 'pollResults', poll: $poll_id },
                       function(data){
                       $('#pollSubHolder').html(data);
                     });
                    }
                if ( data == '400' ){
                    alert('Ошибка, такого опроса нет.'); }
                //if ( data == '500' ){
                //    $('#status').val(''); }
                if ( data == '600' ){
                    alert('Ошибка. Голос не учтен.'); }
                //if ( data == '700' ){
                //    $('#status').html('Вы уже проголосовали сегодня.');
                    
                 //   $.get('/frontend_ajax_tools.pl', { query: 'pollResults', poll: $poll_id },
                 //      function(data){
                 //      $('#pollSubHolder').html(data);
                 //    });                   
                 //   }
                if ( data == '800' ){
                    alert('Техническая ошибка.'); }
        });
   });

function loadPoll(poll_id) {
   //if ( param('query') eq 'pollCheckForIP' && param('poll') =~ /\d+/
   $.get('/frontend_ajax_tools.pl', { query: 'pollCheckForIP', poll: poll_id },
      function(data){
         //alert(data);
         //alert(poll_id,data);
         //already vote
         if ( data == 700 ) {
            $.get('/frontend_ajax_tools.pl', { query: 'pollResults', poll: poll_id },
               function (data) {
               $('#poll').html(data);
               });
         }
         //free to vote
         if ( data == 100 ) {
            $.get('/frontend_ajax_tools.pl', { query: 'getPollDataById', poll: poll_id },
               function (data) {
               $('#poll').html(data);
               });
         }
         
   });
   
}

function ajaxLoad (uri, element){
    //alert(element);
    $(element).load(uri);
}

