/** Page: javascript functions for studiesabroad.com
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @copyright   Copyright (c) 2003 by International Studies Abroad Inc.
 * @version     $Id: main.js,v 1.11 2007/06/15 15:38:24 cnienhuis Exp $
 * @package     studiesabroad.com-lib
 * @subpackage  Javascript
 */

// store previous onload events.
var previousOnload = window.onload;

// set onload.
window.onload = globalOnload;

// print window global.
var printWindow;

// storage of global color values.
var navColors = Array();

navColors['countryCellDefault'] = '#224E73';
navColors['countryCellHover']   = '#132F45';

// Method: globalOnload() {{{
/** Function/Method: globalOnload()
 *
 * Do window onload without overloading any onloads
 * being set by other include libraries.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @global      previousOnload  storage for former onload function.
 * @return      boolean false
 */
function globalOnload() {
    // call previously existing events.
    if (previousOnload != null) {
        previousOnload();
    }

    // write onclick events for popups of print windows.
    replacePrintLinks();

    // print window if we are print-friendly document.
    detectPrintablePage();

    return;
}
// }}}

function clearMenuCell(id) {
    if (null == id) {
        return;
    }

    obj = document.getElementById(id);

    if (null != obj) {
        obj.style.backgroundColor = navColors['countryCellDefault'];
    }
}

function highlightMenuCell(id) {
    if (null == id) {
        return;
    }

    obj = document.getElementById(id);

    if (null != obj) {
        obj.style.backgroundColor = navColors['countryCellHover'];
    }
}

// Method: quickJump() {{{
/** Method: quickJump()
 *
 * Expects to find the form 'quick-jump' containing a select
 * box, 'nav-select', that has valid urls as the option values.
 * Changes the page location to the choosen value.
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @access      public
 * @return      boolean         FALSE
 */
function quickJump() {
    if (null == document.forms['quick-jump'] && null == document.forms['quick-jump'].elements['nav-select']) {
        return false;
    }

    form = document.forms['quick-jump'];
    index = form.elements['nav-select'].selectedIndex;

    if (0 < index) {
        option = form.elements['nav-select'].options[index].value;
        location.href=option;
        form.onsubmit = deadSubmit;
    }

    return false;
}
// }}}

// Method: deadSubmit() {{{
/** Method: deadSubmit()
 *
 * Removes the submit action from a form.
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @access      public
 * @return      boolean         FALSE
 */
function deadSubmit() {
    return false;
}
// }}}

// Method: replacePrintLinks() {{{
/** Function/Method: replacePrintLinks()
 *
 * Dicovers <a> tags that correspond to a print-view or print-page
 * based on class name "print-link" and writes popup event instead.
 *
 * see openPrintWindow() for the properties of the popup that is
 * the result.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @global      mixed   printWindow     window object.
 * @return      boolean void
 */
function replacePrintLinks() {
    nodes = document.getElementsByTagName('A');

    for (i = 0; i < nodes.length; i++) {
        n = nodes[i];
        thisClass = '';
        if (n.className != null) {
            thisClass = n.className;
        } else {
            thisClass = n.getAttribute('class');
        }
        if (thisClass == 'print-link') {
            url = n.getAttribute('href');
            n.setAttribute('href', 'javascript:openPrintWindow(\'' + url + '\');');
            n.setAttribute('target', '');
        }
    }
    return;
}
// }}}

// Method: openPrintWindow() {{{
/** Function/Method: openPrintWindow()
 *
 * Opens a printable size popup window to the specified url.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @param       string url      destination url.
 * @global      printWindow     target window object.
 * @return      boolean void
 */
function openPrintWindow(url) {

    /* if no print feature, give them menu options */
    if (window.print) {
        menu = 'no';
    } else {
        menu = 'yes';
    }

    /* open the window */
    printWindow = window.open(url, "printWindow",
      'height=700,width=650,innerHeight=650,innerWidth=650,'
      + 'scrollbars=yes,resizable=yes,position=3,'
      + 'location=no,menubar=' + menu + ',status=no'
      + 'screenX=10,screenY=10'
      );
    return;
}
// }}}

// Method: detectPrintablePage() {{{
/** Function/Method: detectPrintablePage()
 *
 * Always run this, if we see a special node indicating this
 * page was generated from the print buttons, confirm and print.
 *
 * looks for a node by the id of "printer-friendly"
 * htmlprint.xsl inserts this node.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @return      boolean void
 */
function detectPrintablePage() {
    if (document.getElementById('printer-friendly')) {
        if (window.print) {
            /* we have print functionality, confirm and print */
            if (confirm("Print this page now?")) {
                window.print();
            }
        } else {
            /* can't do anything for these folks */
        }
    }
    return;
}
// }}}

// Method: showVCard() {{{
/** Function/Method: showVCard()
 *
 * Pops up window to show vcard for specified staff member
 *
 *
 * @author      Angela Roles <aroles@studiesabroad.com>
 * @access      public
 * @param       string cardId      staffId of desired staff member
 * @return      boolean void
 */
function showVCard(cardId) {
   var openUrl = '/viewStaffVCard/'+cardId;
   window.open(openUrl, 'VCard', "toolbar=no,location=no,status=no,scrollbars=no,resizable=yes,width=350,height=200");
}
//}}}

// Method: showGalleryImage() {{{
/** Function/Method: showGalleryImage()
 *
 * Pops up window to show Gallery Image
 *
 *
 * @author      Chris Nienhuis <cnienhuis@studiesabroad.com>
 * @access      public
 * @param       string oid     Gallery Object Id (IE infoObjectId)
 * @param       string gclass  Gallery Class
 * @param       string gtype   Gallery Type
 * @param       string gtable  Gallery Table
 * @param       string gkey    Gallery Table Key
 * @param       string h,w     Height and Width of popup
 * @return      boolean void
 */
function showGalleryImage(oid, gclass, gtype, gtable, gkey,h,w) {
   var openUrl = '/popups/viewGalleryPopUp.php?oid='+oid+'&gclass='+gclass+'&gtype='+gtype+'&gtable='+gtable+'&gkey='+gkey;
   window.open(openUrl, 'VGallery', "toolbar=no,location=no,status=no,scrollbars=no,resizable=yes,width="+w+",height="+h);
   return;
}
//}}}


// Method: urgentNotice(url) {{{
/** Function/Method: urgentNotice()
 *
 * Pops up window to show an arbitrary urgent notice
 *
 *
 * @author      Eric Minto <eric@studiesabroad.com>
 * @access      public
 * @param       string url      URL of page that contains the notice, assuming '.php' extension
 * @return      boolean void
 */
function urgentNotice(url) {
   window.open(url+'.php', 'Notice', "toolbar=no,location=no,status=no,scrollbars=auto,menubars=no,resizable=yes,width=338,height=443");
}
