function hideFlash() {
  var embeds = document.getElementsByTagName('embed');
  for (i = 0; i < embeds.length; i++) {
    embeds[i].style.visibility = 'hidden';
  }
  var objects = document.getElementsByTagName('object');
  for (i = 0; i < objects.length; i++) {
    objects[i].style.visibility = 'hidden';
  }
}

function showFlash() {
  var embeds = document.getElementsByTagName('embed');
  for (i = 0; i < embeds.length; i++) {
    embeds[i].style.visibility = 'visible';
  }
  var objects = document.getElementsByTagName('object');
  for (i = 0; i < objects.length; i++) {
    objects[i].style.visibility = 'visible';
  }
}

function theSlider(slidetime, interval) {
  setInterval('rotate(' + slidetime + ')', interval);
}

function rotate(slidetime) {
  var selector = 'div.slider ul.frames';
  var current = ($(selector + ' li.current') ? $(selector + ' li.current') : $(selector + ' li:first'));
  var next = current.next('li').length > 0 ? current.next('li') : $(selector + ' li:first');
  var selIdx = 'div.slider ul.navi';
  var currIdx = ($(selIdx + ' li.selected') ? $(selIdx + ' li.selected') : $(selIdx + ' li:first'));
  var nextIdx = currIdx.next('li').length > 0 ? currIdx.next('li') : $(selIdx + ' li:first');
  current.fadeOut(slidetime, function() {
    $(this).removeClass('current');
    currIdx.removeClass('selected');
  });

  next.fadeIn(slidetime, function() {
    nextIdx.addClass('selected');
    $(this).addClass('current');
  });
};

$(document).ready(function() {
  
  // site search form submit ---------
  $('#sitesearch').delegate(null, 'submit', function() {
    if (jQuery.trim($('#searchquery').val()) != '') {
      window.location.href = $(this).attr('action') + '?sq=' + encodeURI($('#searchquery').val());
    }
    return (false);
  });
  // ---------------------------------

  // flash banners -------------------
  $('div.swfbnr').each(function() {
    $(this).flash({
        src: $(this).find('a').attr('href'),
          width: $.browser.mozilla ? 193 : 191
      },
      { version: 8 }
    );
    $(this).find('div.alt').remove();
  });
  // ---------------------------------

  // quick catalog group links -------
  $('a.quickgrp').delegate(null, 'click', function() {
    var id = $(this).attr('href');
    $('div.prodwrapper').slideUp('slow').addClass('collapsed');
    $(id).parent().next().find('h3.clps').trigger('click');
    return (false);
  });
  // ---------------------------------

  // collapsing blocks ---------------
  $('.clps').delegate(null, 'click', function() {
    if (!$(this).next().hasClass('collapsed')) {
      $(this).next().slideUp('slow').toggleClass('collapsed');
    }
    else {
      $(this).next().slideDown('slow').toggleClass('collapsed');
    }
  });
  // ---------------------------------

  // main page slider ----------------
  theSlider(2000, 9000);
  $('div.slider ul.navi a').delegate(null, 'click', function() {
    var newIdx = $('div.slider ul.navi li').index($(this).parent());
    $('div.slider ul.frames li.current').removeClass('current').hide();
    $('div.slider ul.navi li.selected').removeClass('selected');
    $('div.slider ul.frames li:eq(' + newIdx + ')').fadeIn('slow').addClass('current');
    $('div.slider ul.navi li:eq(' + newIdx + ')').addClass('selected');
    return (false);
  });
  // ---------------------------------


  // swf popup box -----------------
  $.each($('a.videopopup'), function () {
    var url = $(this).attr("href");
    $(this).colorbox({
      width: "768px",
      height: "600px",
      initialWidth: '20px',
      initialHeight: '20px',
      transition: 'elactic',
      html:"<p>Загрузка видео</p>",
      opacity: 0.7,
      'onLoad': function () {
        $('#cboxClose').hide();
        hideFlash();
      },
      'onComplete': function () {

        $.getScript('/scripts/jwplayer.js', function (data, textStatus) {
          jwplayer("cboxLoadedContent").setup({
            flashplayer: "/scripts/player.swf",
            file: url,
            width: 718,
            height: 510,
            autostart: true
          });
        });
        $('#cboxClose').show();
      },
      'onClosed': function () {
        jwplayer().stop();
        showFlash();
      }

    });
  });
  // ------------------------------  

  // photos popup box --------------
  $('a.thumb[rel="popup"]').colorbox({   
    initialWidth: '20px',
    initialHeight: '20px',
    transition: 'elactic',
    maxWidth: '1000px',
    opacity: 0.7,
    current: "",
    'onLoad': function () {
      $('#cboxClose').hide();
      hideFlash();
    },
    'onComplete': function () {
      $('#cboxClose').show();
    },
    'onClosed': function () {
        showFlash();
    }
  });
  
    $('a.popup').colorbox({
    
    initialWidth: '20px',
    initialHeight: '20px',
      maxWidth: '1000px',
    transition: 'elactic',
    opacity: 0.7,
    current: "",
    'onLoad': function () {
      $('#cboxClose').hide();
      hideFlash();
    },
    'onComplete': function () {
      $('#cboxClose').show();
    },
    'onClosed': function () {
        showFlash();
    }
      
    
  });
  // ------------------------------  

});

