// bgGoogle.js 6 | requires jquery.js 1.6.2+ | made by Kyle Weems of Mindfly Web Studio (http://mindfly.com/)
// Created Aug 18, 2009 | Last Modified :  Aug 16, 2011

if (!bg) var bg = {};
if (!BrainGnat) var BrainGnat = bg;
var debug_v = null;

bg.Google = {
    version: "bg.Google Extension - v6 (requires jquery.js 1.6.2+)",
    map: {
        request: function(request_string, callback) {
            request_string += "&callback=bg.Google.map.process_request";
            $.getScript(request_string, function() { });            
        },
        process_request: 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");
                })
            }
        },
        load: function(elem, callback) {
            bg.Google.map.mapElem = elem;
            if(bg.Google.map.z == null) bg.Google.map.z = 13;
            if (!!callback) bg.Google.map.mapCallback = callback;
            var script = "http://maps.google.com/maps/api/js?sensor=false&async=2";
            bg.Google.map.request(script);
        },
        to_latlng: function(e, callback) {
            var geocoder = new google.maps.Geocoder();
            var address = "";
            switch (typeof e) {
                case "string":
                    address = e;
                    break;
                case "object":
                    if (e.hasClass('vcard')) {
                        address += e.find('.street-address').text();
                        address += ' ' + e.find('.locality').text();
                        address += ', ' + e.find('.region').text();
                        address += ' ' + e.find('.postal-code').text();
                    } else if (e.hasClass('geo')) {
                        var ll = new google.maps.LatLng(e.find('.latitude').text(), e.find('.longitude').text());
                        callback(ll);                
                    }
                    break;
                default:
                    break;
            }
            if (address != "") {
                address = address.replace(/[\r\n]/g, " ");
                geocoder.geocode({'address': address}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var ll = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                        callback(ll);
                    }
                });
            }
        },
        mark: function(ll, info, index, click_func) {
            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 (!!click_func) { google.maps.event.addListener(marker, 'click', function() { click_func(this); }); }
        },
        center: function(latlng, callback) {
            try {
                var test = latlng.lat();
                bg.Google.map.data.setCenter(latlng);
                if (!!callback) callback();
            } catch (e) {
                bg.Google.map.to_latlng(latlng, function(ll){
                    bg.Google.map.data.setCenter(ll);
                    if (!!callback) callback();
                });
            }
        },
        add_mark: function(e, map, callback) {
            if ((typeof map) == "function") callback = map;
            if (e instanceof Array || ((typeof e) == "object" && e.length > 1)) {
                bg.Google.map.multiple_marks(e, map, callback);
            } else {
                if (!bg.Google.map.mapElem) {
                    bg.Google.map.load(map, function() { 
                        bg.Google.map.to_latlng(e, function(ll) {
                            bg.Google.map.center(ll);
                            bg.Google.map.mark(ll);
                            if (!!callback) callback();
                        });
                    });
                } else if (!!bg.Google.map.mapElem && !bg.Google.map.data) {
                    if (!map) map = null;
                    if (!callback) callback = null;
                    var t = setTimeout(function(){bg.Google.map.add_mark(e, map, callback);}, 100);
                } else if (!!bg.Google.map.mapElem && !!bg.Google.map.data) {
                    bg.Google.map.to_latlng(e, function(ll) {
                        bg.Google.map.mark(ll);
                        if (!!callback) callback();
                    });
                }          
            }            
        },
        multiple_marks: function(e, map, callback) {
            if ((typeof map) == "function") callback = map;
            if ((typeof e) == "object") {
                for (i = 0; i < e.length; i ++ ){
                    if (i == (e.length - 1) && !!callback) {
                        bg.Google.map.add_mark(e.eq(i), map, function(){ callback(); });
                    } else {
                        bg.Google.map.add_mark(e.eq(i), map);
                    }
                }
            } else {
                for (i = 0; i < e.length; i++) {
                    if (i == (e.length - 1) && !!callback) {
                        bg.Google.map.add_mark(e[i], map, function() { callback(); });
                    } else {
                        bg.Google.map.add_mark(e[i], map);
                    }
                }
            }
        },
        create: function(map, e, zoom, callback) {
            if ((typeof zoom) == "function") callback = zoom;
            if ((typeof zoom) == "number") bg.Google.map.z = zoom;
            if (e instanceof Array || (typeof e) == "object") {
                bg.Google.map.multiple_marks(e, map, function() { if (!!callback) callback(); });
            } else {
                bg.Google.map.add_mark(e, map, function() { if (!!callback) callback(); });
            }
        },
        zoom: function(n) {
            bg.Google.map.z = n;
            bg.Google.map.data.setZoom(n);
        },
        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();
                }
            });
        }
    }
}


