$(window).load(function () {
    $('p a.fnPopupLink').each(function () {
        var fnId = $(this).attr('href').substr(1);
        var fnSubStr = $('dt#' + fnId).next().html().toLowerCase().indexOf('class="back') - 4;
        // in IE7 jquery.html()strips quotes off class attributes
        if (! jQuery.support.htmlSerialize) {
            var fnSubStr = $('dt#' + fnId).next().html().toLowerCase().indexOf('class=back') - 3;
        }
        var fnLabel = "Footnote ";
        $(this).attr('title', '');
        if (fnId.substring(0, 3) == 'app') {
            fnLabel = " "
        };
        $(this).qtip({
            style: {
                width: 350,
                name: 'light',
                tip: 'topLeft',
                border: {
                    width: 3,
                    radius: 3
                }
            },
            position: {
                adjust: {
                    screen: true
                }
            },
            content: {
                text: "<h3>" + fnLabel + $('dt#' + fnId).html() + "</h3><div>" + $('dt#' + fnId).next().html().substring(0,(fnSubStr)) + "</div>"
            },
            show: {
                delay: 50, effect: {
                    type: 'grow', length: 400
                }
            },
            hide: {
                delay: 400, fixed: true, effect: {
                    length: 400
                }
            }
        });
    });
    
    $('dl dd a.hasMapIdx').each(function () {
        var locId = $(this).parents('dd:first').prev('dt').children('label').attr('id');
        $(this).attr('title', '');
        $(this).qtip({
            style: {
                width: 325,
                name: 'light',
                tip: 'topLeft',
                border: {
                    width: 3,
                    radius: 3
                }
            },
            position: {
                adjust: {
                    screen: true
                }
            },
            content: {
                url: '/popup_locIdx_' + locId + '.html'
            },
            show: {
                delay: 50, effect: {
                    type: 'grow', length: 400
                }
            },
            hide: {
                delay: 400, fixed: true, effect: {
                    length: 400
                }
            }
        });
    });
    
    $('div.hybridText p a.place, div.transText p a.place').each(function () {
        var locId = $(this).attr('id').substr(0, $(this).attr('id').indexOf('-'));
        //var locId = "blah";
        $(this).attr('title', '');
        $(this).qtip({
            style: {
                width: 325,
                name: 'light',
                tip: 'topLeft',
                border: {
                    width: 3,
                    radius: 3
                }
            },
            position: {
                adjust: {
                    screen: true
                }
            },
            content: {
                url: '/popup_loc_' + locId + '.html'
            },
            show: {
                delay: 50, effect: {
                    type: 'grow', length: 400
                }
            },
            hide: {
                delay: 400, fixed: true, effect: {
                    length: 400
                }
            }
        });
    });
    
    $('div.hybridText p a.person, div.transText p a.person').each(function () {
        var perId = $(this).attr('id').substr(0, $(this).attr('id').indexOf('-'));
        $(this).attr('title', '');
        $(this).qtip({
            style: {
                width: 325,
                name: 'light',
                tip: 'topLeft',
                border: {
                    width: 3,
                    radius: 3
                }
            },
            position: {
                adjust: {
                    screen: true
                }
            },
            content: {
                url: '/popup_pers_' + perId + '.html'
            },
            show: {
                delay: 50, effect: {
                    type: 'grow', length: 400
                }
            },
            hide: {
                delay: 400, fixed: true, effect: {
                    length: 400
                }
            }
        });
    });
});


$(document).ready(function () {
    var showAllText = 'Show all data';
    var hideAllText = 'Hide all data';
    
    $('#mainContent .resourceList a.g3').click(function () {
        if ($(this).text() == showAllText) {
            $(this).attr({
                title: hideAllText
            });
            $(this).text(hideAllText);
            $(this).addClass('s02');
            $(this).removeClass('s01');
            $("#mainContent .resourceList li.s01").addClass('s02');
            $("#mainContent .resourceList li.s01").removeClass('s01');
        } else {
            $(this).attr({
                title: showAllText
            });
            $(this).text(showAllText);
            $(this).addClass('s01');
            $(this).removeClass('s02');
            $("#mainContent .resourceList li.s02").addClass('s01');
            $("#mainContent .resourceList li.s02").removeClass('s02');
        }
    });
    
    $("#mainContent .resourceList ul.t01 li.s01 a.x01").click(function () {
        var $this = $(this);
        if ($this.parent('li').is('.s01')) {
            $this.text('Collapse');
            $this.attr({
                title: "Click to collapse extended data"
            });
            $this.parent('li').removeClass('s01');
            $this.parent('li').addClass('s02');
        } else {
            $this.text('Expand');
            $this.attr({
                title: "Click to expand extended data"
            });
            $this.parent('li').removeClass('s02');
            $this.parent('li').addClass('s01');
        }
        
        return false;
    });

    $('#selRange').change(function () {
        location.href = $('#selRange option:selected').val();
    });
    
    $('#membrane_selector').change(function () {
        location.href = $('#membrane_selector option:selected').val();
    });
    
    $('#date_selector').change(function () {
        location.href = $('#date_selector option:selected').val();
    });
    
});

