$(document).ready(function(){
  // initialize
  sortHeight = parseInt($('.sortiment .item').css('height'));
  checkHeight();
  run_ajax_run();
  
  $('p + p').css({'margin-top':'16px'});
  $('h4 + h5').css({'margin-top':'-6px'});
  $('table + h5').css({'margin-top':'16px'});
  $('p + h4,p + h3').css({'margin-top':'20px'});
  
  $('.rel3-sttip').easysttips({'thema':'long2'});
  $(".oeffnungszeiten .easysttip-wrapper").css({"width":$(".oeffnungszeiten .easysttip-wrapper h4 span").width()+40});
  $(".feiertage .easysttip-wrapper").css({"width":$(".feiertage .easysttip-wrapper h4 span").width()+40});

  $('.llink').fancybox({ titleShow: false, hideOnOverlayClick: true});
  $('.inline-piclist a, a.inline-pic').fancybox({ hideOnOverlayClick: true});
  $('.rel4-sttip').easysttips();
  $('.jqtrans').jqTransform({imgPath:'site/css/img/'});

  $('.llink').mousemove(function(e){
    img  = $(this).find('img');
    icon = $(this).find('.plus-icon');
    posX = (e.pageX - img.offset().left);
                posY = (e.pageY - img.offset().top);
    fadeOut = 0;

     if (posX < (icon.width()/2)) {
      posX = (icon.width()/2);
      fadeOut = 1;
    }

     if (posX > (img.width() - icon.width()/2)) {
      posX = (img.width() - icon.width()/2);
      fadeOut = 1;
    }

    if (posY < (icon.height()/2)) {
      posY = (icon.height()/2);
      fadeOut = 1;
    }

    if (posY > (img.height() - icon.height()/2)) {
      posY = (img.height() - icon.height()/2);
      fadeOut = 1;
    }

    if (fadeOut == 1) {
      icon.fadeOut();
    }
    else {
      icon.fadeIn();
    }

    icon.css('left',(posX - icon.width()/2)+'px');
    icon.css('top', (posY - img.height() - icon.height()/2 - 2)+'px');
  });
  $('.llink').mouseenter(function(e){
    $('.plus-icon').remove();
    $(this).append('<div class="plus-icon" style="display: none;"></div>');
  })
  $('.llink').mouseleave(function(e){
    $('.plus-icon').remove();
  })
  $('.llink').click(function(){
    $('.plus-icon').addClass('active').remove();
  });

  //Show and Hide Value
  $('.inputtxt').click(function() {
    if (this.value == this.defaultValue) {
      this.value = '';
    }
  });
  $('.inputtxt').blur(function() {
    if (this.value == '') {
      this.value = this.defaultValue;
      $(this).focus().select();
    }
  });
  $('#txtarea').click(function() {
      $(this).focus().select();
  });

  // initialize tooltips
  sttip(".rel2-sttip");

    // get current nav point an span
  $('li.rex-active.lev1, li.rex-current.lev1').append('<span></span>');

  $('.landnav ul ul li').hover(function() {
    $(this).parent().addClass('hover');
  }, function() {
    $(this).parent().removeClass('hover');
  });

  // location list + map hover
  $('.adress-box').hover(function() {
    $(this).addClass('hover');
    var _mappoints = this.id;
    var _tiprel = $('.'+_mappoints).addClass('hover');
    var _tipul = $('.'+_mappoints+' div ul.stul');
    $('.'+_mappoints+' a.tiplink').addClass('hover');
    $('.'+_mappoints+' a.tlink').addClass('hover');
    var _tipul_position_left = parseInt(_tiprel.width()/2) - parseInt(_tipul.width()/2);
    var _tipul_position_top = (0 - _tipul.height()-25)-7;

    $('.'+_mappoints).addClass('ie7hover');

    if(jQuery.support.opacity) {
      _tipul.css({'display':'block'}).stop().animate({'top':_tipul_position_top,'opacity':'1'},{queue:false,duration:110});
    } else {        
      _tipul.css({'display':'block'}).stop().animate({'top':_tipul_position_top},{queue:false,duration:110});
    }

    $('.adress-box#'+_mappoints+' a').click(function() {
      $('.'+_mappoints).removeClass('ie7hover');
      
      $('.'+_mappoints).removeClass('hover');
      $('.'+_mappoints+' a.tiplink').removeClass('hover');
      $('.'+_mappoints+' a.tlink').removeClass('hover');
      $('a.tiplink').removeClass('current');
      $('.'+_mappoints+' a.tiplink').removeClass('hover').addClass('current');
      $('a.tlink').removeClass('current');
      $('.'+_mappoints+' a.tlink').removeClass('hover').addClass('current');
      if(jQuery.support.opacity) {
        _tipul.stop().animate({'top':(_tipul_position_top-10),'opacity':'0'}, 110, function() {
            $(this).css({'display':'none'});
        });
        } else {
        _tipul.stop().animate({'top':(_tipul_position_top-5)}, 110, function() {
            $(this).css({'display':'none'});
          });
      }
    });
    
  }, function() {
    $(this).removeClass('hover');
    var _mappoints = this.id;
    var _tiprel = $('.'+_mappoints).removeClass('hover');
    var _tipul = $('.'+_mappoints+' div ul.stul');
    $('.'+_mappoints+' a.tiplink').removeClass('hover');
    $('.'+_mappoints+' a.tlink').removeClass('hover');
    var _tipul_position_left = parseInt(_tiprel.width()/2) - parseInt(_tipul.width()/2);
    var _tipul_position_top = (0 - _tipul.height()-25)-7;

    $('.'+_mappoints).removeClass('ie7hover');

    if(jQuery.support.opacity) {
      _tipul.stop().animate({'top':(_tipul_position_top-10),'opacity':'0'}, 110, function() {
        $(this).css({'display':'none'});
        });
      } else {
      _tipul.stop().animate({'top':(_tipul_position_top-5)}, 110, function() {
        $(this).css({'display':'none'});
        });
    }
  });

  // map hover + location list
  $('.rel1-sttip, .rel2-sttip').hover(function() {
    $('.'+ this.id).addClass('hover');
  }, function() {
    $('.'+ this.id).removeClass('hover');
  });

  // create main dropdown
  $('#top li:has(ul) a.alev1').attr('href','#');
  $('#top li ul.rex-navi2').prepend('<li class="firstli"><span></span></li>');
  $('#top li:has(ul)').hover(function() {
    var bigsize = 0;
    var maptipsize = $(this).width();
    $(this).each(function(index) {
       $(this).find("li").each(function(index) {
        if($(this).width() > bigsize) {
          bigsize = $(this).width();
        }
      });
      mywith = bigsize+10;
    });
    if(mywith < maptipsize){
      mywith = $(this).width()+10;
      leftposition = 0;
    } else {
      // leftposition = parseInt((maptipsize - mywith)/2);
      // new set ever left
      leftposition = 0;
    }
    $(this).find("ul").css({'left':leftposition,'z-index':'80','width':mywith});
    $(this).next().find("span").css({'visibility':'hidden'});
    $(this).find("ul li").css('width',mywith);
    $(this).addClass('hover');
  }, function(){
    window.setTimeout(function() {

    }, 160);

    $(this).find("ul").css({'left':'-99999em','width':'auto'});
    $(this).next().find("span").css({'visibility':'visible'});
    $(this).find("ul li").css('width','auto');
    $(this).removeClass('hover');
  });
  
  // check if filter_id isset
  var filter_id = window.location.href.search(/filter_id/i);
  var filter = $('.filter');
  if (filter.length > 0) {
    var arrPosFilter = $('.filter').position();
    if(filter_id > 0) {
     // $('.filter').css('left', (arrPosFilter.left - arrPosFilter.left/6));
      $('#filter-title').addClass('active');
        $('.filter').css({'margin-left':'-'+$('.filter').width()/2+'px'});
    } else {
      $('#filter-option').hide();
    }
    $('.filter-radio').click(function(e) {
      $('.filter-radio').removeClass('active');
      $(this).addClass('active');
    });
    $('#filter-title').click(function(e) {
      if($(this).hasClass('active')){
        $(this).removeClass('active');
       // $('.filter').css('left', arrPosFilter.left);
        $('#filter-option').hide();
        $('.filter').css({'margin-left':'-'+$('.filter').width()/2+'px'});
      } else {
        $(this).addClass('active');
       // $('.filter').css('left', (arrPosFilter.left - arrPosFilter.left/6));
        $('#filter-option').show();
        $('.filter').css({'margin-left':'-'+$('.filter').width()/2+'px'});
      }
    });
  }
 // $('body').append('<div class="look"></div>');
 // $('.look').css({'position':'absolute','display':'block','width':'150px','height':'30px','border':'1px solid #000','top':'5px','left':'5px','padding':'10px'});
});

$(window).resize(function() {
    checkHeight();
});

function checkHeight(){
  var _filterpadding,
  _randpadding = (($(window).height()/5.31)*(0.0065))*50,
  _randheigh = (($(window).height()/5.61)*(0.0065))*110,
  _rp = (($(window).height()*5.61)*(0.0065))*0.4,
  _fp = (($(window).height()*5.61)*(0.0065))*1,
  _randposition1 = 115-(_rp),
  _randposition2 = 125-(_rp),
  _randposition3 = 1-(_rp);
  
  
  if($(window).height() <= 820) {
    if(_randheigh >= 110) {
      _randheigh = 110;
    } else if(_randheigh<=80) {
      _randheigh = 80;
    }
    if(_randpadding >= 50) {
      _randpadding = 50;
    } else if(_randpadding <= 40) {
      _randpadding = 40;
      _fp = 22;
    }
    $('.sortiment .item .firstheadline').css({'height': _randheigh});
    $('.sortiment .item .firstheadline h1').css({'margin-top': _randpadding});
    $('.sortiment .isubmit').css({'padding-top': _randheigh});
    $('.brownbg-sortiment').css({backgroundPosition: '0% '+_randposition1+'%'});
    $('.brownbg').css({backgroundPosition: '0% '+_randposition2+'%'});
    $('.sortiment #next, .sortiment #prev').css({'margin-top': _randheigh});
    $('.sortiment .item').css({'height': (sortHeight - _rp)});
  } else {
    $('.brownbg-sortiment').css({backgroundPosition: '0% 100%'});
    $('.brownbg').css({backgroundPosition: '0% 100%'});
  }
  if(_fp>=36) { _fp=36; }
  if(_randheigh>=118) { _randheigh=118; }
  _filterpadding = 351+_fp+(_randheigh);
  
  $('.filter').css({'top': parseInt(_filterpadding)});
 // $('.look').html(_fp + ' ' + _randheigh);

  if($(window).height() > 765) {
        $('#footernav').css('position', 'fixed').css('left', '0px').css('right', '0px').css('width', 'auto');
        $('#ifooter').css('margin-right', '23px').css('margin-left', '23px');
        $('#footernav .rightnav').css('right', '23px');
        $('#footernav .leftnav').css('left', '23px');
        $('#content').css('padding-bottom','0px');
    } else {
        $('#footernav').css('position', 'absolute').css('width', '100%').css('left', '0px').css('right', '0px');
        $('#ifooter').css('margin-right', '0px').css('margin-left', '0px');
        $('#footernav .rightnav').css('right', '0px');
        $('#footernav .leftnav').css('left', '0px');
        $('#content').css('padding-bottom','43px');
    }
}

// AJAX
function run_ajax_run(){
  accslider('#list2 li', '0', {showSpeed:250,hideSpeed:550});  
  $("ul li:first-child").addClass('first');
  $("ul li:last-child").addClass('last');
  $(".rel2-sttip ul li a").addClass('rel2tip');
  
  var _joblink = $(".landnav.jobs a");
  var _jobarea = $("#jobs");
  var _jobtitle = $(".jobstitle");
  var _jobcnav = $(".cnavjobs");
 
/*  _joblink.unbind();
  _joblink.bind('click', function(){
  $.ajaxSetup ({
      cache: false
    });

    var jobfile = $(this).attr("href") + '?&ajax=jobs';
    var jobtitle = $(this).attr("href") + '?&ajax=title';
    var jobcnav = $(this).attr("href") + '?&ajax=subnav';
    _jobarea.stop().load(jobfile);
    _jobtitle.stop().load(jobtitle);
    _jobcnav.stop().load(jobcnav);
    return false;
    });*/

  var _servicelink = $(".servicenav a, .clocktype a");
  var _servicearea = $("#service");
  var _servicecnav = $(".cnavjobs");
 
  _servicelink.unbind();
  _servicelink.bind('click', function(){
  $.ajaxSetup ({
      cache: false
    });
    if($(this).attr("rel")=='ku') {
      var reltyp = 'ku';
    } else {
      var reltyp = 'gu';
    }
    var servicfile = $(this).attr("href") + '?&ajax=jobs&reltyp='+reltyp;
    var serviccnav = $(this).attr("href") + '?&ajax=subnav&reltyp='+reltyp;
    _servicearea.stop().load(servicfile);
    _servicecnav.stop().load(serviccnav);
    return false;
    });
};

// st-tool-tip
function sttip(items, options) {
  var slideway = options && options.slideway || 15;
  var showSpeed = options && options.showSpeed || 110;
  var hideSpeed = options && options.hideSpeed || 110;
  var count = 0;
  $(items).each(function() {
    count++;
    var _tiprel = $(this);
    var _tiprel_id = this.id;
    

    
    var _tipul = $('#'+ _tiprel_id +' div ul.stul');
    var _tipul_position_left = parseInt(_tiprel.width()/2) - parseInt(_tipul.width()/2);
    var _tipul_position_top = (0 - _tipul.height()-25)-7;
    _tipul.hover(function() {
      $('#'+ _tiprel_id +' a.tiplink').addClass('hover');
    }, function(){         
      $('#'+ _tiprel_id +' a.tiplink').removeClass('hover');
    });
    
    if(jQuery.support.opacity) {
      _tipul.css({'left':_tipul_position_left,'opacity':'0','top':(_tipul_position_top-10)});
    } else {
      _tipul.css({'left':_tipul_position_left,'top':(_tipul_position_top-5)});
    }
    _tiprel.hover(function() {
      $(this).addClass('ie7hover');
      if(!_tiprel.hasClass('current')) {
        if(jQuery.support.opacity) {
          _tipul.css({'display':'block'}).stop().animate({'top':_tipul_position_top,'opacity':'1'},{queue:false,duration:110});
        } else {        
          _tipul.css({'display':'block'}).stop().animate({'top':_tipul_position_top},{queue:false,duration:110});
        }
      }
      _tiprel.click(function() {
        if(jQuery.support.opacity) {
          _tipul.stop().animate({'top':(_tipul_position_top-10),'opacity':'0'}, 110, function() {
               $(this).css({'display':'none'});
          });
          } else {
          _tipul.stop().animate({'top':(_tipul_position_top-5)}, 110, function() {
              $(this).css({'display':'none'});
            });
        }
      });
    }, function() {
      $(this).removeClass('ie7hover');
      if(jQuery.support.opacity) {
        _tipul.stop().animate({'top':(_tipul_position_top-10),'opacity':'0'}, 110, function() {
             $(this).css({'display':'none'});
           });
         } else {
        _tipul.stop().animate({'top':(_tipul_position_top-5)}, 110, function() {
             $(this).css({'display':'none'});
         });
      }
    });    
  });
};

// acc-slider 
function accslider(items, first, options) {
  var active = first;    
  var onClick = options && options.onClick || function(){};
  var onShow = options && options.onShow || function(){$(this).removeClass('default').addClass('current');};
  var onHide = options && options.onHide || function(){$(this).removeClass('current').addClass('default');};
  var showSpeed = options && options.showSpeed || 'slow';
  var hideSpeed = options && options.hideSpeed || 'fast';
  var showIt = function(){$(this).slideDown(showSpeed, function(){ });};
  var hideIt = function(){$(this).slideUp(hideSpeed, function(){ });};
  var typ = options && options.typ || 'accopenclose';

  $(items).not(active).children('div').hide();
  $(items).not(active).each(onHide);
  $(items).each(function(index) {
    $(this).attr("id",'accitem'+ index);
    var _accitem = '#accitem'+ index;
    var _accrel = $('#accitem'+ index +' h3').addClass('accrel'+ index);
    var _acccontent = $('#accitem'+ index +' h3').next('div');
    
    
    if(active==_accitem) {
      $(active).each(onShow);
      _acccontent.each(showIt);
    }

  _accrel.unbind();

  _accrel.bind('click', function(){
      if(typ=="openclose")
      {
        active = _accitem;
        if(!_acccontent.is(":hidden")) {
          _acccontent.each(hideIt);
          $(active).each(onHide);
        } else {
          _acccontent.each(showIt);
          $(active).each(onShow);
        }
      } 
      else if(typ=="openoneway")
      {
        _acccontent.each(showIt);
        active = _accitem;
        $(active).each(onShow);
      }
      else
      {
        if(typ=="accopenclose") {
          if(active==_accitem) {
            $(active).children('div').not(':hidden').each(hideIt);
            $(active).each(onHide);
            active = 0;
          } else {
            $(active).children('div').not(':hidden').each(hideIt);
            _acccontent.each(showIt);
            $(active).each(onHide);
            active = _accitem;
            $(active).each(onShow);
          }
        } else {
          if(!_acccontent.is(":hidden")) return false;
          $(active).children('div').not(':hidden').each(hideIt);
          _acccontent.each(showIt);
          $(active).each(onHide);
          active = _accitem;
          $(active).each(onShow);
        }
      }
      return false;
    });
  });
};

