
  var gullposition;
  var gullwidth = 0;
  var gullheight = 0;
  //var bn = '';
  var bn = currentbasename();
  
    function ColorImg(obj) {
    var src = obj.attr('src');
    if (src.indexOf('index') < 0) {    
      if (src.indexOf('_grey')> -1 || src.indexOf('_in')> -1 ) {
        src = src.replace('_in','_out');
        obj.attr('src',src);
        MakeGrey();
        bn = '#i' + tbasename(obj.parent().attr('href'));
        if ($(bn).length > 0) {
          src = $(bn).attr('src');
          gullposition = $('#gull').position();
          FlyGull(bn);
          if (src) {
            src = src.replace('_grey','');
            $(bn).attr('src',src);
          };
        };
      };
    };
  }
  
  function MakeGrey() {
    $(".ntable img").each(function(i){
      var sid = '#'+this.id;
    	var src = $(sid).attr('src');
    	if (src.indexOf('_grey')<0) {
      	src = src.replace('.jpg','_grey.jpg');
        $(sid).attr('src',src);
      };
    });
  }
  
  
  //https://github.com/MmmCurry/jquery.crSpline
  function FlyGull(target) {
  
    var vx=Math.floor(gullposition.left);
    var vy=Math.floor(gullposition.top);
    
    if (vx != Math.floor($('#gull').position().left)) {return};
  
		
    var minX = 200;
  	var minY = 200;
  	var maxX = $(window).width() - 300;
  	var maxY = $(window).height() - 300;
  
  	var numPoints = 5;
  	var dotsPerSeg = 50;
  	var i;
  	var ntime = 7000;
  
  	var points = [];
  	points.push([vx,vy]); 
  	
  	var oldnsrc = $('#gull').attr('src');
    var oldnsrc_bn = tbasename(oldnsrc); 
    
    if (target.length == 0 || $(target).length == 0) {     
     for (i=0; i<numPoints; i++) {
    		points.push([Math.floor(Math.random()*(maxX-minX))+minX, Math.floor(Math.random()*(maxY-minY))+minY]);
     };
  	 points.push([vx,vy]);
    }
    else if ($(target).length > 0) {
      var vx1=Math.floor($(target).position().left + $(target).width()/2);
      var vy1=Math.floor($(target).position().top - $('#gull').height() + 10);
      points.push([vx1,vy1]);
      var st = parseInt((vx1-vx) * (vy1-vy));
      if(st == 0) { 
        ntime = 1000;
      }
      else {
        ntime = 2500;
      };
    };
    
    
       
    var nsrc = oldnsrc.replace(oldnsrc_bn,'gull_r');
    $('#gull').attr('src',nsrc);

  	var spline = $.crSpline.buildSequence(points);
		
		
		$('#gull').stop().animate({
        crSpline: spline
      }, ntime, function() {
        if (target.length == 0) {
          $('#gull').removeAttr('style');
        }
        else {
          oldnsrc = oldnsrc.replace(oldnsrc_bn,'gull_sitting_left');
        };
        $('#gull').attr('src',oldnsrc);
      });
		
		
  }
  
  function setbasehref(basehref) { 
    var thebase = document.getElementsByTagName("base"); 
    thebase[0].href = basehref; 
  }
  
  function currentbasename() {
    var path = document.URL;
  	return tbasename(path);
  } 
  
  function tbasename(path) {
  	var dbn = path.replace(/\\/g,'/').replace( /.*\//, '' );
  	dbn = dbn.substring(0, dbn.lastIndexOf("."));
  	return dbn;
  } 

function DoLayout() {

  
    //do layout because CSS is so adequate
    
    /* impossible to make differences according to previous element 
    The basic goal of layout is to setup relations between elements. 
    The cascading concept is as useful as a hole in the head.*/
    var bbb = $('h2').prev().outerHTML();
    if (bbb.indexOf('<h1')<0) {
      $('h2').css('margin-top','4ex');
    };
    
    var cssscks = $('#topics').height();
    var cssssht = $('.tdiv').height();
    if (cssscks > 2*cssssht) {
      var ooo = '<div class="tdiv" style="margin-bottom:0;padding-bottom:0">';
      var fff = '</div>';
      var ddd = $('.anotherdivbecausecssissoadequate').children().length;
      var cssthll = parseInt(ddd/2)+2;
      var hcss = '';
      for (i=cssthll;i<=ddd;i++) {
        hcss += ooo + $(".anotherdivbecausecssissoadequate :nth-child("+i+")").html() + fff;
      };
      for (i=cssthll;i<=ddd;i++) {
        $(".anotherdivbecausecssissoadequate :nth-child("+i+")").html('');
      };
      var lll = parseInt($('.tdiv').width()/2);
      $("#dontbelieveit").prepend('<div class="anotherdivbecausecssissoadequate" style="padding-right:'+lll+'px">' + hcss + '</div>');
      lll = parseInt($(".anotherdivbecausecssissoadequate :nth-child("+ddd+")").position().left - $("#logoimg").width());
      lll = parseInt((lll - $("#content").position().left)/2);
      $("#logoimg").css('left',lll+'px');
      lll = parseInt($(".anotherdivbecausecssissoadequate :nth-child(1)").position().top + cssssht - $("#logoimg").height());
      $("#logoimg").css('top',lll+'px');
    }
    else {
      var ddd = $('.anotherdivbecausecssissoadequate').children().length;
      var lll = $(".anotherdivbecausecssissoadequate :nth-child("+ddd+")").position().left;
      var rrr = $("#logoimg").offset().left + $("#logoimg").width();
      if (rrr > lll) {
        lll = rrr - lll;
        lll = parseInt($("#logoimg").width() - lll);
        $("#logoimg").css('width',lll+'px');
      };
    };
    cssscks = $('#anotherwrapper').position().top + $('#anotherwrapper').height();
    var menwra = $('#anotherwrapper').position().top + $('#menuwrapper').height();
    cssssht = $(window).height() * 0.93;
    if( $(window).height() > cssscks && cssscks > menwra) {
      cssscks = cssssht - cssscks + $('#anotherwrapper').height();
      if ($.browser.msie) {cssscks = cssscks + 30}
      $('#anotherwrapper').css('height',cssscks+'px');
    }
    else if (menwra > cssscks) {
      $('#anotherwrapper').css('height',menwra+'px');
    };
    
    cssscks = $('#anotherwrapper').height() + $('#menueimg').height();
    if(cssscks < 1039) {
      $('#content').css('background-position','top left');
    };
    
    if (bn.indexOf('index') > -1) {
      $('#footer').css('display','none');
    };
    
    gullposition = $('#gull').position();
    
    var ppp = $(location).attr('href');
    if(ppp.indexOf('_')<0) {
      //???setbasehref("http://www.windjammer-shop.de");
    };
    
    /* because of cascading misconception */
    if(bn.indexOf('_') > 0) {
      $(".current").css('color','#c00000');
    }
    else {
        $(".current").css('display','none');
    };

    
}


/* Flags */

function Flags() {
  
  reins= document.fflags.elements[0].value;
  var j=document.fflags.elements[0].value.length;
  
  if (j>20) {j=20; alert("Leider sind 20 Zeichen das Maximum. Überzählige Zeichen werden abgeschnitten.")}
  
  var imgs = [];
  
   
  $("#pflags img").each(function(k){
    $(this).attr('src','images/blank.gif');
    imgs.push($(this)); 
  });

  for (var ii=0;ii<j;ii++) {
   chara=reins.substring(ii,ii+1);
   chara=chara.toLowerCase();
   if (chara != " ") {
    imgs[ii].attr('src','images/' + chara + '.gif');
   };
  };


}


/* Bootsausstellung */


  
function MapOver(salt){  
  if (salt) {
      var io = salt.indexOf('halle');
      if (io>1) {
        MapOut(lasthref);
        var divno = salt.substring(io+5);
        divno = divno.replace('.html','');
        $('#hr'+divno).css('color','#900');
        var simg = $('#im'+divno).attr('src');
        simg = simg.replace('.gif','_o.gif');
        $('#im'+divno).attr('src',simg);
        simg = $('#hr'+divno).html();
        $('#mapdescr div').html('<p>'+simg+'</p>');
        lasthref = salt;
      };
  };
}

function MapOut(salt){
  $("#maptable tr td a").each(
    function(intIndex) {
    $(this).removeAttr('style');
    }
  );
  $("#maptable tr td img").each(
    function(intIndex) {
      var simg = $(this).attr('src');
      simg = simg.replace('_o.gif','.gif');
      $(this).attr('src',simg);
    }
  );
}
  
function DoAnimation() {

  //duplicate div, because animation needs position absolute
  //as CSS sucks, FF / IE / Opera are incompatible here regarding the required CSS style -- all in different ways
  //therefore, top and left pos must be made using pixel parameters
  var newhtml = '<div id="mapdescr2">' + $('#mapdescr').html() + '</div>';
  var obj = $(newhtml);
  
  obj.width($('#mapdescr').width());  
  var le = parseInt($('#mapdescr').position().left);
  var tox = parseInt($('#mapdescr').position().top);
  
  obj.css('top',tox+'px');
  obj.css('left',le+'px');
  
  // actually it would be required to append to #badiv because of CSS cascading misconception, but due to IE bugs this is not possible
  var whyever = 0;
  if ($.browser.msie) {
    $('#throwieoffthemarket').css('display','block');
    if (lasthref.length > 0) {
      document.location.href = lasthref;
    };
  }
  else {
    obj.appendTo('#badiv');
    whyever = parseInt(obj.position().top) - parseInt($('#mapdescr').position().top);
    obj.css('margin-top','-'+whyever+'px'); 
  };
  
 
  $('#badiv').css('display','block');
  
  //make original div unvisible without modifying div positions
  $('#mapdescr').html('')
                .css('border-color','transparent')
                .css('background','transparent');
  
  //animation target values
  var wi = $('#menueimg').width()-5;
  le = parseInt($('#menueimg').position().left);
  tox = parseInt($('#menueimg').position().top)+whyever+$('#menueimg').height();
   
  if (lasthref.length > 0) { // page bootsausstellung.html
    
    if ($.browser.msie) {
      //alert('throw ie off the market');
    }
    else {
    $('#mapdescr2').animate({
          left: le,
          width: wi,
          top: tox
        }, 1000, function() {
          $('#mapdescr2 div').html(initext);
          $('#mapimg').css('display','none');
          $('#maptable').css('display','none');
          document.location.href = lasthref;
    
        }); 
    };
       
  }
  
  else if (bn.indexOf('_halle')>-1) { // page bootsausstellung_halle*.html

    $('#mapdescr2').css('left',le+'px')
                   .css('width',wi+'px')
                   .css('top',tox+'px');
    $('#mapimg').css('display','none');
    $('#maptable').css('display','none');
    $('#closemap').css('display','block');
    
    if ($.browser.msie) {
      $('h2').css('margin-top','0');
    }
    else {
      var hhh = $('#badiv').height();
      $('h2').css('margin-top','-'+hhh+'px');
    };
    
  }
  
  else {
  
    return true;
  
  };
}

/* Others */

function OpenWin(w) {
var ww
if (w.search(".htm") > -1) {ww=w}
else {ww= w + ".html"}
openedw=window.open(ww,'obenedw', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=650,height=500');
}


function PopPic(w) {
if (w.search("images/") < 0) {ww="images/" + w}
window.open( "picpopup.html?"+ww, "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=200,height=200");
}


function switchPage(fullPath) {
document.location.replace('frame_' + fullPath.substring(fullPath.lastIndexOf('/')+1,fullPath.length));
} 


jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};
