$(function(){

    // to abstract this, conf:
    // active class
    // also need to check that the a tag parent is the li in the root ul
    // else keep going till is so if the a is in an em for in stance it'll still work

    Q.Menu.highlight('nav');
    $('#nav ul li:last-child').css('border-bottom', '0');

});

var Q = {};

Q.Menu = {
    highlight: function(id) {
        $('#' + id + ' li').removeClass('active');
        var found = false;
        $('#' + id + ' li a').not('#nav ul li a').each(function(){
            var len = this.href.length;
            if (window.location.toString().substring(0, len) == this.href) {
                found = $(this);
            }
            var $that = $(this);
            $(this).siblings().each(function(){
                if (this.tagName == 'UL') {
                    $that.css('cursor','default');
                    $that.click(function(){
                        return false;
                    });
                }
            });
        });
        if (found) {
            found.parent().addClass('active');
        }
    }
};

Q.GoogleMap = function(params) {        
    this.map;
    this.latlang;
    this.id            = params.id;
    this.mapId         = params.id + '-map';
    this.buttonId      = this.id + '-button';
    this.width         = $('#' + this.id).width();
    this.height        = $('#' + this.id).height();
    params.zoom        ? this.zoom = params.zoom : this.zoom = 12;
    params.marker      ? this.marker = params.marker : this.marker = true;
    params.control     ? this.mapControl = params.mapControl : this.mapControl = true;
    params.typeControl ? this.typeControl = params.typeControl : this.typeControl = true;
    
    var coords = $('#' + this.id).attr('title').split(":")[1].split(',');
    $('#' + this.id).attr('title', '');
    this.x = coords[1];
    this.y = coords[0];
    
    $('#' + this.id).html("<div class='button zoom' id='" + this.buttonId + "'></div><div id='" + this.mapId + "'></div>");
    $('#' + this.mapId).css({
        width: this.width,
        height: this.height,
        overflow: 'hidden',
        border: '1px solid #000'
    });
    
    switch (params.type) {
        case 'normal':
            this.defaultType = G_NORMAL_MAP;
            break;
        case 'satellite':
            this.defaultType = G_SATELLITE_MAP;
            break;
        case 'hybrid':
            this.defaultType = G_HYBRID_MAP;
            break;
        case 'physical':
            this.defaultType = G_PHYSICAL_MAP;
            break;
        default:
            this.defaultType = G_NORMAL_MAP;
    }
    this.insertMapFromCoords();
    if (params.expand) {
        this.expand(params.expand);
    } else if (params.fullscreen) {
        this.fullscreen()
    }
}

Q.GoogleMap.prototype = {
    
    expand: function(params) {
        var that = this;
        $('#' + this.buttonId).toggle(
            // open
            function(){
                that.expandMap({
                    elm: $(this),
                    width: params.to.width,
                    height: params.to.height,
                    hide: params.hide
                });
            },
            // close
            function(){     
                that.contractMap({
                    elm: $(this),
                    width: that.width,
                    height: that.height,
                    hide: params.hide
                });
            }
        );
    },
    
    fullscreen: function() {
        var that = this;
        $('#' + this.buttonId).toggle(
            // open
            function(){
                that.fullscreenShow({
                    elm: $(this)
                });
            },
            // close
            function(){     
                that.fullscreenHide({
                    elm: $(this)
                });
            }  
        );
    },
    
    fullscreenShow: function(params) {
        var mask = Q.Overlay.mask();
        $('#container').hide();
        var zIndex = parseInt($('#' + mask).css('z-index')) + 1;
        $('#' + this.mapId).css({
            'visibility': 'hidden'
        });
        var that = params.elm;
        var other = this;
        var map = this;
        var height = $('body').height();
        if (height < $(window).height()) {
            height = $(window).height();
        }
        var width = $('body').width();
        if (width < $(window).width()) {
            width = $(window).width();
        }
        width = width + 'px';
        height = height + 'px';
        $('#' + this.id).appendTo('body');
        $('#' + this.id).css({
            'z-index': zIndex,
            'position': 'absolute',
            top: 0,
            left: 0,
            background: '#CCC',
            width: width,
            height: height,
            margin: 0,
            padding: 0,
            border: 'none'
        });
        that.css({bottom: 0, 'z-index': zIndex + 1});
        that.removeClass('zoom');
        that.addClass('close');
        $('#' + map.mapId).css({
            width: width,
            height: height,
            visibility: 'visible',
            'z-index': zIndex,
            margin: 0,
            padding: 0,
            border: 'none'
        });
        map.resize();
        map.center();
        that.fadeIn();
        $('body').keyup(function(e){
            if (e.keyCode == '27') {
                other.fullscreenHide(that);
            }
        });
    },
    
    fullscreenHide: function(params) {
        $('#container').show();
        
        var map = this;
        
        $('#' + this.id).appendTo('#container');
        
        params.elm.removeClass('close');
        params.elm.addClass('zoom');
        $('#' + this.id).css({
            width: params.width + 'px',
            height: params.height + 'px'
        });   
        $('#' + this.mapId).css({
            visibility: 'visible',
            width: params.width + 'px',
            height: params.height + 'px'
        });
        for (var i = 0, len = params.hide.length; i < len; i++) {
            $(params.hide[i]).fadeIn('fast');
        }
        params.elm.fadeIn('fast');
        $('#' + this.mapId).css('border', '1px solid #000');
        this.resize();
        this.center();
        Q.Overlay.unmask();
    },
    
    expandMap: function(params) {
        params.elm.hide();
        for (var i = 0, len = params.hide.length; i < len; i++) {
            $(params.hide[i]).hide();
        }
        $('#' + this.mapId).css({
            'visibility': 'hidden'
        });
        var that = params.elm;
        var map = this;
        $('#' + this.id).animate({
            width: params.width + 'px',
            height: params.height + 'px'
        },{
            duration: 200,
            complete: function(){
                that.removeClass('zoom');
                that.addClass('close');
                $('#' + map.mapId).css({
                    width: params.width + 'px',
                    height: params.height + 'px',
                    visibility: 'visible'
                });
                $('#' + map.mapId).css('border', '1px solid #000');
                map.resize();
                map.center();
                that.fadeIn();
            }
        });
    },
    
    contractMap: function(params) {
        params.elm.removeClass('close');
        params.elm.addClass('zoom');
        $('#' + this.id).css({
            width: params.width + 'px',
            height: params.height + 'px'
        });   
        $('#' + this.mapId).css({
            visibility: 'visible',
            width: params.width + 'px',
            height: params.height + 'px'
        });
        for (var i = 0, len = params.hide.length; i < len; i++) {
            $(params.hide[i]).fadeIn('fast');
        }
        params.elm.fadeIn('fast');
        $('#' + this.mapId).css('border', '1px solid #000');
        this.resize();
        this.center();
    },
    
    resize: function(){
        this.map.checkResize();
    },
    
    center: function() {
        var latlang = new GLatLng(this.y, this.x);
        this.map.setCenter(latlang, this.map.getZoom(), this.map.getCurrentMapType());
    },
    
    insertMapFromCoords: function() {
        if (GBrowserIsCompatible()) {
            this.map = new GMap2(document.getElementById(this.mapId), {draggableCursor: 'default', draggingCursor: 'default'});
            this.latlang = new GLatLng(this.y, this.x);
            this.map.setCenter(this.latlang, this.zoom, this.defaultType);
            if (this.marker) {
                this.map.addOverlay(new GMarker(this.latlang));
            }
            if (this.mapControl) {
                this.map.addControl(new GSmallMapControl());
            }
            if (this.typeControl) {
                this.map.addControl(new GMapTypeControl());
            }
        }
    },
    
    editMarker: function() {
        var latlang = this.latlang;
        var map = this.map;
        GEvent.addListener(this.map,"click", function(overlay, latlang) {     
          if (latlang) { 
            var myHtml = latlang.y + " " + latlang.x;
            map.openInfoWindow(latlang, myHtml);
          }
        });
    }
    
};

/**
 * Object for managing overlays
 *
 * Overlays are elements that mask other elements by sitting over the top of them.
 * They have variable transparency to indicate they are masking underlying
 * elements (or not if they are opaque)
 * An overlay may be set to mask a whole page or all elements on the page except one
 */

Q.Overlay = {

    curIndex: 10000,
    
    masks: null,

    mask: function(params) {
        if (!this.masks) {
            this.masks = new Q.Array;
        }
        params ? null : params = {};
        params.elm ? null : params.elm = 'body';
        params.id ? null : params.id = 'q-mask';
        params.color ? null : params.color = '#000';
        params.opacity ? null : params.opacity = 0.85;
        params.zindex ? this.curIndex = params.zindex : null;
        if (params.elm === 'body') {
            $('body').append("<div id='" + params.id + "'></div>");
        } else if ($('#' + params.elm).length) {
            $('#' + params.elm).append("<div id='" + params.id + "'></div>");
        }
        var parent = $('#' + params.id).parent();
        $('#' + params.id).css({
            display: 'block',
            height: parent.height(),
            width: parent.width(),
            position: 'fixed',
            left: parent.position().left,
            top: parent.position().top,
            'z-index': Q.Overlay.curIndex,
            opacity: params.opacity,
            background: params.color
        });
        Q.Overlay.curIndex++;
        if ($('body').height() < $(window).height()) {
            $('#' + params.id).height($(window).height());
        }
        return this.masks[params.elm] = params.id;
    },

    unmask: function(params) {
        params ? null : params = {};
        if (!params.elm) {
            $('#' + this.masks.get('body')).remove();
            delete this.masks['body'];
        } else {
            $('#' + params.id).remove();
            delete this.masks[params.elm];
        }
    }

};

Q.Reg =  {};
