// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
/*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none
    ---------------------------------------------------------*/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
    if ($('tab_content'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tab_header'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tab_header_active');
                    h.id = temph;
                }
                var tempc = 'tab_content'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{
                            duration:0.5,
                            queue:{
                                scope:'menus',
                                limit: 3
                            }
                        });
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('tab_header'+num);
        if (h)
            h.id = 'tab_header_active';
        h.blur();
        var c = $('tab_content'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('tab_content'+num,'blind',{
                duration:0.5,
                queue:{
                    scope:'menus',
                    position:'end',
                    limit: 3
                }
            });
        }else{
            toggleDisp('tab_content'+num);
        }
    }
}

/*-----------------------------------------------------------
    Toggles subtabs - Closes any open tabs, and then opens current tab
    Input:     1.Prefix for the subtab header
               2.The number of the current tab
               3.The number of tabs
               4.(optional)The number of the tab to leave open
               5.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none
    ---------------------------------------------------------*/
function togglesubTab(prefix,num,numelems,opennum,animate) {
    if ($(prefix+'tab_content'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = prefix+'tab_header'+i;
                var h = $(temph);
                if (!h){
                    var h = $(prefix+'tab_header_active');
                    h.id = temph;
                }
                var tempc = prefix+'tab_content'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{
                            duration:0.5,
                            queue:{
                                scope:'menus',
                                limit: 3
                            }
                        });
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $(prefix+'tab_header'+num);
        if (h)
            h.id = prefix+'tab_header_active';
        h.blur();
        var c = $(prefix+'tab_content'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle(prefix+'tab_content'+num,'blind',{
                duration:0.5,
                queue:{
                    scope:'menus',
                    position:'end',
                    limit: 3
                }
            });
        }else{
            toggleDisp(prefix+'tab_content'+num);
        }
    }
}

function activateSubTabHeader(activetab) {
    var h = $(activetab);
    if (h)
        h.id = 'subtab_header_active';
    h.blur();
}

// ------------- GOOGLE MAPS RELATED -------------

function directionstoaddress(baseadr)
{
    if ($('latlong').value != '')
    {
        toAddress = $('latlong').value;
    }
    else
    {
        toAddress = $('addressinfo').value;
    }
    overlayDirections(baseadr, toAddress);
}

function directionsfromaddress(baseadr)
{
    if ($('latlong').value != '')
    {
        fromAddress = $('latlong').value;
    }
    else
    {
        fromAddress = $('addressinfo').value;
    }
    overlayDirections(fromAddress, baseadr);
}

function overlayDirections(fromAddress, toAddress)
{
    var qs = new Querystring();
    
    gdir.load("from: " + fromAddress + " to: " + toAddress, {
        "locale": qs.get("locale", "en")
    });
}

function overlayDirections2(baseaddress)
{
    map.clearOverlays();
    gdir.load(baseaddress + " to " + $('latitude').value + "," + $('longitude').value);
}

function parseDirectionLatLong(inputstring)
{
 var word = inputstring.split("|");
 $('addressinfo').value = word[0];
 $('latlong').value = word[1];
}

function resetOverlays()
{
    map.clearOverlays();
    map.addOverlay(marker);
}

function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else alert("An unknown error occurred.");
}

// ------------- ---------------- -------------
function getWindowHeight() {
    if (window.innerHeight && window.scrollMaxY)
    {
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        yScroll = document.body.scrollHeight;
    }
    else
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
    }
    return yScroll + "px";
}
      
function displayMyoverlay() {
    $('myoverlay').style.height = getWindowHeight()
    $('myoverlay').style.display = 'block'
}

// ------------- ---------------- -------------
// fix precision of reported Lat/Lng
function fix6ToString(n) {
    return n.toFixed(6).toString();
}

function nudgeW() {
    var ll = marker.getPoint();
    var newll = new GLatLng(ll.lat(), (ll.lng() - nudgeFactor().lng()));
    marker.setPoint(newll);
    marker.redraw();
    GEvent.trigger(marker,'click');
}
function nudgeE() {
    var ll = marker.getPoint();
    var newll = new GLatLng(ll.lat(), (ll.lng() + nudgeFactor().lng()));
    marker.setPoint(newll);
    marker.redraw();
    GEvent.trigger(marker,'click');
}
function nudgeN() {
    var ll = marker.getPoint();
    var newll = new GLatLng(ll.lat() + nudgeFactor().lat(), ll.lng());
    marker.setPoint(newll);
    marker.redraw();
    GEvent.trigger(marker,'click');
}
function nudgeS() {
    var ll = marker.getPoint();
    var newll = new GLatLng(ll.lat() - nudgeFactor().lat(), ll.lng());
    marker.setPoint(newll);
    marker.redraw();
    GEvent.trigger(marker,'click');
}
// nudgeFactor returns GLatLng with Lat & Lng per pixel
function nudgeFactor() {
    var b = map.getBounds().toSpan(); // LatLng as size of the map
    var s = map.getSize(); // width and height of map in pixels
    return new GLatLng(b.lat()/s.height , b.lng()/s.width );
}

function save_marker() {
    //var myp = marker.getPoint();
    $('dining_module_restaurant_latitude').value=globallat;
    $('dining_module_restaurant_longitude').value=globallng;
}


