/*extern JSON document SWFObject _gel google checkUndef initLytebox window ActiveXObject DOMParser G_MAP_MARKER_PANE */
var geoplayer = {
    bounds:{},
    geocoder:{},
    map:{},
    agent:{},
    profile:{},
    tooltip:{},
    searchFunc: {},
    ajaxRequests: [],
    markers: [],
    business: [],
    favorites: [],
    icons:[],
    profileList:[],
    clicked: false,
    showProperties:false,
    form:false,
    nl:"\n",
    infoWindowLength: 250,
    lastSearch: 0,
    _id: pid,
    _uid: uid,
    _token: token,
    _request: {},
    iconSets: {},
    tabs: ['map','search','profile','favorites'],
    profileTabs: [
    {name:'noProfile',clear:true},
    {name:'ProfileOuter',clear:false},
    {name:'Video',clear:true},
    {name:'Gallery',clear:true},
    {name:'Contact',clear:false}
    ],
    searchTabs: ['Community','Property','School','Business','Results'],
    favoriteTabs: ['All','Contact','Business','City','Community','Property','School'],
    lookup: ['city','state','zip'],

    prefs: {

        data: {},

        load: function () {
            var the_cookie = document.cookie.split(';');
            for(var i=0;i<the_cookie.length;i++) {
                try {
                    this.data = JSON.parse(unescape(the_cookie[i]));
                } catch (e) {
                    e = null;
                }
            }
            return this.data;
        },

        save: function (expires, path) {
            var d = expires || new Date(2020, 2, 2);
            var p = path || '/';
            document.cookie = escape(JSON.stringify(this.data)) + ';path=' + p + ';expires=' + d.toUTCString();
        }
    },

    activate: function(tab) {
        if(_gel(tab+'Tab').className != 'selected') {
            if(tab != 'map') {
                this.map.closeInfoWindow();
            }
            this.hideTabs();
            _gel(tab+'Tab').className = 'selected';
            _gel(tab+'Nav').style.display='block';
            _gel(tab).style.display='block';
            if(tab == 'map' && !checkUndef(this.map)) {
                this.map.checkResize();
            }
            switch(tab) {
                case 'favorites':
                this.activateFavoriteTab('All');
                this.buildFavorites();
                break;

                case 'search':
                if(!this.form) {
                    this.buildSearchForm();
                } else if(this.getActiveSearchTab() == 'Results') {
                    this.buildSearchResults();
                }
            }
        }
    },

    activateProfileTab: function(tab) {
        if(_gel('pTab'+tab).className != 'selected') {
            this.hideProfileTabs();
            if(tab == 'Contact') {
                _gel('contactFormSubject').value = 'RE: ' + (!checkUndef(this.profile.name)?this.profile.name:'');
            }
            _gel('pTab'+tab).className = 'selected';
            _gel(tab).style.display='block';
        }
    },

    activateSearchTab: function(tab) {
        if(_gel('sTab'+tab).className != 'selected') {
            this.hideSearchTabs();
            _gel('sTab'+tab).className = 'selected';
            _gel('search'+tab).style.display = 'block';
        }
    },

    activateFavoriteTab: function(tab) {
        if(_gel('fTab'+tab).className != 'selected') {
            this.hideFavoriteTabs();
            _gel('fTab'+tab).className = 'selected';
        }
    },

    addFavorite: function() {
        // assume that the add to the favorites list is only on clickable on the active profile.
        var profile = this.profile;
        if(this.findFavorite(profile.pid) === false) {
            var obj = {};
            obj.pid = profile.pid;
            obj.category = profile.category;
            obj.name = profile.name;
            obj.description = profile.description[0].value.strip_tags();
            if(!checkUndef(profile.photos)) {
                obj.photo = profile.photos[0].value;
                if(!checkUndef(profile.photos[0].alt)) {
                    obj.photoAlt = profile.photos[0].alt;
                } else {
                    obj.photoAlt = '';
                }
            }
            this.favorites.push(obj);
            this.prefs.data.favorites = this.favorites;
            this.prefs.save();
        }
    },

    buildBusiness: function() {
        google.maps.DownloadUrl('business.php?id=' + this.agent.pid, function(data, responseCode) {
            var xmlDoc = google.maps.Xml.parse(data);
            var items = xmlDoc.documentElement.getElementsByTagName("item");
            if(items.length > 0) {
                for (i = 0; i < items.length; i++) {
                    geoplayer.business[i] = geoplayer.buildMarker(items[i]);
                }
            }
            for(i=0;i<geoplayer.business.length;i++) {
                geoplayer.map.addOverlay(geoplayer.business[i]);
            }
        });
    },

    buildContact: function (obj) {
        var getstr = "?";

        if(obj.childNodes.length > 0) {
            getstr += this.buildObjectChild(obj);
        }

        var url = '_email.php' + getstr;
        google.maps.DownloadUrl(url);
        if(!checkUndef(this.profile.pid)) {
            this.showProfile(this.profile.pid);
        }
    },

    buildContactForm: function(div,form) {
        var oh = '';
        oh += '<form action="javascript:geoplayer.buildContact(_gel(\'contact' + div + '\'));" id="contact' + div + '">';
        oh += '<p class="profileHeader">Contact Us Form</p>';
        for(var i=0;i<form.length;i++) {
            if(checkUndef(form[i].type)) {
                form[i].type = 'text';
            }
            switch(form[i].type.toLowerCase()) {
                case 'textarea':
                oh+= '<p><textarea style="width:400px;height:92px;" name="' + form[i].name + '" id="contactForm' + form[i].name + '" title="Please enter your ' + form[i].name.replace(/_/g,' ') + '" onfocus="if(this.value==this.name.replace(/_/g,\' \')) this.value = \'\';" onblur="if(this.value==\'\') this.value=this.name.replace(/_/g,\' \');" >' + form[i].name.replace(/_/g,' ') + '</textarea></p>';
                break;

                case 'text':
                oh += '<p><input style="width:400px;" type="text" name="' + form[i].name + '" id="contactForm' + form[i].name + '" title="Please enter your ' + form[i].name.replace(/_/g,' ') + '" value="' + form[i].name.replace(/_/g,' ') + '" onfocus="if(this.value==this.name.replace(/_/g,\' \')) this.value = \'\';" onblur="if(this.value==\'\') this.value=this.name.replace(/_/g,\' \');" /></p>';
                break;

                default:
                oh += '<p><input style="width:400px;" type="text" name="' + form[i].name + '" id="contactForm' + form[i].name + '" title="Please enter your ' + form[i].name.replace(/_/g,' ') + '" value="' + form[i].name.replace(/_/g,' ') + '" onfocus="if(this.value==this.name.replace(/_/g,\' \')) this.value = \'\';" onblur="if(this.value==\'\') this.value=this.name.replace(/_/g,\' \');" /></p>';
                break;
            }
        }
        oh += '<p><button type="submit">Submit</button></p>';
        oh += '</form>';
        _gel(div).innerHTML = oh;
    },

    buildFavorites: function() {
        if(this.favorites.length>0) {
            var i;
            var oh = '';
            var shown = 0;
            var filter = (checkUndef(arguments[0])?'':arguments[0]);

            for(i=0;i<this.favorites.length;i++) {
                if(filter === '' || this.favorites[i].category.toLowerCase() == filter.toLowerCase()) {
                    oh += '  <li class="alt' + (i%2) + ((this.profile.pid == this.favorites[i].pid)?(' selected'):('')) +'">' + this.nl;
                    oh += '    <div class="listResult">' + this.nl;
                    oh += '      <button type="button" onclick="geoplayer.removeFavorite(' + this.favorites[i].pid + ');geoplayer.buildFavorites(\'' + filter + '\');" title="Remove from Favorites" class="listResultMarker">Remove</button>';
                    if(!checkUndef(this.favorites[i].photo)) {
                        oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.favorites[i].pid + ');return false;" class="listResultImage">' + this.nl;
                        oh += '      <img src="' + this.favorites[i].photo + '" title="' + this.favorites[i].photoAlt + '" alt="' + this.favorites[i].photoAlt + '" class="thumbnail" />' + this.nl;
                        oh += '    </a>' + this.nl;
                    } else {
                        oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.favorites[i].pid + ');return false;" class="listResultImage">' + this.nl;
                        oh += '      <img src="img/spacer.gif" title="" alt="" class="thumbnail" />' + this.nl;
                        oh += '    </a>' + this.nl;
                    }
                    oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.favorites[i].pid + ');return false;" class="listResultContent">' + this.nl;
                    oh += '      <p class="resultsHeader">' + this.favorites[i].name + '</p>' + this.nl;
                    oh += '      <p>' + this.smartText(this.favorites[i].description,200) + '</p>' + this.nl;
                    oh += '    </a>' + this.nl;
                    oh += '    </div>' + this.nl;
                    oh += '  </li>' + this.nl;
                    shown++;
                }
            }
            if(shown == 0) {
                oh += '<p style="padding:5px;"><strong>You currently have no favorites for the selected category!</strong></p>';
            } else {
                oh = '<ul>' + oh + '</ul>';
            }
        } else {
            oh = '<p style="padding:5px;"><strong>You currently have no favorites!</strong></p>';
        }
        _gel('favorites').innerHTML = oh;
    },

    buildHash: function() {
        var i;
        var hashString = '';
        for(i=0;i<arguments.length;i++) {
            hashString += arguments[i] + '/';
        }
        hashString = hashString.substring(0,hashString.length-1);
        location.hash = hashString;
    },

    buildMarker: function(item) {
        if(google.maps.Xml.value(item.getElementsByTagName("map")[0]) !== '') {
            var coords = google.maps.Xml.value(item.getElementsByTagName("map")[0]).split(',');
            var point = new google.maps.LatLng(coords[0],coords[1]);
            var category = item.getAttribute('category').toLowerCase();
            var marker;
            var j;
            if(!checkUndef(geoplayer.iconSets[category])) {
                marker = new google.maps.Marker(point,{icon:geoplayer.iconSets[category]});
            }  else {
                marker = new google.maps.Marker(point);
            }
            marker.attributes = {};
            for(j = 0;j<item.attributes.length;j++) {
                marker.attributes[item.attributes[j].nodeName] = item.attributes[j].nodeValue ;
            }
            marker.attributes.point = point;
            for(j = 0;j<item.childNodes.length;j++) {
                if(item.childNodes[j].nodeName != '#text') {
                    marker[item.childNodes[j].nodeName] = item.childNodes[j].firstChild.nodeValue;
                }
            }
            var oh = '';
            switch(category.toLowerCase()) {
                case 'city':
                oh += '<table class="infoWindow">';
                oh += '<tr><td><strong>';
                oh += marker.attributes.name;
                oh += '</strong></td></tr>';
                oh += '<tr><td style="text-align:justify">';
                oh += geoplayer.smartText(marker.description,geoplayer.infoWindowLength);
                oh += '&nbsp;&nbsp;&nbsp;<a target="_self" href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ');return false;">Learn More</a></td></tr>';
                oh += '<tr><td>';
                if(!checkUndef(marker.attributes.cid) && !checkUndef(marker.attributes.video)) {
                    oh += '<a href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ',true,true);return false;">Play Video</a><br />';
                }
                if(marker.attributes.communities > 0) {
                    oh += '<a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Community&Market=' + marker.attributes.pid + '&ParentID=' + marker.attributes.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Communities (' + marker.attributes.communities + ')</a><br />';
                }
                if(marker.attributes.properties > 0 && geoplayer.showProperties) {
                    oh += '<a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Property&City=' + marker.attributes.name + '&ParentID=' + marker.attributes.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Properties (' + marker.attributes.properties + ')</a><br />';
                }
                if(marker.attributes.schools > 0) {
                    oh += '<a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=School&County=' + marker.attributes.county + '&ParentID=' + marker.attributes.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Area Schools (' + marker.attributes.schools + ')</a><br />';
                }
                oh += '</tr></td>';
                oh += '</table>';
                break;

                case 'community':
                oh += '<table class="infoWindow">';
                oh += '<tr><td><strong>';
                oh += marker.attributes.name;
                oh += '</strong></td></tr>';
                oh += '<tr><td style="text-align:justify">';
                oh += geoplayer.smartText(marker.description,geoplayer.infoWindowLength);
                oh += '&nbsp;&nbsp;&nbsp;<a target="_self" href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ');return false;">Learn More</a></td></tr>';
                oh += '<tr><td>';
                if(!checkUndef(marker.attributes.cid) && !checkUndef(marker.attributes.video)) {
                    oh += '<a href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ',true,true);return false;">Play Video</a><br />';
                }
                if(marker.attributes.properties > 0 && geoplayer.showProperties) {
                    oh += '<a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Property&Community=' + marker.attributes.name + '&ParentID=' + marker.attributes.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Properties (' + marker.attributes.properties + ')</a><br />';
                }
                if(marker.attributes.schools > 0) {
                    oh += '<a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=School&County=' + marker.attributes.county + '&ParentID=' + marker.attributes.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Area Schools (' + marker.attributes.schools + ')</a><br />';
                }
                oh += '</tr></td>';
                oh += '</table>';
                break;

                case 'property':
                oh += '<table class="infoWindow">';
                oh += '<tr><td><strong>';
                oh += marker.attributes.name;
                oh += '</strong></td></tr>';
                oh += '<tr><td style="text-align:justify">';
                if(!checkUndef(marker.description)) {
                    oh += geoplayer.smartText(marker.description,geoplayer.infoWindowLength);
                }
                oh += '&nbsp;&nbsp;&nbsp;<a target="_self" href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ');return false;">Learn More</a></td></tr>';
                if(!checkUndef(marker.attributes.cid) && !checkUndef(marker.attributes.video)) {
                    oh += '<tr><td>';
                    oh += '<a href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ',true,true);return false;">Play Video</a>';
                    oh += '</td></tr>';
                }
                oh += '</table>';
                break;

                default:
                oh += '<table class="infoWindow">';
                oh += '<tr><td><strong>';
                oh += marker.attributes.name + ' (' + marker.attributes.category + ')';
                oh += '</strong></td></tr>';
                oh += '<tr><td style="text-align:justify">';
                if(!checkUndef(marker.description)) {
                    oh += geoplayer.smartText(marker.description,geoplayer.infoWindowLength);
                }
                oh += '&nbsp;&nbsp;&nbsp;<a target="_self" href="#" onclick="geoplayer.showProfile(' + marker.attributes.pid + ');return false;">Learn More</a></td></tr>';
                oh += '</table>';
                break;
            }

            marker.html = oh;

            // Another switch statement for the tooltip, since we want propeties to look differently,
            // just building in the funcionality incase we want to customize others.
            oh = '<div class="tooltip">';
            switch(category.toLowerCase()) {
                case 'property':
                oh += '<ul>';
                oh += '<li style="text-align:center;">' + marker.attributes.name + '</li>';
                if(!checkUndef(marker.attributes.photo)) {
                    oh += '<li style="float:left;"><img src="' + marker.attributes.photo + '" title="' + marker.attributes.name + '" alt="' + marker.attributes.name + '" style="width:90px;height:60px;border:0px;vertical-align:middle;display:inline;" />&nbsp;&nbsp;</li>';
                }
                oh += '<li><strong>beds .:</strong> ' + marker.attributes.bedrooms + '</li>';
                oh += '<li><strong>baths .:</strong> ' + marker.attributes.full_baths + '</li>';
                if(!checkUndef(marker.attributes.square_footage)) {
                    oh += '<li><strong>sq. ft. .:</strong> ' + marker.attributes.square_footage + '</li>';
                }
                if(!checkUndef(marker.attributes.rental)) {
                    oh += '<li><strong>rental .:</strong> ' + marker.attributes.rental + '</li>';
                }
                if(!checkUndef(marker.attributes.listing_price)) {
                    oh += '<li><strong>price .:</strong> ' + marker.attributes.listing_price + '</li>';
                }
                break;

                default:
                oh += marker.attributes.name;
                break;
            }
            oh += '</div>';

            marker.tooltip = oh;
            google.maps.Event.addListener(marker, "mouseover", function() {
                geoplayer.showTooltip(this);
            });
            google.maps.Event.addListener(marker, "mouseout", function() {
                geoplayer.tooltip.style.visibility="hidden";
            });

            return marker;
        } else {
            return false;
        }
    },

    buildMarkers: function(requestString,showMap,showResults,noAppend) {
        if(checkUndef(noAppend)) {
            noAppend = false;
        }
        this.markers = [];
        if(showMap) {
            this.activate('map');
        } else if(showResults) {
            this.activateSearchTab('Results');
            _gel('searchResults').innerHTML = '<p style="padding:5px;">Searching for profiles that match your criteria ... <img src="img/hourglass_icon.gif" alt="Loading..." title="Loading..." /></p>';
        }
        var i, j;
        this.map.addControl(this.loadingMessage);
        this.map.clearOverlays();
        this.lastSearch = new Date().getTime();
        if(!noAppend) {
            requestString += searchAppend
        }
        this.xmlRequest(requestString, function(ajax) {
            if(ajax.timestamp >= geoplayer.lastSearch) {
                var xmlDoc = google.maps.Xml.parse(ajax.request.responseText);
                var items = xmlDoc.documentElement.getElementsByTagName("item");
                if(items.length > 0) {
                    geoplayer.bounds = new google.maps.LatLngBounds();
                    for (i = 0; i < items.length; i++) {
                        geoplayer.markers[i] = geoplayer.buildMarker(items[i]);
                        geoplayer.bounds.extend(geoplayer.markers[i].attributes.point);
                        geoplayer.map.addOverlay(geoplayer.markers[i]);
                    }
                    geoplayer.map.setCenter(geoplayer.bounds.getCenter(),geoplayer.map.getBoundsZoomLevel(geoplayer.bounds)-1);
                }
                for(i=0;i<geoplayer.business.length;i++) {
                    geoplayer.map.addOverlay(geoplayer.business[i]);
                }
                geoplayer.map.removeControl(geoplayer.loadingMessage);
                geoplayer.buildSearchResults();
            }
        });
    },

    buildObjectChild: function(obj) {
        var getstr = '';
        var i;
        var j;
        for (i=0; i<obj.childNodes.length; i++) {
            if(obj.childNodes[i].childNodes.length > 0) {
                getstr += this.buildObjectChild(obj.childNodes[i]);
            }
            if (obj.childNodes[i].tagName == "INPUT") {
                if (obj.childNodes[i].type == "text" && obj.childNodes[i].value !== '') {
                    getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                }
                if (obj.childNodes[i].type == "hidden" && obj.childNodes[i].value !== '') {
                    getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                }
                if (obj.childNodes[i].type == "checkbox") {
                    if (obj.childNodes[i].checked) {
                        getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                    }
                }
                if (obj.childNodes[i].type == "radio") {
                    if (obj.childNodes[i].checked) {
                        getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                    }
                }
            }
            if (obj.childNodes[i].tagName == "SELECT") {
                var sel = obj.childNodes[i];
                for(j=0;j<sel.options.length;j++) {
                    if(sel.options[j].selected) {
                        getstr += sel.name + "=" + sel.options[j].value + "&";
                    }
                }
            }

        }
        return getstr;
    },

    // http://www.captain.at/howto-ajax-form-post-get.php
    buildObject: function(obj) {
        var getstr = "?";

        if(obj.childNodes.length > 0) {
            getstr += this.buildObjectChild(obj);
        }

        var url = 'search.php' + getstr;
        _gel('quickSearchList').value = '-1';
        this.buildMarkers(url,false,true);
    },

    buildPrintable: function() {
        var i;
        var printable = window.open('','printable_brochure','width=680, height=500, resizable=1, scrollbars=1');
        printable.document.writeln('<html>');
        printable.document.writeln('<head>');
        printable.document.writeln('<title>' + this.profile.name + '</title>');
        printable.document.writeln('<link rel="stylesheet" media="all" href="base.css" type="text/css" />');
        printable.document.writeln('</head>');
        printable.document.writeln('<body onload="window.print();">');
        printable.document.writeln('<img src="img/printable_header.gif" alt="Header" />');
        printable.document.writeln('<br />');
        printable.document.writeln('<br />');
        printable.document.writeln('<div style="width:650px;margin:8px;">');
        printable.document.writeln(_gel('printableBrochure').innerHTML);
        printable.document.writeln('</div>');
        printable.document.writeln('<br />');
        printable.document.writeln('<br />');
        var oh = '';
        for(i = 1;i<this.profile.photos.length;i++) {
            oh += '<img src="' + this.profile.photos[i].value + '" alt="' +  this.profile.photos[i].alt + '" title="' +  this.profile.photos[i].alt + '" width="180" height="120" style="float:left;padding:6px;" />';
        }
        printable.document.writeln(oh);
        printable.document.writeln('<br style="clear:both;" />');
        printable.document.writeln('<br />');
        printable.document.writeln(_gel('contactInfo').innerHTML);
        printable.document.writeln('<img src="img/printable_footer.gif" alt="Footer" />');
        printable.document.writeln('</body>');
        printable.document.writeln('</html>');
        printable.document.close();

    },

    buildProfile: function() {
        // @todo
        // Probably want to refine what is displayed with the title along with some sort of hashing mechanism.
        //document.title = profile.name;

        var i;
        var profile = this.profile;
        var contactLookup = false;
        /**
        There will only be 2 states for a profile build
        buildAll
        */
        var buildAll = (checkUndef(arguments[0]))?true:arguments[0];
        var buildVideo = (checkUndef(arguments[1]))?false:arguments[1];
        _gel('contactInformationHeader').innerHTML = 'contact info';
        _gel('contactInformation').innerHTML = '';
        _gel('additionalInformation').innerHTML = '';
        _gel('videoLinks').innerHTML = '';

        if(buildAll) {
            this.profileList.push({pid:profile.pid,name:profile.name});
        }

        // Update Profile Image
        _gel('profileImage').src = 'http://www.dvls.tv/content_distribution/products/geoplayer/img/no_image_available.gif';
        if(!checkUndef(profile.photos)) {
            _gel('profileImage').src = profile.photos[0].value;
            if(!checkUndef(profile.photos[0].alt)) {
                _gel('profileImage').alt = profile.photos[0].alt;
                _gel('profileImage').title = profile.photos[0].alt;
            } else {
                _gel('profileImage').alt = '';
                _gel('profileImage').title = '';
            }
        }
        // Update the places in which the Category for the profile is displayed.
        var catElements = _gel('profileNav').getElementsByTagName('span');
        for(i=0;i<catElements.length;i++) {
            switch(profile.category.toLowerCase()) {
                case 'region':
                catElements[i].innerHTML = 'Regional';
                break;

                case 'contact':
                if(catElements[i].className == 'Profile') {
                    if(!checkUndef(profile.type[0].value)) {
                        catElements[i].innerHTML = profile.type[0].value;
                    } else {
                        catElements[i].innerHTML = 'Agent';
                    }
                } else {
                    catElements[i].innerHTML = 'Spotlight';
                }
                break;

                case 'school':
                if(catElements[i].className == 'Profile') {
                    catElements[i].innerHTML = profile.category;
                } else {
                    catElements[i].innerHTML = 'Spotlight';
                }
                break;

                default:
                catElements[i].innerHTML = profile.category;
                break;
            }

        }

        var oh = '';
        var ah = '';
        var ch = '';
        var shown = ['name','map','category','pid','video_tour','related_tours','market','regional_tour','regional_profiles','photos','community_tour','properties','schools','city_tour','type','keywords','region','related_profiles','uid'];
        var addressInfo = ['city','state','phone','street1','zip','website','street2','tollfree_phone','fax','email','cell_phone'];

        switch(profile.category.toLowerCase()) {
            /* Build City Profile */
            case 'city':
            shown.push('activities_and_tourism','description','historical_perspective','overview','group_title','county','state','academic_institutions','attractions','business_resources','community_links','culture','government','local_coverage','medical_facilities','school_information','shopping','telephone_directories','weather_info');
            shown = shown.concat(addressInfo);

            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = ((!checkUndef(profile.county))?(profile.county[0].value + ' County, '):('')) + ((!checkUndef(profile.state))?(profile.state[0].value):(''));

            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }
            if(profile.communities > 0) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Community&Market=' + profile.pid + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Communities (' + profile.communities + ')</a></p>';
            }
            if(profile.properties > 0 && geoplayer.showProperties) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Property&City=' + profile.name + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Properties (' + profile.properties + ')</a></p>';
            }
            if(profile.schools > 0) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=School&County=' + profile.county[0].value + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Area Schools (' + profile.schools + ')</a></p>';
            }
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=1" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Comparative Census Report">Comparative Census Report</a></p>';
            ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=2" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Income & Housing Report">Income &amp; Housing Report</a></p>';
            ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=3" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Occupation and Employment Report">Occupation and Employment Report</a></p>';
            ah += '<p><a href="#" onclick="geoplayer.buildPrintable();return false;">Printable Brochure</a></p>';

            if(!checkUndef(geoplayer.agent)) {
                contactLookup = true;
                this.lookupProfile(geoplayer.agent.pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }

            if(!checkUndef(profile.description)) {
                oh += this.buildProfileItem('description',profile.description[0].value);
            }
            break;

            /* Build Region Profile */
            case 'region':
            shown.push('description');
            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = ((!checkUndef(profile.county))?(profile.county[0].value + ' County, '):('')) + ((!checkUndef(profile.state))?(profile.state[0].value):(''));

            if(!checkUndef(geoplayer.agent)) {
                contactLookup = true;
                this.lookupProfile(geoplayer.agent.pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }

            // Build Additional Information
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            if(!checkUndef(profile.state) && !checkUndef(profile.county)) {
                ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=1" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Comparative Census Report">Comparative Census Report</a></p>';
                ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=2" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Income & Housing Report">Income &amp; Housing Report</a></p>';
                ah += '<p><a href="demographics.php?State=' + profile.state[0].value + '&amp;County=' + encodeURI(profile.county[0].value) + '&amp;Report=3" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Occupation and Employment Report">Occupation and Employment Report</a></p>';
            }

            if(!checkUndef(profile.description)) {
                oh += this.buildProfileItem('description',profile.description[0].value);
            }
            break;

            /* Build Community Profile */
            case 'community':
            shown.push('city','state','description','zip');
            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = (!checkUndef(profile.city)?(profile.city[0].value + ', '):('')) + (!checkUndef(profile.state)?(profile.state[0].value):('')) + (!checkUndef(profile.zip)?(' ' + profile.zip[0].value):(''));


            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }
            if(profile.properties > 0 && geoplayer.showProperties) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Property&Community=' + profile.name + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Properties (' + profile.properties + ')</a></p>';
            }
            if(profile.schools > 0) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=School&County=' + profile.county[0].value + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Area Schools (' + profile.schools + ')</a></p>';
            }
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=1" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Comparative Census Report">Comparative Census Report</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=2" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Income & Housing Report">Income &amp; Housing Report</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=3" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Occupation and Employment Report">Occupation and Employment Report</a></p>';
            ah += '<p><a href="#" onclick="geoplayer.buildPrintable();return false;">Printable Brochure</a></p>';

            if(!checkUndef(geoplayer.agent)) {
                contactLookup = true;
                this.lookupProfile(geoplayer.agent.pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }

            oh += this.buildProfileItem('description',profile.description[0].value);
            break;

            /* Build Property Profile */
            case 'property':
            shown.push('city','description','state','street','zip','street1','agent','bedrooms','full_baths','half_baths','square_footage');
            oh += '<ul>';
            if(!checkUndef(profile.bedrooms)) {
                oh += '<li><span class="profileHeader">beds</span> ' + profile.bedrooms[0].value + '</li>';
            }
            if(!checkUndef(profile.full_baths)) {
                oh += '<li><span class="profileHeader">baths</span> ' + profile.full_baths[0].value + '</li>';
            }
            if(!checkUndef(profile.half_baths)) {
                oh += '<li><span class="profileHeader">half baths</span> ' + profile.half_baths[0].value + '</li>';
            }
            if(!checkUndef(profile.square_footage)) {
                oh += '<li><span class="profileHeader">sq. ft.</span> ' + profile.square_footage[0].value + '</li>';
            }
            oh += '</ul><div style="height:30px;"></div>';

            if(!checkUndef(profile.agent)) {
                contactLookup = true;
                this.lookupProfile(profile.agent[0].pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }
            _gel('contactInformationHeader').innerHTML = 'listing agent info';


            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }
            if(profile.schools > 0) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=School&County=' + profile.county[0].value + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display Area Schools (' + profile.schools + ')</a></p>';
            }
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=1" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Comparative Census Report">Comparative Census Report</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=2" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Income & Housing Report">Income &amp; Housing Report</a></p>';
            ah += '<p><a href="demographics.php?Zip=' + profile.zip[0].value + '&amp;Report=3" rel="lyteframe[reports]" rev="width:400px;height:400px;scrolling:no;" title="Occupation and Employment Report">Occupation and Employment Report</a></p>';
            ah += '<p><a href="#" onclick="geoplayer.buildPrintable();return false;">Printable Brochure</a></p>';

            _gel('profileName').innerHTML = profile.street1[0].value;
            var lh = profile.city[0].value + ', ' + profile.state[0].value + ' ' + profile.zip[0].value;
            lh += '<br /><br />';
            if(!checkUndef(profile.rental)) {
                lh += '<span class="profileHeader">' + profile.rental[0].value + ' rental</span>';
            } else if(!checkUndef(profile.listing_price)) {
                lh += '<span class="profileHeader">' + profile.listing_price[0].value + '</span>';
            }
            _gel('profileLocation').innerHTML = lh;

            oh += this.buildProfileItem('description',profile.description[0].value);
            break;

            /* Build Contact Profile */
            case 'contact':
            shown.push('additional_domain','fax','email','street1','street2','tollfree_phone','website','zip','business','billing_cycle','billing_email','comments','domain_name','effective_date','first_name','industry','initial','last_name','login','market','password','phone','salutation','subscription','suffix','title','uid','use_business_address','webmaster','webmaster_email');
            shown = shown.concat(addressInfo);

            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = profile.business[0].value;

            if(!checkUndef(profile.street1)) {
                ch += '<p class="contactText">' + profile.name + '</p>';
                ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + profile.business[0].pid + ',false);return false;">' + profile.business[0].value + '</a></p>';
                ch += '<p class="contactText">' + profile.street1[0].value + '</p>';
            }
            if(!checkUndef(profile.street2)) {
                ch += '<p class="contactText">' + profile.street2[0].value + '</p>';
            }
            ch += '<p class="contactText">' + (!checkUndef(profile.city)?(profile.city[0].value + ', '):('')) + (!checkUndef(profile.state)?(profile.state[0].value):('')) + (!checkUndef(profile.zip)?(' ' + profile.zip[0].value):('')) + '</p>';
            if(!checkUndef(profile.phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + profile.phone[0].value + '</p>';
            }
            if(!checkUndef(profile.cell_phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">cell: </span>' + profile.cell_phone[0].value + '</p>';
            }
            if(!checkUndef(profile.tollfree_phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + profile.tollfree_phone[0].value + '</p>';
            }
            if(!checkUndef(profile.fax)) {
                ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + profile.fax[0].value + '</p>';
            }
            if(!checkUndef(profile.email)) {
                ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + profile.email[0].value + '">' + profile.email[0].value + '</a></p>';
            }
            if(!checkUndef(profile.website)) {
                ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + profile.website[0].value + '</p>';
            }

            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }
            if(profile.properties > 0 && geoplayer.showProperties) {
                ah += '<p><a href="#" onclick="geoplayer.buildMarkers(\'search.php?Category=Property&Agent=' + profile.pid + '&ParentID=' + profile.pid + '\',true);_gel(\'quickSearchList\').value = \'-1\';return false;">Display My Listings (' + profile.properties + ')</a></p>';
            }
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            ah += '<p><a href="#" onclick="geoplayer.buildPrintable();return false;">Printable Contact Sheet</a></p>';
            break;

            /* Build Business Profile */
            case 'business':
            shown.push('comments','contact');
            shown = shown.concat(addressInfo);

            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = profile.city[0].value + ', ' + profile.state[0].value + ' ' + profile.zip[0].value;

            if(!checkUndef(profile.street1)) {
                ch += '<p class="contactText">' + profile.name + '</p>';
                ch += '<p class="contactText">' + profile.street1[0].value + '</p>';
            }
            if(!checkUndef(profile.street2)) {
                ch += '<p class="contactText">' + profile.street2[0].value + '</p>';
            }
            ch += '<p class="contactText">' + (!checkUndef(profile.city)?(profile.city[0].value + ', '):('')) + (!checkUndef(profile.state)?(profile.state[0].value):('')) + (!checkUndef(profile.zip)?(' ' + profile.zip[0].value):('')) + '</p>';
            if(!checkUndef(profile.phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + profile.phone[0].value + '</p>';
            }
            if(!checkUndef(profile.cell_phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">cell: </span>' + profile.cell_phone[0].value + '</p>';
            }
            if(!checkUndef(profile.tollfree_phone)) {
                ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + profile.tollfree_phone[0].value + '</p>';
            }
            if(!checkUndef(profile.fax)) {
                ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + profile.fax[0].value + '</p>';
            }
            if(!checkUndef(profile.email)) {
                ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + profile.email[0].value + '">' + profile.email[0].value + '</a></p>';
            }
            if(!checkUndef(profile.website)) {
                ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + profile.website[0].value + '</p>';
            }

            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }
            ah += '<p><a href="#" onclick="geoplayer.activateProfileTab(\'Contact\');return false;">Contact Us</a></p>';
            ah += '<p><a href="#" onclick="geoplayer.buildPrintable();return false;">Printable Contact Sheet</a></p>';
            break;

            case 'school':
            shown.push('ratings');
            shown = shown.concat(addressInfo);

            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = profile.city[0].value + ', ' + profile.state[0].value;
            _gel('contactInformationHeader').innerHTML = 'contact info';

            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }

            for(i=0;i<profile.ratings.length;i++) {
                ah += '<p>' + profile.ratings[i].value + '<p>';
            }

            if(!checkUndef(geoplayer.agent)) {
                contactLookup = true;
                this.lookupProfile(geoplayer.agent.pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }
            break;

            /* Build Default Profile */
            default:
            shown.push('city','state');
            shown = shown.concat(addressInfo);

            _gel('profileName').innerHTML = profile.name;
            _gel('profileLocation').innerHTML = profile.city[0].value + ', ' + profile.state[0].value;

            // Build Additional Information
            if(this.findFavorite(profile.pid) !== false) {
                ah += '<p>Saved to Favorites</p>';
            } else {
                ah += '<p><a href="#" onclick="geoplayer.addFavorite();this.parentNode.innerHTML = \'Saved to Favorites\';return false;">Add to Favorites</a></p>';
            }

            if(!checkUndef(geoplayer.agent)) {
                contactLookup = true;
                this.lookupProfile(geoplayer.agent.pid, function(data, responseCode) {
                    var xmlDoc = google.maps.Xml.parse(data);
                    var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
                    var agent = {};
                    agent.name = profileDoc.getAttribute('name');
                    agent.pid = profileDoc.getAttribute('pid');
                    var profileNodes = profileDoc.childNodes;
                    for (var i = 0; i < profileNodes.length; i++) {
                        if(profileNodes[i].nodeName != '#text') {
                            if(checkUndef(agent[profileNodes[i].nodeName])) {
                                agent[profileNodes[i].nodeName] = [];
                            }
                            var obj = {};
                            obj.value = profileNodes[i].firstChild.nodeValue;
                            if(profileNodes[i].attributes.length > 0) {
                                obj.pid = profileNodes[i].getAttribute('pid');
                            }
                            agent[profileNodes[i].nodeName].push(obj);
                        }
                    }
                    var ch = '';
                    ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.pid + ',false);return false;">' + agent.name + '</a></p>';
                    if(!checkUndef(agent.business)) {
                        ch += '<p class="contactText"><a href="#" onclick="geoplayer.showProfile(' + agent.business[0].pid + ',false);return false;">' + agent.business[0].value + '</a></p>';
                    }
                    ch += '<p class="contactText">' + agent.street1[0].value + '</p>';
                    ch += '<p class="contactText">' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '</p>';
                    if(!checkUndef(agent.phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">phone: </span>' + agent.phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.tollfree_phone)) {
                        ch += '<p class="contactText"><span class="contactHighlight">tollfree: </span>' + agent.tollfree_phone[0].value + '</p>';
                    }
                    if(!checkUndef(agent.fax)) {
                        ch += '<p class="contactText"><span class="contactHighlight">fax: </span>' + agent.fax[0].value + '</p>';
                    }
                    if(!checkUndef(agent.email)) {
                        ch += '<p class="contactText"><span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a></p>';
                    }
                    if(!checkUndef(agent.website)) {
                        ch += '<p class="contactText"><span class="contactHighlight">website: </span>' + agent.website[0].value + '</p>';
                    }
                    _gel('contactInformation').innerHTML = ch;

                });
            }

            _gel('contactInformationHeader').innerHTML = 'contact info';
            break;
        }

        if(!contactLookup) {
            _gel('contactInformation').innerHTML = ch;
        }
        _gel('additionalInformation').innerHTML = ah;

        var vh = '';
        if(!checkUndef(profile.video_tour)) {
            vh += '<p><a href="#" onclick="geoplayer.showVideo(' + profile.video_tour[0].cid + ');return false;">' + profile.video_tour[0].value + '</a></p>';
        }
        if(!checkUndef(profile.community_tour)) {
            vh += '<p><a href="#" onclick="geoplayer.showVideo(' + profile.community_tour[0].cid + ',\'' + escape(profile.community_tour[0].value) + '\');return false;">' + profile.community_tour[0].value + '</a></p>';
        }
        if(!checkUndef(profile.city_tour)) {
            vh += '<p><a href="#" onclick="geoplayer.showVideo(' + profile.city_tour[0].cid + ',\'' + escape(profile.city_tour[0].value) + '\');return false;">' + profile.city_tour[0].value + '</a></p>';
        }
        if(!checkUndef(profile.regional_tour)) {
            vh += '<p><a href="#" onclick="geoplayer.showVideo(' + profile.regional_tour[0].cid + ',\'' + escape(profile.regional_tour[0].value) + '\');return false;">' + profile.regional_tour[0].value + '</a></p>';
        }
        if(!checkUndef(profile.related_tours)) {
            vh += '<div style="height:25px;"></div>';
            vh += '<div class="profileHeader" id="relatedTourHeader">related tours</div>';
            for(i=0;i<profile.related_tours.length;i++) {
                vh += '<p><a href="#" onclick="geoplayer.showVideo(' + profile.related_tours[i].cid + ',\'' + escape(profile.related_tours[i].value) + '\');return false;">' + profile.related_tours[i].value + '</a></p>';
            }
        }
        _gel('videoLinks').innerHTML = vh;


        if(!checkUndef(profile.related_profiles)) {
            for(var i=0;i<profile.related_profiles.length;i++) {
                if(!checkUndef(profile.related_profiles[i].pid) && buildAll) {
                    this.profileList.push({pid:profile.related_profiles[i].pid,name:profile.related_profiles[i].value});
                }
            }
        }

        for(i in profile) if(profile.hasOwnProperty(i)) {
            if(!this.in_array(i,shown) && !checkUndef(profile[i][0])) {
                if(!checkUndef(profile[i][0].value)) {
                    if(profile[i][0].value.length > 0) {
                        oh += this.buildProfileItem(i.replace(/_/g,' '),profile[i].objJoin('; ','value'));
                    }
                }
            }
        }

        try {
            _gel('profileInfo').innerHTML = oh;
        } catch(e) {
            e = null;
        }

        var pl = '';
        for(i=0;i<this.profileList.length;i++) {
            if(this.profileList[i].pid == this.profile.pid) {
                pl += '<p>' + this.profileList[i].name + '</p>';
            } else {
                pl += '<p><a href="#" onclick="geoplayer.showProfile(' + this.profileList[i].pid + ',false);return false;">' + this.profileList[i].name + '</a></p>';
            }
        }
        _gel('profileLinks').innerHTML = pl;


        this.activate('profile');
        if(buildVideo) {
            this.showVideo(profile.video_tour[0].cid);
        }
        initLytebox();
        _gel('profile').scrollTop = 0;
    },

    buildProfileItem: function(header,text) {
        var oh = '';
        oh += '<div class="profileHeader">' + header + '</div>';
        oh += '<div class="profileText">' + text + '</div>';
        oh += '<div style="height:8px;"></div>';
        return oh;
    },

    buildSearchForm: function(type) {
        var lookup,outputDiv;
        switch(type) {
            case 'property':
            this.activateSearchTab('Property');
            outputDiv = 'Property';
            lookup = 231;
            break;

            case 'school':
            this.activateSearchTab('School');
            outputDiv = 'School';
            lookup = 478;
            break;

            case 'business':
            this.activateSearchTab('Business');
            outputDiv = 'Business';
            lookup = 476;
            break;

            case 'community':
            this.activateSearchTab('Community');
            outputDiv = 'Community';
            lookup = 475;
            break;

            default:
            this.activateSearchTab('Community');
            outputDiv = 'Community';
            lookup = 475;
            break;
        }
        if(_gel('search' + outputDiv).innerHTML.length <= 9) {
            google.maps.DownloadUrl('text_only.php?ID=' + lookup,function(data,responseCode) {
                geoplayer.form = true;
                _gel('search' + outputDiv).innerHTML = data;
            });
        }

    },

    buildSearchResults: function() {
        var i;
        var oh = '';
        if(this.markers.length > 0) {
            this.markerAttribSort(this.markers,'name','A');
            oh += '<ul>' + this.nl;
            for(i=0;i<this.markers.length;i++) {
                oh += '  <li class="alt' + (i%2) + ((this.profile.pid == this.markers[i].attributes.pid)?(' selected'):('')) +'">' + this.nl;
                oh += '    <div class="listResult">' + this.nl;
                oh += '    <a href="#" onclick="geoplayer.activate(\'map\');google.maps.Event.trigger(geoplayer.markers['+i+'],\'click\');return false;" class="listResultMarker"><img src="' + this.iconSets[this.markers[i].attributes.category.toLowerCase()].image + '" alt="' + this.markers[i].attributes.category + '" align="left" /></a>' + this.nl;
                if(!checkUndef(this.markers[i].attributes.photo)) {
                    oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.markers[i].attributes.pid + ');return false;" class="listResultImage">' + this.nl;
                    oh += '      <img src="' + this.markers[i].attributes.photo + '" title="' + this.markers[i].attributes.photo_alt + '" alt="' + this.markers[i].attributes.photo_alt + '" class="thumbnail" />' + this.nl;
                    oh += '    </a>' + this.nl;
                } else {
                    oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.markers[i].attributes.pid + ');return false;" class="listResultImage">' + this.nl;
                    oh += '      <img src="img/spacer.gif" title="" alt="" class="thumbnail" />' + this.nl;
                    oh += '    </a>' + this.nl;
                }
                oh += '    <a href="#" onclick="geoplayer.showProfile(' + this.markers[i].attributes.pid + ');return false;" class="listResultContent">' + this.nl;
                oh += '      <p class="resultsHeader">' + this.markers[i].attributes.name + '</p>' + this.nl;
                oh += '      <p>' + this.smartText(this.markers[i].description,200) + '</p>' + this.nl;
                oh += '    </a>' + this.nl;
                oh += '    </div>' + this.nl;
                oh += '  </li>' + this.nl;
            }
            oh += '</ul>';
        } else {
            oh += '<p style="padding:5px;"><strong>There are no profiles that meet all of your criteria. Please change your criteria and search again!</strong></p>';
        }

        _gel('searchResults').innerHTML = oh;
    },

    download: function(url,callback) {
        google.maps.DownloadUrl('profile.php?ProfileID=' + x,callback);
    },

    findFavorite: function(x) {
        var i;
        for(i=0;i<this.favorites.length;i++) {
            if(this.favorites[i].pid == x) {
                return i;
            }
        }
        return false;
    },

    getActiveSearchTab: function() {
        for(var i=0;i<this.searchTabs.length;i++) {
            if(_gel('sTab'+this.searchTabs[i]).className == 'selected') {
                return this.searchTabs[i];
            }
        }
    },

    getCounty: function(place) {
        var accuracy = place.AddressDetails.Accuracy;
        if(accuracy >= 3) {
            return place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
        } else {
            return null;
        }
    },

    getCity: function(place) {
        var accuracy = place.AddressDetails.Accuracy;
        if(accuracy >= 4) {
            if(checkUndef(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea)) {
                return place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
            }
            return place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
        } else {
            return null;
        }
    },

    getHeight: function() {
        var myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
        }
        return myHeight;
    },

    getState: function(place) {
        var accuracy = place.AddressDetails.Accuracy;
        if(accuracy >= 2) {
            return place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
        } else {
            return null;
        }
    },

    getWidth: function() {
        var myWidth = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
        }
        return myWidth;

    },

    getZip: function(place) {
        var accuracy = place.AddressDetails.Accuracy;
        if(accuracy >= 5) {
            return place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
        } else {
            return null;
        }
    },

    hideGallery: function() {
        this.activateProfileTab('ProfileOuter');
    },

    hideProfileTabs: function() {
        for(var i=0;i<this.profileTabs.length;i++) {
            if(_gel('pTab'+this.profileTabs[i].name) !== '') {
                _gel('pTab'+this.profileTabs[i].name).className = '';
            }
            if(this.profileTabs[i].clear) {
                _gel(this.profileTabs[i].name).innerHTML = '';
            }
            _gel(this.profileTabs[i].name).style.display='none';
        }
    },

    hideFavoriteTabs: function() {
        for(var i=0;i<this.favoriteTabs.length;i++) {
            if(_gel('fTab'+this.favoriteTabs[i]) != null) {
                _gel('fTab'+this.favoriteTabs[i]).className = '';
            }
        }
    },

    hideSearchTabs: function() {
        for(var i=0;i<this.searchTabs.length;i++) {
            if(_gel('sTab'+this.searchTabs[i]) != null && _gel('search'+this.searchTabs[i]) != null) {
                _gel('sTab'+this.searchTabs[i]).className = '';
                _gel('search'+this.searchTabs[i]).style.display = 'none';
            }
        }
    },

    hideTabs: function() {
        for(var i=0;i<this.tabs.length;i++) {
            _gel(this.tabs[i]+'Tab').className = '';
            _gel(this.tabs[i]+'Nav').style.display='none';
            _gel(this.tabs[i]).style.display='none';
        }
    },

    hideVideo: function() {
        this.activateProfileTab('ProfileOuter');
    },

    in_array: function(myValue,myArray) {
        for (var i in myArray) {
            if (myArray[i] === myValue) {
                return true;
            }
        }
        return false;
    },

    LoadingMessageControl: function() {
    },

    lookupProfile: function(x,callBack) {
        google.maps.DownloadUrl('profile.php?ProfileID=' + x,callBack);
    },

    MapControl: function() {
    },

    markerAttribSort: function(array,property,order) {
        array.sort(function(a,b) {
            if(order == 'D') {
                var temp = a;
                a = b;
                b = temp;
            }
            if(typeof a.attributes[property] != 'string') {
                return a.attributes[property] - b.attributes[property];
            } else {
                x = 0;
                if (a.attributes[property] < b.attributes[property]) {
                    x=-1;
                }  else {
                    x=1;
                }
                return x;
            }
        });
    },

    removeFavorite: function(num) {
        if(this.findFavorite(num) !== false) {
            this.favorites.splice(this.findFavorite(num),1);
            this.prefs.data.favorites = this.favorites;
            this.prefs.save();
        }
    },

    searchMDM: function() {
        var request;
        if(_gel('quickSearchBox').value === '' || _gel('quickSearchBox').value == 'Enter search text & GO') {
            if(_gel('quickSearchList').value == 'BUSINESS') {
                this.activate('search');
                this.buildSearchForm('business');
            } else {
                if(_gel('quickSearchList').value == '-1') {
                    _gel('quickSearchList').value = 'CITY';
                }
                request = 'search.php?';
                request += 'Category=' + _gel('quickSearchList').value;
                if(_gel('quickSearchList').value == 'PROPERTY') {
                    request += '&Agent=' + pid;
                    this.buildMarkers(request,false,false,true);
                } else {
                    this.buildMarkers(request);
                }
            }
            _gel('quickSearchBox').value = 'Enter search text & GO';
        } else {
            request = 'search.php?';
            request += 'Name=' + _gel('quickSearchBox').value;
            this.buildMarkers(request);
            _gel('quickSearchBox').value = 'Enter search text & GO';
        }
    },

    showGallery: function(x,header) {
        if(checkUndef(x)) {
            x = this.profile.pid;
        }
        if(checkUndef(header)) {
            switch(this.profile.category.toLowerCase()) {
                case 'city':
                header = this.profile.name + ', ' + this.profile.county[0].value + ' County, ' + this.profile.state[0].value;
                break;

                case 'community':
                case 'school':
                header = this.profile.name + ', ' + this.profile.city[0].value + ', ' + this.profile.state[0].value;
                break;

                default:
                header = this.profile.name;
                break;
            }
        }

        header = decodeURI(header);

        var so = new SWFObject('flash/gallery.swf','gallery_swf',480,343,8,'#ffffff');
        so.addParam('quality','autohigh');
        so.addParam('base','flash');
        so.addParam('allowScriptAccess','always');
        so.addVariable('pid',x);
        var gh = '';
        gh += '<div style="padding:10px;"></div>';
        gh += '<div style="width:100%;text-align:center;">';
        gh += '<div class="profileHeader">' + header + '</div>';
        gh += so.getSWFHTML();
        gh += '</div>';
        this.activateProfileTab('Gallery');
        _gel('Gallery').innerHTML = gh;
    },

    showProfile: function(x) {
        this.hideVideo();
        var buildAll = (checkUndef(arguments[1]))?true:arguments[1];
        var buildVideo = (checkUndef(arguments[2]))?false:arguments[2];
        if(buildAll) {
            this.profileList = [];
        }
        this.buildHash('showProfile',x,true,buildVideo);
        this.lookupProfile(x,function(data,responseCode) {
            var i,j;
            var xmlDoc = google.maps.Xml.parse(data);
            var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
            geoplayer.profile = {};
            for(i = 0;i<profileDoc.attributes.length;i++) {
                geoplayer.profile[profileDoc.attributes[i].nodeName] = profileDoc.attributes[i].nodeValue ;
            }
            var profileNodes = profileDoc.childNodes;
            for (i = 0; i < profileNodes.length; i++) {
                if(profileNodes[i].nodeName != '#text') {
                    if(checkUndef(geoplayer.profile[profileNodes[i].nodeName])) {
                        geoplayer.profile[profileNodes[i].nodeName] = [];
                    }
                    var obj = {};
                    obj.value = profileNodes[i].firstChild.nodeValue;
                    if(profileNodes[i].attributes.length > 0) {
                        for(j=0;j<profileNodes[i].attributes.length;j++) {
                            obj[profileNodes[i].attributes[j].nodeName] = profileNodes[i].attributes[j].nodeValue;
                        }
                    }
                    geoplayer.profile[profileNodes[i].nodeName].push(obj);
                }
            }
            geoplayer.buildProfile(buildAll,buildVideo);
        });
    },

    showVideo: function(cid,header) {
        if(checkUndef(header)) {
            switch(this.profile.category.toLowerCase()) {
                case 'city':
                header = this.profile.name + ', ' + this.profile.county[0].value + ' County, ' + this.profile.state[0].value;
                break;

                case 'community':
                case 'school':
                header = this.profile.name + ', ' + this.profile.city[0].value + ', ' + this.profile.state[0].value;
                break;

                default:
                header = this.profile.name;
                break;
            }

        }
        var vh = '';
        vh += '<div style="padding:10px;"></div>';
        if(!checkUndef(cid)) {
            header = decodeURI(header);

            vh += '<div style="width:100%;text-align:center;">';
            vh += '<div class="profileHeader">' + header + '</div>';
            if(this.profile.category.toLowerCase() == 'property') {
                vh += '<iframe frameborder="0" id="content_iframe" width="480" height="420" allowtransparency="true" scrolling="no" vspace="0" hspace="0" marginwidth="0" marginheight="0" src="http://www.dvls.tv/content_distribution/products/singleplayer_embed.php?uid=' + this._uid + '&token=' + this._token + '&contentid=' + cid + '&videourl=../open/' + cid + '.flv" ></iframe>';
            } else {
                vh += '<iframe frameborder="0" id="content_iframe" width="480" height="420" allowtransparency="true" scrolling="no" vspace="0" hspace="0" marginwidth="0" marginheight="0" src="http://www.dvls.tv/content_distribution/products/singleplayer_embed.php?uid=' + this._uid + '&token=' + this._token + '&contentid=' + cid + '" ></iframe>';
            }
            vh += '</div>';
        } else {
            vh += '<div style="width:100%;text-align:center;"><img src="img/no_video_available.gif" alt="No Video Available" /></div>';
        }
        this.activateProfileTab('Video');
        _gel('Video').innerHTML = vh;
    },

    showTooltip: function(marker) {
        var tooltipWidth = 225;
        this.tooltip.innerHTML = marker.tooltip;
        this.tooltip.style.width= tooltipWidth + 'px';
        var point = this.map.fromLatLngToDivPixel(marker.getPoint());
        var anchor=marker.getIcon().iconAnchor;
        var width=marker.getIcon().iconSize.width;
        var height=this.tooltip.clientHeight;
        var xPoint = (point.x - anchor.x + (width/2)) - (tooltipWidth/2) ;
        var yPoint = point.y - anchor.y - (height+5);
        var pos = new google.maps.ControlPosition(google.maps._ANCHOR_TOP_LEFT, new google.maps.Size(xPoint,yPoint));
        pos.apply(this.tooltip);
        this.tooltip.style.visibility="visible";
    },

    smartText: function(text,length) {
        text = unescape(text);
        if(length > text.length) {
            return text;
        } else {
            return text.substr(0,text.lastIndexOf(' ',length)) + '...';
        }
    },

    setupRequest: function() {
        var xmlHttp;
        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
            // Internet Explorer
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                try
                {
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e)
                {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }
        return xmlHttp;
    },

    xmlRequest: function(url,callback) {
        var requestObject = {timestamp:new Date().getTime(),request:this.setupRequest()};
        this.ajaxRequests.push(requestObject);
        this.ajaxRequests[this.ajaxRequests.length-1].request.onreadystatechange=function() {
            if(geoplayer.ajaxRequests[geoplayer.ajaxRequests.length-1].request.readyState==4) {
                callback(geoplayer.ajaxRequests[geoplayer.ajaxRequests.length-1]);
            }
        };
        this.ajaxRequests[this.ajaxRequests.length-1].request.open('GET',url,true);
        this.ajaxRequests[this.ajaxRequests.length-1].request.send(null);
    }
};

function _gel(x) {
    return document.getElementById(x);
}


function checkUndef(object) {
    return (typeof object == 'undefined');
}

Array.prototype.objJoin = function(seperator,element) {
    var returnString = '';
    for(var i = 0;i<this.length;i++) {
        returnString += this[i][element] + seperator;
    }
    return returnString.substring(0,returnString.length-seperator.length);
};

window.onresize = function() {
    _gel('container').style.height = (geoplayer.getHeight()-15) + 'px';
    _gel('contentContainer').style.height = (geoplayer.getHeight()-134) + 'px';
};

window.onload = function() {
    // Load favorites from cookie if set.
    geoplayer.prefs.load();
    if(!checkUndef(geoplayer.prefs.data.favorites)) {
        geoplayer.favorites = geoplayer.prefs.data.favorites;
    }
    geoplayer.MapControl.prototype = new google.maps.Control();
    geoplayer.MapControl.prototype.initialize = function(map) {
        var container = document.createElement("div");
        var oh = '';
        oh += '<a href="#" onclick="geoplayer.map.panDirection(0,1);return false;"><img src="img/up_arrow.gif" style="padding-left:10px;padding-bottom:4px;border:0;" /></a><br />';
        oh += '<a href="#" onclick="geoplayer.map.panDirection(1,0);return false;"><img src="img/left_arrow.gif" style="float:left;border:0;" /></a>';
        oh += '<a href="#" onclick="geoplayer.map.panDirection(-1,0);return false;"><img src="img/right_arrow.gif" style="padding-left:6px;padding-bottom:4px;border:0;" /></a><br />';
        oh += '<a href="#" onclick="geoplayer.map.panDirection(0,-1);return false;"><img src="img/down_arrow.gif" style="padding-left:10px;padding-bottom:4px;border:0;" /></a><br />';
        oh += '<a href="#" onclick="geoplayer.map.zoomIn();return false;"><img src="img/plus_icon.gif" style="padding-left:10px;padding-bottom:4px;border:0;" /></a><br />';
        oh += '<a href="#" onclick="geoplayer.map.zoomOut();return false;"><img src="img/minus_icon.gif" style="padding-left:10px;padding-bottom:4px;border:0;" /></a><br />';
        container.innerHTML = oh;
        map.getContainer().appendChild(container);
        return container;
    };
    geoplayer.MapControl.prototype.getDefaultPosition = function() {
        return new google.maps.ControlPosition(google.maps._ANCHOR_TOP_LEFT, new google.maps.Size(7, 7));
    };
    geoplayer.MapControl.prototype.setButtonStyle_ = function(button) {
        button.style.backgroundColor = "white";
        button.style.border = "1px solid black";
        button.style.padding = "2px";
        button.style.marginBottom = "3px";
        button.style.textAlign = "center";
        button.style.width = "6em";
    };
    geoplayer.customControls=new geoplayer.MapControl();

    geoplayer.LoadingMessageControl.prototype = new google.maps.Control();
    geoplayer.LoadingMessageControl.prototype.initialize = function(map) {
        var container = document.createElement("div");
        container.innerHTML = '<img src="img/hourglass_icon.gif" alt="Loading..." title="Loading..." />';
        map.getContainer().appendChild(container);
        return container;
    };
    geoplayer.LoadingMessageControl.prototype.getDefaultPosition = function() {
        return new google.maps.ControlPosition(google.maps._ANCHOR_TOP_LEFT, new google.maps.Size(60, 7));
    };
    geoplayer.loadingMessage=new geoplayer.LoadingMessageControl();

    window.onresize();

    var map_container = _gel('map');
    if (google.maps.BrowserIsCompatible()) {
        var query = window.location.search.substring(1);
        if(query.length > 0) {
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                geoplayer._request[pair[0]] = pair[1].replace(/%20/g,' ');
            }
        }
        if(!checkUndef(geoplayer._request.id)) {
            geoplayer._id = geoplayer._request.id;
        }
        if(!checkUndef(geoplayer._request.showProperties)) {
            if(geoplayer._request.showProperties == 'true') {
                geoplayer.showProperties = true;
            }
        }

        // Map init
        geoplayer.map = new google.maps.Map2(map_container);
        if(!checkUndef(geoplayer._request.large)) {
            geoplayer.map.addControl(new google.maps.LargeMapControl());
            geoplayer.map.addControl(new google.maps.MapTypeControl());
        } else {
            geoplayer.map.addControl(geoplayer.customControls);
        }

        geoplayer.map.setCenter(new google.maps.LatLng(0,0),7);
        geoplayer.map.enableScrollWheelZoom();
        geoplayer.map.enableContinuousZoom();

        // Event init
        google.maps.Event.addListener(geoplayer.map, "click", function(overlay, point) {
            if (overlay) {
                if(overlay.html) {
                    overlay.openInfoWindowHtml(overlay.html);
                    geoplayer.tooltip.style.visibility='hidden';
                }
            }
        });

        //ads = new GAdsManager(map,'9450658691831691',{channel:'Geoplayer'});
        //ads.enable();

        var iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_community.png';
        geoplayer.iconSets.community = iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_property.png';
        geoplayer.iconSets.property = iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_school.png';
        geoplayer.iconSets.school = iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_city.png';
        geoplayer.iconSets.city = iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_business.png';
        geoplayer.iconSets.business = iconObj;

        iconObj = new google.maps.Icon();
        iconObj.iconSize = new google.maps.Size(28, 40);
        iconObj.iconAnchor = new google.maps.Point(12, 39);
        iconObj.infoWindowAnchor = new google.maps.Point(25, 16);
        iconObj.image = 'img/marker_business.png';
        geoplayer.iconSets.contact = iconObj;
        // We don't need these for now.
        /*
        var iconObj;
        iconObj = new google.maps.Icon();
        iconObj.image = 'img/community_icon.gif';
        iconObj.iconSize = new google.maps.Size(25, 25);
        iconObj.iconAnchor = new google.maps.Point(12, 13);
        iconObj.infoWindowAnchor = new google.maps.Point(12, 1);
        */

        // Tooltip init.
        geoplayer.tooltip = document.createElement('div');
        geoplayer.map.getPane(G_MAP_MARKER_PANE).appendChild(geoplayer.tooltip);
        geoplayer.tooltip.style.visibility='hidden';

        // Contact div init.
        geoplayer.lookupProfile(geoplayer._id, function(data, responseCode) {
            var xmlDoc = google.maps.Xml.parse(data);
            var profileDoc = xmlDoc.documentElement.getElementsByTagName("profile")[0];
            var agent = {};
            agent.name = profileDoc.getAttribute('name');
            agent.pid = profileDoc.getAttribute('pid');
            var profileNodes = profileDoc.childNodes;
            for (var i = 0; i < profileNodes.length; i++) {
                if(profileNodes[i].nodeName != '#text') {
                    if(checkUndef(agent[profileNodes[i].nodeName])) {
                        agent[profileNodes[i].nodeName] = [];
                    }
                    var obj = {};
                    obj.value = profileNodes[i].firstChild.nodeValue;
                    if(profileNodes[i].attributes.length > 0) {
                        obj.pid = profileNodes[i].getAttribute('pid');
                    }
                     agent[profileNodes[i].nodeName].push(obj);
                }
            }
            geoplayer.agent = agent;
            var oh = '';
            if(!checkUndef(agent.photos)) {
                oh += '<img src="' + agent.photos[0].value + '" alt="' + agent.name + '" width="180" height="120" style="float:left;" />';
            }
            oh += '&nbsp;&nbsp;&nbsp;Courtesy of<br />';
            if(!checkUndef(agent.business)) {
                oh += '&nbsp;&nbsp;&nbsp;' + agent.name + ', ' + agent.business[0].value + '<br />';
            } else {
                oh += '&nbsp;&nbsp;&nbsp;' + agent.name + '<br />';
            }
            oh += '&nbsp;&nbsp;&nbsp;' + agent.street1[0].value + ', ' + agent.city[0].value + ', ' + agent.state[0].value + ' ' + agent.zip[0].value + '<br />';

            if(!checkUndef(agent.phone)) {
                oh += '&nbsp;&nbsp;&nbsp;<span class="contactHighlight">phone: </span>' + agent.phone[0].value + '<br />';
            }
            if(!checkUndef(agent.email)) {
                oh += '&nbsp;&nbsp;&nbsp;<span class="contactHighlight">email: </span><a href="mailto:' + agent.email[0].value + '">' + agent.email[0].value + '</a><br />';
            }
            if(!checkUndef(agent.website)) {
                oh += '&nbsp;&nbsp;&nbsp;<span class="contactHighlight">website: </span>' + agent.website[0].value + '<br />';
            }
            _gel('contactInfo').innerHTML = oh;
            geoplayer.buildBusiness();

        });

        // Geocoder Init
        geoplayer.geocoder = new google.maps.ClientGeocoder();
        geoplayer.searchMDM();
    }
};

window.onunload = function() {
    google.maps.Unload();
};

String.prototype.strip_tags = function() {
    $text = this;
    return $text.replace(/<\/?[^>]+>/gi, '');
}

function updateDisplay(x,y,div) {
    if(x === '') {
        _gel(div).style.display = 'none';
        _gel(div).innerHTML = '';
    } else {
        var url = 'keys.xml.php?DID=' + y + '&PID=' + x;
        google.maps.DownloadUrl(url, function(msg, responseCode) {
            var xmlobject;
            if (document.implementation.createDocument){
                // Mozilla, create a new DOMParser
                var parser = new DOMParser();
                xmlobject = parser.parseFromString(msg, "text/xml");
            } else if (window.ActiveXObject){
                // Internet Explorer, create a new XML document using ActiveX
                // and use loadXML as a DOM parser.
                xmlobject = new ActiveXObject("Microsoft.XMLDOM");
                xmlobject.async="false";
                xmlobject.loadXML(msg);
            }
            xmlobject = xmlobject.documentElement;
            var items = xmlobject.getElementsByTagName('item');
            var oh = '';
            oh += '<table width="100%">';
            for(var i=0;i<items.length;i++) {
                if(items[i].firstChild !== null) {
                    oh += '<tr>';
                    oh += '  <td width="35%" style="text-align:right;vertical-align:top;color:#">' + items[i].getAttribute('name') + ' .:</td>';
                    oh += '  <td>' + items[i].firstChild.nodeValue + '</td>';
                    oh += '</tr>';
                }
            }
            oh += '</table>';
            _gel(div).innerHTML = oh;
            _gel(div).style.display = 'block';
        });
    }
}

