// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// http://codesnippets.joyent.com/posts/show/835
Position.GetWindowSize = function(w) {
	var width, height;
        w = w ? w : window;
        this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        
        return this;
};



function selection(){
  result = '';
  if (window.getSelection){
    result = window.getSelection();
  }else{
    result = document.selection.createRange().text;
  }
  return result;
}
function set_dictionary(e) {
  if($('dictionary_search')) {
    var text = selection();
    if(text == '') {}
    else {$('dictionary_search').value = text}
  }
  return true;
}


// create a window to display the article
function show_article_window(url, headline){
  Try.these(
    // check if the window already exists; if so, show it.
    function() {Windows.getWindow('read_article_window').showCenter();},
    
    // if the window doesn't exist yet, create it.
    function() {
      var dim = Position.GetWindowSize();
      var win = new Window({
        id:'read_article_window', 
        title: headline, 
        recenterAuto:false, 
        minimizable:false, 
        maximizable:false, 
        resizable:true, 
        top:5,
        left:5,
        width:(dim.width - 30), 
        height:(dim.height - 165),
        url: url, 
        showEffectOptions: {duration:0}
      });
      win.show();
      $('read_article_window').setStyle({top:'5px', zIndex:'400'});
    }
  );
}


// create a window to display a wikipedia search
function show_wiki_window(url, keyword){
  
  Try.these(
    
    // if the window doesn't exist yet, create it.
    function() {
      var dim = Position.GetWindowSize();
      
      var win = new Window({
        id:'search_wiki_window', 
        title: keyword, 
        recenterAuto:false, 
        minimizable:false, 
        maximizable:false, 
        resizable:true, 
        bottom:200, 
        width:dim.width - 20, 
        height:dim.height - 200, 
        url: url, 
        showEffectOptions: {duration:1.5},
        destroyOnClose: 	true
      });
      win.showCenter();
    }
  );
}



// Showing and hiding listening popups
function close_listening_popups() {
  $$('.listening_popup').each(function(elmt) {elmt.hide();} );
}
function open_listening_popup(elmt, id) {
  close_listening_popups();
  $(id).show();
}


// Showing and hiding lesson popups
function close_lesson_popups() {
  $$('.lesson_popup').each(function(elmt) {elmt.hide();} );
}
function open_lesson_popup(elmt, id) {
  close_lesson_popups();
  $(id).show();
}


// Switiching between tabs
function switch_tabs(elmt, id) {
  $(elmt).up('.tabbed_box').select('.boxed').each(function(e) {e.hide();}); 
  $(id).show(); 
  $(elmt).up('.headers').select('h2').each(function(e) {e.className = e.className.gsub('(^current)|( current)', ' not_current');}); 
  $(elmt).up('h2').className = $(elmt).up('h2').className.gsub('not_current', 'current'); 
}
