var areaInfoInited = false, mapInited = false, photocarousel = false;

function ViewMorePhotos() {

    var image = new Image();

    // Wire up mainImage onload event
    image.onload = function() {
        prepModalDialog();
        $('#UFrame1').append(image);
        showModalCover();
        setModalDimensions();
        modalVisible = true;
    };
    image.src = this.href;
    return false;
}

function photoCarousel_initCallback(carousel) {
    $('.photoCarousel', $AreaInfoPanel).parents('li:eq(0)').removeClass("concertinaExpandDefault");
}

function AreaOnChange(expanded) {
    if (expanded) {
        if (!photocarousel) {
            var parent = $(this).parents("li:eq(0)");
            if (parent.attr("id").indexOf("Photos") > -1) {
                $('.photoCarouselList', $AreaInfoPanel).setUpPhotoCarousel().find('a').click(ViewMorePhotos);
                photocarousel = true;
            }
        }
    }
}

function ShowTab(e) {
    var panels = $('#TabPanels'), obj = $(this).parents('li:eq(0)');
    if (!panels.data('offsetTop')) { panels.data('offsetTop', Math.floor(panels.offset().top)); }
    var $window = $(window), winScroll = { top: $window.scrollTop(), left: $window.scrollLeft() };

    panels.removeClass().addClass(obj.attr('class'));
    if (panels.data('selectedtab')) { panels.data('selectedtab').removeClass('selected'); }
    else { obj.siblings().removeClass("selected"); };
    panels.data('selectedtab', obj.addClass('selected'));
    if (!areaInfoInited && obj.hasClass('btnAreaInfo')) {
        window.$AreaInfoPanel = $('#AreaInfoPanel');
        $('.concertina', $AreaInfoPanel).concertina({ onChange: AreaOnChange });
        areaInfoInited = true;
    }
    else if (obj.hasClass("btnMap") || obj.hasClass("btnStreetView")) {
        if (!mapInited) {
            if (!FMAP) {
                FMAP = new FapMaps();
                $(window).one('unload', FMAP.cleanUpMarkers);
            }
            FMAP.initMapWithStreetView();
            mapInited = true;
        }
        if (FMAP) {
            if (obj.hasClass("btnStreetView")) {
                FMAP.setMapType('G_STREET_TYPE', true);
            }
            else if (obj.hasClass("btnMap")) {
                FMAP.setMapType('G_MAP_TYPE', true);
                FMAP.map.setCenter(FMAP.mapPoint, 15);
            }
        }
    }

    if (!arguments.callee.inProgress && (winScroll.top !== panels.data('offsetTop'))) {
        $('html, body').animate({ scrollLeft: winScroll.left, scrollTop: panels.data('offsetTop') - 30 }, 1000, 'swing', function() { delete window.ShowTab.inProgress; });
        arguments.callee.inProgress = true;
    }

    return false;
}

function ShowMapTab(e) {
    var panels = $('#TabPanels'); obj = $(this);
    panels.removeClass()
    if (obj.attr("id") == 'G_STREET_TYPE') {
        panels.addClass('btnStreetView');
    }
    else {
        panels.addClass('btnMap');
        FMAP.map.setCenter(FMAP.mapPoint, 15);
    }

    if (panels.data("selectedtab")) { panels.data("selectedtab").removeClass("selected") }
    else { $('li', '#MoreInfoButtons').removeClass("selected"); };
    panels.data("selectedtab", $('.' + panels.attr("class"), '#MoreInfoButtons').addClass("selected"));
}

function ChangeImage(i) {
    $(this).mouseover(function(e) { DisplayImage(i, e); PrevNext(i); }).click(function() { return false; });
}

function ChangeImagePrevNext(i, e) {

    DisplayImage(i, e);

    $('#PropertyThumbs').find('li:eq(' + i + ')').addClass("selected").siblings().removeClass("selected");

    PrevNext(i);
    return false;

}

function thumbsCarousel_initCallback(carousel) {
    $('#NextPhoto').attr("href", "#1").click(function(e) {
        var i = parseInt(this.href.split('#')[1]);
        if (isNaN(i)) { return false };
        var x = (i + 1) - (i % 3);
        carousel.scroll($.jcarousel.intval(parseInt(x)));
        return ChangeImagePrevNext(i, e);

    });

    $('#PrevPhoto').click(function(e) {
        var i = parseInt(this.href.split('#')[1]);
        if (isNaN(i)) { return false };
        var x = (i + 1) - (i % 3);
        carousel.scroll($.jcarousel.intval(parseInt(x)));
        return ChangeImagePrevNext(i, e, carousel);
    });



}


function PrevNext(i) {
    var x = 0;
    if (i < (imagearray.length - 1)) {
        x = parseInt(i) + 1;
        $('#NextPhoto').attr("href", "#" + x).removeClass("next_disabled");
    } else {
        $('#NextPhoto').addClass("next_disabled").attr("href", "#");
    };

    if (i > 0) {
        x = parseInt(i) - 1;
        $('#PrevPhoto').attr("href", "#" + x).removeClass("prev_disabled");
    } else {
        $('#PrevPhoto').addClass("prev_disabled").attr("href", "#");

    };
}

function ShowLocalityToolTip() {
    var obj = $(this);
    if (obj.find('.tooltip').length == 0) {

        var tooltipContent = obj.prev(".tooltipTable:eq(0)"), toolTip = $('<div class="tooltip"><div class="closer"></div></div>');
        if (tooltipContent) {
            tooltipContent.appendTo(toolTip);
            toolTip.appendTo(obj);
        }

    }

    return window.FAP.UI.openToolTip(obj);
}

/* image swapping code */

function imagedata(url, title, height, width) {
    this.url = url; 				// url of image
    this.title = title; 			// image title (alt text)
    this.height = height; 		// image height
    this.width = width; 			// image width
}

var currentimage = 0; 			// current displayed image
var imageresize = false; 		// if the main image will resize (PhotoAlbum sets in ASPX code)


// display image n (0-based) using image control ctl and optional label (title)
function DisplayImage(n, e) {
    var ctl = $(".defaultPhotoImage", '#PropertyGallery')[0];
    var labelctl = document.getElementById('labelMainPhoto');
    if (ctl != null) {
        // is image within imagedata range
        if ((n >= 0) && (n < imagearray.length)) {

            ctl.src = imagearray[n].url;
            ctl.title = imagearray[n].title;
            // optionally resize
            if (imageresize) {
                ctl.width = imagearray[n].width;
                ctl.height = imagearray[n].height;
            };
            // optional label control
            if (labelctl != null) {
                labelctl.innerHTML = imagearray[n].title;
                currentimage = n;
            };
        };
    };
    //UpdateNavigation();
    if (!e) {
        e = window.event;

    }
    if (e) {
        target = (e.target) ? e.target : e.srcElement;
        $(target).parents('li:eq(0)').addClass("selected").siblings().removeClass("selected");

    }

}

