
$(document).ready(function() {

  var section = 'drainage';
  if ($('body').attr('class').search(/drainage/) == -1) {
    section = 'pressure';
  }

  // select-all in searchbox
  $('#buttonmenu input.textbox').focus(function() { $(this).select(); });

  // lightbox
  var lightboxoptions = { overlayBgColor: '#666666',
                          overlayOpacity: 0.6,
                          imageLoading: '/style/lightbox-ico-loading.gif',
                          imageBlank: '/style/lightbox-blank.gif',
                          imageBtnClose: '/style/lightbox-btn-close.gif',
                          imageBtnPrev: '/style/lightbox-btn-prev.gif',
                          imageBtnNext: '/style/lightbox-btn-next.gif'
                        };
  $('a.lightbox').lightBox(lightboxoptions);

  // mainmenu
  $('.menu').each(function() {
    var menu = this;
    $('li', menu).hover(function() { var menuitem = this; $(menuitem).addClass('active'); if ($('ul', this).length) $(menuitem).addClass('active-more'); }, 
                        function() { var menuitem = this; $(menuitem).removeClass('active'); $(menuitem).removeClass('active-more'); });
  });

  // blocks rounded corners
  $('.portal div.block').each(function() {
    $(this).prepend('<img class="t" alt="" src="/style/portal-block-t.gif"/><img class="b" alt="" src="/style/portal-block-b.gif"/>');
  });

  $('.snippet div.block').each(function() {
    $(this).prepend('<img class="tl" src="/style/'+section+'/sideblock-tl.gif"/><img class="tr" src="/style/'+section+'/sideblock-tr.gif"/><img class="bl" src="/style/'+section+'/sideblock-bl.gif"/><img class="br" src="/style/'+section+'/sideblock-br.gif"/>');
  });

  $('.page-content div.block').each(function() {
    $(this).prepend('<img class="tl" src="/style/'+section+'/main-tl.gif"/><img class="tr" src="/style/'+section+'/main-tr.gif"/><img class="bl" src="/style/'+section+'/main-bl.gif"/><img class="br" src="/style/'+section+'/main-br.gif"/>');
  });

  // linklist fix ie
  if ($.browser.msie) $('.linklist img.header').each(function() {
      var img = this;
      var linklist = $(this).parent();
      var header = $('h2', linklist);
      var fixedhtml = '<img class="header" src="'+$(img).attr('src')+'" alt=""/><h2>'+$(header).html()+'</h2>';
      $(img).remove();
      $(header).remove();
      $(linklist).prepend(fixedhtml);
  });
  
  // linklist rounded corner + shadows
  $('.linklist').each(function() {
    $(this).prepend('<div class="shadow-b"></div><div class="shadow-r"></div><img class="br" alt="" src="/style/'+section+'/linklist-br.gif"/>');
  });
  $('.linklist li a').each(function() {
    $(this).before('<img alt="*" src="/style/'+section+'/arrow-black.gif"/>');
  });
  // if linklistheight > enclosing block, make the block longer
  $('.match-linklist-height').each(function() {
      var myheight = $(this).height();
      var listheight = $('.linklist', this).height();
      if (myheight<listheight-20) $(this).height(listheight-20);
      $('.linklist', this).css({'bottom':'-1px', 'top':'auto'});
  });

  // linklist mouseover
  $('.linklist').each(function() {
    var ll = this;
    var llimg = $('img.header', ll);
    var llimgsrc = llimg.attr('src');
    $('li', ll).hover(function() { llimg.attr('src', $('a img', this).attr('src')); }, 
                      function() { llimg.attr('src', llimgsrc); });
  });

  // special tooltip
  $('div.atooltip a').hover(function() { $(this).parents('.atooltip').addClass('active'); }, 
                            function() { $(this).parents('.atooltip').removeClass('active'); });

  // info toggles
  $('dt').each(function() {
    $(this).prepend('<img src="/style/'+section+'/expanded.gif" alt="-" class="icon"/>');
  });
  $('dt.closed').each(function() {
    $(this).next('dd').css({'display': 'none'});
    $('img.icon', this).attr('src', '/style/'+section+'/collapsed.gif').attr('alt', '+');
  });
  $('dt').click(function() {
    if ($(this).next('dd').css('display') != 'block') {
      $(this).next('dd').css({'display': 'block'});
      $('img.icon', this).attr('src', '/style/'+section+'/expanded.gif').attr('alt', '-');
    }
    else {
      $(this).next('dd').css({'display': 'none'});
      $('img.icon', this).attr('src', '/style/'+section+'/collapsed.gif').attr('alt', '+');
    }
  });

  // flash replacement of image
  $('img.flash').each(function() {
    var replacement_for = $(this).prev('img');
    var embedstring = '<div id="banner"><embed src="'+$(this).attr('src')+'" width="644" height="186" menu="false" wmode="opaque" loop="true"/></div>';
    $(replacement_for).css({'display': 'none'});
    $(replacement_for).before(embedstring);
    $(replacement_for).remove();
  });
  $('img.mceItemFlash').each(function() {
    var paramstring = $(this).attr('title');
    var regex = new RegExp("src:('|\")([^'\"]+)('|\")", "i");
    var src = regex.exec(paramstring);
    //var embedstring = '<embed src="'+src[2]+'" width="'+$(this).attr('width')+'" height="'+$(this).attr('height')+'" menu="false" wmode="opaque" loop="true"/>';
    var embedstring = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+$(this).attr('width')+'" height="'+$(this).attr('height')+'" id="flashmovie" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+src[2]+'" /><param name="quality" value="high" /><param name="wmode" value="opaque" /><param name="menu" value="false" /><param name="loop" value="true" /><param name="bgcolor" value="#ffffff" /><embed src="'+src[2]+'" quality="high" bgcolor="#ffffff" width="'+$(this).attr('width')+'" height="'+$(this).attr('height')+'" name="flashmovie" align="middle" menu="false" wmode="opaque" loop="true" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    $(this).replaceWith(embedstring);
  });

  // print click
  $('a[href=#print]').click(function() {
    print_preview();
    return false;
  });

});

function setActiveStyleSheet(title)
{
  $('link').each(function() {
    if ($(this).attr('rel').indexOf('style') != -1 && $(this).attr('title')) {
      $(this).attr('disabled', true);
      if ($(this).attr('title') == title) {
        $(this).attr('disabled', false);
      }
    }
  });
}

function print_preview()
{
    setActiveStyleSheet('print-preview');
}

function cancel_print_preview()
{
    setActiveStyleSheet('default');
}