var tutorPage = 0;

function splitText(source, page, strings, max_string_len) {
    page           = page < 0 ? 0 : page;  
    max_string_len = max_string_len ? max_string_len : 60;
    strings        = strings ? strings : 4;
    var sa = source.split(" ");
    
    var res             = ''; 
    var res_string      = '';
    var tmp_res_string  = '';
    var cur_strings     = 0;
    var cur_pages       = 0;
    if (sa.length > 0) {
        res_string = sa[0];
        tmp_res_string  = sa[0];
    }
    for( i=1; i<sa.length; i++ ) {
        tmp_res_string = tmp_res_string + ' ' + sa[i];
//        alert(tmp_res_string.length);
        if (tmp_res_string.length > max_string_len) {
            cur_strings ++;
            if (cur_strings == strings) {
                if (cur_pages == page) break;
                res_string  = sa[i];
                cur_strings = 0;
                cur_pages++; 
            }
            else {
                res_string = res_string + '<br>' + sa[i];
            }
            tmp_res_string = sa[i];
        } else {
            res_string = res_string + ' ' + sa[i];
        }
    }
    return res_string; 
}

function speechSource(direction, len) {
    if (direction == 0) {
	    if ($('SpeechDestination')) {
	        if (!$('SpeechSourcePage') || $('SpeechSourcePage').value == '') {
	            closeBubble($('SpeechDestination').parentNode.id.substr(6, 1));
	        } else {
	            $('SpeechDestination').innerHTML = splitText($('SpeechSourcePage').value, 0, 1, len);
                showBubble($('SpeechDestination').parentNode.id.substr(6, 1));
	        }
	    }
	    return false;
    }
    direction == 1 ? direction : -1;
    tutorPage += direction;
    if (tutorPage >= 0) {
        if ($('SpeechSourcePage') && $('SpeechDestination')) {
            var tmpText = splitText($('SpeechSourcePage').value, tutorPage, 1, len);
            if ($('SpeechDestination').innerHTML == tmpText) {
                tutorPage --;
            } else {
                $('SpeechDestination').innerHTML = tmpText;
            }
        }
    } else {
        tutorPage = 0;
    }
} 


window.onload = function () {
    if (window.location.href.match('student')) {
        speechSource(0, 100);
    } else {
        speechSource(0, 105);
    }
}

function WOpen(oURL, iWidth, iHeight, scrollBars)
{
  var iLeft = (screen.width  - iWidth) / 2 ;
  var iTop  = (screen.height - iHeight) / 2 ;

  var   sOptions = "toolbar=no, status=yes, resizable=yes, dependent=yes, scrollbars=" + (scrollBars ? "yes" : "no") ;
        sOptions += ",width="   + iWidth ;
        sOptions += ",height="  + iHeight ;
        sOptions += ",left="    + iLeft ;
        sOptions += ",top="     + iTop ;

  var oWindow = window.open(oURL, '', sOptions);
  return oWindow;
}

function closeBubble(number) {
    if ($('png_bubble_div'+number)) {
        $('png_bubble_div'+number).style.display = 'none';
        $('bubble'+number).style.display = 'none';        
    }
}
function showBubble(number) {
    if ($('png_bubble_div'+number)) {
        $('png_bubble_div'+number).style.display = 'inline';
        $('bubble'+number).style.display = 'inline';
    }
}