/* ---------------------------------------------*\
|
| some functions for gliding images
|
| (c) Jody Weissmann jody.xha@gmail.com
\*----------------------------------------------*/

//-- width of shrinked images - to be filled at runtime
aWidths = new Array();

//-- img elements
aElems = new Array();

sImgDir = ""; // "images/";


delta = 1;
iPause=70;

nextImgIndex  = 0;
firstElIndex = 0;
nextElIndex  = 0;
curOffset  = 0;
numElems   = 0;
numVis     = 0;
numImgs    = 0;

g_iW = 0;
g_iH = 0;
g_bFullWidth=false;

var divGlideBox;

iStep = 0;


//----------------------------------------
// start0
//   preload the images
//
function start(iW, iH, aImgs) {
    g_iW = iW;
    g_iH = iH;
//alert("started with g_iW="+g_iW+", g_iH="+g_iH);
    iLeft=0;
    numImgs = aImgs.length;
    // set box style


    divGlideBox = document.getElementById(g_sGlideBox);
    sAttrs = "margin:auto;position: absolute; height:"+g_iH+"px ; overflow:hidden; bottom:0px;";
    sAttrs += "background-image:url(images/header_grad.png); background-repeat:repeat-y; background-position: right;";
    if (g_iW < 0) {         
        g_bFullWidth=true;
        if(g_browser.isIE6) {
          // disgusting IE6 hack, because it can't handle "left" and "right"
          var par = divGlideBox.parentNode.parentNode.parentNode;
          iWP = par.offsetWidth;
          iWP -= 20;
          sAttrs += "left:-100px; width:"+iWP+"px;";
        } else {
          sAttrs += "left:10px; right:10px;";
        }
    } else {
	sAttrs += "width:"+g_iW+"px;";
    }

//alert("setting style of glidebox: "+divGlideBox+" to "+sAttrs);
    _setStyle(divGlideBox, sAttrs);

    // get actual width of box
    g_iW = divGlideBox.offsetWidth;
  
    positionImages(/*iW, iH,*/ aImgs);
}

//----------------------------------------
// positionImages
//   fill the box initially
//
function positionImages(/*iW, iH,*/ aImgs) {
    totWidth = 0;
    while (totWidth < g_iW) {
  //            alert(totWidth + " pixels used");
	if (nextImgIndex > 0) {
	    totWidth += aElems[nextImgIndex-1].width;
	}


	var e = document.createElement("img");
	e.setAttribute("height", g_iH);
	e.setAttribute("id", "img_"+nextImgIndex);
	e.setAttribute("src", sImgDir+aImgs[nextImgIndex]);
	e.setAttribute("linkid", aLinks[nextImgIndex]);
//	e.setAttribute("onclick", "display('"+aLinks[nextImgIndex]+"');");
        _setOnclick(e);
	_setStyle(e, "position:absolute; left:"+totWidth+"px; height:"+g_iH+"px; bottom:0px; cursor:pointer");

	//	var e = createNode(sImgDir+aNames[nextImgIndex], aLinks[nextImgIndex], g_iH, totWidth); 

	divGlideBox.appendChild(e);
	aElems[numElems] = e;
	aWidths[numElems] = e.width;
	numElems++;
	numVis++;
	nextImgIndex++;
	if (nextImgIndex >= numImgs) {
	    nextImgIndex = 0;
	}
	
    }	
    
    nextElIndex = 0;

    //    alert("now moving images");

    moveImages(/*iW, iH*/);
}


 
//------------------------------------------
// moveImages
//   moveImages to the left
//   if total visible length less than box:
//     if !available img element
//        create new
//     get next available, position it , set src
//   }
//   if curoffs = width[firstElIndex]
//     make image element available
//
function moveImages(/*iW, iH*/) {
    iStep++;
    
    // move all images
    iL = -curOffset;
    i = 0;
    while (i < numVis) {
	j = i + firstElIndex;
	if (j >= numElems) {
	    j-= numElems;
	}
	var ei = aElems[j];
	
	_setStyle(ei, "position:absolute; bottom:0px; left:"+iL+"px; cursor:pointer;");
	iL += aWidths[j];
	i = i+1;
    }

    curOffset+=delta;

    // do we need next at tail?
    if (iL < g_iW) {
	
	
	// create new node
	var ei0 = document.createElement("img");
	ei0.setAttribute("height", g_iH);
	ei0.setAttribute("src", sImgDir+aNames[nextImgIndex]);
	ei0.setAttribute("id", "img_"+nextElIndex);
	ei0.setAttribute("linkid", aLinks[nextImgIndex]);
//	ei0.setAttribute("onclick", "display('"+aLinks[nextImgIndex]+"');");
        _setOnclick(ei0);
	_setStyle(ei0, "position:absolute; height:"+g_iH+"px; bottom:0px; left:"+iL+"px; cursor:pointer");
	
	//var ei0 = createNode(sImgDir+aNames[nextImgIndex], aLinks[nextImgIndex], g_iH, iL); 

		
	if (nextElIndex == firstElIndex) {
	    // first is still occupied, make new one
	    divGlideBox.appendChild(ei0);
	    
	    // shift array contents to make space for new one 
	    backShiftE(aElems,  firstElIndex, numElems-firstElIndex);
	    backShiftW(aWidths, firstElIndex, numElems-firstElIndex);
	    
	    aElems[firstElIndex]  = ei0;
	    aWidths[firstElIndex] = ei0.width;
	    numElems++;
            
	    firstElIndex++;
	    
	} else {
	    var ei = aElems[nextElIndex];
	    divGlideBox.replaceChild(ei0, ei);
	    
	    aElems[nextElIndex] = ei0;
	    aWidths[nextElIndex] = ei0.width;
	}
	
	numVis++;
	// now we definitely have a free img: set src and position
	
	nextImgIndex++;
	if (nextImgIndex >= numImgs) {
	    nextImgIndex=0;
	}

	nextElIndex++;
	if (nextElIndex >= aElems.length) {
	    nextElIndex = 0;
	}
	
	
    }
    
    // can we remove the head?
    
    if (curOffset >= aWidths[firstElIndex]) {
        aElems[firstElIndex].style.display="none";
	//alert("popping");
	firstElIndex++;
	if (firstElIndex >= numElems) {
	    firstElIndex = 0;
	}
	curOffset=0;
	numVis--;
    }  
    
    setTimeout("moveImages("+/*iW+","+iH+*/")", iPause);
    
}


function createNode(src, lnk, id, iH, iL) {
    var ei0 = document.createElement("img");
    ei0.setAttribute("src", src);
    ei0.setAttribute("height", iH);
    ei0.setAttribute("id", "img_"+id);
    ei0.setAttribute("onclick", lnk);
    //   _setStyle(ei0, "position:absolute; height:"+this._iH+"px; top:-1px; left:"+iL+"px;  cursor:pointer");
    return ei0;
}

//------------------------------------------
// backShiftE
//   shift iNum elements of contents of
//   array aDat starting at iStart
//
function backShiftE(aDat, iStart, iNum) {
    i = iStart+iNum;
    while (i > iStart) {
	aDat[i] = aDat[i-1];
	aDat[i].setAttribute("id", "img_"+i);
	i--;
    }
}

//------------------------------------------
// backShiftW
//   shift iNum elements of contents of
//   array aDat starting at iStart
//
function backShiftW(aDat, iStart, iNum) {
    while (iNum > 0) {
	aDat[iStart+iNum] = aDat[iStart+iNum-1];
	iNum--;
    }
}


function handleResize() {
    if (g_bFullWidth) {
        var eb = document.getElementById(g_sContainer);
	g_iW = eb.offsetWidth;
    } else {
    }	
}


//-- IE workaround for setAttribute("style", declaration)
function _setStyle(element, declaration) {
    element.style.cssText=declaration
}

//-- for IE ocnlick-workaround
//-- calls display function with argument from linkid-attribute
function myOnclick() {
    display(this.getAttribute("linkid"));
}

function operaClick() {
    alert("Der Opera-Browser hat Probleme den Inhalt dieer Seite korrekt darzustellen. Besuchen Sie diese Seite bitte mit Firefox oder Internet Explorer.");
}

//-- IE workaround for setAttribute("onclick", ...);
//-- no arguments possible, but this-pointer is available
//-- in fuction myOnclick
function _setOnclick(element) {
    if (g_browser.isOpera924) {
	element.onclick=operaClick;
    } else {
	element.onclick=myOnclick;
    }
}

function _removeChild(sParent, sChild) {
    var e = document.getElementById(sChild);
    var b = document.getElementById(sParent);
    if (b) {
        b.removeChild(e);
    }
    /*
    try {
        b.removeChild(e);
    } catch(ex) {
      alert("fail: "+ex);
    }
    */
}

function _appendChild(sParent, sChild) {
    var e = document.getElementById(sChild);
    var b = document.getElementById(sParent);
alert("container : "+b+", child : "+e);
    b.appendChild(e);
}

