var timeout_id = false;

var NN=(document.layers ? true : false);
if(NN) {
    window.captureEvents(Event.RESIZE);
}
window.onresize = showYourConsult;

function restore_menu() {
   if ( top_menu_id > 0 ) {
      $('#glabalmenu li').removeClass('current');
      $('#menu-id-'+top_menu_id).addClass('current');

      $('#secondarymenu ul').css('display', 'none');

      $('#secondarymenu ul#submenu-'+top_menu_id).animate({
         opacity: 'show'
      }, 300);
   }
   else {
      $('#secondarymenu ul').css('display', 'none');
   }
}

$(document).ready(function() {
   jQuery("#glabalmenu li a").mouseover(function() {
      clearTimeout(timeout_id);
      var menu_id = $(this).attr('rel');

      $('#glabalmenu li').removeClass('current');
      $(this).parents('li').addClass('current');

      $('#secondarymenu ul').css('display', 'none');

      $('#secondarymenu ul').animate({
         opacity: 'hide'
      }, 300);

      //$('#secondarymenu ul#submenu-'+menu_id).css('display', 'block');
      $('#secondarymenu ul#submenu-'+menu_id).animate({
         opacity: 'show'
      }, 300);
   })
});

$(document).ready(function() {
   jQuery("#glabalmenu li a, #secondarymenu").mouseout(function() {
      timeout_id = setTimeout(restore_menu, 2000);
   })
});

$(document).ready(function() {
   jQuery("#secondarymenu").mouseover(function() {
      clearTimeout(timeout_id);
   })
});


$(document).ready(function() {
   jQuery("#photo-pager li a").click(function() {
      var photo_id = $(this).attr('rel');

      $('#photo-pager li').removeClass('active');
      $(this).parents('li').addClass('active');

      $('#catalog-gallery div').css('display', 'none');
      $('#img-gallery-' + photo_id).animate({
         opacity: 'show'
      }, 600);

      return false;
   })
});

$(document).ready(function() {
   $('.inputborder2 input, #order-date').datepick({
      showOn: 'both',
      minDate: 0, 
      maxDate: '+2y', 
      buttonImageOnly: true,
      buttonImage: 'img/icon_calendar.jpg'
   });
});

$(document).ready(function() {
   window.setInterval(function() {
      $("#sent-message").css("display", "none")
   }, 4000);

   window.setInterval(function() {
      $("#form-fast-query").animate({
         opacity: 'show'
      }, 600);
   }, 4000);
});


/**/
$(document).ready(function() {
   jQuery("select#order-area").change(function() {
      if ( this.value == -1 ) {
         $('#order-address').show();
      }
      else {
         $('#order-address').hide();
      }
   })
});

$(document).ready(function() {
   jQuery("select#order-group").change(function() {
      document.location = 'order.html?group_id='+this.value;
      /*
      if ( $('#order-good') ) {
         if ( $('#order-model') ) {
            $('#order-model option').remove();
         }
         $('#order-good option').remove();
         $('#order-good').append('<option value="0"></option>');
         $.post(
         '/scripts/ajax/get_goods.php',
         'group_id=' + $(this).val(),
         function ( data ) {
            $('#order-good option').remove();
            $('#order-good').append('<option value="0">Выберите модель</option>');
            for (var i=0; i<data.length; i++) {
               $('#order-good').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
            }
         },
         "json"
      );
      }*/
   })
});

$(document).ready(function() {
   jQuery("select#order-good").change(function() {
      if ( $('#order-model') ) {
         $('#order-model option').remove();
         $('#order-model').append('<option value="0"></option>');
		 
		 var objid = $(this).find('option:selected').attr('obj_id');
//		 alert(objid);
         $.post(
         '/scripts/ajax/get_models.php',
         'good_id='+objid,
         function ( data ) {
            $('#order-model option').remove();
            $('#order-model').append('<option value="0">Выберите модификацию</option>');
            for (var i=0; i<data.length; i++) {
               $('#order-model').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
            }
         },
         "json"
      );
      }
   })
});

$(document).ready(function() {
   jQuery("#catalog-card-table td.desc div.add a").click(function() {
      $(this).parents('td.desc').find("div.add").toggleClass('active');

      var str = ( $(this).parents('td.desc').find("div.add").hasClass('active') ) ? 'Скрыть фотографии и описание' : 'Фотографии и описание';
      $(this).html(str);

      if ( $(this).parents('td.desc').find("div.add").hasClass('active') ) {
         $(this).parents('td.desc').find('div.desc_add').animate({opacity: 'show'}, 600);
         $(this).parents('tr').find('td.img').find('div.desc_add').animate({opacity: 'show'}, 600);
      }
      else {
         $(this).parents('td.desc').find('div.desc_add').animate({opacity: 'hide'}, 600);
         $(this).parents('tr').find('td.img').find('div.desc_add').animate({opacity: 'hide'}, 600);
      }

      return false;
   })
});

$(document).ready(function() {
   jQuery("#catalog-card-table td.desc div.desc_add ul.tabs li a").click(function() {
      $(this).parents('ul.tabs').find('li').removeClass('current');
      $(this).parents('li').addClass('current');

      var tab = $(this).attr('rel');
      var tab_hide = ( tab == 1 ) ? 2 : 1;

      $(this).parents('div.desc_add').find('div.tab' + tab).show();
      $(this).parents('div.desc_add').find('div.tab' + tab_hide).hide();

      return false;
   })
});

/* ONLINE CONSULT */

$(document).ready(function() {
  showYourConsult();
  //showConsultWindow();
});

function showConsultWindow() {
  $('#your_consult #messagesBlock').slideDown();
}

function hideConsultWindow() {
  $('#your_consult #messagesBlock').slideUp();
}

function showYourConsult() {
  $('#your_consult').fadeIn();
}

function consultWindowToggle() {
  if ($('#your_consult #messagesBlock').css('display') == 'none')
    showConsultWindow();
  else
    hideConsultWindow();
}


var lastCheck = '';
var inboxCnt = 0;

$(document).ready(function(){
 
  getUpdates();
  setInterval("getUpdates()", 5000);
 
});

function sendMessage() {
  getUpdates('sendMessage')
}

function getUpdates() {
  var foo = 'r=' + Math.random();
  
  if (arguments.length > 0) { // Отправка сообщения
    foo = foo + '&sendMessageContent=' + encodeURIComponent($('#new_message').val());
    if ($('#userName').length) {
      foo = foo + '&sendMessageName=' + encodeURIComponent($('#userName').val());
      foo = foo + '&sendMessageEmail=' + encodeURIComponent($('#userEmail').val());
      foo = foo + '&sendMessagePhone=' + encodeURIComponent($('#userPhone').val());
      $('#messagesBlock #userProfile').hide();
    }
    $('#new_message').val('');
  }

  if (lastCheck.length)
    foo = foo + '&lastCheck=' + encodeURIComponent(lastCheck);
  
  $.post('../wake/consult/ajax.php', foo, function(xml){
            if ($(xml).find('message').length)
              showMessages($(xml).find('message'));
            lastCheck = $(xml).find('data > time').text();
        },
        'xml');


}

function showMessages(messages) {
  var html = '<table>';
  var is_admin;
  var newInboxCnt = 0;
  var name;
  
  for (i=0; i<messages.length; i++) {
    if($(messages[i]).find('dir').text() == '1') {
      is_admin = true;
      newInboxCnt = newInboxCnt + 1;
    } else {
      is_admin = false;
    }
    
    name = $(messages[i]).find('name').text();
    if (name.indexOf(':') != -1)
      name = name.substr(0, name.indexOf(':'));
  
    html = html + '<tr' + ((!is_admin) ? ' class="outbox"' : '') + '>';
    //html = html + '<td>' + $(messages[i]).find('time').text() + '</td>';
    html = html + '<td' + ((is_admin) ? ' style="color: #000;"' : '') + '><b>' + name + ':</b></td>';
    html = html + '<td style="color: #000;">' + $(messages[i]).find('content').text() + '</td>';
    html = html + '</tr>';
  }
  html = html + '</table>';

  if (messages.length) {
    $('#messagesHistory').show();
    $('#messagesHistory').html(html);
  }

  if (newInboxCnt > inboxCnt) {
    document.getElementById('bgsound').contentWindow.location.href = '../wake/consult/sound.php';
    showConsultWindow();
  }
  inboxCnt = newInboxCnt;
}
