﻿/* --- Foundation Page RedDot Menu --- */

function toggleFDRM(subMenu) {
    var subDivs = document.getElementById(subMenu).getElementsByTagName('div');
    var divImg = subDivs[0].getElementsByTagName('img');
                
    if (subDivs[1].style.display == "block") {
        subDivs[1].style.display = "none";
        divImg[0].src = "/images/interface/widget_plus.gif";
    }
    else {
        subDivs[1].style.display = "block";
        divImg[0].src = "/images/interface/widget_minus.gif";
    }
}

/* --- End --- */



/* --- Google Maps --- */

function loadDefaultLocation() {
    // Read formatted address from hidden tag for selected item
    var address = document.getElementById('defDiv').getElementsByTagName('span')[0].firstChild.data;
    var capCount = document.getElementById('defDiv').getElementsByTagName('span');
    //alert("CapCount: "+capCount.length);
    if ((capCount.length > 1) & (capCount[1].innerHTML != "")) {
        var caption = capCount[1].innerHTML;
    }
    else {
        var caption = address;
    }
    //alert("Blurb: "+caption);    

    var map = new GMap2(document.getElementById("map_canvas"));
    var geocoder = new GClientGeocoder();
    map.setUIToDefault();
    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
            alert(address + " not found");
            } else {
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                marker.openInfoWindowHtml(caption);
            }
        }
    );
}

function showAddress(sel) {

    var tmp; // holds list of location divs needing to be hidden
    
    if (sel == "nAmAddr") {
        var dv2 = document.getElementById('nAmAddr');
        var adrs = document.getElementById('nAmDivs');
        document.getElementById('globalAddr').selectedIndex = 0;
    }
    else if (sel == "globalAddr") {
        var dv2 = document.getElementById('globalAddr');
        var adrs = document.getElementById('gDivs');
        document.getElementById('nAmAddr').selectedIndex = 0;
    }

    //alert("dv2: "+dv2.selectedIndex);
    if (dv2.selectedIndex > 0) {

        // Hide all address divs
        document.getElementById('defDiv').style.display = 'none';

        tmp = document.getElementById('nAmDivs').getElementsByTagName('div');
        for (var inx = 0; inx < tmp.length; inx++) {
            tmp[inx].style.display = 'none';
        }

        tmp = document.getElementById('gDivs').getElementsByTagName('div');
        for (var inx = 0; inx < tmp.length; inx++) {
            tmp[inx].style.display = 'none';
        }

        // Show selected address div
        adrs.getElementsByTagName('div')[dv2.selectedIndex - 1].style.display = 'block';

        // Read formatted address from hidden tag for selected item
        var address = adrs.getElementsByTagName('div')[dv2.selectedIndex - 1].getElementsByTagName('span')[0].firstChild.data;
        var capCount = adrs.getElementsByTagName('div')[dv2.selectedIndex - 1].getElementsByTagName('span');
        //alert("CapCount: "+capCount.length);
        if ((capCount.length > 1) & (capCount[1].innerHTML != "")) {
            var caption = capCount[1].innerHTML;
        }
        else {
            var caption = address;
        }
        //alert("Blurb: "+caption);

        var map = new GMap2(document.getElementById("map_canvas"));
        var geocoder = new GClientGeocoder();
        map.setUIToDefault();
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                alert(address + " not found");
                } else {
                    map.setCenter(point, 13);
                    var marker = new GMarker(point);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(caption);
                }
            }
        );
    }
}

/* --- End --- */



/* --- Twitter --- */

function loadRnTwitter(tweets) {
    // Create an AJAX Request to the Twitter API, requesting JSONp data
    jQuery.getJSON('http://twitter.com/statuses/user_timeline/RightNowNews.json?count='+tweets+'&callback=?', function(data) {
        // For each "tweet":
        jQuery.each(data, function() {
            // Before posting the tweet we need to match and replace the URLs and Titter users hastags
            // to create hyperlinks just as they are on twitter.com
            var tweet = this.text; //this('text');
            var RegExp = /(((https?)|(ftp)):\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2, })?)*(([\w\-\.\?\\/+@&#;`~+%!]*)(\.\w{2, })?)*\/?)/i
            tweet = tweet.replace(RegExp, '<a href="$1">$1</a>');
            tweet = tweet.replace(/(^|\s)@(\w+)/g, '$1@<a href="http://twitter.com/$2">#$2</a>');
            tweet = tweet.replace(/(^|\s)#(\w+)/g, '$1@<a href="http://search.twitter.com/search?q=%23$2">#$2</a>');
            
            // Now we can write the tweet to the page
            jQuery("#rn-twitter").append("<li>"+tweet+"</li>");
        });
    });
}

/* --- End --- */


/* --- Homepage Banner --- */

function bannerLoadPage(url) {
    // Used by the banner overlay text to load a page when the 'All Text Clickable' style is set
    
    window.location.href = url;
}

/* --- End --- */
