$(document).ready(function() {
	// jQuery SmoothScroll
    $('a[href*=#]').click(function(){
    
        // duration in ms
        var duration = 1500;
        
        // easing values: swing | linear
        var easing = 'swing';
        
        // make sure it's the same location      
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname && this.hash.replace(/#/, '')) {
            // get parameters
            var hash = this.hash;
            var target = $(hash).offset().top;
            
            // animate to target and set the hash to the window.location after the animation
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: target
            }, duration, easing, function(){
                location.hash = hash;
            });
            
            // cancel default click action
            return false;
        }
    });
    
    /*
     * Menupunkte
     */
    $('#col1_content li img').each(function(){
        var $imageHeight = $(this).height();
        if($imageHeight > 23) {
            $(this).parent().parent().height(35);
        }
        else {
            $(this).parent().parent().height(17);
        }
    }); 
    
    // custom easing called "custom" 
    $.easing.custom = function (x, t, b, c, d) { 
        var s = 1.70158;  
        if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
        return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
    }
    
    // initialize scrollable  
    $("div.scrollable").scrollable({ 
        vertical:true,  
        size: 3,
        autoplay: true,
        steps: 1,
        interval: 2000,
        disabledClass: "disabled",
        hoverClass: "active",
        loop: false,
        keyboardSteps: 1
    }).mousewheel().circular().autoscroll();
    
    $("div.newsteaseritem, div.newsteaseritem2").hover(function() {
        $(this).stop().animate({ backgroundColor: "#dddddd" }, 600);
        },function() {
        $(this).stop().animate({ backgroundColor: "#FFFFFF" }, 400);
    });
    
    var itemwidth = 0;
    $("div.yearscroller .items .item").each(function(index){
        itemwidth = itemwidth + $(this).outerWidth() + 10;        
    });
    var offset = 0;
    //console.log(itemwidth);
    if (itemwidth > 500) {
        //offset = 319;
        offset = 600;
        $("div.yearscroller .items").css("left","-" + (itemwidth - offset) +"px");
    }
    else {
        $("div.yearnavigation a.prev").css("display","none");
    }
    $("div.yearnavigation a.next").addClass('disabled');
    
    $("div.yearscroller").scrollable({  
        size: 8,
        loop: false,
        disabledClass: "disabled",
        hoverClass: "active",
        keyboard: false 
    }).mousewheel();
    $("div.yearnavigation a.prev").removeClass('disabled');
    
    
    $("div.yearscroller div.items div a").click(function(event) {
        $("div.yearscroller div.items div a").removeClass('act');
        $("div.yearscroller div.items div").removeClass('act');
        $(this).addClass('act');
        var url = $(this).attr('href');
        $(".stripecolumns").append('<h3 id="loading">Bitte warten ...</h3>');
        $('.ajaxcontent').hide().load(url + ' .ajaxcontent', function()
         {
             $("#loading").remove();
             $('.ajaxcontent').fadeIn('slow');
             $("div.newsteaseritem").hover(function() {
                $(this).stop().animate({ backgroundColor: "#dddddd" }, 600);
                },function() {
                $(this).stop().animate({ backgroundColor: "#FFFFFF" }, 400);
            });
         });
        event.preventDefault();
    });
    
    // MODALBOX!!! 
    //
    // Teaserelement-Animationen
    $('div.body div#prev a img, div.body div#next a img').each(function(index){
        var $navitem = $(this);
        $navitem.css({opacity: 1.0});
        
        // Modalbox-Hover-Fading
        function navitemhoverover(){
            $navitem.stop().animate({
                opacity: '0.5'
            }, 'fast');
        };
        function navitemhoverout(){
            $navitem.stop().animate({
                opacity: '1.0'
            }, 'slow');
        };
        $navitem.hoverIntent({
            sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
            interval: 100, // number = milliseconds for onMouseOver polling interval
            over: navitemhoverover, // function = onMouseOver callback (required)
            timeout: 0, // number = milliseconds delay before onMouseOut
            out: navitemhoverout // function = onMouseOut callback (required)
        });
    });
    
    
jQuery.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: ""
  }, settings);
  var lookups = {
    title: function(input) {
      return $(input).attr("title");
    },
    label: function(input) {
      //return $("label[for=" + input.id +"]").text();
      //console.debug($(input).parent().prev().find('label').text());
      return $(input).parent().prev().find('label').text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = $(this);
  return $(this).each(function() {
    
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    };
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }

    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    $(this).data("label",lookup(this).replace(/\n/g,''));

    $(this).focus(function() {
        if (this.value === $(this).data("label")) {
        this.value = this.defaultValue;
        $(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
          if (this.value === $(this).data("label")) {
            this.value = this.defaultValue;
            $(this).removeClass(settings.labelledClass);
          }
      })
    };
    
    $(this).parents("form").submit(removeValuesOnExit);
    $(window).unload(removeValuesOnExit);

    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    // actually set the value
    if ($(this).val()) {
        this.value = $(this).val();       
    }
    else {
        this.value = $(this).data("label");
        $(this).addClass(settings.labelledClass);
    }
  });
};

// Nur die Labels von den Textfeldern eliminieren!
$("div.stripecolumns input[type=text], div.stripecolumns input[type=input], div.stripecolumns input[type=password]").parent().prev().find('label').css({display: 'none'});
$("div.stripecolumns input[type=text], div.stripecolumns input[type=input], div.stripecolumns input[type=password]").labelify({
  text: "label",
  labelledClass: "labelHighlight"
});


$('table.checkboxtable td.col1').each(function(index){
    $(this).click(function(){
        $(this).next().find('input').click();
    });        
});

// SubmitButton
$('div.submitbtn input').each(function(index){
    var inputwidth = $(this).width();
    $(this).parent().width(inputwidth + 20);        
});

$('div.submitbtn a').each(function(index){
    var inputwidth = $(this).width();
    $(this).parent().parent().width(inputwidth + 20);        
});


/*
* Slidecontainer
*/
$('.stripecolumns_small div.slideheader:first').addClass('highlight').css({'margin-bottom': '0px'});
$('div.slidecontainer:gt(0)').hide();
$('div.slideheader').each(function(){
    $slideheader = $(this);
    $slideheader.click(function(event){
        
        $(this).toggleClass("highlight");
        var $moretext = $(this).next();
        $moretext.animate({
            opacity: 'toggle',
            height: 'toggle'
        }, 'slow');
        if($moretext.css("opacity") != "1") {
            $(this).css({'margin-bottom': '0px'});    
        }
        else {
            $(this).css({'margin-bottom': '5px'});    
        }

        return false;
    
    });

});

$('#hiddence').hide();
$('a.more').click(function() {
    $('#hiddence').show();
    $('a.more').hide();
    return false;  
});


/*$("div.newsteaseritem, div.newsteaseritem2").each(function(index){
    $(this).click(function(event){
        var $tgt = $(event.target);
        if ($tgt.is('img, p, td')) {
            
            
            var href = $(this).find('a:last').attr('href');
            var $tgtlink = $(this).find('a:last');


            
            console.debug($tgt);
            console.debug(href);
            console.debug($tgtlink);
            $tgtlink.click(function(){'));console.debug('dsadas');});
            $tgtlink.trigger('click');
            //$tgtlink.click();              
            //window.location.replace(href);
            //event.stopPropagation();
            //return false;   
        }
    });
     
    

    if($(this).is("div.newsteaseritem2")) {
        var $sourcelink = $(this).find('a:last');    
    }
    else {
        var $sourcelink = $(this).find('a:last');
    }
    //console.debug($sourcelink);
    var $url="";
    var $target="";
    var $rel="";
    var $klasse="";
    
    var $url = 'href="' + $sourcelink.attr("href") + '"';
    if($sourcelink.attr("target")) $target = 'target="' + $sourcelink.attr("target") + '"';
    if($sourcelink.attr("rel")) $rel = 'rel="' + $sourcelink.attr("rel") + '"';
    if($sourcelink.attr("class")) $klasse = $sourcelink.attr("class");
    
    $(this).wrap('<a ' + $url + ' ' + $target + ' ' + $rel + ' ' + $klasse + ' />');
    $(this).parent().addClass($klasse + ' nolink');
    return false;

}); */

if ($('a.facebox').length > 0) {
    $faceboxurl = $('a.facebox').attr('href');
    $('a.facebox').attr('rel','facebox').attr('href', $faceboxurl + '?facebox=1');
    $.facebox.settings.opacity = 0.9;
    $('a[rel*=facebox]').facebox();
}


}); 
