jQuery( function($) {

    /*
        Drop-down menu in header, two levels
    */
    var $menu_parents  = $('header').find('nav').find('> ul > li'),
        $menu_children = $('> ul', $menu_parents);

    $menu_parents.each(function(){
        var $this       = $(this),
            $this_child = $this.find('> ul');
        $this.hover(
            function() {
                $menu_children.slideUp(75);
                $this_child.slideDown(125);
            },
            function() {
                $menu_children.slideUp(75);
            }
        );
    });


    /*
        Archive mouseover
    */
    var $archive_items = $('div').filter('.archive');
    if ( $archive_items.length > 0 ) {
        $archive_items.each(function(){
            var $this  = $(this),
                $items = $('.image', $this).add( $('h2 > a', $this) );
            $items.hover(
                function(){
                    $items.addClass('hover');
                }, function(){
                    $items.removeClass('hover');                    
                }
            );
        });
    }


    /*
        Google map, footer
    */
    if (window.SALONS_MAPS && window.SALONS_MAPS.length > 0) {
        /*
            Hash check
        */
        if ( ( window.location.hash ) && ( window.location.hash.replace('#', '') !== '' ) ) {
            var original_location = false,
                needle = window.location.hash.replace( '#', '' );

            for ( var i = 0; i < window.SALONS_MAPS.length; i++ ) {
                if ( needle === window.SALONS_MAPS[i].identifier ) {
                    original_location = new google.maps.LatLng( window.SALONS_MAPS[i]['latitude'], window.SALONS_MAPS[i]['longitude'] );
                    break;
                }
            }
            if (original_location === false) {
                original_location = new google.maps.LatLng( window.SALONS_MAPS[0]['latitude'], window.SALONS_MAPS[0]['longitude'] );
            }
        } else {
            var original_location = new google.maps.LatLng( window.SALONS_MAPS[0]['latitude'], window.SALONS_MAPS[0]['longitude'] );
        }

        /*
            Map options
        */
        var styles = [{
            featureType: "all",
            elementType: "all",
            stylers: [{
                saturation: -100
            }]
        }];
        var myOptions = {
            zoom: 16,
            center: original_location,
            mapTypeControl: false,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.SATELLITE, 'grayscale']
            },
            zoomControl: true,
            streetViewControl: false
        };
        var map = new google.maps.Map( document.getElementById('map-container'), myOptions );

        var grayMapType = new google.maps.StyledMapType( styles, {name: 'Kart'} );
        map.mapTypes.set( 'grayscale', grayMapType );
        map.setMapTypeId( 'grayscale' );
    
        /*
            Map points
        */
        var infobox = new google.maps.InfoWindow();
        var map_markers = [];
        for ( var i = 0; i < window.SALONS_MAPS.length; i++ ) {
            (function () {
                var point = window.SALONS_MAPS[i];
                var contents = 
                    '<div id="map-infobox">' +
                    point.text +
                    '</div>';
    
                map_markers[ point.identifier ] = new google.maps.LatLng( point.latitude, point.longitude );

                var marker = new google.maps.Marker({
                    position: map_markers[ point.identifier ],
                    map: map,
                    title: point.name
                });
         
            })(i);
        }

        /*
            Map navigation
        */
        var $map_anchors = $('a.map-anchor');
        if ( $map_anchors.length > 0 ) {

            $map_anchors.bind('click',function(){
                map.setCenter( map_markers[ $(this).attr('rel') ] );
            });

            /* If were on a salon, this salon should be the chosen one on the map */
            if ( $map_anchors.filter( 'li.selected > a' ).length === 1 ) {
                var $selected_anchor = $map_anchors.filter( 'li.selected > a' );

                map.setCenter( map_markers[ $selected_anchor.attr( 'rel' ) ] );

                $( '.salons-list' ).find( 'figcaption' ).scrollTo( $selected_anchor.attr( 'href' ), 0 );
            }
        }
    }

    /*
        Embed box height assimilation
    */
    $(window).load( function() {
        var $box_container = $('.box-row');
        if ( $box_container.length > 0 ) {
        
            var height;
            function setHeight( val ) {
                height = val;
            }
        
            $box_container.each( function() {
                height = 0;
                var $boxes = $(this).find( '> .embed' );
                $boxes.each( function() {
                    $this = $(this);
                    if ( height < $this.height() ) {
                        setHeight( $this.height() );
                    }
                }).css( 'height', height );
            });
        }
    });

});

