var LocationNotFoundError = function(){};

window.addEvent('domready', function() {
    var printers = $$('a.print');

    printers.each(function(el) {
	el.addEvent('click', function(clicker) {
	    var event = new Event(clicker);
	    event.preventDefault();
	    window.print();
	});
    });
});

function setupGoogleMap (address) {
    if (GBrowserIsCompatible() && $('map_canvas')) {
        if (address !== "Address Withheld") {
            var setup = function(point) {
                if(point == null) throw new LocationNotFoundError();
                var map = new GMap2(document.getElementById("map_canvas"));
    
            map.setCenter(point, 10);
            map.addOverlay(new GMarker(point));
            map.setUIToDefault();
            };
    
            var sorry = function() {
                var div = new Element('p', {
                    'class': 'sorry',
                    'text': 'sorry, this address could not be found'
                });
                div.replaces($('map_canvas'));
            };
    
            try {
                var addressString = "{StreetNumber} {StreetName}, {Region} New Zealand".substitute(address);
                var geo = new GClientGeocoder();
                var point = geo.getLatLng(addressString, setup);
            } catch(e) {
                if (e instanceof LocationNotFoundError) sorry();
                else throw (e);
            }
        } else {
            $('map_canvas').set('text', 'Address Withheld');
        }
    }
}

var myShowcase;

var callWithPropertyDetails = function (id, fn, failure) {
    // if id is false, we assume that you're on a property page

    var data = id ? {
        "altTemplate": "PropertyDetailsJSON",
        "id": id
    }
    :  {
        "altTemplate": "PropertyDetailsJSON"
    };

    var url = id ? "/property-details.aspx" : "" ;

    var req = new Request.JSON({
        "url": url,
        "data": data,
        "onSuccess": function(obj, text) { return fn(obj); },
        "onFailure": failure || function () {}
    }).get();
};

window.addEvent(
    'domready', function() {
        if($$('.showcaseItem').length > 0) {
            myShowcase = new Showcase(
                $$(".showcaseItem"), {
                    playOnStart: true,
                    interval: 6000
                });
        }

        if ($('photos')) {
            var largeLinks = $$('a.mainPropertyImageLink');

            function show (index) {
                largeLinks.fade('hide');
                largeLinks[index].fade('show');
            };

            show(0);

            $$('#photos .thumbnail a').each(
                function(item, index){
                    var link = largeLinks[index];
                    item.addEvent(
                        'click', function (event) {
                            new Event(event).preventDefault();
                            show(index);
                        });
                });

            /* Setup the maximise/minimise animation */


            var tween = new Fx.Tween (
                $('thumbnailsWrapper'), {
                    'property': 'height'
                });

            var smallSize = function () {
                return $$('.mainPropertyImage')[0].get('height').toInt() + 2;
            };

            var bigSize = function () {
                return $$('ul.thumbnails')[0].getSize().y;
            };

            tween.set(smallSize());

            var showLink = new Element('a', {'text': "maximise" , 'class': "hideShow maximise"});
            var hideLink = new Element('a', {'text': "minimise", 'class': "hideShow minimise"});

            showLink.addEvent('click', function  () {
                tween.start(bigSize());
                hideLink.replaces(showLink);
            });

            hideLink.addEvent('click', function () {
                tween.start(smallSize());
                showLink.replaces(hideLink);
            });

            showLink.inject('photos', 'bottom');
        }

        function setupMapTab (propertyDetails) {

            if ($$('.tabContainer')) {
                var myTabs = new Tabset($$('.tabContainer')[0]);
                var mapready = false;
                myTabs.getPair('map').addEvent('active', function () {
                    if (!mapready) {
                        if ('Address' in propertyDetails) {
                            setupGoogleMap(propertyDetails.Address);
                        }
                        mapready = true;
                    }
                });
            }
        };

        if ($('map')) callWithPropertyDetails(false, setupMapTab);

    });

