/*
 *  Rotate at runtime 
 *  [2006-07-19 | heinemann@kompetenzz.de]
 */

/*
 * rotateTwoImgsSimultaniously
 *
 * Run this from opening body tag. 
 *
 * To ease usage of many images, this needs a naming pattern instead of 
 * explicit image name parameters. Read about the args to get a clou:
 * 
 * Args are
 * o period: Interval in seconds to rotate images (int)
 * o infix_cnt: how much images are in queue? (int)
 * o img_1_tag: img name or tag id of first image (string)
 * o img_1_pre: prefix for img_1's name. The current index will become infix. (string)
 *   If image name pattern is "myImage_N.gif" where "N" differs from 1 to 10, 
 *   the img_1_pre argument is "myImage_"
 * o img_1_post: postfix for img_1's name. Usually s/th like ".gif" (string)
 *   If image name pattern is "myImage_N_up.png" where "N" differs from 1 to 10, 
 *   the img_1_post argument is "_up.png"
 * o img_1_bg: true to decide we fill whateverelement.style.background-image,
 *             false to fill imgelement.src (boolean)
 * o img_1_urls: String of urls with ":::" as separator. Beware of 
 *               messing the order. (OPTIONAL!)
 * o img_2_*: aequivalent to img_1_*
 *
 */

function rotateTwoImgsSimultaniously (period,
				      infix_cnt,
				      img_1_tag,
				      img_1_pre,
				      img_1_post,
				      img_1_bg,
				      img_1_urls,
				      img_2_tag,
				      img_2_pre,
				      img_2_post,
				      img_2_bg,
				      img_2_urls){
    // Run at start
    changeTwoImgsSimultaniously(infix_cnt,img_1_tag,img_1_pre,img_1_post,img_1_bg,img_1_urls,img_2_tag,img_2_pre,img_2_post,img_2_bg,img_2_urls);
    // Run daemon-like
    window.setInterval("changeTwoImgsSimultaniously ('"+infix_cnt+"', '"+img_1_tag+"', '"+img_1_pre+"', '"+img_1_post+"', "+img_1_bg+",'"+img_1_urls+"', '"+img_2_tag+"', '"+img_2_pre+"', '"+img_2_post+"', "+img_2_bg+",'"+img_2_urls+"')",
		       period*1000);
}

var currentStepIndex = 1;
function changeTwoImgsSimultaniously (infix_cnt,
				      img_1_tag,
				      img_1_pre,
				      img_1_post,
				      img_1_bg,
				      img_1_urls,
				      img_2_tag,
				      img_2_pre,
				      img_2_post,
				      img_2_bg,
				      img_2_urls){
    //
    // Prepare data:
    //
    var img_1_urls = img_1_urls.split(":::");
    var currentImgUrl_1 = img_1_pre + currentStepIndex + img_1_post;
    var img_2_urls = img_2_urls.split(":::");
    var currentImgUrl_2 = img_2_pre + currentStepIndex + img_2_post;

    //
    // Set data:
    //
    // Link 1
    if (img_1_urls.length)
    {
        if ( document.getElementById(img_1_tag) )
        {
            document.getElementById(img_1_tag).href = img_1_urls[currentStepIndex - 1];
        }
    }
    // Img 1
    if (img_1_bg){
	document.getElementById(img_1_tag).style.background = "url("+currentImgUrl_1+")";
    }
    else {
        if ( document.images[img_1_tag] )
        {
            document.images[img_1_tag].src = currentImgUrl_1;
        }
    }
    // Link 2
    if (img_2_urls.length){
	document.getElementById(img_2_tag).href = img_2_urls[currentStepIndex - 1];
    }
    // Img 2
    if (img_2_bg){
	document.getElementById(img_2_tag).style.background = "url("+currentImgUrl_2+")";
    }
    else {
	document.images[img_2_tag].src = currentImgUrl_2;
    }

    //
    // Prepare next run
    //
    currentStepIndex = (currentStepIndex < infix_cnt) 
	? currentStepIndex + 1 
	: 1;

}
