// Hey emacs this is -*- Java -*- Script
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1998-2001 by Technische Universität München (TUM)
//
// Diese Seite ist Bestandteil des Projekts 
// 
//       INTEGRATION IN DER INGENIEUR-ANALYSIS
// 
// des Zentrums Mathematik und des Medienzentrums der TUM.
// 
// Kontaktadresse: integration@mathematik.tu-muenchen.de
//
///////////////////////////////////////////////////////////////////////////
//
// Author: Dominik Eberlein (eberlein@mathematik.tu-muenchen.de)
//

img_srcs = new Array(100);
nols = new Array(30);
nors = new Array(30);
sel_left = new Array(30);
sel_right = new Array(30);
arrow_down = new Array(10);
arrow_right = new Array(10);
arrow_left = new Array(10);
def_flag = new Array(10);

var tid=0;

img_path = '../../../../images/ch2/u/';

chk_gif = 'richtig.gif';
true_gif = 'weiter.gif';
true2_gif = 'weiter2.gif';
false_gif = 'nochmal.gif';
blank_gif = '../../../../images/1x1.gif';
def_gif = '../../../../images/ch2/u/definition.gif';
waehle_gif = '../../../../images/ch2/u/waehle.gif';
help_gif = '../../../../images/ch2/u/hilfe.gif';

arrow_down[0] = '../../../../images/1x1.gif';
arrow_down[1] = img_path + 'a_d_red.gif';
arrow_down[2] = img_path + 'a_d_green.gif';
arrow_down[3] = img_path + 'a_d_blue.gif';
arrow_down[4] = img_path + 'a_d_yellow.gif';

arrow_right[0] = '../../../../images/1x1.gif';
arrow_right[1] = img_path + 'a_r_red.gif';
arrow_right[2] = img_path + 'a_r_green.gif';
arrow_right[3] = img_path + 'a_r_blue.gif';
arrow_right[4] = img_path + 'a_r_yellow.gif';

arrow_left[0] = '../../../../images/1x1.gif';
arrow_left[1] = img_path + 'a_l_red.gif';
arrow_left[2] = img_path + 'a_l_green.gif';
arrow_left[3] = img_path + 'a_l_blue.gif';
arrow_left[4] = img_path + 'a_l_yellow.gif';

img_srcs['u11_1'] = 'u11_1';
nols['u11_1'] = 4;
nors['u11_1'] = 4;
sel_left['u11_1'] = 0;
sel_right['u11_1'] = 0;
def_flag['u11_1'] = 1;

img_srcs['u11_2'] = 'u11_2';
nols['u11_2'] = 3;
nors['u11_2'] = 4;
sel_left['u11_2'] = 0;
sel_right['u11_2'] = 0;
def_flag['u11_2'] = 0;

img_srcs['u12_1'] = 'u12_1';
nols['u12_1'] = 4;
nors['u12_1'] = 4;
sel_left['u12_1'] = 0;
sel_right['u12_1'] = 0;
def_flag['u12_1'] = 1;

img_srcs['u12_2'] = 'u12_2';
nols['u12_2'] = 2;
nors['u12_2'] = 4;
sel_left['u12_2'] = 0;
sel_right['u12_2'] = 0;
def_flag['u12_2'] = 0;

img_srcs['u13_1'] = 'u13_1';
nols['u13_1'] = 4;
nors['u13_1'] = 4;
sel_left['u13_1'] = 0;
sel_right['u13_1'] = 0;
def_flag['u13_1'] = 1;

img_srcs['u13_2'] = 'u13_2';
nols['u13_2'] = 2;
nors['u13_2'] = 4;
sel_left['u13_2'] = 0;
sel_right['u13_2'] = 0;
def_flag['u13_2'] = 0;

img_srcs['u14_1'] = 'u14_1';
nols['u14_1'] = 4;
nors['u14_1'] = 4;
sel_left['u14_1'] = 0;
sel_right['u14_1'] = 0;
def_flag['u14_1'] = 1;

img_srcs['u14_2'] = 'u14_2';
nols['u14_2'] = 4;
nors['u14_2'] = 4;
sel_left['u14_2'] = 0;
sel_right['u14_2'] = 0;
def_flag['u14_2'] = 0;

img_srcs['u15_1'] = 'u15_1';
nols['u15_1'] = 4;
nors['u15_1'] = 4;
sel_left['u15_1'] = 0;
sel_right['u15_1'] = 0;
def_flag['u15_1'] = 1;

img_srcs['u15_2'] = 'u15_2';
nols['u15_2'] = 4;
nors['u15_2'] = 4;
sel_left['u15_2'] = 0;
sel_right['u15_2'] = 0;
def_flag['u15_2'] = 0;

img_srcs['u16_1'] = 'u16_1';
nols['u16_1'] = 4;
nors['u16_1'] = 4;
sel_left['u16_1'] = 0;
sel_right['u16_1'] = 0;
def_flag['u16_1'] = 1;

img_srcs['u16_2'] = 'u16_2';
nols['u16_2'] = 3;
nors['u16_2'] = 4;
sel_left['u16_2'] = 0;
sel_right['u16_2'] = 0;
def_flag['u16_2'] = 0;

img_srcs['u17_1'] = 'u17_1';
nols['u17_1'] = 4;
nors['u17_1'] = 4;
sel_left['u17_1'] = 0;
sel_right['u17_1'] = 0;
def_flag['u17_1'] = 1;

img_srcs['u17_2'] = 'u17_2';
nols['u17_2'] = 2;
nors['u17_2'] = 4;
sel_left['u17_2'] = 0;
sel_right['u17_2'] = 0;
def_flag['u17_2'] = 0;


// sel_left()
// 
// noe:      number of exercise, e.g. u11_1
// nob:      number of button selected, 
//             e.g. 1 (means u11_1l1 = first button in left row)
//
// example (don't forget to load this file 'exercise.js'!)
// select_left('u11_1',1)

function select_left(noe,nob) {
  if (tid>0) {
    window.clearTimeout(tid);
    tid=0;
  }
  if (sel_left[noe]>0) {
    return;
  }
  if (nob==0) {
    tid=window.setTimeout("select_left2('"+noe+"','"+nob+"')",500);
  }
  else {
    tid=window.setTimeout("select_left2('"+noe+"','"+nob+"')",100);
  }
}

function select_left2(noe,nob) {
  if (nob==0) {
    for (var i=1; i<=nors[noe]; i+=1) {
      document.images[noe+'r'+i].src = img_path+img_srcs[noe]+'_r'+nob+i+'0.gif';
      document.images[noe+'r_p'].src = arrow_right[0];
      document.images['r_head'].src = blank_gif;
    } 
  }
  document.images['def'].src = img_path+img_srcs[noe]+'ba_l'+nob+'.gif';
  document.images['chk'].src = img_path+img_srcs[noe]+'bb_l'+nob+'.gif';
  document.images['def_p'].src = arrow_down[nob];
  if ((nob == 0) || (def_flag[noe] == 0)) {
     document.images['def_head'].src = blank_gif;
  } 
  else {
     document.images['def_head'].src = def_gif;
  }
}


function fix_left(noe,nob) {
     document.images['help_p'].src = arrow_left[0];
  if (sel_left[noe] == nob) {         // deslect button
    clear_all(noe);
    return;
  }

  // deselect previously selected button
  if (sel_left[noe] > 0) {
    document.images[noe+'l'+sel_left[noe]].src = img_path+img_srcs[noe]+'_l'+sel_left[noe]+'0.gif';
  }

  // select button
  document.images[noe+'l'+nob].src = img_path+img_srcs[noe]+'_l'+nob+'1.gif';
  document.images[noe+'r_p'].src = arrow_right[nob];
  document.images['r_head'].src = waehle_gif;

  for (var i=1; i<=nors[noe]; i+=1) {
    document.images[noe+'r'+i].src = img_path+img_srcs[noe]+'_r'+nob+i+'0.gif';
  } 

  sel_left[noe] = nob;
  sel_right[noe] = 0;
  select_left2(noe,nob);
}

function select_right(noe,nob) {
  if (sel_left[noe]==0) {
    // print some message
    return;
  }
  if (sel_right[noe]>0) {
    return;
  }
  select_right2(noe,nob);
}

function select_right2(noe,nob) {
  // do something nice here...
}


function fix_right(noe,nob) {
  
  // click on help
  if (sel_left[noe]==0) {
    // print some message ?
    return;
  }


  //deslect button
  if (sel_right[noe] == nob) {
     document.images['def_head'].src = def_gif;
    document.images[noe+'r'+nob].src = img_path+img_srcs[noe]+'_r'+sel_left[noe]+nob+'0.gif';
    document.images['def'].src = img_path+img_srcs[noe]+'ba_l'+sel_left[noe]+'.gif';
    document.images['chk'].src = img_path+img_srcs[noe]+'bb_l'+sel_left[noe]+'.gif';
    sel_right[noe] = 0;
    return;
  }

  //deslsect previously selected button
  if (sel_right[noe] > 0) {
    document.images[noe+'r'+sel_right[noe]].src = img_path+img_srcs[noe]+'_r'+sel_left[noe]+sel_right[noe]+'0.gif';
  }

  document.images['def_head'].src = blank_gif;
  document.images[noe+'r'+nob].src = img_path+img_srcs[noe]+'_r'+sel_left[noe]+nob+'1.gif';
  document.images['def'].src = img_path+img_srcs[noe]+'ba_r'+sel_left[noe]+nob+'1.gif';
  document.images['chk'].src = img_path+chk_gif

  sel_right[noe] = nob;
  select_right2(noe,nob);
}

function chk_solution(noe,page,clear_right_flag,type) {
  if ((sel_left[noe] == 0) 
       || (sel_right[noe] == 0)) {
    return;
  }

  // Nochmal
  if (document.images['chk'].src.indexOf(false_gif)!=-1) {
     if (clear_right_flag == 1) {
        clear_all(noe);
     }
     else {
        clear_right(noe);
     }
     return;
  }

  // Weiter2 ?
  if (document.images['chk'].src.indexOf(true2_gif)!=-1) {
     select_left2(noe,sel_left[noe]+1)
     fix_left(noe,sel_left[noe]+1)
     return;
  }

  // Weiter?
  if (document.images['chk'].src.indexOf(true_gif)!=-1) {
     if (page == '') {
	return;
     }

     if (page == 'main') {
        load_frames('../../../../html/ch2/u/u_main.htm', 'right','../../../../html/ch2/u/u_title.htm','title');
     }
     else {
        top.frames['right'].location = page;
     }
     return;
  }
 

  // Richtig? 
  document.images['def'].src = img_path+img_srcs[noe]+'ba_c'+sel_left[noe]+sel_right[noe]+'.gif';
  for (var i = 3; i < chk_solution.arguments.length; i += 3) {
     if ((sel_left[noe] == chk_solution.arguments[i]) && (sel_right[noe] == chk_solution.arguments[i+1])) {
        if (chk_solution.arguments[i+2] == 0) {
           document.images['chk'].src = img_path+true_gif;
	}
	else {
	   document.images['chk'].src = img_path+true2_gif;
	}
        return;
     }
  }
  document.images['chk'].src = img_path+false_gif;
  document.images['def_p'].src = arrow_down[0];
  document.images['help_p'].src = arrow_left[sel_left[noe]];
  document.images['def_head'].src = help_gif;
}

function clear_all(noe) {
     document.images[noe+'l'+sel_left[noe]].src = img_path+img_srcs[noe]+'_l'+sel_left[noe]+'0.gif';
     document.images['help_p'].src = arrow_left[0];
     document.images['def_head'].src = blank_gif;
      document.images['r_head'].src = blank_gif;
     sel_left[noe] = 0;
     sel_right[noe] = 0;
     select_left2(noe,0)
}

function clear_right(noe) {
     var h = sel_left[noe];
     document.images['help_p'].src = arrow_left[0];
     document.images['def_head'].src = blank_gif;
     sel_left[noe] = 0;
     select_left2(noe,h)
     fix_left(noe,h,0)
}
