// JavaScript Document

var calNr;
var imgList;
var playSpeed;
var playPause;

var play_typ;
var play_nr;

function getImageList() {
    prList = get_project()
    li = new Array();
    for (p=0;p<prList.length;p++) {
        pr = prList[p];

        findIn = "";
        // findCont == ;

        showProject = 0;
        if (play_typ == "p") {
            if (p == play_nr) showProject = 1;
        }


        if (play_typ == "m") findIn = 4;
        if (play_typ == "y") findIn = 6;
        if (play_typ == "a") findIn = 3;
        if (play_typ == "d") findIn = 5;
        if (play_typ == "c") findIn = 7;

        if (typeof(findIn) == "number") {
            contList = pr[findIn].split(",");
            for (c=0;c<contList.length;c++) {
                if (contList[c] == play_nr) showProject = 1;
            }
        }
        if (showProject == 1) {
            if (pr.length >= 11) {
                imgCount = pr[11];
                if (imgCount > 0) {
                    //alert(pr[0] + " " + imgCount);
                    for (i=1;i<imgCount;i++) {
                        li.push(new Array(p,i,"-"));
                    }
                }
            }
        }
    }
    // make randomList
//alert(li);
    li = getRandomList(li);
    return li;
}
		
	
function getRandomList(li) {
    res = new Array();
    cnt = li.length;
    for (i=0;i<cnt;i++) {
        randNr =  Math.round(Math.random() * (li.length-1));

        if (randNr < 0) alert ("<0");
        if (randNr > li.length) alert ("> " + randNr + " " + li.length);

        add = li[randNr];
        if (typeof(add) != "object") {
                alert("randNr = " + randNr + " i = " + i + " cnt = " + cnt + " li.length "+li.length);
        } else {
                res.push (li[randNr]);
        }
        li.splice(randNr,1);
    }
    return res;
}
		
		
	
function showImage() {
    if (typeof(imgList) != "object") {
        imgList = getImageList();
    }

    if (typeof(calNr) != 'number') {
        calNr = 0;
    } else {
        calNr++;
    }

    if (calNr >= imgList.length) calNr = 0;

    data = imgList[calNr];
    if (typeof(data) != "object") {
        alert("no data for "+calNr+" is "+data);
    }

    pr = imgList[calNr][0];
    im = imgList[calNr][1];

    //if (playPause == 1) alert ("show Image "+pr+" "+im+" calNr"+calNr);

    tabStart = "<table width='100%' height='100%' border='0'><tr><td align='center' valign='middle'>";
    tabMiddle = "</td></tr><tr height='70' ><td align='left' valign='top'>";
    tabEnd  = "</td></tr></table>";


    imgText = "<img src='img/"+(pr+1)+"/"+im+".jpg' height='100%' alt=''>";

//    navText = ""; // " + calNr + " / " + imgList.length+ "  "; // "<br>";
//    navText= navText + "<a href='#' onClick=javascript:goBack(); class='menu_link'><img src='back.gif' border='0' width='21px' height='21px' alt=''></a>";
//    if (playPause == 0) {
//            navText = navText + "<a href='#' onClick=javascript:doPause(); class='menu_link'><img src='pause.gif' border='0' width='21px' height='21px' alt=''></a>";
//    } else {
//            navText = navText + "<a href='#' onClick=javascript:doPlay(); class='menu_link'><img src='play.gif' border='0' width='21px' height='21px' alt=''></a>";
//    }
//    navText = navText + "<a href='#' onClick=javascript:goFor(); class='menu_link'><img src='forward.gif' border='0' width='21px' height='21px' alt=''></a>";
//    //navText = navText + "&nbsp;&nbsp;"+speedNav();


    t = tabStart  + imgText +  tabEnd; // 
    t = imgText;
    setlayer('img1',t,null,null,1);
}

function timer() {
    if (play != 1) return 0;

    if (playPause == 1) return "pause";
    showImage();

    showNavText(pr);

    setTimeout('timer()',playSpeed * 1000);
}




function doPause() {
    playPause = 1;
    navLine()
}

function doPlay() {
    playPause = 0;
    timer();
    navLine();
}

function goBack() {
    if (typeof(calNr) != 'number') {
            calNr = 2;
    } else {
            calNr--;
            calNr--;
    }

    if (calNr < 0) calNr = imgList.length + calNr;
    // alert("goBack "+calNr);
    // playPause = 1;
    showImage();
    showNavText(pr);
    //timer();
    //navLine();
}

function goFor() {
    //playPause = 1;
    showImage();
    showNavText(pr);
//    timer();
 //   navLine();
}

function setSpeed(speed) {
    playSpeed = speed;
    navLine();
}

function speedNav() {
    if (playSpeed == null) playSpeed = 3;
    t = "<img src='speed.gif' width='57px' height='21px' alt=''>";
    t = "";
    invert = 1;

    for (x=1; x<10; x++) {
       if (invert == 1) i = 10-x;
       else i = x;
       // t = t + i;
       if (i == playSpeed) t = t +  "<img src='big.png' height='13px' alt=''>";
       else t = t + "<a href='#' onClick=javascript:setSpeed("+(i)+"); class='menu_link'><img src='small.png' border='0'  height='13px' alt=''></a>";
    }
   
    return t
}
		


function getContent(typ,inh) {
    if (inh == "") return 0;
    contList = new Array();


    cont = inh.split(",");
    for(i=0;i<cont.length;i++) {
        st = "undefined";
        ty = "";
        if (typ == "y") {ty = "Jahr : "; st = cont[i];}
        if (typ == "a") {ty = "Area : "; st = get_areaTitle(cont[i]-1);}
        if (typ == "c") {ty = "Customer : "; st = get_custTitle(cont[i]-1);}
        if (typ == "m") {ty = "Worker : "; st = get_personTitle(cont[i]-1);}
        if (typ == "d") {ty = "Dealer : "; st = get_dealerTitle(cont[i]-1);}

        contList.push( new Array(typ,cont[i],st,ty));
    }

    return contList;
}
	
	
		


function showNavText(prNr) {
    t = ""; //showNavText " + pr;


    showList = new Array();

    prList = get_project()
    if (typeof(prList) != "object") { alert("no Object" + prNr); return 0; }
    pr = prList[prNr];
    if (typeof(pr) != "object") { alert("no Project-Object" + pr); return 0; }



    showList.push( new Array("p",prNr, pr[0]));

    add = getContent("m",pr[4]);
    if (typeof(add) == "object") {
            for (i=0;i<add.length;i++) showList.push(add[i]);
    }

    add = getContent("a",pr[3]);
    if (typeof(add) == "object") {
            for (i=0;i<add.length;i++) showList.push(add[i]);
    }

    add = getContent("d",pr[5]);
    if (typeof(add) == "object") {
            for (i=0;i<add.length;i++) showList.push(add[i]);
    }

    add = getContent("y",pr[6]);
    if (typeof(add) == "object") {
            for (i=0;i<add.length;i++) showList.push(add[i]);
    }

    add = getContent("c",pr[7]);
    if (typeof(add) == "object") {
            for (i=0;i<add.length;i++) showList.push(add[i]);
    }


    showList = getRandomList(showList);

    for (i=0;i<showList.length;i++) {

            t = t + "<br>";
            zufa = Math.round(Math.random() * 30);
            for (z=0;z<zufa;z++) t = t + "&nbsp; ";

            akt = 1;
            if (showList[i][0] == play_typ) if (showList[i][1] == play_nr) akt = 0;
            
            if (akt == 1) {
                    style = "play_link" + Math.round(Math.random() * 3);
                    if (useHtml == 1) t = t + "<a href='index.php?wahl="+showList[i][0]+"&nr="+showList[i][1]+"' class='"+style+"'>";
                    else t = t + "<a href='#' onClick=javascript:SwitchPlay('"+showList[i][0]+"','"+showList[i][1]+"'); class='"+style+"'>";

                       //  t = t + "<a href='#' onClick=javascript:SwitchPlay('"+showList[i][0]+"','"+showList[i][1]+"'); class='"+style+"'>";
            }
            t = t + showList[i][2];
            if (akt == 1) t = t + "</a>";
    }



    tabStart = "<table width='100%' height='100%' border='0'>";
    tabStart = tabStart + "<tr height ='80%'><td align='left' valign='middle'>";
    tabMiddle = "</td></tr><tr height ='20%'><td align='right' valign='bottom'>";
    tabEnd  = "</td></tr></table>";

    //  t = t + "<br><br><br>"  + speedNav();
    setT = tabStart + t + tabMiddle + "" + tabEnd; // speedNav()
    setlayer('playNav',setT,null,null,1);
	
}


function SwitchPlay(typ,nr) {
    satz = imgList[calNr];

    play_typ = typ;
    play_nr = nr;
    imgList = getImageList();
    //alert(satz+" calNr "+calNr);
    calNr = -1;
   
    for (i=0;i<imgList.length;i++) {
        if (imgList[i][0] == satz[0]) {
            if (imgList[i][1] == satz[1]) {
                calNr = i - 1;
                //alert ("found "+imgList[i]+" calNr "+calNr);
                
            }
        }
    }
    showImage();
    showNavText(pr);
}
	


function StartPlay() {
    hideProjectImage ();
    // alert("wahl = " + wahl+ " id = " + wahl_id);
    play_typ = wahl;
    play_nr =  parseInt(wahl_id) + 1;//wahl_id + 1;
    if (wahl == "y") {
       play_typ = wahl;
       play_nr = wahl_id;
    }
    if (wahl == "p") play_nr = parseInt(wahl_id);
   
    if (play_typ == null) play_typ = "m";
    if (play_nr == null) play_nr = 1;
    if (playSpeed == null) playSpeed = 5;
    play = 1;
    playPause = 0;
    imgList = getImageList();
    calNr = -1;
    
    setlayer('topic',"",null,null,0);
    setlayer('inhalt',"",null,null,0);
    timer();
    navLine();
}

function StopPlay() {
    play = 0;
    setlayer('img1',"",null,null,0);
    setlayer('playNav',"",null,null,0);

    //alert("wahl = " + play_typ+ " id = " + play_nr);
    wahl_id = play_nr - 1 ;
    wahl = play_typ;
    if (wahl == "y") wahl_id = play_nr;
    if (wahl == "p") wahl_id = play_nr;
   // play_nr = nr;
    goPage(wahl,wahl_id);
    navLine();
}
