var GB_ANIMATION = true;
var panelHeight = 194;

swfobject.embedSWF("business/flash/kasperskyHome.swf", "business_index_flash", "550", "388", "9.0.0", "business/flash/expressInstall.swf", {
  urlwork: 'protection_at_work.php',
  urlroad: 'protection_on_the_road.php',
  urlhome: 'protection_at_home.php',
  urlcloud: 'protection_in_the_cloud.php'
}, {
  wmode: 'transparent'
});

swfobject.embedSWF("business/flash/protectionEverywhere.swf", "protection_flash", "550", "358", "9.0.0", "business/flash/expressInstall.swf", {
  urlwork: 'protection_at_work.php',
  urlroad: 'protection_on_the_road.php',
  urlhome: 'protection_at_home.php',
  urlcloud: 'protection_in_the_cloud.php'
}, {
  wmode: 'transparent'
});



$(document).ready(function(){
  $(".lightbox").lightbox();

  $("a.greybox_buy, a.greybox_call").click(function(){
    GB_show(this.title || $(this).text() || this.href, this.href, 800, 420);
    return false;
  });
  $("a.greybox_chat").click(function(){
    GB_show(this.title || $(this).text() || this.href, this.href, 800, 800);
    return false;
  });
  $("a.greybox_email").click(function(){
    GB_show(this.title || $(this).text() || this.href, this.href, 780, 420);
    return false;
  });

  var maxPanelScroll, panelScrollHeight;
  $('#accordion').accordion({
    header: "h1",
    autoHeight: false,
    change: function(event, ui) {
      initSlider(ui.newContent, ui.oldContent);  
    }
  });
  initSlider($(".panel .ui-accordion-content-active"), false);
});

function initSlider(activePanel, oldPanel) {
  if (oldPanel) {
    $(".vertical_slider", oldPanel).slider("destroy");
  }

  var content = $(".slider_content", activePanel);
  if (content.length == 0) return;

  panelScrollHeight = content.get(0).scrollHeight;
  if (panelScrollHeight > panelHeight) {
    maxPanelScroll = panelScrollHeight - panelHeight + 100;
    /* + 100 to fix the incomplete scrolling */
    var slider = $(".vertical_slider", activePanel)
      .show()
      .slider({
        animate: true,
        orientation: "vertical",
        slide: handleSliderSlide,
        value: 100
      });
    var handleHeight = slider.find('.ui-slider-handle').height();
    slider.height($(".vertical_slider", activePanel).parents().find('.slider_wrapper').height() - handleHeight).css('margin-top', handleHeight/2);
    $('.panel .ui-accordion-content-active .slider_content').get(0).scrollTop = 0;
  }
}

function handleSliderSlide(e, ui) {
  //$("#debug").text("ui.value = " + ui.value + "; scrollTop = " + ( maxPanelScroll - Math.round(ui.value * (maxPanelScroll / 100)) ));
  $(ui.handle).parents().find('.slider_content').get(0).scrollTop = maxPanelScroll - Math.round(ui.value * (maxPanelScroll / 100));
}
