﻿/// <reference path="jquery.intellisense.js"/>

var site = {
    start: function() {
        externalLinks.start();
        searchBar.start();
        bannerTabs.start();
        googleInline.start();
        findUs.start();
        iceAgeObjects.start();
        lightBox.start();
        emailHyperLink.start();
        imageTab.start();
    }
};

var externalLinks = {
    start: function() {
        $('a[rel="external"]').attr('target', '_blank');
    }
};

var searchBar = {
    open: false,

    start: function() {
        //$('#navmain').css('position', 'fixed');
        var sl = $('#searchlink');
        if (sl.length > 0) {
            sl.attr('href', 'javascript:void(0)');
            sl.click(function(e) {
                var tn = $('#topnav');
                var strap = $('#strapmain');
                if (searchBar.open) {
                    tn.animate({
                        top: '0px'
                    }, 500);
                    strap.animate({
                        top: '0px'
                    }, 500);
                    sl.html('search');
                } else {
                    tn.animate({
                        top: '35px'
                    }, 500);
                    strap.animate({
                        top: '20px'
                    }, 500);
                    sl.html('close');
                }
                searchBar.open = !searchBar.open;
            });
        }
    }
};

var imageTab = {
    imageRowHeight: 65,
    imageRows: null,
    imageCount: null,
    scrollMax: null,
    imageMax: null,
    scrollGrid: null,
    currentImageRow: 0,
    controlsLocked: false,
    sequenceTimer: null,

    start: function() {
        if ($('#medialayer').length > 0) {
            // init images
            var imagesEl = $('dd.images .showcase');
            if (imagesEl.length > 0) {
                // dom elements
                var inner = $('dd.images .inner');
                var handle = $('dd.images .handle');
                var ul = $('dd.images ul');

                // render dd.images so we can calculate dimensions
                $('dd.images').css({
                    display: 'block',
                    visibility: 'hidden'
                });

                // reset scroll top (sometimes maintained after refresh)
                ul.scrollTop(0);

                // calculations
                imageTab.imageCount = $('dd.images ul img').length;
                imageTab.imageRows = Math.ceil(imageTab.imageCount / 3);
                imageTab.scrollMax = inner.height() - handle.height();
                imageTab.imageMax = (imageTab.imageRows - 4) * imageTab.imageRowHeight;
                imageTab.scrollGrid = imageTab.scrollMax / (imageTab.imageRows - 4);

                // hide dd.images
                $('dd.images').css({
                    display: 'none',
                    visibility: 'visible'
                });

                // scroll up
                var up = $('dd.images .up');
                up.click(function(e) {
                    imageTab.abortSequence();
                    var ul = $('dd.images ul');
                    var handle = $('dd.images .handle');
                    if (imageTab.currentImageRow > 0) {
                        imageTab.currentImageRow--;
                        var imageOffset = imageTab.currentImageRow * imageTab.imageRowHeight;
                        // move scroll handle
                        var htop = parseInt(handle.css('top')) - imageTab.scrollGrid;
                        handle.css('top', htop + 'px');
                        // animate image scroll
                        ul.stop().scrollTo(imageOffset + 'px', { duration: 600 });

                        imageTab.updateImageCount();
                    }
                    return false;
                });

                // scroll down
                var down = $('dd.images .down');
                down.click(function(e) {
                    imageTab.abortSequence();
                    var ul = $('dd.images ul');
                    var handle = $('dd.images .handle');
                    if (imageTab.currentImageRow < imageTab.imageRows - 4) {
                        imageTab.currentImageRow++;
                        var imageOffset = imageTab.currentImageRow * imageTab.imageRowHeight;
                        // move scroll handle
                        var htop = parseInt(handle.css('top')) + imageTab.scrollGrid;
                        handle.css('top', htop + 'px');
                        // animate image scroll
                        ul.stop().scrollTo(imageOffset + 'px', { duration: 600 });

                        imageTab.updateImageCount();
                    }
                    return false;
                });

                // setup scrollbar
                $('dd.images .handle').draggable({
                    containment: 'dd.images .inner',
                    scroll: false,
                    grid: [imageTab.scrollGrid, imageTab.scrollGrid],
                    drag: function(e, ui) {
                        imageTab.abortSequence();

                        // dom elements
                        var inner = $('dd.images .inner');
                        var ul = $('dd.images ul');

                        // calculations
                        var scrollOffset = ui.offset.top - inner.offset().top;
                        var imageOffset = scrollOffset * imageTab.imageMax / imageTab.scrollMax;

                        // move images
                        ul.stop().scrollTo(imageOffset + 'px', { duration: 0 });

                        // set currentImageRow
                        imageTab.currentImageRow = scrollOffset / imageTab.scrollGrid;
                    }
                }).click(function(e) {
                    return false;
                });

                // mousewheel
                $('dd.images .scroll').mousewheel(function(e, delta) {
                    imageTab.abortSequence();
                    var ul = $('dd.images ul');
                    var handle = $('dd.images .handle');
                    var newImageRow = imageTab.currentImageRow - delta;
                    if (newImageRow >= 0 && newImageRow < imageTab.imageRows - 3) {
                        imageTab.currentImageRow = newImageRow;
                        var imageOffset = imageTab.currentImageRow * imageTab.imageRowHeight;
                        // move scroll handle
                        var htop = imageTab.currentImageRow * imageTab.scrollGrid;
                        handle.css('top', htop + 'px');
                        // animate image scroll
                        ul.stop().scrollTo(imageOffset + 'px', { duration: 100 });
                        imageTab.updateImageCount();
                    }
                    return false;
                });

                // next / previous controls
                $('dd.images .controls a.next').click(function() {
                    imageTab.abortSequence();
                    imageTab.nextImage(1);
                    return false;
                });
                $('dd.images .controls a.previous').click(function() {
                    imageTab.abortSequence();
                    imageTab.nextImage(-1);
                    return false;
                });

                // thumbnail click
                $('dd.images ul a').click(function(e) {
                    imageTab.abortSequence();

                    // one at a time, please
                    if (imageTab.controlsLocked)
                        return false;

                    // dom elements
                    var a = $(this);

                    // data
                    var img = a.attr('href');
                    var link = a.siblings('a.link').attr('href');
                    var desc = a.siblings('.description').html();

                    // selected state
                    $('dd.images ul li a').removeClass('selected');
                    a.addClass('selected');

                    // change image
                    imageTab.loadImage(img, link, desc);
                    return false;
                });
            }
        }
    },

    beginSequence: function() {
        imageTab.abortSequence();
        imageTab.sequenceTimer = setInterval(function() {
            imageTab.nextImage(1);
        }, 4000);
    },

    abortSequence: function() {
        if (imageTab.sequenceTimer != null) {
            clearInterval(imageTab.sequenceTimer);
            imageTab.sequenceTimer = null;
        }
    },

    updateImageCount: function() {
        var istart = $('.imagecount .start');
        var iend = $('.imagecount .end');
        istart.text(imageTab.currentImageRow * 3 + 1);
        var end = (imageTab.currentImageRow * 4) + 12;
        end = end < imageTab.imageCount ? end : imageTab.imageCount;
        iend.text(end);
    },

    nextImage: function(delta) {
        // one at a time, please
        if (imageTab.controlsLocked)
            return;

        // dom elements
        var links = $('dd.images ul li a').not('.link, .description a');
        var previous = links.filter('.selected');

        // current index
        var index;
        links.each(function(i, link) {
            if ($(link).hasClass('selected'))
                index = i;
        });
        index += delta;
        if (index < 0) index = links.length - 1;
        if (index >= links.length) index = 0;

        // selected state
        previous.removeClass('selected');
        var current = $(links[index]);
        current.addClass('selected');

        // data
        var img = current.attr('href');
        var link = current.siblings('a.link').attr('href');
        var desc = current.siblings('.description').html();

        // change image
        imageTab.loadImage(img, link, desc);

        // update scroll
        var minindex = imageTab.currentImageRow * 3;
        var maxindex = minindex + 11;
        //alert('index: ' + index + ' min: ' + minindex + ' max: ' + maxindex);
        if (index < minindex || index > maxindex) {
            // elements
            var ul = $('dd.images ul');
            var handle = $('dd.images .handle');
            // work out currentImageRow to display current index
            var newImageRow = Math.floor(index / 3);
            if (newImageRow > imageTab.imageRows - 4)
                newImageRow = imageTab.imageRows - 4;
            imageTab.currentImageRow = newImageRow;
            // move scroll handle
            var htop = imageTab.currentImageRow * imageTab.scrollGrid;
            handle.css('top', htop + 'px');
            // animate image scroll
            var imageOffset = imageTab.currentImageRow * imageTab.imageRowHeight;
            ul.stop().scrollTo(imageOffset + 'px', { duration: 100 });
            imageTab.updateImageCount();
        }
    },

    loadImage: function(img, link, desc) {
        // one at a time, please
        if (imageTab.controlsLocked)
            return;
        imageTab.controlsLocked = true;

        // dom elements
        var mainimg = $('dd.images .imgshowcase');
        var mainimgfg = $('dd.images .imgshowcasefg');
        var description = $('#imgoverlay .description');
        var imglink = $('#imgoverlay .btn_more');
        var imgoverlay = $('#imgoverlay');

        // ie
        //mainimg.css('position', 'relative');

        // slide down description bar
        var originalMargin = parseInt(imgoverlay.css('marginTop'));
        var margin = originalMargin + imgoverlay.height();
        imgoverlay.animate({ marginTop: margin + 'px' }, 200, 'linear', function() {
            // load in new image                 
            $(new Image()).load(function() {
                // set description / link
                description.html(desc);
                imglink.attr('href', link);

                // switch images
                mainimg.css({
                    backgroundImage: 'url(' + img + ')'
                });
                mainimgfg.animate({ opacity: 0 }, 700, 'linear', function() {
                    mainimgfg.css({
                        backgroundImage: 'url(' + img + ')',
                        opacity: 1
                    });

                    // slide up description bar
                    imgoverlay.animate({ marginTop: originalMargin + 'px' }, 200, 'linear', function() {
                        // ie
                        //mainimg.css('position', 'inherit');
                        imageTab.controlsLocked = false;
                    });
                });
            }).attr('src', img);
        });
    }
};

var bannerTabs = {
    map: false,

    start: function() {
        // homepage swf
        if ($('#homebanner').length > 0) {
            swfobject.embedSWF("Images/bnr_home.swf", "homebanner", "613", "251", "9.0.0", "Images/expressInstall.swf");
        }

        if ($('#medialayer').length > 0) {
            // init map
            var mapEl = $('dd.map .showcase');
            if (mapEl.length > 0) {
                // set left nav position
                var content = $('dd.map');
                var ul = $(content.find('ul')[0]);
                ul.css('marginTop', '0px');
                var offset = ul.offset().top - content.offset().top;
                var margin = content.height() - offset - ul.height() + 8;
                ul.css('marginTop', margin + 'px');

                // init map
                mapEl.googlemap({
                    latitude: 53.262492,
                    longitude: -1.198025
                });
                bannerTabs.map = mapEl.googlemap();

                // load config xml
                var link = $($('dd.map ul li h4 a')[0]);
                $.get(link.attr('href'), null, function(xml) {
                    bannerTabs.xml = $(xml);
                    bannerTabs.map.loadXML(xml);
                    // move controls
                    $('#lmc3d').css('left', '555px');
                });

                // map events
                bannerTabs.map.mapMoveStart(function(evt) {
                    $('#map_bubble').css('display', 'none');
                }).mapClick(function(evt) {
                    $('#map_bubble').css('display', 'none');
                }).mapZoomEnd(function(evt, data) {
                    $('#map_bubble').css('display', 'none');
                });

                // marker events
                bannerTabs.map.markerClick(function(evt, marker) {
                    // find elements
                    var title = marker.getTitle();
                    var img = $('dd.map img[title="' + title + '"]');
                    var bubble = $('#map_bubble');

                    // set bubble content
                    var node = $(bannerTabs.xml.find('marker[title="' + title + '"]'));
                    var bubbleimg = bubble.find('img');
                    bubble.find('h5').text(title);

                    var description = node.attr('description');
                    if (!description) {
                        description = node.find('description').text();
                    }

                    bubble.find('.description')[0].innerHTML = description;
                    bubbleimg.attr('src', '/images/' + node.attr('image')).attr('alt', title);
                    if (node.attr('image') == null) {
                        bubbleimg.css('display', 'none');
                        bubble.addClass('noimage');
                    } else {
                        bubbleimg.css('display', 'block');
                        bubble.removeClass('noimage');
                    }

                    bubble.css('display', 'block');

                    // work out bubble position
                    var top = 20 + img.offset().top - bubble.height() / 2;
                    var left = 12 + img.offset().left - bubble.width();
                    bubble.css('top', top + 'px');
                    bubble.css('left', left + 'px');
                }).markerMouseOver(function(evt, marker) {
                    var icon = marker.getIcon().image.replace('.png', '_on.png');
                    marker.setImage(icon);
                }).markerMouseOut(function(evt, marker) {
                    var icon = marker.getIcon().image.replace('_on.png', '.png');
                    marker.setImage(icon);
                });
            }

            // handle map left nav click
            $('dd.map ul li h4 a').click(function(e) {
                // remove selected state from all nav links
                var links = $('dd.map ul li h4 a');
                links.each(function(i) {
                    var link = $(links[i]);
                    link.attr('class', link.attr('class').replace('_on', ''));
                });

                var link = $(this);
                link.attr('class', link.attr('class') + '_on');
                bannerTabs.map.clearOverlays();
                $.get(link.attr('href'), null, function(xml) {
                    bannerTabs.xml = $(xml);
                    bannerTabs.map.loadXML(xml);
                });
                return false;
            });

            // handle tab click
            $('#medialayer dt a').click(function(e) {
                var bannercontent = $('#bannercontent');
                var content = $(this).parent('dt').next('dd');
                var showcase = content.find('.showcase');
                var oldshowcase = $('.rml_on .showcase');
                var currenttab = $(this).parent('dt');

                if (currenttab.hasClass('selected'))
                    return;

                // ie8 hack
                if ($.browser.msie && $.browser.version == '8.0')
                    oldshowcase.children().animate({ 'opacity': 0 }, 200, 'linear', null);

                // fade out old showcase
                oldshowcase.animate({ 'opacity': 0 }, 200, 'linear', function() {
                    // hide map bubble
                    $('#map_bubble').css('display', 'none');

                    // tabs
                    $('#medialayer dt.selected').removeClass('selected');
                    currenttab.addClass('selected');

                    // prepare layer according to type
                    if (content.hasClass('map')) {
                        $(content.find('ul')[0]).css('marginTop', '0px');
                    }

                    // work out new heights
                    var height = content.height();
                    var bannerheight = height + 95;

                    // prevent overflow
                    content.css('overflow', 'hidden');
                    content.css('min-height', '0px');
                    content.height($('.rml_on').height() + 'px');

                    // hide showcase
                    showcase.css('opacity', 0);
                    // ie8 hack
                    if ($.browser.msie && $.browser.version == '8.0')
                        showcase.children().css('opacity', 0);

                    // content
                    $('#medialayer dd.rml_on').removeClass('rml_on').css('display', 'none');
                    content.addClass('rml_on');
                    bannercontent.animate({ height: bannerheight + 'px' }, 200);
                    content.animate({ height: height + 'px' }, 200, 'linear', function() {
                        content.css('overflow', 'visible');
                        showcase.animate({ opacity: 1 }, 200);
                        // ie8 hack
                        if ($.browser.msie && $.browser.version == '8.0')
                            showcase.children().animate({ 'opacity': 1 }, 200, 'linear', null);
                    });

                    // keep map left nav at bottom of content
                    if (content.hasClass('map')) {
                        var ul = $(content.find('ul')[0]);

                        // set left nav position
                        ul.css('marginTop', '0px');
                        var offset = ul.offset().top - content.offset().top;
                        var margin = height - offset - ul.height() + 8;
                        ul.css('marginTop', margin + 'px');
                    }

                    // trigger image sequence
                    if (content.hasClass('images')) {
                        imageTab.beginSequence();
                    }
                });
            }).attr('href', 'javascript:void(0)');
        }
    }
};

var googleInline = {
    start: function() {
        var googleMaps = $('.googleinline');

        googleMaps.each(function(i) {
            var el = $(googleMaps[i]);
            var link = el.find('a.link');

            if (el.css('display') == 'block') {
                // init map
                el.googlemap({
                    latitude: 53.262492,
                    longitude: -1.198025,
                    height: el.height(),
                    width: el.width(),
                    controls: false,
                    smallcontrols: true
                });

                var map = el.googlemap();
                var xml = null;

                // load config xml
                $.get(link.attr('href'), null, function(thexml) {
                    xml = $(thexml);
                    map.loadXML(xml);
                });

                // bubble
                if ($('#map_bubble').length == 0) {
                    $('#sitewrapper').append("<div id=\"map_bubble\"><div class=\"top\"></div><div class=\"mid\"><img src=\"\" alt=\"\" /><div class=\"content\"><h5></h5><div class=\"description\"></div></div><div class=\"clear\"></div></div><div class=\"bot\"></div></div>");
                }

                // map events
                map.mapMoveStart(function(evt) {
                    $('#map_bubble').css('display', 'none');
                }).mapClick(function(evt) {
                    $('#map_bubble').css('display', 'none');
                }).mapZoomEnd(function(evt, data) {
                    $('#map_bubble').css('display', 'none');
                });

                map.markerClick(function(evt, marker) {
                    // find elements
                    var title = marker.getTitle();
                    var img = el.find('img[title="' + title + '"]');
                    var bubble = $('#map_bubble');

                    // set bubble content
                    var node = $(xml.find('marker[title="' + title + '"]'));
                    var bubbleimg = bubble.find('img');
                    bubble.find('h5').text(title);

                    var description = node.attr('description');
                    if (!description) {
                        description = node.find('description').text();
                    }

                    bubble.find('.description')[0].innerHTML = description;
                    bubbleimg.attr('src', '/images/' + node.attr('image')).attr('alt', title);
                    if (node.attr('image') == null) {
                        bubbleimg.css('display', 'none');
                        bubble.addClass('noimage');
                    } else {
                        bubbleimg.css('display', 'block');
                        bubble.removeClass('noimage');
                    }

                    bubble.css('display', 'block');

                    // work out bubble position
                    var top = 20 + img.offset().top - bubble.height() / 2;
                    var left = 12 + img.offset().left - bubble.width();
                    bubble.css('top', top + 'px');
                    bubble.css('left', left + 'px');
                }).markerMouseOver(function(evt, marker) {
                    var icon = marker.getIcon().image.replace('.png', '_on.png');
                    marker.setImage(icon);
                }).markerMouseOut(function(evt, marker) {
                    var icon = marker.getIcon().image.replace('_on.png', '.png');
                    marker.setImage(icon);
                });
            }
        });
    }
};

var textBox = {
    focus: function(id, defaultText) {
        var tb = $('#' + id);
        if(tb.val() == defaultText) {
            tb.removeClass('default');
            tb.val('');
        }
        tb.addClass('focus');
    },
    
    blur: function(id, defaultText) {
        var tb = $('#' + id);
        if(tb.val() == '') {
            tb.addClass('default');
            tb.val(defaultText);
        }
        tb.removeClass('focus');
    }
};

var findUs = { 
    start: function() {
        var el = $('.findus');
        if(el.length > 0) {
            el.find('input').each(function(i, el) {
                var link = $(el).siblings('a')[0];
                link.oldhref = link.href;
                link.href = link.oldhref + '&saddr=' + el.value.replace(' ', '+');
            }).keydown(function(e) {
                if(e.keyCode == 13) {
                    e.preventDefault();
                    return false;
                }
            }).keyup(function(e) {
                var link = $(this).siblings('a')[0];
                link.href = link.oldhref + '&saddr=' + this.value.replace(' ', '+');
            });
        }
    }
};

var formMessage = {
    mouseOver: function(id) {
        // add hover behaviour for IE6
        if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
            var el = jQuery(jQuery('#' + id).parents('.fg-tooltip')[0]);
            el.css('display', 'block');
        }
    },

    mouseOut: function(id) {
        // add hover behaviour for IE6
        if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
            var el = jQuery(jQuery('#' + id).parents('.fg-tooltip')[0]);
            el.css('display', 'none');
        }
    }
};

var events = {
    hidden: false,

    start: function() {
        // booking
        /*var el = $('#eventbooking');
        if (el.length > 0) {
            var link = $(el.find('a')[0]);
            var div = $(el.find('div')[0]);
            var height = div.height();
            div.css('display', 'none');
            link.click(function(e) {
                div.css({
                    display: 'block',
                    height: 0,
                    overflow: 'hidden',
                    opacity: 0
                });
                div.animate({ height: height + 'px' }, 100, 'linear', function() {
                    div.css('height', 'auto');
                    link.animate({ opacity: 0 }, 100, 'linear', function() {
                        link.css('visibility', 'hidden');
                    });
                    div.animate({ opacity: 1 }, 200, 'linear');
                });
            });
        }*/
    },

    ajaxStart: function(sender, eventArgs) {
        if (events.hidden) {
            events.hidden = false;
            return true;
        }

        // fix column height - todo: fix bug when getting bigger
        var c2 = $('#coltwo');
        c2.height(c2.height() + 'px');

        var target = eventArgs.get_eventTarget();
        var argument = eventArgs.get_eventArgument();
        var els = $('.listentry');
        if (els.length > 0) {
            var list = $('#centrelist2');

            events.fadeOutEvents(els, 0);
            list.animate({ 'height': 0 }, 200 + els.length * 200, 'easeInQuad', function() {
                events.hidden = true;
                sender.ajaxRequestWithTarget(target, argument);
            });

            eventArgs.set_cancel(true);
            return false;
        }
    },

    fadeOutEvents: function(entries, index) {
        if (index < entries.length) {
            var entry = $(entries[entries.length - index - 1]);
            entry.animate({
                opacity: '0'
            }, 200, 'linear', function() {
                index++;
                if (index < entries.length) {
                    events.fadeOutEvents(entries, index);
                }
            });
        }
    },

    ajaxFinish: function() {
        var els = $('.listentry');
        var list = $('#centrelist2');
        els.css('opacity', 0);
        list.css({
            position: 'absolute',
            height: 'auto'
        });
        var h = list.height();// +els.length * 5;
        list.css({
            height: '0px',
            position: 'inherit'
        });
        list.animate({
            height: h + 'px'
        }, 200 + els.length * 200, 'easeOutQuad', function() {
            list.css('height', 'auto');
        });
        events.fadeInEvents(els, 0);
    },

    fadeInEvents: function(entries, index) {
        if (index < entries.length) {
            var entry = $(entries[index]);
            entry.animate({
                opacity: '1'
            }, 200, 'linear', function() {
                index++;
                if (index < entries.length) {
                    events.fadeInEvents(entries, index);
                }
            });
        }
    }
};

var iceAgeObjects = {
    narrowOpen: false,
    topicsOpen: false,

    start: function() {
        // search bar
        if ($('#objectsearch').length > 0) {
            // red buttons
            var buttons = $('#objectsearch .btn_objnarrow, #objectsearch .btn_objtopics');
            buttons.click(function(e) {
                var link = $(this);
                var block = $(this).attr('class') == 'btn_objnarrow' ? $('#objnarrow') : $('#objtopics');

                block.css({
                    display: 'block',
                    marginTop: '-39px',
                    opacity: 0
                });

                if ($(this).attr('class') == 'btn_objnarrow') {
                    if (iceAgeObjects.topicsOpen) // topics already open
                        $('#objtopics').css('marginLeft', '20px');
                    // opening narrow
                    iceAgeObjects.narrowOpen = true;

                    // narrow down accordion
                    $('#objnarrow ul').accordion({
                        collapsible: true,
                        active: false
                    });
                } else {
                    // opening topics
                    if (!iceAgeObjects.narrowOpen) // topics opened before narrow
                        block.css('marginLeft', '308px');
                    iceAgeObjects.topicsOpen = true;
                }

                link.animate({ 'opacity': 0 }, 400, 'linear', function() {
                    link.css('visibility', 'hidden');
                });
                block.animate({ 'opacity': 1 }, 400);
                return false;
            });

            // if exanded setup accordion
            if ($('#objectsearch .expanded').length > 0) {
                // narrow down accordion
                $('#objnarrow ul').accordion({
                    collapsible: true,
                    active: false
                });
            }
        }

        // glossary tooltips
        var glossary = $('.glossary a');
        if (glossary.length > 0) {
            glossary.tooltip({
                track: true,
                delay: 0,
                showURL: false,
                showBody: " - ",
                fade: 250
            }).click(function(e) { return false; });
        };
    },

    museumDrop1SelectedIndexChanged: function(sender, eventArgs) {
        // data set for second drop
        var data = [
            { title: 'Bolton Museum and Art Gallery', data: ["Rooke Pennington"] },
            { title: 'Brewhouse Yard Museum, Nottingham', data: ["Mello"] },
            { title: 'Buxton Museum and Art Gallery', data: ["Jackson", "Dawkins"] },
            { title: 'Cambridge University Museum of Archaeology and Anthropology', data: ["McBurney"] },
            { title: 'Cliffe Castle Museum, Keighley', data: ["Mello"] },
            { title: 'Creswell Crags Museum and Education Centre', data: ["Campbell", "Gwynne-Griffiths", "Armstrong"] },
            { title: 'Derby Museum and Art Gallery', data: ["T.Heath and J.M. Mello"] },
            { title: 'Nottingham Natural History Museum', data: ["Nottingham"] },
            { title: 'Oxford University Museum of Natural History', data: ["Mullins"] },
            { title: 'Private Possession', data: ["Private possession"] },
            { title: 'Sedgwick Museum, Cambridge', data: ["Duckworth"] },
            { title: 'Sheffield Galleries and Museums Trust', data: ["Hunter Archaeological Society", "Armstrong"] },
            { title: 'The British Museum', data: ["Armstrong", "Christy"] },
            { title: 'The Manchester Museum', data: ["Dawkins", "Armstrong"] },
            { title: 'The Natural History Museum', data: ["Christy", "Armstrong", "Mello"] },
        ];

        // get references to elements
        var li = $('#narrow_museums');
        var option = $('#narrow_museums .searchoption');
        var second = $('#narrow_museums .searchoptiondrop');
        var drop1 = $(li.find('.RadComboBox_CreswellHome')[0]);
        var drop2 = $(li.find('.RadComboBox_CreswellHome')[1]);
        var drop1obj = sender;
        var drop1txt = eventArgs.get_item().get_text();
        var drop2obj = $find(drop2.attr('id'));
        var drop2items = drop2obj.get_items();

        // show second drop
        second.css({
            display: 'block'
        });
        option.animate({ 'height': '82px' }, 200, 'linear');

        // get data item
        var dataitem;
        $(data).each(function(i, item) {
            if (item.title == drop1txt) {
                dataitem = item;
                return false;
            }
        });

        // load second drop data
        drop2obj.trackChanges();
        drop2items.clear();
        self.disableMuseumDrop2 = true;

        var comboItem = new Telerik.Web.UI.RadComboBoxItem();
        comboItem.set_text("Please select a collection...");
        drop2items.add(comboItem);
        comboItem.select();

        // set url for when second selection made
        var baseUrl = (document.location + '').toLowerCase();
        baseUrl = baseUrl.substring(0, baseUrl.indexOf('exhibition-objects') + 18);

        $(dataitem.data).each(function(i, item) {
            comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text(item);
            var url = '/Museum/' + drop1txt + '/' + item;
            url = baseUrl + url.replace(/,/g, '').replace(/ /g, '-');
            comboItem.set_value(url);
            drop2items.add(comboItem);
        });

        drop2obj.commitChanges();
        self.disableMuseumDrop2 = false;
    },

    museumDrop2SelectedIndexChanged: function(sender, eventArgs) {
        if (!self.disableMuseumDrop2) {
            var url = eventArgs.get_item().get_value();
            document.location = url;
        }
    },

    valleyDrop1SelectedIndexChanged: function(sender, eventArgs) {
        // data set for second drop
        var data = [
            { title: 'Anston Stones', data: ["Dead Man's Cave"] },
            { title: 'Burnhill Grips', data: ["Ash Tree Cave"] },
            { title: 'Creswell Crags', data: ["Church Hole", "Dog Hole Fissure", "Mother Grundy's Parlour", "Pin Hole", "Robin Hood Cave"] },
            { title: 'Elmton and Whaley Valley', data: ["Whaley Rock Shelter", "Elmton Village"] },
            { title: 'Langwith Valley', data: ["Langwith Basset Cave"] },
            { title: 'Steetley Quarry', data: ["Steetley Quarry"] },
            { title: 'Whaley Valley', data: ["Whaley Rock Shelter No2"] }
        ];

        // get references to elements
        var li = $('#narrow_sites');
        var option = $('#narrow_sites .searchoption');
        var second = $('#narrow_sites .searchoptiondrop');
        var drop1 = $(li.find('.RadComboBox_CreswellHome')[0]);
        var drop2 = $(li.find('.RadComboBox_CreswellHome')[1]);
        var drop1obj = sender;
        var drop1txt = eventArgs.get_item().get_text();
        var drop2obj = $find(drop2.attr('id'));
        var drop2items = drop2obj.get_items();

        // show second drop
        second.css({
            display: 'block'
        });
        option.animate({ 'height': '82px' }, 200, 'linear');

        // get data item
        var dataitem;
        $(data).each(function(i, item) {
            if (item.title == drop1txt) {
                dataitem = item;
                return false;
            }
        });

        // load second drop data
        drop2obj.trackChanges();
        drop2items.clear();
        self.disableValleyDrop2 = true;

        var comboItem = new Telerik.Web.UI.RadComboBoxItem();
        comboItem.set_text("Please select a cave...");
        drop2items.add(comboItem);
        comboItem.select();

        // set url for when second selection made
        var baseUrl = (document.location + '').toLowerCase();
        baseUrl = baseUrl.substring(0, baseUrl.indexOf('exhibition-objects') + 18);

        $(dataitem.data).each(function(i, item) {
            comboItem = new Telerik.Web.UI.RadComboBoxItem();
            comboItem.set_text(item);
            var url = '/Site/' + drop1txt + '/' + item;
            url = baseUrl + url.replace(/,/g, '').replace(/ /g, '-').replace(/\'/, '');
            comboItem.set_value(url);
            drop2items.add(comboItem);
        });

        drop2obj.commitChanges();
        self.disableValleyDrop2 = false;
    },

    valleyDrop2SelectedIndexChanged: function(sender, eventArgs) {
        if (!self.disableValleyDrop2) {
            var url = eventArgs.get_item().get_value();
            document.location = url;
        }
    }
};

var lightBox = {
    start: function () {
        $('a[rel="lightbox"]').lightBox();
        $('.sf_photoListLightbox a').lightBox();
    }
};

var emailHyperLink = {
    start: function() {
        $('.ehl').each(function(i, el) {
            el = $(el);
            var href = 'mailto:' + el.attr('href').replace('mailto:', '').split('').reverse().join('');
            var text = el.text().split('').reverse().join('');
            el.attr('href', href);
            el.text(text);
            el.removeClass('ehl');
        });
    }
};

$(site.start);
