URCM = {
    params: {},
    addLoadEvent: function(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    },
    doPopups: function() {
        if (!document.getElementsByTagName) return false;
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
            if (links[i].className.match("popup")) {
                links[i].onclick = function() {
                    window.open(this.href, "", "top=40,left=40,width=785,height=625,scrollbars=1,menubar=1,location=1,resizable=1");
                    return false;
                }
            }
        }
    },
    setParams: function() {
        this.params = {};
        var searchString = document.location.search;
        searchString = searchString.substring(1);
        var nvPairs = searchString.split("&");
        for (i = 0; i < nvPairs.length; i++) {
            var nvPair = nvPairs[i].split("=");
            this.params[nvPair[0]] = nvPair[1];
        }
    },
    init: function() {
        URCM.setParams();
        URCM.doPopups();
    }
}
URCM.addLoadEvent(URCM.init);
window.ntgmap = function() {
    var _pub = {
        pins: [],
        textblock: $('phototext'),
        showMap: function() {
            window.gmap = new google.maps.Map2(document.getElementById('gmap'));
            gmap.setCenter(new google.maps.LatLng(53.6967, -3.2080), 5);
            gmap.addControl(new google.maps.LargeMapControl());
        },
        showPhotos: function() {
            if (!jsonFromFlickr) {
                alert('Flickr photos failed to load');
                return;
            }
            var ul = document.getElementById('photos');
            while (ul.hasChildNodes()) {
                ul.removeChild(ul.firstChild);
            }
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0,
            photo; photo = jsonFromFlickr.items[i]; i++) {
                var li = document.createElement('li');
                var a = document.createElement('a');
                a.href = photo.link;
                var img = document.createElement('img');
                img.src = photo.media.m.replace('_m', '_s');
                img.title = photo.title;
                img.alt = photo.title;
                a.appendChild(img);
                li.appendChild(a);
                ul.appendChild(li);
                var point = new google.maps.LatLng(photo.latitude, photo.longitude);
                bounds.extend(point);
                photo.marker = new google.maps.Marker(point);
                ntgmap.addPin(photo.marker);
                gmap.addOverlay(photo.marker);
                google.maps.Event.addDomListener(a, 'click', this.makeClickCallback(photo));
                google.maps.Event.addListener(photo.marker, 'click', this.makeClickCallback(photo));
            }
            gmap.setZoom(gmap.getBoundsZoomLevel(bounds));
            gmap.setCenter(bounds.getCenter());
        },
        buildInfoWindow: function(photo) {
            var div = document.createElement('div');
            div.className = 'infoBox';
            div.innerHTML = ['<img src="', photo.media.m, '" alt="', photo.title, '">'].join('');
            return div;
        },
        stopEvent: function(ev) {
            ev = ev || window.event;
            if (ev) {
                if (ev.preventDefault) {
                    ev.preventDefault()
                } else {
                    ev.returnValue = false;
                }
            }
        },
        makeClickCallback: function(photo) {
            return function(ev) {
                ntgmap.stopEvent(ev);
                var content = ntgmap.buildInfoWindow(photo);
                photo.marker.openInfoWindow(content);
                gmap.setZoom(2);
                var info = gmap.getInfoWindow();
                closeHandle = google.maps.Event.addListener(info, 'closeclick',
                function(ev) {
                    ntgmap.restore();
                    google.maps.Event.removeListener(closeHandle);
                });
                ntgmap.updatePhotoText(photo);
                ntgmap.highlightPhoto(photo);
                ntgmap.stopEvent(ev);
            }
        },
        makeZoomCallback: function(photo) {
            return function(ev) {
                ntgmap.stopEvent(ev);
                gmap.setCenter(new google.maps.LatLng(photo.latitude, photo.longitude), 14);
                photo.marker.openInfoWindow(ntgmap.buildInfoWindow(photo));
                ntgmap.highlightPhoto(photo);
            }
        },
        highlightPhoto: function(photo) {
            var ul = document.getElementById('photos');
            var links = ul.getElementsByTagName('a');
            for (var i = 0,
            a; a = links[i]; i++) {
                var img = a.getElementsByTagName('img')[0];
                if (a.href == photo.link) {
                    img.style.opacity = 1;
                    img.style.filter = 'alpha(opacity=100)';
                } else {
                    img.style.opacity = 0.4;
                    img.style.filter = 'alpha(opacity=40)';
                }
            }
        },
        setCommentPin: function(c, g) {
            var bounds = gmap.getBounds();
            var point = new google.maps.LatLng(g.latitude, g.longitude);
            bounds.extend(point);
            pin = new google.maps.Marker(point);
            google.maps.Event.addListener(pin, 'click', this.renderCommentInfoWindow(c));
            gmap.addOverlay(pin);
            gmap.setZoom(gmap.getBoundsZoomLevel(bounds));
            gmap.setCenter(bounds.getCenter());
            ntgmap.addPin(pin);
        },
        renderCommentInfoWindow: function(c) {
            return function(ev) {
                ntgmap.stopEvent(ev);
                gmap.setZoom(3);
                this.openInfoWindow('<p>No photo available</p>');
                ntgmap.updatePhotoTextForComment(c);
            }
        },
        renderComment: function(c) {
            var header = '<p class="commentHeader">made by <strong>' + c.name;
            if (c.euid != '') {
                header += '( ' + c.euid + ') ';
            }
            header += '</strong> on <strong>' + c.date + '</strong> at <strong>' + c.time + '</strong></p>';
            var geotags = '';
            c.geotags.each(function(g) {
                ntgmap.setCommentPin(c, g);
                geotags += '<li><a class="popup" href="location.htm?i=' + c.id + '&amp;x=' + g.latitude + '&amp;y=' + g.longitude + '" title="' + g.location + '" >' + g.location + '</a></li>';
            });
            var main = '<p>' + c.comment + '</p>';
            var footer = '';
            if (geotags != '') {
                footer = '<ul class="comment"><li>Show on map: ' + geotags + '</ul>';
            }
            $('urcm_comments').insert('<div id="comment_' + c.id + '"class="comment">' + header + main + footer + '</div>', 'bottom');
        },
        addPin: function(pin) {
            this.pins.push(pin);
        },
        restore: function() {
            var bounds = gmap.getBounds();
            this.resetPhotoText();
        },
        updatePhotoText: function(photo) {
            var paras = photo.description.split('</p>');
				
            this.textblock.innerHTML = '<p style="font-size: 120%; font-weight: bold;">' + photo.title + '</p>' + paras[paras.length - 2];
            this.textblock.highlight();
        },
        resetPhotoText: function() {
            this.textblock.innerHTML = '<p>Click a photo to see it on the map</p>';
        },
        updatePhotoTextForComment: function(c) {
            this.textblock.innerHTML = this.getCommentContent(c);
            this.textblock.highlight();
        },
        getCommentContent: function(c) {
            var comment = cstools_article.commentList.find(function(x) {
                return x.id == c.id;
            });
            var content = comment.comment;
            return this.boldCommentTitle(content);
        },
        boldCommentTitle: function(content) {
            return content.substr(0, 2) + " style='font-size: 120%; font-weight: bold;'" + content.substr(2, content.length - 2);
        }
    }
    google.load('maps', '2');
    return _pub;
} ();
function jsonFlickrFeed(json) {
    window.jsonFromFlickr = json;
}
function setComment(json) {
    ntgmap.singleComment = json;
}