﻿document.addEvent('domready', function() { Page.Setup(); });

var Page = {
    Setup: function() {
       // new Dropdown();

        $$('#sectionnav li').each(function(x) {
            if (!x.hasClass('selected'))
                x.setOpacity(0.8);
        });


        $$('#groupnav li ul').setStyles({ 'width': '0', 'margin-left': '0' });
        $$('#groupnav ul li').each(function(x) {
            var z = x;
            z.addEvent('mouseenter', function(y) {
                z.getElements('ul').morph({ 'width': '250px', 'margin-left': '10px' });

            });

            if (z.getElements('ul').length > 0) {
                $('groupnav').addEvent('mouseleave', function(y) {
                    z.getElements('ul').morph({ 'width': '0', 'margin-left': '0' });
                });
            }
        });

        if ($('imageinner')!=null)
            Scroller.setup();

        if ($('map') != null) {

            var initialize = function() {
                var latlng = new google.maps.LatLng(51.474701, -0.240734);
                var myOptions = {
                    zoom: 15,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map($('map'), myOptions);

                var marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    title: "Sonny's"
                });
            }
            initialize();
        }
    }
}


var Scroller = {

    setup: function() {
        // first of all duplicate the images that are there

        var urls = $$('#imageinner img').get('src');
        $$('#imageinner img').setOpacity(0);

        var w = 0;

        var doScroll = function() {
            $('imageinner').set('tween', {
                'duration': w * 80,
                transition: Fx.Transitions.linear,
                'onComplete': function() {
                    $('imageinner').setStyle('left', '0');
                    doScroll();
                }
            });
            $('imageinner').tween('left', -w + 'px');
        }
        
        var setSize = function() {

            var cloned = $$('#imageinner img').clone();
            cloned.setOpacity(0);
            cloned.inject('imageinner');

            $('imageinner').setStyle('width', (w * 2) + 'px');

            $$('#imageinner img').fade(1);

            doScroll();

        }
        
        var loadImg = function(id) {

            var a = Asset.image(urls[id], { onload: function(y) {

                w += (y.get('width') * 1) + 1;
                if (id < urls.length - 1) {
                    loadImg(id + 1);
                }

                else {
                    setSize();
                }


            }
            }
            )
        }

        if (urls.length > 0) {
            loadImg(0);
        }

      

       
    }
};



var Dropdown = new Class({
    initialize: function() {
        this.Dropdowns = $$('.dropdown a');
        var y = this.OpenMenu.bind(this);
        var x = this.StartClose.bind(this);
        this.Dropdowns.each(function(e) {
            e.addEvent('mouseover', y.pass(e));
            e.addEvent('mouseout', x.pass(e));
        })
    },
    OpenMenu: function(e) {
        $clear(this.timeout);

        var parent = e.getParent().getParent();
        parent.getElements('a').each(function(k) {

            if (k.hasClass('open') && k != e) {
                k.removeClass('open');
                k.getParent().getElements('ul').each(function(i) {
                    i.setStyle('display', 'none');
                });
            }
        }
                );

        if (!e.hasClass('open')) {
            e.addClass('open');


            if (e.getParent().getChildren().length > 1) {

                var children = e.getParent().getChildren()[1];
                children.setOpacity(0);
                children.setStyles({ 'display': 'block' });
                children.fade(1);

                var root = parent.getParent();
                var left, top;
                if (root.get('tag') == 'div') {
                    left = 0;  //e.getPosition($('navigation')).x;
                    top = e.getPosition($('navigation')).y + e.getSize().y + 1;
                }
                else {
                    left = 0;  //e.getPosition().x + e.getSize().x + 1;
                    top = e.getPosition(parent).y + 1; 
                }

                children.setStyle('top', top + 'px');
                children.setStyle('left', left + 'px');

            }
        }
    },
    StartClose: function(e) {
        this.timeout = this.CloseMenu.delay(500);
    },
    CloseMenu: function() {
        $clear(this.timeout);

        $$('.dropdown ul').each(function(i) {
            i.fade(0);
            i.getParent().getElements('a').removeClass('open');
        });

    }
});


function initialize() {
    var myLatlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 8,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function loadScript() {
    if ($('map_canvas') != null) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
        document.body.appendChild(script);
    }
}

window.onload = loadScript;
