
    var photoMarker = null;
    var mediaLng = 0;
    var mediaLat = 0;
    var icon = null;
    var mouseOverMediaArea = false;
    var slideshowArea;
    var resultCount = 0;
    var entityType;
    var slideshow;
    var filmstrip;
    var filmstripSize = 5;

function handleKeyPress(event) {
    if (! window.slideshow)
        return;
    var keyCode = getKeyCode(event);
    if (keyCode == 37)
        slideshow.movePrevious();
    else if (keyCode == 39)
        slideshow.moveNext();
    else
        return true;

    cancelEvents(event);
    return false;
}

    function init() {
        document.onkeydown = handleKeyPress;

        // load slideshow
        slideshowArea = document.getElementById("slideshowArea");
        if (slideshowArea) {
            var query = "entityType=2&sort=rating&";
            entityType = document.form.entityType.value;
            if (entityType == '6')
                query += "location=" + document.form.location.value;
            else if (entityType == '7')
                query += "tripSection=" + document.form.item.value;
            else if (entityType == '8')
                query += "listing=" + document.form.item.value;
            slideshow = new Slideshow(slideshowArea, query, false, 2);
        }

        // if uses maps, start the main one
        if (window.initMap)
            initMap();
        
        // load news
        processRss();
    }

    // separated out, so occurs after load
    function processRss() {
        var rssNewsUrl = document.form.rssNewsUrl;
        if (rssNewsUrl && rssNewsUrl.value)
            readRSS(rssNewsUrl.value);
    }

    addOnload(init);

    function renderRss() {
        var newsRow = document.getElementById("newsRow");
        for (var j=0; j < rssLines.length; j++) {
            var line = rssLines[j];
            var row = newsRow.cloneNode(true);
            newsRow.parentNode.appendChild(row);
            row.style.display = "";
            row.firstChild.firstChild.href = line.link;
            row.firstChild.firstChild.firstChild.data = line.title;
        }
    }

    /* for Media mainView */
    function renderVideoPlayer() {
        var va = document.getElementById("videoArea");
        if (va) {
            var html = va.innerHTML;
            if (html.match(/^<!--/)) {
                html = html.replace(/^<!--/,'').replace(/-->$/,'');
                if (html.indexOf("videoegg.com") >= 0) {
                    var params = html.split(",");
                    var w = params[0];
                    var h = params[1];
                    var path = params[2];
                    html = getPlayer(path, w, h);
                } else if (html.indexOf("<") < 0) {
                    html = "<embed src='" + html + "' width='360' height='260'></embed>";
                }
                va.innerHTML = html;
            } 
        }
    }

    function getPlayer(path,w,h) {
        var VE_api = VE_getPlayerAPI('1.2');
        return VE_api.getPlayerHTML(path, w, h, false, '/ui/media/video-logo.png', '', false,'','');
    }

    function moveFilmstripLeft() {
        updateFilmstrip = true;
        slideshow.index = slideshow.index - filmstripSize + 1;
        slideshow.movePrevious();
    }

    function moveFilmstripRight() {
        updateFilmstrip = true;
        slideshow.index = slideshow.index + filmstripSize - 1;
        slideshow.moveNext();
    }

    var leftFilmstripArea, rightFilmstripArea;

    function initFilmstrip() {
        leftFilmstripArea = document.createElement("TD");
        leftFilmstripArea.innerHTML = "<a href='#' onclick='moveFilmstripLeft();return false'><img src='/ui/media/filmstrip-l.png'></a>";
        rightFilmstripArea = document.createElement("TD");
        rightFilmstripArea.innerHTML = "<a href='#' onclick='moveFilmstripRight();return false'><img src='/ui/media/filmstrip-r.png'></a>";
        filmstrip = document.createElement("TD");
        var filmstripArea = document.getElementById("filmstripArea");

        var bodyArea = document.createElement("TBODY");
        filmstripArea.appendChild(bodyArea);
        var rowArea = document.createElement("TR");
        bodyArea.appendChild(rowArea);

        rowArea.appendChild(leftFilmstripArea);
        rowArea.appendChild(filmstrip);
        rowArea.appendChild(rightFilmstripArea);

        filmstrip.style.backgroundColor = "#000000";
        filmstrip.style.padding = "0px 3px";
    }

    function slideshowEmpty() {
        var filmstripArea = document.getElementById("filmstripArea");
        filmstripArea.parentNode.style.display = "none";
    }

    function renderFilmstrip(results, index, startIndex, resultCount) {
        updateFilmstrip = false;

        var fsStartIndex = startIndex;
        if (index >= startIndex + filmstripSize)
            fsStartIndex += filmstripSize;

        if (index == 0)
            leftFilmstripArea.style.visibility = "hidden";
        else
            leftFilmstripArea.style.visibility = "visible";
        if (fsStartIndex >= resultCount - filmstripSize)
            rightFilmstripArea.style.visibility = "hidden";
        else
            rightFilmstripArea.style.visibility = "visible";

        var n = filmstripSize;
        if (resultCount - fsStartIndex < n)
            n = resultCount - fsStartIndex;
        //var x = findPosX(slideshowArea) + 130;
        //x = x - (n * 104) / 2;
        //filmstrip.style.left = x + "px";
        filmstrip.style.width = 6 + (104 * n) + "px";

        var html = "<div class='filmstripTop'><img src='/ui/media/empty.gif' border='0' height='1' width='1'></div><div>";
        var pos;
        for (var j=0; j < n; j++) {
            if (index-startIndex < filmstripSize)
                pos = j;
            else
                pos = j + filmstripSize;
                    
            var result = results[pos];
            var media = result.getElementsByTagName("media");
            if (media.length > 0) {
                media = media[0];
                html += "<div style='width:100px; float:left; text-align:center; border:solid 2px #000000;'";
                var src = media.getAttribute("path-small");
                var h = parseInt(media.getAttribute("height"));

                h = 75 * h / 188;
                if (h > 75) h = 75;
                html += "><a href='#' onclick='selectSlide(" + pos + ");return false;'><img height='" + h + "' src='" + src + "' border='0'></a>";
                html += "</div>";
            }
        }
        html += "</div><div style='clear:both' class='filmstripBottom'><img src='/ui/media/empty.gif' border='0' height='1' width='1'></div>";
        filmstrip.innerHTML = html;
    }

    function selectSlide(pos) {
        slideshow.selectSlide(pos);
    }

    function showMediaMarker(show) {
        mouseOverMediaArea = show;

        if (map && entityType =='6') {
            if (show) {
                var newMediaLng = document.form.lng.value;
                var newMediaLat = document.form.lat.value;
                var redraw = true;
                if (photoMarker) {
                    if (newMediaLng == mediaLng && newMediaLat == mediaLat)
                        redraw = false;
                    else
                        map.removeOverlay(photoMarker) 
                }
                if (redraw && map.isLoaded()) {
                    mediaLng = newMediaLng;
                    mediaLat = newMediaLat;
                    if (icon == null) {
                        icon = new GIcon();
                        icon.iconSize = new GSize(23, 23);
                        icon.iconAnchor = new GPoint(11, 11);
                        icon.image = '/ui/media/geo-media.png';
                    }
                    photoMarker = new GMarker(new GPoint(mediaLng,mediaLat), icon);
                    map.addOverlay(photoMarker);
                }
            } else {
                if (photoMarker) {
                    map.removeOverlay(photoMarker) 
                    photoMarker = null;
                }
            }
        }
    }

    function addMediaSet(mediaSet) {
        var key = window.location.pathname.replace(/\.html/, "");
        key = key.substring(1);
        var type = "journal";
        if (window.location.href.indexOf("Travel-Guide") > 0)
            type = "guide";
        else if (window.location.href.indexOf("Listing") > 0)
            type = "activity";
        var params = window.location.search;
        addMediaParams = 'event=addMedia&type='+type+'&media=' + mediaSet + "&key=" + encode(key);
        setTimeout(doAddMediaSet, 10);
    }

    function doAddMediaSet() {
        if (addMediaParams)
            sendRequest("/XmlService", addMediaParams)
        addMediaParams = null;
    }

    var addMediaParams = null;

    function addMediaHandler(result) {
        if (result.firstChild.data == "OK") {
            // reload!
            window.location = window.location;
        } else {
            valert(result.firstChild.data);
        }
    }

    /* Offensive handler functions */
    function markOffensive() {
        if (!confirm("Do you want to flag this as offensive?"))
            return; 
        var item = document.form.item.value;
        var url = '/XmlService?event=markOffensive&item=' + item;
        sendRequest(url)
    }
    function markOffensiveHandler() {
        document.getElementById("offensiveFlagged").style.display="";
        document.getElementById("offensiveFlag").style.display="none";
        valert("You have marked this page offensive.  If your Account Settings hide offensive material, this page will not be shown in the future.");
    }

    /* Edit Review */
    function editReview(id) {
        var url = window.location.href;
        url += "?event=displayReview";
        url += "&review=" + id;
        window.location = url;
    }
    /* Delete Review */
    function deleteReview(id) {
        if (confirm("Are you sure you want to delete your review?")) {
            var url = window.location.href;
            url += "?event=deleteReview";
            url += "&review=" + id;
            window.location = url;
        }
    }
    
    function mediaImageMouseover() {
        var expandIcon = document.getElementById("expandIcon");
        var image = document.getElementById("image");
        expandIcon.style.left = (findPosX(image) + image.width / 2 - 10) + "px";
        expandIcon.style.top = (findPosY(image) + image.height / 2 - 5) + "px";
        expandIcon.style.display = "";
    }

    function mediaImageMouseout() {
        var expandIcon = document.getElementById("expandIcon");
        expandIcon.style.display = "none";
    }

    function expandMediaImage() {
        var image = document.getElementById("image");
        var largeImage = document.getElementById("largeImageArea");
        largeImage.style.left = findPosX(image) + "px";
        largeImage.style.top = findPosY(image) + "px";
        largeImage.style.zIndex = "10";
        largeImage.style.display = "";

        var largeImageIcons = document.getElementById("largeImageIcons");
        largeImageIcons.style.left = (findPosX(largeImage) + largeImage.offsetWidth - 115) + "px";
        largeImageIcons.style.top = (findPosY(largeImage) + 10) + "px";
        largeImageIcons.style.display = "";

        document.getElementById("expandIcon").style.display = "none";
        document.getElementById("mainArea").style.display = "none";

        var height = 300 + largeImage.offsetHeight;
        document.getElementById("marginLeft").height = height;
        document.getElementById("marginRight").height = height;
    }

    function collapseMediaImage() {
        document.getElementById("image").style.display = "";
        document.getElementById("largeImageArea").style.display = "none";
        document.getElementById("largeImageIcons").style.display = "none";
        document.getElementById("mainArea").style.display = "";
    }

    // Add City from country, visit it
    function visitWhenLocationCreated() { return true; }

    var filmstrip;
    var updateFilmstrip = true;

    function slideshowStarted() {
        initFilmstrip();
    }

    function slideshowFrameRendering(slideshow, result) {
        if (updateFilmstrip)
            renderFilmstrip(slideshow.results, slideshow.index, slideshow.startIndex, slideshow.resultCount);
        document.form.lat.value = result.getAttribute("lat");
        document.form.lng.value = result.getAttribute("lng");
    }

    function slideshowFrameRendered(slideshow) {
        var fs = filmstrip.childNodes[1];
        var n = slideshow.index - slideshow.startIndex;
        n = n % filmstripSize;
        fs.childNodes[n].style.borderColor = "#acde4a";
        if (n > 0)
            fs.childNodes[n-1].style.borderColor = "#000000";
        if (n < 4 && slideshow.index+1 < slideshow.resultCount)
            fs.childNodes[n+1].style.borderColor = "#000000";

        window.resultCount = slideshow.resultCount;
        showMediaMarker(true);
    }

    function slideshowMovedNext(slideshow) {
        if (slideshow.index % filmstripSize == 0)
            updateFilmstrip = true;
    }

    function slideshowMovedPrevious(slideshow) {
        if (slideshow.index % filmstripSize == 4)
            updateFilmstrip = true;
    }

    function slideshowFrameSelected(slideshow) {
            updateFilmstrip = true;
    }
