/*
* Навигационное меню сайта
*/
function mainmeny(){
   if (!document.getElementById) return;
   var meny = document.getElementsByTagName('td');
   for (var i = 0; i < meny.length; i++){
     if (meny[i].className == 'navl'){
         meny[i].onmouseover = function(){
              this.style.cursor='hand';
              this.style.backgroundColor='#ecb430';

         }
         meny[i].onmouseout = function(){
              this.style.cursor='hand';
              this.style.backgroundColor='';
         }
     }
   }
   weatroll();
}


function weatroll(){
  if (!document.getElementById) return;
  var weat = document.getElementsByTagName('td');
  for (var i = 0; i < weat.length; i++){
   if (weat[i].className == 'twb' || weat[i].className == 'twe'){
       weat[i].onmouseover = function(){
            this.style.cursor='pointer';
            this.style.backgroundColor='#e0f0ff';

       }
       weat[i].onmouseout = function(){
            this.style.cursor='default';
            this.style.backgroundColor='';
       }
   }
  }
}
window.onload = mainmeny;

/*
* Погодный информер
*/
ArrowDown = new Image; ArrowDown.src="http://ukrpost.info/images/RectangleBlueDown.gif";
ArrowUp   = new Image; ArrowUp.src="http://ukrpost.info/images/RectangleBlueUp.gif";
Loading   = new Image; Loading.src="http://ukrpost.info/images/loading.gif";

var active = 0;
var show   = 0;
var timer;

$(document).ready(function() {
    $("body").click(function(){
      if(show == 0 && active == 1){
       showhide('cityArr', 'cityButton', 'wtitleActive');
      }
      show = 0;
      window.clearInterval(timer);
    });
});

function showhide(id, button, classname){
  var blockToShow = $('#'+id);
  var button      = $('#'+button);
   show   = 1;
    if(active==0){
      blockToShow.animate({opacity: "show"}, "slow");
      button.addClass(classname);
      active = 1;
      var timer = setTimeout(function(){
        blockToShow.animate({opacity: "hide"}, "slow");
        button.removeClass(classname);
        active = 0;
        show   = 0;
        window.clearInterval(timer);
        } , 7200);
    }
    else if(active==1){
      blockToShow.animate({opacity: "hide"}, "slow");
      button.removeClass(classname);
      active = 0;
      show   = 0;
      window.clearInterval(timer);
    }
}

function getWeather(param, url, div, wait, cityName, button, classname){
    var button       = $('#'+button);
    var blockToShow  = $('#'+div);
    var blockToWait  = $('#'+wait);
    var option = {};
    option['param'] = param;
    $.ajax({
        type: "POST",
    	url:  url,
        async: true,
    	data: option,
        beforeSend: function(){
          blockToWait.addClass('loading');
        },
    	success: function(data) {
    	  blockToWait.removeClass('loading');
          if(data){
            blockToShow.html(data);
            button.html(cityName);
            button.removeClass(classname);
          }
          else{
            blockToShow.html("<p>Нет данных</p>");
          }
		},
	    error: function() {alert('Ошибка обработки запроса');}
	});
}


function showCommentsBlock(nid, pg){
    var CommentsBox  = $('#CommentsBox');
    var option = {};
    option['nid']    = nid;
    option['pg']     = pg;
    $.ajax({
        type: "POST",
    	url:  "http://ukrpost.info/comments/",
        async: true,
    	data: option,
        beforeSend: function(){
          CommentsBox.addClass('loading');
        },
    	success: function(data) {
    	  CommentsBox.removeClass('loading');
          if(data){
            CommentsBox.html(data);
          }
          else{
            CommentsBox.html("<p>&nbsp;&nbsp;К данной новости еще нет ни одного коментария</p>");
          }
		},
	    error: function() {alert('error');}
	});
}
/*
* Форма отправки коментариев
*/

$(document).ready(function() {
    var CommentsBox      = $('#CommentsBox');
    var commentFormDiv   = $("#commentFormDiv");
    var commentForm      = $("#commentForm");
    var ErrorList        = $("#ErrorList");
    var nid              = $("#nid").attr('value');
    $("#capcha").attr({value: "jsoncode0245"});

    commentForm.ajaxForm({
      //target: CommentsBox,
      beforeSend: function(){
        if (!$("#name").fieldValue()[0].length) {
          ErrorList.html('<h3>Ошибка</h3>Введите имя!');
          ErrorList.show(200);
          return;
        }
        if (!$("#body").fieldValue()[0].length) {
          ErrorList.html('<h3>Ошибка</h3>Введите текст комментария!');
          ErrorList.show(200);
          return;
        }
      },
      success: function() {
        if($("#name").fieldValue()[0].length>0 && $("#body").fieldValue()[0].length>0){
          ErrorList.hide(200);
          CommentsBox.fadeIn('slow');
          commentFormDiv.slideToggle('fast');
          commentForm.resetForm();
          showCommentsBlock(nid);
        }
      },
      error: function() {ErrorList.html('Ошибка обработки запроса');ErrorList.show(200);return;}
    });
});

$(document).ready(function() {
    var commentFormDiv = $("#commentFormDiv");
    $("#btn-slide").click(function(){
      commentFormDiv.slideToggle("slow");
      return false;
    });
});


