/*------------------------------------------------------------------------------

   Copyright (c) 2005 Tyrell Corporation.

    This file is part of the Locative Blog project.
    http://locblog.sourceforge.net/

   Author   : $Author: darkeye $
   Version  : $Revision: 1.1 $
   Location : $Source: /cvsroot/locblog/website/var/htdocs/rotatingGlobe/sample/misc.js,v $

   Abstract : 

    Utility functions.

   Copyright notice:

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License  
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.
   
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    GNU General Public License for more details.
   
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

------------------------------------------------------------------------------*/

/**
 *  Constructor to reate an object, based on a named element from the document,
 *  browser-independently.
 *  The created object will have two properties:
 *  obj   - the named object itself
 *  style - the stylesheet object for the named object.
 *
 *  based on http://www.quirksmode.org/js/dhtmloptions.html
 *
 *  @param name the name of the element to create this object upon.
 */
function getObj(name)
{
    if (document.getElementById) {
        this.obj   = document.getElementById(name);
        this.style = document.getElementById(name).style;
    } else if (document.all) {
        this.obj   = document.all[name];
        this.style = document.all[name].style;
    } else if (document.layers) {
        this.obj   = document.layers[name];
        this.style = document.layers[name];
    }
}

/**
 *  Find the X coordinate of an element in the document.
 *  based on http://www.quirksmode.org/js/findpos.html
 *
 *  @param obj the element in the document.
 *  @return the x coordinate for this element.
 */
function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

/**
 *  Find the Y coordinate of an element in the document.
 *  based on http://www.quirksmode.org/js/findpos.html
 *
 *  @param obj the element in the document.
 *  @return the x coordinate for this element.
 */
function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

/**
 *  Replace the contenst of a DOM element.
 *  based on http://www.quirksmode.org/js/layerwrite.html
 *
 *  @param id the id of the element.
 *  @param text the new text of the element.
 */
function replaceContent(id, text)
{
    if (document.getElementById) {
        x = document.getElementById(id);
        x.innerHTML = '';
        x.innerHTML = text;
    } else if (document.all) {
        x = document.all[id];
        x.innerHTML = text;
    } else if (document.layers) {
        x = document.layers[id];
        text2 = '<P CLASS="testclass">' + text + '</P>';
        x.document.open();
        x.document.write(text2);
        x.document.close();
    }
}


/**
 *  Function to calculate the X coordinate of an orthographic projection.
 *  based on http://mathworld.wolfram.com/OrthographicProjection.html
 *
 *  @param phi the lattitude of the point on the sphere
 *  @param lambda the longitude of the point on the sphere
 *  @param phi1 the lattitude of the viewpoint
 *  @param lambda1 the longitued of the viewpoint
 *  @return the x coordinate of the specified point.
 */
function orthographicX(phi, lambda, phi1, lambda1)
{
    var x = Math.cos(phi) * Math.sin(lambda - lambda1);
    return x;
}

/**
 *  Function to calculate the Y coordinate of an orthographic projection
 *  based on http://mathworld.wolfram.com/OrthographicProjection.html
 *
 *  @param phi the lattitude of the point on the sphere
 *  @param lambda the longitude of the point on the sphere
 *  @param phi1 the lattitude of the viewpoint
 *  @param lambda1 the longitued of the viewpoint
 *  @return the y coordinate of the specified point.
 */
function orthographicY(phi, lambda, phi1, lambda1)
{
    var y = Math.cos(phi1) * Math.sin(phi)
          - Math.sin(phi1) * Math.cos(phi) * Math.cos(lambda - lambda1);
    return y;
}

/**
 *  Helper function to convert a latitude or longitued into radii.
 *
 *  @param degrees the latitude or longitude
 *  @return the same value, in radii
 */
function toRadii(degrees)
{
    return (degrees / 180.0) * Math.PI;
}


