/******** include common js (bookmark, special chars) ******/
document.writeln('<script src="/javascripts/shared.js"></script>');


// JavaScript Document
function spielen(filename){
  play(filename);
}

function play(filename){
	/*
	if(filename.indexOf('http://www.123')>-1){
		filename = filename.replace(/www.123teachme.com/, 'static.123teachme.com');
	} else if(filename.indexOf('http://123')>-1){
		filename = filename.replace(/123teachme.com/, 'static.123teachme.com');
	}else if(filename.match(/^\//){
		filename = 'http://static.123teachme.com' + filename;
	}
	*/
	filename = filename.replace('www.123teachme.com', 'static.123teachme.com');
	//var player = '<object type="application/x-shockwave-flash" data="http://static.123teachme.com/musicplayer.swf?&autoplay=true&song_url='+filename+'&" width="0" height="0"><param name="movie" value="/musicplayer.swf?&autoplay=true&song_url='+filename+'" /><img src="noflash.gif" width="0" height="0" alt="" /></object>';	
	var player = '<object type="application/x-shockwave-flash" data="http://static.123teachme.com/musicplayer.swf?&autoplay=true&song_url='+filename+'&" width="0" height="0"><param name="movie" value="/musicplayer.swf?&autoplay=true&song_url='+filename+'" /></object>';	
	document.getElementById('player').innerHTML = player;
}

function showAnswer(container, answer){
    document.getElementById(container).innerHTML = answer;
}

function eraseAnswer(container){
    document.getElementById(container).innerHTML = '';
}


//// for the exercises
function check_answers(){
  for (var i in answers){
    var curEl = document.getElementById('pregunta' + i);
    var selIdx = curEl.selectedIndex;
    if(curEl.options && curEl.options[selIdx].value == answers[i]){
      //document.getElementById(i).innerHTML = "bueno";
      document.getElementById(i).innerHTML = '<img src="/images/list-add.png" />';
    }
    else if (curEl.value == answers[i]){
      document.getElementById(i).innerHTML = '<img src="/images/list-add.png" />';
    }
    else {
      //document.getElementById(i).innerHTML = "x";
      document.getElementById(i).innerHTML = '<img src="/images/dialog-error.png" />';
    }
  }
}

function show_answers(){
  for (var i in answers){
    document.getElementById(i).innerHTML = answers[i];
  }
}

function print_options(options){
  for(var i in options){
    document.writeln('<option value="' + options[i] + '">' + options[i] + '</option>');
  }
}

/************ Multiple Choice ***********/
document.writeln('<script src="/javascripts/multi-choice.js"></script>');
/********************************************************************
function multi_choice_correct(el){
  el.style.border = 'solid 1px #0f0';
  el.style.background = 'url("/images/multi_choice_green.jpg") no-repeat';
}

function multi_choice_incorrect(el){
  el.style.border = 'solid 1px #f00';
  el.style.background = 'url("/images/multi_choice_red.jpg") no-repeat';
}

var multi_questions = new Array();
var CORRECT = true;

function add_multi_question(text){
  text = insert_sound(text);
  var q = new Object();
  q.text = text;
  q.answers = new Array();
  multi_questions[multi_questions.length] = q;
}

function add_multi_answer(text, is_correct){
  var idx = multi_questions.length - 1;
  var last_q = multi_questions[idx];
  var num_answers = last_q.answers.length;
  last_q.answers[num_answers] = new Object();
  var last_ans = last_q.answers[num_answers];
  last_ans.text = text;
  if(is_correct!=undefined && is_correct==true) last_ans.correct = true;
  else last_ans.correct = false;
  
}

function print_multi_choice(){
  document.writeln('<ol>');
  for(var i=0; i<multi_questions.length; i++){
    var q = multi_questions[i];
    document.writeln('<li>' + q.text + '<br />');
    document.writeln('<div class="multi-choice-answers">');
    for(var j=0; j<q.answers.length; j++){
      var ans = q.answers[j];
      var span_id = i + '_' + j;
      var correct_incorrect = ans.correct ? 'correct' : 'incorrect';
      var select_button = '<input type="button" value="select" onclick="multi_choice_' + correct_incorrect;       
      select_button += '(document.getElementById(\'' + span_id + '\'))" />';
      document.writeln(select_button);
      document.writeln('<span class="multi-choice-answer" id="' + span_id + '"> - ' + ans.text + '</span><br />');
    }
    document.writeln('</div>');
    document.writeln('</li>');
  }
  document.writeln('</ol>');
}
**************************/

/*
 * Replaces "[sound-link:/path/to/file.mp3]" with html link
 */
function insert_sound(text){
  new_text = text;
  var idx = 0;
  var begin_match = "[sound-link:";
  var end_match = ".mp3]";
  while((idx = new_text.indexOf(begin_match)) > -1){
    end_idx = new_text.indexOf(end_match, idx);
    sound_path = new_text.substring(idx + begin_match.length, end_idx + end_match.length - 1);
    var sound_html = '<a href="' + sound_path + '" onclick="play(this.href);';
    sound_html += ' return false;"><img src="/cms_images/hear-small.gif" ';
    sound_html += 'alt="Sound" border="0" /></a>';
    new_text = new_text.replace(/\[sound-link:[^ ]*mp3\]/i, sound_html)
  }
  return new_text;
}

