var svr = '';

var wsCarousel = {
	checkAjax: function() {
		var httpRequest = this.getHTTPRequest('text/plain');
    return (!httpRequest && !httpRequest.open)?false:true;
	},
	getHTTPRequest: function(mime) {
    var httpRequest = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
      try {      
         httpRequest = new XMLHttpRequest();
         if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType(mime);
         }
      }
      catch(e) { httpRequest =  false; }
    }
    else if (window.ActiveXObject) { // IE
      try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP");    } catch (e) { httpRequest = false; }
      try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { httpRequest = false; }        
            
    }
    return httpRequest;
  },
  setCarouselHTML: function(pTxt, pInt) {
  	/* Reset Animation */
  	wsCarousel.clearTeaserAnim();
  	
  	/* Format Teaser Container */
  	//document.getElementById('wsCarousel').style.width = this.getAnimWidth() + 'px';
  	
  	document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.position = 'absolute';    
    document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.width = this.getAnimWidth() + 'px';  	
  	document.getElementById('carouselSpacer').style.height = this.getAnimHeight() + 'px';
  	document.getElementById('carouselSpacer').style.position = 'static';
    
    /* Create new Teaser */
    var tmpDiv = document.createElement('div');
    tmpDiv.innerHTML = pTxt;    
    
    tmpDiv.getElementsByTagName('div')[0].style.position = 'absolute';
    tmpDiv.getElementsByTagName('div')[0].style.marginLeft = pInt * this.getAnimWidth() + 'px';
    tmpDiv.getElementsByTagName('div')[0].style.width  = this.getAnimWidth() + 'px';
    tmpDiv.getElementsByTagName('div')[0].style.clip = 'rect(0,0,0,0)';
    
    document.getElementById('wsCarousel').insertBefore(tmpDiv.getElementsByTagName('div')[0],document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx]);
    
    /* Reset strict Attributes */
    wsCarousel.setLinkAttributes();
  	
  	/* Update MediaArray */
  	var tmpArr = pTxt.split("mediaArr.push(new mediaObj('");  	
  	
  	for(var i = 1; i < tmpArr.length; i++) { 
  	    var tmpStr = '';
  	    tmpStr += tmpArr[i].split("'))\;")[0];
  	
  	    var isInArray = false;
  	    for(var j = 0; j < mediaArr.length; j++) {
  	    	 if(mediaArr[j].nodeId == tmpStr) { 
  	    	 	 mediaArr.splice(j,new mediaObj(tmpStr));
  	    	 	 mediaArr[j].init();
  	    	 	 isInArray = true;
  	    	 }
  	    }
  	    if(!isInArray) {    
  	       mediaArr.push(new mediaObj(tmpStr));
  	       mediaArr[(mediaArr.length - 1)].init();
  	    }
  	}
  	/* Animate Teaser Change */
  	this.animId = setTimeout('wsCarousel.animTeaser('+pInt+')',wsCarousel.animDelay);
  },
  animWidth:  512,
  animHeight: 200,  
  getAnimWidth:  function() { return document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetWidth; },
  getAnimHeight: function() { return document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetHeight; },
  animState: false,
  animDelay: 20,
  animScrollAmountX: 16,
  animTeaser: function(pInt) {
  	
  	this.animState = true;  	
    
    /* scroll Y */
    var tmpScrollHeight = Math.ceil(this.animHeight + (this.getAnimHeight() - this.animHeight)/6);
    this.animHeight = tmpScrollHeight;   

  	/* scroll X */
  	var tmpScrollOffset = parseInt(document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.marginLeft);  	
  	tmpScrollOffset = (!isNaN(tmpScrollOffset))?tmpScrollOffset:0;
  	if(tmpScrollOffset != 0) {
  	   if(pInt > 0 && tmpScrollOffset > 0) {
  		    tmpScrollOffset -= this.animScrollAmountX;
  		    tmpScrollOffset = Math.max(0,tmpScrollOffset);
  	   }
  	   else if(pInt < 0 && tmpScrollOffset < 0) {
  		    tmpScrollOffset += this.animScrollAmountX;
  		    tmpScrollOffset = Math.min(tmpScrollOffset,0);
  	   }
  	   else {
  		    tmpScrollOffset = 0;
  	   }
  	   var tmpTeaserPos = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx + 1].offsetWidth;  	   
  	   tmpTeaserPos *= -1*pInt;
  	   tmpTeaserPos += tmpScrollOffset;
  	    
  	   /* Anim 2nd Teaser */
  	   
  	   var clipRectStr = 'rect(0,0,0,0)';
  	   
  	   if(tmpTeaserPos > 0) {
  	      clipRectStr  = 'rect(0, ';  
  	      clipRectStr += 2 + wsCarousel.getAnimWidth() + 'px, ';
  	      clipRectStr += this.animHeight + 'px, ';
  	      clipRectStr += wsCarousel.getAnimWidth() - tmpTeaserPos + 'px)';
  	   }
  	   else if(tmpTeaserPos < 0) {
  	   	  clipRectStr  = 'rect(0, ';  
  	      clipRectStr += 2 + (-1*tmpTeaserPos) + 'px, ';
  	      clipRectStr += this.animHeight + 'px, ';  	      
  	      clipRectStr += '0)';  	        
  	   }
  	   document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.marginLeft = tmpScrollOffset + 'px';
  	   document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.clip = clipRectStr;
  	   
  	   var tmpOtherTeaserPos = wsCarousel.getAnimWidth();
  	   tmpOtherTeaserPos *= -1*pInt;
  	   tmpOtherTeaserPos += tmpScrollOffset;
  	   
  	   /* Anim 1st Teaser */
  	   
  	   clipRectStr = 'rect(0,0,0,0)';
  	   
  	   if(tmpOtherTeaserPos > 0) {
  	      clipRectStr  = 'rect(0, ';  
  	      clipRectStr += 2 + wsCarousel.getAnimWidth() - tmpOtherTeaserPos + 'px, ';
  	      clipRectStr += this.animHeight + 'px, ';  	      
  	      clipRectStr += '0)';
  	   }
  	   else if(tmpOtherTeaserPos < 0) {
  	   	  clipRectStr  = 'rect(0, ';  
  	      clipRectStr += 2 + wsCarousel.getAnimWidth() + 'px, ';
  	      clipRectStr += this.animHeight + 'px, ';  	      
  	      clipRectStr += -1*tmpOtherTeaserPos + 'px)';      
  	   }
  	   document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx + 1].style.marginLeft = tmpOtherTeaserPos + 'px';
  	   document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx + 1].style.clip = clipRectStr;
  	   
  	   document.getElementById('carouselSpacer').style.height = this.animHeight + 'px';
  	   
  	   
  	   this.animId = setTimeout('wsCarousel.animTeaser('+pInt+')',wsCarousel.animDelay);
    }
  	else { wsCarousel.clearTeaserAnim(); }
  },
  clearTeaserAnim: function() {
  	if(wsCarousel.animState) { 
  		document.getElementById('wsCarousel').removeChild(document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx + 1]);
  		wsCarousel.animState = false;
  	}
  	clearTimeout(wsCarousel.animId);
  	
  	document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.marginLeft = '0';
  	
  	document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.width = '512px';
  	document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.overflow = 'visible';
  	
  	document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].style.position   = 'static';  	
  	document.getElementById('carouselSpacer').style.height = 0 + 'px';
  	document.getElementById('carouselSpacer').style.position = 'absolute';
  	
  	this.animHeight = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetHeight; 
    //this.formatNaviElements();
  },
  getCarouselHTML: function(pUrl,pInt) {
  	var httpRequest = this.getHTTPRequest('text/plain');
    if (!httpRequest) { return false; }
    try {    
    	httpRequest.open('GET', pUrl, true);
      httpRequest.send(null);    
      httpRequest.onreadystatechange = function() {
      	if (httpRequest.readyState == 4) {
           if (httpRequest.status == 200) { wsCarousel.setCarouselHTML(httpRequest.responseText,pInt); }
           else {  }
        }
        else {  }
      }
         
    } catch(e) {  }
  },
  
  
  linkIdx: 1,
  linkIdxMax: 0,
  
  divIdx: 0,
  initializeCarouselNavi:  function() {  	
  	/* which DIV is Top-Teaser */
  	var carouselDivArr = document.getElementById('wsCarousel').getElementsByTagName('div');
  	for(var i = 0; i < carouselDivArr.length; i++) { 
  		if(carouselDivArr[i].className == 'wsTeaserCont') { this.divIdx = i; break; }
  	}
  	
  	/* Hide No-JS-Navi */
  	document.getElementById('wsCarouselNaviCont').style.position = 'absolute';
  	document.getElementById('wsCarouselNaviCont').style.top = '-5000px';
  	
  	/* set fixed Height */
  	document.getElementById('ewTopTeaser').style.height = 320/16 + 'em';
  
    /* get Number of possible Teasers (length of li) */
  	var carouselLinkArr = document.getElementById('wsCarouselNavi').getElementsByTagName('a');
  	this.linkIdxMax = carouselLinkArr.length;
  	
  	/* get Index of visible Teaser (1st <span /> in List) */
  	this.linkIdx = parseInt(document.getElementById('wsCarouselNavi').getElementsByTagName('span')[0].innerHTML);
  	this.linkIdx = (!isNaN(this.linkIdx))?this.linkIdx:1;   	
  	
  	/* Initialise Navi-Buttons */
  	this.setOnClickToNavi();
  	
  	/* Position Navi-Buttons
  	this.formatNaviElements();
  	
  	this.correctionAfterImageLoading(); */

 },
  setOnClickToNavi: function() {
  	document.getElementById('wsCarouselBack').style.display = 'block';
  	document.getElementById('wsCarouselBack').onclick = function() {
                                   if(!wsCarousel.animState) {
                                   closeAllOpenPlayer();
                                   
                                   wsCarousel.animWidth  = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetWidth;
                                   wsCarousel.animHeight = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetHeight;
                                   
                                   wsCarousel.linkIdx = (((--wsCarousel.linkIdx) > 0)?wsCarousel.linkIdx:(wsCarousel.linkIdxMax - 1));
                                   wsCarousel.getCarouselHTML(svr + '/wahl/codebase/jsp/showkteaser.jsp?pt_rubrik=' + carouselRubrik + '&pt_num=' + wsCarousel.linkIdx,-1);
                                   }            
                                   return false;
  		                            };
		document.getElementById('wsCarouselNext').style.display = 'block';  		                            
  	document.getElementById('wsCarouselNext').onclick = function() {
                                   if(!wsCarousel.animState) {
                                   closeAllOpenPlayer();
                                   
                                   wsCarousel.animWidth  = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetWidth;
                                   wsCarousel.animHeight = document.getElementById('wsCarousel').getElementsByTagName('div')[wsCarousel.divIdx].offsetHeight;
                                   
                                   wsCarousel.linkIdx = (((++wsCarousel.linkIdx) < wsCarousel.linkIdxMax)?wsCarousel.linkIdx:1);
                                   wsCarousel.getCarouselHTML(svr + '/wahl/codebase/jsp/showkteaser.jsp?pt_rubrik=' + carouselRubrik + '&pt_num=' + wsCarousel.linkIdx,+1);
                                   }              
                                   return false;
  		                            };
  },
  setLinkAttributes: function() {
  	setLinkAttrCollection(document.getElementById('wsCarousel').getElementsByTagName('a'));
  }
};

if(wsCarousel.checkAjax()) {
addLoadEvent(
  function() {   	
   	wsCarousel.initializeCarouselNavi();
  }
);
}

