$(function(){
  dbg('document loaded');
  
  // pimp navi
  if(document.getElementById("secnavi_17")){
		document.getElementById("secnavi_17").firstChild.onclick = function(){ return !showbox('loginbox', 1); };
	}
	if(document.getElementById("secnavi_115")){
		document.getElementById("secnavi_115").firstChild.onclick = function(){ return !showbox('logoutbox', 1); };
	}
	/*
	if(document.getElementById("secnavi_16")){
		document.getElementById("secnavi_16").firstChild.onclick = function(){ return !showbox('searchbox', 1); };
	}
	if(document.getElementById("secnavi_14")){
		document.getElementById("secnavi_14").firstChild.onclick = function(){ return !showbox('newsletterbox', 1); };
	}
	*/
	
	// some image content types become slideshows
	var slideShow = {
	  slideTime: 4000,
	  currentImage: 0,
	  oldImage: 0,
	  imgLength: 0,
	  imgHeight: 0,
	  fadeOutTime: 200,
	  fadeInTime: 200
	};
  slideShow.allImages = $('.csc-textpic-intext-left.csc-textpic-border > div.csc-textpic-imagewrap div.csc-textpic-imagerow');
  dbg('slideShow.allImages', slideShow.allImages);
  slideShow.imgLength = slideShow.allImages.length;
  dbg('slideShow.allImages.length', slideShow.imgLength);
  
  // use height and position of first image
  slideShow.imgHeight = $('img', slideShow.allImages[0]).height();
  slideShow.imgWidth = $(slideShow.allImages[0]).width();
  slideShow.imgPos = $(slideShow.allImages[0]).offset();
  
  // all images : set properties and hide all but first
  var firstImg = true;
  $(slideShow.allImages).each(function(){
    dbg('image-this', this);
    $(this).css({
      'position': 'absolute'
      /*
      ,
      'height': slideShow.imgHeight,
      'top': slideShow.imgPos.top,
      'left': slideShow.imgPos.left
      */
    });
    // don't hide first
    if(firstImg){
      firstImg = false;
    }else{
      $(this).hide();
    }
    
  });
  
  dbg('slideShow', slideShow);
  
  // set container css
  $('.csc-textpic-intext-left.csc-textpic-border > div.csc-textpic-imagewrap').css({
   //'position': 'relative',
   'height': slideShow.imgHeight,
   'width': slideShow.imgWidth,
   'overflow': 'hidden'
  });
  
  // start loop
  slideShow.interVal = setInterval(function(){
    //dbg('now hiding image #', slideShow.currentImage);
    $(slideShow.allImages[slideShow.currentImage]).fadeOut(slideShow.fadeOutTime, function(){
      
      slideShow.currentImage += 1;
      if(slideShow.currentImage >= slideShow.imgLength){
        slideShow.currentImage = 0;
      }
      //dbg('now showing image #', slideShow.currentImage);
      $(slideShow.allImages[slideShow.currentImage]).fadeIn(slideShow.fadeInTime / 5);
      
    });
    
  }, slideShow.slideTime);
});

var showbox = function(boxname, soh){
	document.getElementById(boxname).style.display = soh == 1 ? 'block' : 'none';
	return true;
}

var dbg = function(title, obj){
  if(obj === undefined){
    obj = title;
    title = 'unnamed';
  }
  try{
    console.debug(title, obj);
  }catch(e){};
}

/* thanks to PPK (http://www.quirksmode.org/js/cookies.html) */
var readCookie = function(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 null;
}

