// bgGoogle.js 5 | requires jquery.js 1.6.2+ | made by Kyle Weems of Mindfly Web Studio (http://mindfly.com/)
// Created Aug 18, 2009 | Last Modified :  Aug 11, 2011

if (!bg) var bg = {};
if (!BrainGnat) var BrainGnat = bg;
var debug_v = null;

bg.Google = {
    version: "bg.Google Extension - v5 (requires jquery.js 1.6.2+)",
    map: {
        load: function(elem, sensor, z, callback) {
            if (!sensor) sensor = false;
            if (!z) z = 13;
            bg.Google.map.mapElem = elem;
            bg.Google.map.z = z;
            if (!!callback) bg.Google.map.mapCallback = callback;
            var script = "http://maps.google.com/maps/api/js?sensor=" + sensor + "&async=2&callback=bg.Google.map.runload";
            $.getScript(script, function() { });
        },
        runload: function(data) {
            var ll = new google.maps.LatLng(0, 0);
            var domElem = $(bg.Google.map.mapElem).get(0);
            var myOptions = { zoom: bg.Google.map.z, center: ll, mapTypeId: google.maps.MapTypeId.ROADMAP };
            bg.Google.map.data = new google.maps.Map(domElem, myOptions);
            if (bg.Google.map.mapCallback != null) {
                google.maps.event.addListener(bg.Google.map.data, 'tilesloaded', function() {
                    bg.Google.map.mapCallback();
                    google.maps.event.clearListeners(bg.Google.map.data, "tilesloaded");
                })
            }

        },
        addMarker: function(latLng, info, index, clickFunc) {
            if (latLng.length > 0) {
                var ll = new google.maps.LatLng(latLng[0], latLng[1]);
            } else {
                var ll = latLng;
            }
            if (index == null) {
                var marker = new google.maps.Marker({ map: bg.Google.map.data, position: ll, title: info, html: info });
            } else {
                var letter = String.fromCharCode("A".charCodeAt(0) + index);
                var marker = new google.maps.Marker({ map: bg.Google.map.data, position: ll, title: info, html: info, icon: 'http://www.google.com/mapfiles/marker' + letter + '.png' });
            }
            marker.setMap(bg.Google.map.data);
            if (clickFunc != null) { google.maps.event.addListener(marker, 'click', function() { clickFunc(this); }); }
        },
        addMarkerByAddress: function(address, info, index, clickFunc) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    bg.Google.map.addMarker([results[0].geometry.location.lat(), results[0].geometry.location.lng()], info, index, clickFunc);
                }
            });
        },
        setCenter: function(latlng, callback) {
            if (latlng.lng()) {
                var ll = latlng;
            } else {
                var ll = new google.maps.LatLng(latlng[0], latlng[1]);
            }
            bg.Google.map.data.setCenter(ll);
            if (!!callback) callback();
        },
        setCenterByAddress: function(address, callback) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    bg.Google.map.data.setCenter(results[0].geometry.location);
                    if (callback != null) callback();
                }
            });
        },
        loadFromHCard: function(card, map, sensor, z, callback) {
            if (!z) z = 13;
            var address = bg.Google.map.getAddressFromHCard(card);
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address, function() { callback(); }) });
            }
        },
        loadFromGeo: function(geo, map, sensor, z, callback) {
            if (!z) z = 13
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.getLatLngFromGeo(geo, function(ll) { bg.Google.map.setCenter(ll); }) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.getLatLngFromGeo(geo, function(ll) { bg.Google.map.setCenter(ll, callback); }) });
            }
        },
        loadFromAddress: function(address, map, sensor, z, callback) {
            if (!z) z = 13;
            if (!callback) {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address) });
            } else {
                bg.Google.map.load(map, sensor, z, function() { bg.Google.map.setCenterByAddress(address, function() { callback(); }) });
            }
        },
        getAddressFromHCard: function(card) {
            var address = "";
            address += $(card + ' .street-address').text();
            address += ' ' + $(card + ' .locality').text();
            address += ', ' + $(card + ' .region').text();
            address += ' ' + $(card + ' .postal-code').text();
            return address;
        },
        geo: function(address) {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    alert(results[0].geometry.location[0] + ', ' + results[0].geometry.location[1]);
                }
            });
        },
        getLatLngFromGeo: function(geo, callback) {
            var ll = new google.maps.LatLng($(geo + ' .latitude').text(), $(geo + ' .longitude').text());
            if (!callback) return ll;
            if (!!callback) callback(ll);
        },
        hcardMark: function(hcard, map, sensor, z, callback) {
            if (!bg.Google.map.mapElem) {
                bg.Google.map.loadFromHCard(hcard, map, sensor, z, function() {
                    bg.Google.map.addMarkerByAddress(bg.Google.map.getAddressFromHCard(hcard));
                    if (!!callback) callback();
                });
            } else if (!!bg.Google.map.mapElem && !bg.Google.map.data) {
                if (!map) map = null;
                if (!sensor) sensor = null;
                if (!z) z = null;
                if (!callback) callback = null;
                var t = setTimeout('bg.Google.map.hcardMark("' + hcard + '","' + map + '",' + sensor + ',' + z + ',' + callback + ');', 100);
            } else if (!!bg.Google.map.mapElem && !!bg.Google.map.data) {
                bg.Google.map.addMarkerByAddress(bg.Google.map.getAddressFromHCard(hcard));
                if (!!callback) callback();
            }
        },
        geoMark: function(geo, map, sensor, z, callback) {
            if (!bg.Google.map.mapElem) {
                bg.Google.map.loadFromGeo(geo, map, sensor, z, function() {
                    bg.Google.map.addMarker(bg.Google.map.getLatLngFromGeo(geo));
                    if (!!callback) callback();
                });
            } else if (!!bg.Google.map.mapElem && !bg.Google.map.data) {
                if (!map) map = null;
                if (!sensor) sensor = null;
                if (!z) z = null;
                if (!callback) callback = null;
                var t = setTimeout('bg.Google.map.geoMark("' + geo + '","' + map + '",' + sensor + ',' + z + ',' + callback + ');', 100);
            } else if (!!bg.Google.map.mapElem && !!bg.Google.map.data) {
                bg.Google.map.addMarker(bg.Google.map.getLatLngFromGeo(geo));
                if (!!callback) callback();
            }
        },
        addressMark: function(address, map, sensor, z, callback) {
            if (!bg.Google.map.mapElem) {
                bg.Google.map.loadFromAddress(address, map, sensor, z, function() {
                    bg.Google.map.addMarkerByAddress(address);
                    if (!!callback) callback();
                });
            } else if (!!bg.Google.map.mapElem && !bg.Google.map.data) {
                if (!map) map = null;
                if (!sensor) sensor = null;
                if (!z) z = null;
                if (!callback) callback = null;
                var t = setTimeout('bg.Google.map.addressMark("' + address + '","' + map + '",' + sensor + ',' + z + ',' + callback + ');', 100);
            } else if (!!bg.Google.map.mapElem && !!bg.Google.map.data) {
                bg.Google.map.addMarkerByAddress(address);
                if (!!callback) callback();
            }
        },
        hcardMarkAll: function(map, sensor, z, callback) {
            if (!sensor) sensor = null;
            if (!z) z = null;
            if (!callback) callback = null;
            if ($('.vcard').length > 0) {
                for (i = 0; i < $('.vcard').length; i++) {
                    bg.Google.map.hcardMark('.vcard:eq(' + i + ')', map, sensor, z, callback);
                }
            }
        },
        data: null,
        directions: null,
        mapName: null,
        mapCallback: null,
        mapElem: null,
        z: null
    },
    plusone: {
        load: function(callback) {
            if (!callback) callback = null;
            $.getScript('https://apis.google.com/js/plusone.js', function() { callback; });
        },
        button: function(parent, href, size, counter, click_callback, load_callback) {
            if (!click_callback) click_callback = null;
            bg.Google.plusone.button_noload(parent, href, size, counter, click_callback);
            bg.Google.plusone.load(load_callback);
        },
        button_noload: function(parent, href, size, counter, callback) {
            if (!href) href = document.location.href;
            var parameters = ' href="' + href + '"';
            if (!!size) parameters += ' size="' + size + '"';
            if (!!counter) parameters += ' count="' + counter + '"';
            if (!!callback) parameters += ' callback="' + callback + '"';
            $(parent).append('<g:plusone' + parameters + '></g:plusone>');
        }
    },
    youtube: {
		request: function (request_string, callback) {
			$.ajax({url: request_string, dataType: 'jsonp', success: function(data) { debug_v = data; if (!!callback) callback(data); } });
		},
        insert_feed: function(element, username, start_index, max_results, list_type, callback) {
            var script_string = 'https://gdata.youtube.com/feeds/api/users/' + username + '/uploads?alt=json';
            if (!start_index) start_index = 1;
            if (!list_type) list_type = "ol";
            script_string += '&start-index=' + start_index;
            if (!!max_results) script_string += '&max-results=' + max_results;
			bg.Google.youtube.request(script_string, function(data) {
                $(element).append('<' + list_type + ' class="youtube-videos"></' + list_type + '>');
                for (i = 0; i < data.feed.entry.length; i++) {
                    var id = data.feed.entry[i].id.$t.split('videos/')[1];
                    $(element + ' .youtube-videos').append('<li><object width="480" height="390"><param name="movie" value="http://www.youtube.com/v/' + id + '?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + id + '?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="390"></embed></object></li>');
                    if (!!callback) callback();
                }
            });
        },
		insert_feed_thumbnails: function(element, username, start_index, max_results, list_type, callback) {
            var script_string = 'https://gdata.youtube.com/feeds/api/users/' + username + '/uploads?alt=json';
            if (!start_index) start_index = 1;
            if (!list_type) list_type = "ol";
            script_string += '&start-index=' + start_index;
            if (!!max_results) script_string += '&max-results=' + max_results;
			bg.Google.youtube.request(script_string, function(data) {
                $(element).append('<' + list_type + ' class="youtube-videos"></' + list_type + '>');
                for (i = 0; i < data.feed.entry.length; i++) {
					var thumb = data.feed.entry[i].media$group.media$thumbnail[0].url;
					var link = data.feed.entry[i].link[0].href.split("&")[0];
					var title = data.feed.entry[i].title.$t;
                    $(element + ' .youtube-videos').append('<li><a href="' + link + '" title="' + title + '" target="_blank"><img src="' + thumb + '" alt="thumbnail of ' + title + '" /></a></li>');
                    if (!!callback) callback();
                }
            });
		}
    }
}

