Cufon.set('fontFamily', 'Helvetica Neue');
Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('.intro');
Cufon.replace('#navigation');

window.addEvent('domready', function() {
    $$('a.external').each(function(link) {
        link.set('target', '_blank');
    });
    $$('input[type="text"]').each(function(el) {
        var over                = new OverText(el, {
            'wrap': true
        });
    });
});

function print_page()
{
    try {
        window.print();
    } catch (e) {
    }
    return false;
}

var FVLocation = new Class({
    el: null,
    point: null,
    map: null,
    geocoder: null,
    markers: null,
    pending: null,
    timer: null,

    initialize: function(el, title, point) {
        if (GBrowserIsCompatible()) {
            this.el             = el;
            this.point          = point;
            this.title          = title;
            window.addEvent('domready', this.createMap.bindWithEvent(this));
        }
    },

    createMap: function(e) {
        this.map                = new GMap2($(this.el));
        this.map.addControl(new GSmallMapControl());

        this.geocoder           = new GClientGeocoder();

        if (this.point) {
            this.showMarker(GLatLng.fromUrlValue(this.point));
        } else {
            var fv              = this;
            this.geocoder.getLatLng(this.title, function(point) {
                if (point) {
                    fv.showMarker(point);
                }
            });
        }
    },

    showMarker: function(point) {
        var marker              = new GMarker(point);
        this.map.addOverlay(marker);

        var melbourne           = new google.maps.LatLng(-37.814251, 144.963169);

        directions = new GDirections(this.map, null);
        directions.loadFromWaypoints([point, melbourne], {
            preserveViewport: true
        });
    }
});

var FVMap = new Class({

    el: null,
    map: null,
    geocoder: null,
    markers: null,
    pending: null,
    timer: null,

    initialize: function(el) {
        if (GBrowserIsCompatible()) {
            this.el             = el;
            window.addEvent('domready', this.createMap.bindWithEvent(this));
        }
    },

    createMap: function(e) {
        this.map                = new GMap2($(this.el));
        this.map.addControl(new GSmallMapControl());
        this.map.setCenter(new google.maps.LatLng(-37.025311,145.134148), 7);

        this.geocoder           = new GClientGeocoder();
        this.geocoder.setViewport(this.map.getBounds());

        this.markers            = $$('.shire');
        this.pending            = [];

        this.markers.each(function(marker) {
            this.loadMarker(marker);
        }, this);

        this.loadGeoData.periodical(500, this);
    },

    loadMarker: function(marker) {
        var shireName           = marker.getElement('h2').get('text');
        var shireCoords         = marker.getElement('h2').get('title');

        if (shireCoords !== null) {
            this.showMarker(marker, GLatLng.fromUrlValue(shireCoords));
        } else {
            this.pending[this.pending.length] = [marker, shireName];
        }
    },

    showMarker: function(shire, point) {
        var marker              = new GMarker(point);
        marker.bindInfoWindow(shire, {maxWidth: 260});
        this.map.addOverlay(marker);
    },

    saveMarker: function(marker, point) {
        var id                  = marker.getElement('h2').get('id').replace(/location\-/, '');
        new Request.HTML({
            url:'/region/marker.php',
            onSuccess: this._saveMarkerSuccess.bind(this),
            onError: this._saveMarkerError.bind(this)
        }).get({
            'id': id,
            'point': point.toUrlValue()
        });
    },

    _saveMarkerSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
        //
    },

    _saveMarkerError: function() {
    },

    loadGeoData: function() {
        if (!this.pending.length) {
            $clear(this.timer);
            return false;
        }
        var pending             = this.pending.pop();
        var shireName           = pending[1] + ', Victoria';
        var marker              = pending[0];
        var fv                  = this;
        this.geocoder.getLatLng(shireName, function(point) {
            if (point) {
                fv.showMarker(marker, point);
                fv.saveMarker(marker, point);
            }
        });
    }
});

var SlideShow = new Class({

    els: [],
    waiting: 0,
    timer: 0,
    ctr: 0,
    looping: false,

    Implements: Options,

    options: {
        delay:                  5000,
        duration:               2500,
        transition:             Fx.Transitions.Cubic.easeIn,
        itemClass:              '.promotion',
        controls:               false
    },

    initialize: function(els, options) {
        this.setOptions(options);
        this.waiting            = 0;
        this.el                 = $('slideshow');
        if (!this.el) {
        this.el                 = new Element('div')
            .setProperty('id', 'slideshow')
            .inject($('layout'));
        }
        $A(els).each(function(slide) {
            var img             = new Element('img')
                .set('opacity', 0)
                .addEvent('load', function(evt) {
                    this.waiting--;
                }.bindWithEvent(this))
                .setProperty('src', slide.src)
                .setProperty('title', slide.alt)
                .inject(this.el, 'bottom');
            this.els[this.els.length] = img;
            this.waiting++;
        }.bind(this));
        if (this.els.length) {
            this.checkload();
        }
    },

    checkload: function() {
        if (this.waiting > 0) {
            return this.checkload.delay(100, this);
        }
        this.el.setStyle('background-image', 'none')
        this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 1);
        return this.loop.delay(this.options.delay, this);
    },

    loop: function() {
        if (this.ctr > -1) {
            this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 0);
        }
        this.ctr++;
        if (this.ctr == this.els.length) {
            this.ctr = 0;
        }
        this.els[this.ctr].get('tween', {transition: this.options.transition, duration: this.options.duration}).start('opacity', 1);
        return this.loop.delay(this.options.delay, this);
    }
});

