/*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_TopMenu_Hover.gif");


/* 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:"#centerc",
  needToBeChangedContainer:["#header","#footer","#main","#centerc"],
  expandTableWidth:74,
  isExpandDefault:0,
  morePadding:0,
  defaultWidth:60,

	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();



/* font-resize  */
// Initialize the arrays containing our size info.
var pixelArray = new Array('62.5%','72.5%','82.5%','92.5%','102.5%'); // Possible font px sizes
var countOfPixels = pixelArray.length;
var initSize = 0; // Array position of inital px size
var cookiesitename = "Henkel-font-size";

function fontSizer(inc) {
	/*if (!document.getElementById) return;*/	
	var size = readCookie(cookiesitename);
	size = parseInt(inc)+parseInt(size);
	if (size < 0 ) { size = 0; }	
	if (size > countOfPixels ) { size = countOfPixels-1; }
	
	doFontSizing(size);	
	
	createCookie(cookiesitename, size, 365);
}

function fontSizerOnLoad() {	
	/*if (!document.getElementById) return;*/
	
	var size = readCookie(cookiesitename);	
	if (size < 0 ) { size = initSize; }
	if (size > countOfPixels ) { size = countOfPixels; }	
	/*switchPic();*/
	doFontSizing(size);
}

function doFontSizing(theFontSize) {			
	// Bugfix: Elements inside table would not resize with only the code below, so we do them seperatly.
	// NOTE: This only resizes the font, nothing else.
	// Resize by ID does not work with this.
	//aTables = document.getElementsByTagName('table');
	//for(i = 0; i < aTables.length; i++){
	//		aTables[i].style.fontSize = pixelArray[theFontSize];
	//}
	
	// if you rather want to size an individual element by ID, use this:
	// resizeContainer = document.getElementById('nameOfConainingElement');	
	// and uncomment the if statements at the start of each function
	resizeContainer = document.getElementsByTagName('body')[0];
	if(resizeContainer.style.fontSize != pixelArray[theFontSize])		
	resizeContainer.style.fontSize = (theFontSize != initSize) ? pixelArray[theFontSize] : '';	
	
}

function normalSize() {
	/*if (!document.getElementById) return;*/
	
	var size = initSize;
	
	doFontSizing(size);	
	createCookie(cookiesitename, size, 365);
}

function normalSizePrint() {
	/*if (!document.getElementById) return;*/
	
	var size = initSize;
	
	doFontSizing(size);	
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "expires="+date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name+'='+value+'; '+expires+'; path=/';
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' '){ 
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return initSize;
}

$(document).ready(function() {
  fontSizerOnLoad(readCookie());  
  $(".font_bigger").click(function() {
		fontSizer(1);
	});
	$(".font_normal").click(function() {
		normalSize();
	});	
	$(".font_smaller").click(function() {
		fontSizer(-1);
	});  
});

/* 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');
      }
    });
  });
});

$(document).ready(function(){
  $('#topmenus li').hover(
    function() {
      $(this).addClass('hover');
    },
    function() {
      $(this).removeClass('hover');
    }  
  );
});

/* 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');
 });
});



