/*preload images*/
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = 'typo3conf/ext/kch_template/templates/images/'+arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

jQuery.preLoadImages("Bg_Menu_Active.gif","Icon_Slideshow_TopPrevActive.gif","Icon_Slideshow_DownNextActive.gif","Icon_SearchHover.png");


/* tableruler */
$(document).ready(function(){
  function tableruler() {
    $("tbody tr").each(function(){
      if(this.className == 'headerrow' || $(this).parent().parent().hasClass('normaltable')) {
        return true;
      }
      $(this).hover(
        function() {$(this).addClass('highlight');},
        function() {$(this).removeClass('highlight');} 
      );
      $(this).click(
        function() {$(this).toggleClass('selected');} 
      );
    });    
  }
  tableruler();
});


/* exmpand table*/
var expandTable = {
  
  contentContainer:"#col2",
  needToBeChangedContainer:[".header_top_wrap",".header_bottom_wrap","#main","#footer_content","#col2"],
  expandTableWidth:74,
  isExpandDefault:0,
  morePadding:0,
  defaultWidth:51.8,

	expandTable:function(){
    var contentWidth = $(this.contentContainer).width()/10 - this.morePadding;
    var tableWidth = $(this.contentContainer).find(".expandtdwrap table").width()/10 > this.expandTableWidth ? $(this.contentContainer).find(".expandtdwrap table").width()/10 : this.expandTableWidth;
    var addWidth = tableWidth - contentWidth;
    
    
    $(this.contentContainer).find(".expandtdwrap p.showtable").hide();
    $(this.contentContainer).find(".expandtdwrap p.hidetable").show();
    
    $(this.contentContainer).find(".expandtdwrap").css({ width: "auto", overflow: "visible" });

    $(this.needToBeChangedContainer.toString()).each(function(){
      $(this).css({ width: $(this).width()/10 + addWidth + "em" });
    });
	},

	normalTable:function(){
    $(this.contentContainer).find(".expandtdwrap p.hidetable").hide();
    $(this.contentContainer).find(".expandtdwrap p.showtable").show();
    $(this.contentContainer).find(".expandtdwrap").css({overflow: "hidden",width:this.defaultWidth+'em'});
    
    $(this.needToBeChangedContainer.toString()).each(function(){
      $(this).css({ width: ""});
    });
	},	
	init:function(){
		jQuery(document).ready(function($){
      var mainobj = expandTable;
      var contentWidth = $(mainobj.contentContainer).width()/10 - mainobj.morePadding;
      $(mainobj.contentContainer).find(".expandtdwrap").css({ width: contentWidth + "em", overflow: "hidden" });
      $(mainobj.contentContainer).find(".expandtdwrap p.showtable").show();
      $(".expandtdwrap p.showtable a").click(function() {
        mainobj.expandTable();
        return false;
      })
      $(".expandtdwrap p.hidetable a").click(function() {
        mainobj.normalTable();
        return false;
      })
      if(mainobj.isExpandDefault) {
        $(".expandtdwrap p.showtable a").trigger("click");
      }
		})
	}
}

expandTable.init();

/* IE fix td colspan problem */
$(document).ready(function(){
  if($.browser.msie) {
    $('td[colspan][class^="center"]').each(function() {
      $(this).css({width:'auto'}); 
    });
  }
});

/* IE fix td colspan border bottom problem */
$(document).ready(function(){
  $('td[colspan][class^="center"][class*="1px"]').parent().each(function() {
    $(this).children().each(function() {
      if($(this).attr('rowspan') > 1 || $(this).attr('class').search(/nobottom_border/i) > -1) return true;
      if($(this).attr('class').search(/dar/i) > -1) {
        $(this).addClass('fixcolspandarkgrayborder');
      } else {
        $(this).addClass('fixcolspangrayborder');
      }
    });
  });
});

/* downloadcenter */
$(document).ready(function() {
  $('a.frmdcdownbt').click(function() {
      if($('#frmdc input:checked').length) {
        $('#frmdc').submit();
      }
  });  
  $('a.frmdcresetbt').click(function() {
    $('#frmdc input[type="checkbox"]').attr('checked','');
  });
});

$(document).ready(function(){
	//set lightbox text
	$("a[rel*='lightbox[lb']").each(function(){
   $(this).append('<img src="typo3conf/ext/kch_template/templates/images/zoom.gif" class="zoomimg" />');
   $(this).addClass('lightboxlink');
 });
});


(function($) {
  $.fn.adGallery2 = function(options) {
    var defaults = {
      animation: 15000,
      afterClickAnimation:15000,
      slideShowImage:'.slideshow2_image',
      slideShowNav:'.slideshow2_thumbnails'
    };        
    var settings = $.extend(false, defaults, options);
    $(this).each(function() {
      var gallery = new AdGallery2(this, settings);
    });
   };
    
    function AdGallery2(wrapper, settings) {
      this.init(wrapper, settings);
    };
    AdGallery2.prototype = {
      n:0,
      count:0,
      data:1,
      wrapper: false,
      settings: false,
      countdown_interval: false,
      num:0,
      init: function(wrapper, settings) {
        var context = this;
        context.wrapper = $(wrapper);
        context.settings = settings;
        context.initNav();
        context.count = context.wrapper.find(context.settings.slideShowImage).length;
        context.slideshowImagesArray = context.wrapper.find(context.settings.slideShowImage);
        context.slideshowNavArray = context.wrapper.find(context.settings.slideShowNav).find('a');
        
        if(context.wrapper.find(context.settings.slideShowNav).find('li').length >= 3) {
          context.wrapper.find(context.settings.slideShowNav).find('li').first().removeClass('first');
          context.wrapper.find(context.settings.slideShowNav).find('li').last().prependTo(context.wrapper.find(context.settings.slideShowNav));
          context.wrapper.find(context.settings.slideShowNav).find('li').first().addClass('first');
        }
          
        context.countdown_interval = setInterval(
          function() {
            context.showAuto();
          },
          context.settings.animation
        );
      },
      initNav: function() {
        var context = this;
		//set first nav
		context.wrapper.find(context.settings.slideShowNav).find('a').fadeTo(250, 0.5);
		context.wrapper.find(context.settings.slideShowNav).find('a').first().addClass('active').fadeTo(250, 1);
		
        context.wrapper.find(context.settings.slideShowNav).find('a').each(function() {
          $(this).data('index',context.data);
          context.data++;
        });
        context.wrapper.find('a.prev').bind('click', function() {
          
          if(context.wrapper.find(context.settings.slideShowNav).find('li').length >= 3) {
            context.wrapper.find(context.settings.slideShowNav).find('li').first().removeClass('first');
            for(var i=0;i<3;i++) {
              context.wrapper.find(context.settings.slideShowNav).find('li').last().prependTo(context.wrapper.find(context.settings.slideShowNav));
            }
            context.wrapper.find(context.settings.slideShowNav).find('li').first().addClass('first');
          }
          
        }),
        context.wrapper.find('a.next').bind('click', function() {
          if(context.wrapper.find(context.settings.slideShowNav).find('li').length >= 3) {
            context.wrapper.find(context.settings.slideShowNav).find('li').first().removeClass('first');
            for(var i=0;i<3;i++) {
              context.wrapper.find(context.settings.slideShowNav).find('li').first().appendTo(context.wrapper.find(context.settings.slideShowNav));
            }
            context.wrapper.find(context.settings.slideShowNav).find('li').first().addClass('first');
          }
          
        }),
        
        context.wrapper.find(context.settings.slideShowNav).find('a').bind('click', function(event,isTrigger) {
          if(!isTrigger) {
            clearInterval(context.countdown_interval);
            var i = $(this).data('index') - 1;
            context.n = i;     
          } else {
            var i = context.n;            
          }
          if (i >= context.count) return;
          
          if($(context.slideshowNavArray[i]).hasClass('active')) return;
          context.clearSlideshow();
          $($(context.slideshowNavArray[i])).addClass('active').fadeTo(250, 1);
          
          var activeIndex = context.wrapper.find(context.settings.slideShowNav).find('li a.active').parent().index();
          if(activeIndex > 1) {
            context.wrapper.find(context.settings.slideShowNav).find('li').first().removeClass('first');
            for(var ii=0; ii<activeIndex-1; ii++) {
              context.wrapper.find(context.settings.slideShowNav).find('li').first().appendTo(context.wrapper.find(context.settings.slideShowNav));              
            }
            context.wrapper.find(context.settings.slideShowNav).find('li').first().addClass('first');
          } else if(activeIndex == 0) {
            context.wrapper.find(context.settings.slideShowNav).find('li').first().removeClass('first');
            context.wrapper.find(context.settings.slideShowNav).find('li').last().prependTo(context.wrapper.find(context.settings.slideShowNav));
            context.wrapper.find(context.settings.slideShowNav).find('li').first().addClass('first');
          }
          
          if(context.slideshowImagesArray[i]) {
            $(context.slideshowImagesArray[i]).show();           
          }         
        }).hover(
          function() {
            if(!$(this).is('.active')) {
              $(this).fadeTo(250, 1);
            };
          },
          function() {
            if(!$(this).is('.active')) {
              $(this).fadeTo(250, 0.5);
            };
          });
      },
      clearSlideshow: function() {
        var context = this;
        context.wrapper.find(context.settings.slideShowNav).find('a').removeClass('active').fadeTo(250, 0.5);;
        context.wrapper.find(context.settings.slideShowImage).hide();
      },
      showAuto: function() {
        var context = this;
        context.num++;
        context.n = context.n >= (context.count - 1) ? 0 : context.n + 1;
        context.wrapper.find(context.settings.slideShowNav).find('a').eq(context.n).trigger('click',['true']); 

      }
      
    };
})(jQuery);

$(document).ready(function(){
  $('.slideshow2').adGallery2();
});

/* miniform button */
$(document).ready(function(){
  $('#tm_form input.submit, .btnsubmit, .btnreset').hover(
    function () { 
      $(this).addClass('hover');
    },
    function () { 
      $(this).removeClass('hover');
    }
  );
});

$(document).ready(function(){
  $('input.sbutton').hover(
    function () { 
      $(this).addClass('sbuttonhover');
    },
    function () {
      $(this).removeClass('sbuttonhover');
    }
  );
});

$(document).ready(function(){
  $('.reference_back').hide();
});


