


$(function() {

  // external links
  $('a[rel=external]').attr("target", "_blank").attr("title", "Externer Link offnet in neuem Tab/Fenster");

  // sidebars
  $('#left').hide();
  var left = $('#left').html();
  if (left) $('#col1_content').html(left);

  $('#right').hide();
  var right = $('#right').html();
  if (right) $('#sidebar_right').html(right);
  if ($('#right').attr('class')) $('#sidebar_right').addClass($('#right').attr('class'));

  // gallery
  $("a[rel^='gallery']").prettyPhoto({
    showTitle: false, /* true/false */
    overlay_gallery: false,
    social_tools: '',
    theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
  });

  // fading images
  $('img.fade').hover(function(){
    var s = $(this).attr('src'),
    s = s.replace(/_30\.gif/, '.gif');
    $(this).attr('src', s);
  },function(){
    var s = $(this).attr('src'),
    s = s.replace(/\.gif/, '_30.gif');
    $(this).attr('src', s);
  });


  // popups
  $("a[rel^='popup']").prettyPopin({
       modal : true, /* true/false */
       width : false, /* false/integer */
       height: false, /* false/integer */
       opacity: 0.8, /* value from 0 to 1 */
       animationSpeed: 'fast', /* slow/medium/fast/integer */
       followScroll: true, /* true/false */
       loader_path: '/images/loader.gif', /* path to your loading image */
       start_callback: function() { /* required zeitform version */
	      $('#overlay').bgiframe(); // ie6

	      // handle label-less inputs (value)
	      //$('input').blur();
	      $('input.nolabel').each(function(){
       	         if ($(this).attr('rel') && ! $(this).val()) { 
                    $(this).val($(this).attr('rel')); 
                 }
	      });

	      $('input.nolabel').focus(function(){
		 if ($(this).attr('rel') == $(this).val()) {
		   $(this).val('');
		 }
              });
	      $('input.nolabel').blur(function(){
		 if ($(this).attr('rel') && ! $(this).val()) {
		   $(this).val($(this).attr('rel'));
		 }
              });

	      $('#save').click(function(){
	        $('input.nolabel').each(function(){
       	          if ($(this).attr('rel') == $(this).val()) { 
                    $(this).val(''); 
                  }
	        });
	      });

	      $('#bookhotel').change(function(){$('tr.hotel').toggle();});

	      // pb abonnieren
	      if ($('#input_pb_abonnieren'))
		{
		  //$('input.stueck').parent().hide();
		  $('input[name=pb]').change(function(){
  	            var c = $(this).is(':checked');
                    var s = $(this).parent().parent().next('td').find('span');
		    if (c) { $(s).show(); } else { $(s).hide(); } 
		    //console.log('checked ', c);
                  });
		}

	      //$("a.tooltip,area.tooltip").tooltip({
	      //  bodyHandler: function() {
	      //	  return $($(this).attr("href")).html();
	      //  },
	      //  showURL: false
              //});
              $('.prettyPopin').height('800px'); // looks like this fixes height problems in IE7 
	      
       }
	//callback: function() { 
	//  $('#tooltip').hide();
	//}
    });
 
  
  
  // open popup
  $('.show_popup th span').click(function(e){
    e.stopPropagation();
    //console.log('open');
    $('div.popup').hide();
    $(this).next('div.popup').show().jScrollPane();
  })
    .css('cursor','pointer');

  // scroll
  //$('div.popup').jScrollPane();


  // select entry
  $('div.popup p').click(function(e){
    e.stopPropagation();
    //console.log('open');
    var id = $(this).attr('id');
    if (id) {
      var a = id.split(/_/);
      //$('#' + a[0]).val(a[1]);
      //console.log(a);

      // disable others
      //$('table.show_popup tr th span.auswahl').html('Auswahlmen&uuml;').removeClass('active');
      $('table.show_popup tr th span.auswahl').each(function(){
        $(this).html($(this).next('div').find('h3').html());
        $(this).removeClass('active');
      });
      $('table.show_popup tr td span').removeClass('active');
      //$('#praepsearch input').val();

      $('#pzn').val('Pharmazentralnummer PZN');

      // enable current
      $('#menu_' + a[0]).html($(this).html()).addClass('active');
      $('#title_' + a[0]).addClass('active');

      $('#key').val(a[0]);
      $('#value').val(a[1]);

      search_praeparate();
    }
    $('div.popup').hide();
  })
    .css('cursor','pointer');

  // close popup
  $('body').click(function(e){
    //console.log('close');
    // do not close on clicks on the scrollbar
    if($(e.target).hasClass('jspDrag') || $(e.target).hasClass('jspTrack')){
      return true;
    }
    $('div.popup').hide();
  });

  $('#pzn').blur(function(){
    if ($('#pzn').val().length == 0) $('#pzn').val('Pharmazentralnummer PZN');
  });
  $('#pzn').focus(function(){
    //$('table.show_popup tr th span.auswahl').html('Auswahlmen&uuml;').removeClass('active');
    $(this).val('');
    $('table.show_popup tr th span.auswahl').each(function(){
        $(this).html($(this).next('div').find('h3').html());
        $(this).removeClass('active');
      });
    $('table.show_popup tr td span').removeClass('active');
    $('#searchresults').hide();
  });
  $('#pzn').keyup(function(){
    var v = $('#pzn').val();
    if (v.length > 2)
      {
	$('#key').val('pzn');
	$('#value').val(v);
        search_praeparate();
      }
  });

  // close subnavi
  $('body').click(function(){
    $('ul.pulldown').hide();
  });

  // subnavi
  $('.hlist>ul>li>a').hover(function(){
    $('body').stopTime("hide");
    $('ul.pulldown').hide();
    $(this).next('ul.pulldown').show();
  },function(){
    return;
    //$('body').oneTime(2000, "hide", function() {
    //   $('ul.pulldown').hide();
    //});
  });

  $('ul.pulldown').hover(function(){
    $('body').stopTime("hide");
  },function(){
    //return;
    $('body').oneTime(2000, "hide", function() {
       $('ul.pulldown').hide();
    });
  });

  // add  call for all but top level navi
  //var href = window.location.pathname;
  //if (href.indexOf("/", 1) > 0) { $('ul.pulldown').addClass('border'); };


  // toggle div.infobox
  var visible = $('div[rel="infobox"]').is(':visible');
  //console.log('visible: ', visible);
  $('div[rel="infobox"]').each(function(i){
    var id = $(this).attr('id').replace(/\D+/g, '');
    var self = this;
    if (id) {
      $('#contentnavi').append('<img src="/images/button-contentnav.png" width="20" height="20" alt="v' + id + '" class="v' + id + '" />');
      var tt = $(self).find('h4').html();
      if (tt) { 
	$('img.v' + id).attr('title', tt);
      }
      if ($(self).is(':visible')) {
	$('img.v' + id).attr('src', '/images/button-contentnav_active.png');
      }
      $('span.v' + id + ',img.v' + id).click(function(){
        $('div[rel="infobox"]').hide();
        $('#contentnavi img').attr('src', '/images/button-contentnav.png');
	$(self).show();
	$('img.v' + id).attr('src', '/images/button-contentnav_active.png');

      }).css('cursor','pointer');
    }
    //console.log(i);
    // activate first
    if (!visible && i == 0) {
      $(self).show();
      $('img.v' + id).attr('src', '/images/button-contentnav_active.png');
    }
  });

  $('#search-detail').submit(function(){
    search_consilium_archive();
    return false;
  }).css('cursor','pointer');

  // search on enter
  $('#search').keypress(function(e){
    if(e.which == 13){
      $('#search-detail').submit();
    }
  });

  // full search
  $('#searchbutton').click(function(){
     $('#form_search').submit();
   }).css('cursor','pointer');

  $('#searchlabel').click(function(){
    search_consilium_archive();
    return false;
  }).css('cursor','pointer');


 });

function search_consilium_archive(o) {
  var form = $('#search-detail');
  $('#step').val(o);
  var data = form.serialize(); 

  //data["o"] = o;
  //var search =  $('#search').val();  

  $.ajax({
        type: "POST",
	url: "/cgi-bin/archiv_suche.pl",
	data: data, //{ s : search, o: o },
	success: function(msg){
	  //console.log(msg);
          //console.log(msg.substr(0,3));
  	  //$('#search-results').html(msg);
	  //$('#search-paediatrie').show();
	$('#search-paediatrie').remove();
	$('#contentnavi').remove();
	$('#search-detail').after(msg);
        }
  });
  
}


function search_praeparate() {
  var form = $('#praepsearch');  
  var data = form.serialize(); 

  //console.log(data);

  $.ajax({
        type: "POST",
	url: "/cgi-bin/search_products.pl",
	data: data,
	success: function(msg){
	  //console.log(msg);
          //console.log(msg.substr(0,3));
	  if (msg.substr(0,3) == "NOK") {
	    $('#searchresults').hide();
	    $('#nosearchresults').show();
	  } else {
	    $('#nosearchresults').hide();
	    $('#searchentries').html(msg);
	    $('#searchresults').show();
	  }
        }
  });
  
}




