// +++++++++++++++++++++++++++++++++++ //  
// ++++++ onDocumentReady ++++++++++++ //
// +++++++++++++++++++++++++++++++++++ //

$(document).ready(function() {
    
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Selection Box Replace ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  
    // Region Select
  $('#rSelect').selectbox();
    $('#rSelect_input').css("padding-left", "19px");
    $('#rSelect_input').css("background-image","url(/i/" +$("#rSelect option:selected").attr("id") + "_arrow.gif)");
    $('#rSelect_input').css("background-repeat", "no-repeat");
    $('#rSelect_input').css("background-position", "0px 1px");
    $('#rSelect_input').css("width", "103px");
  
    if (checkBrowserName('MSIE')) {
      $('#rSelect_input').css("width", "102px");
    } 

  $('#rSelect_container ul li').click(function() {
    imgNameArr =  this.id.split('_');
    imgName = imgNameArr[imgNameArr.length-1];
    if(imgName) {
      $('#rSelect_input').css("background-image","url(/i/" + imgName +"_arrow.gif)");
      $('#rSelect_input').css("background-repeat","no-repeat");
      $('#rSelect_input').css("background-position","0 1px");
    } else {
      $('#rSelect_input').css("background-image","");
    }
    option = '#'+imgName;
    if($(option).val()){
      window.location.href = $(option).val();
    }
  });


  // Language Select 
  $('#lSelect').selectbox();
    $('#lSelect_input').css("background-image","url(/i/arrow_select.gif)");
    $('#lSelect_input').css("background-repeat", "no-repeat");
    $('#lSelect_input').css("background-position", "right");
    $('#lSelect_input').css("width", "120px");
  
    if (checkBrowserName('MSIE')) {
    $('#lSelect_input').css("width", "119px");
    } 
    
  $('#lSelect_container ul li').click(function() {
    imgNameArr =  this.id.split('_');
    imgName = imgNameArr[imgNameArr.length-1];
    option = '#'+imgName;
    if($(option).val()){
      window.location.href = $(option).val();
    }
  });
  
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Tabs Settings         ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  
  
  if($("#tabsContainer").length > 0){
    
    // Hoehe festlegen - wenn js deaktiviert wird es
    // dann dennoch korrekt angezeigt
    $("#tabsContainer").css("height","160px");
    
    // perform JavaScript after the document is scriptable.      
    if($.browser.msie){
      var settings = {                
        //start from the beginning after the last tab
        rotate: true
      }
    }
    else{
      var settings = {                
      // enable "cross-fading" effect
      effect: 'fade',
             
      // start from the beginning after the last tab
      rotate: true
      }
    }
    
    $(function() {
        $("ul.tabs").tabs("div.panes > div", settings).slideshow({clickable: false});
        
        $(".tabs").data("slideshow").play();        
    });
    
  }
  
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Overlay               ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  $("a[rel='#overlay'], a[rel='#standard_overlay']").overlay({

        fixed : false,
        left  : "center",

        onBeforeLoad: function() {
            // grab wrapper element inside content
            var wrap = this.getOverlay().find(".contentWrap");

            // load the page specified in the trigger
            if(wrap){
              wrap.load(this.getTrigger().attr("href"));  
            }
            
            this.close();
        },
        
        onLoad: function(){
            //$('body').css("width","950px");
            //$('body').css("margin","auto");
        },
        
        onClose: function(){
            
            $('body').css("width","");
            $('body').css("margin","");
        }
    });
    
    $("a[rel='#image_overlay']").overlay({
      
        oneInstance: false,
      
        onBeforeLoad: function(event) {
          var img = this.getOverlay().find(".contentWrap img");
          var imgPath = this.getTrigger().attr("href");
          
          if(img.attr('src') != imgPath){
            
            img.load($.proxy(function() {
              this.load({'isDefaultPrevented': function(){return false;}});
            },this));
            img.attr('src',imgPath);
            
            event.preventDefault();  
          }else{
            img.unbind('load');
          }
          
        },
        
        onLoad: function(event){
          //console.log('ol Loaded');
        }
    });
  
  
  
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Menue                 ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  /*$('#menue-top a.item').hover(function(event){
    var subMenue = $(event.target.parentElement).find('.sub');
    
    subMenue.css('display','block');
    subMenue.animate({
      opacity: .95
    },100);
  },function(event){
    var subMenue = $(event.target.parentElement).find('.sub');
    
    subMenue.animate({
      opacity: 0
    },100,null,function(){subMenue.css('display','none');});
  });
  
  $('#menue-top .sub').hover(function(event){
    var subMenue = $(event.target);
    subMenue.css('display','block');
    subMenue.css('opacity',0.95);
  },function(event){
    var subMenue = $(event.target);
    subMenue.css('display','none');
    subMenue.css('opacity',0);
  });*/
  
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Designanpassungen     ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  
  // Grafik auf linke Tabellenspalte setzen.
  // Da diese keine eindeutige ID besitzt und uber 
  // CSS das Parent eines Element nicht in jedem
  // Browser ermittelt werden kann wird dies hier
  // uber JS geregelt
  if($('div#left').length == 1){
    var parentTd = $('div#left').closest('td');
    parentTd.attr('id','leftTd');
  }
  
  // +++++++++++++++++++++++++++++++++++ //  
  // ++++++ Aufklapp-Text         ++++++ //
  // +++++++++++++++++++++++++++++++++++ //
  
  $.each($('.collapseLink'), function(item){
    $(this).click(function(){
        $(this).parent().next('div.collapsed').slideDown('slow');
        $(this).hide();
        return false;
    });
 });
 
 $.each($('.slideUp'), function(item){
    $(this).click(function(){
        $(this).parent().slideUp('slow');
        $(this).parent().prev('.teaser').find('.collapseLink').show();
        return false;
    });
 });
  
});

// ++++++++++++++++++++++++++++++++++ //  
// ++++++++ Check Browser +++++++++++ //
// ++++++++++++++++++++++++++++++++++ //
function checkBrowserName(name){  
  var agent = navigator.userAgent.toLowerCase();  
  if (agent.indexOf(name.toLowerCase())>-1) {  
   return true;  
  }  
  return false;  
}
