﻿



$(document).ready(function () {
    // header search events
    $("#search-button").click(function () {
        var param = "";
        if ($(this).attr("rel") == "gaelic") {
            param = "&zoom_cat=0";
        }
        SiteSearch(param);
    });
    $('#search-text').keypress(function (event) {
        if (event.keyCode == '13') {
            var param = "";
            if ($(this).attr("rel") == "gaelic") {
                param = "&zoom_cat=0";
            }
            return SiteSearch(param);
        }
    });

    // bind events to minimap
    if ($("#minimap").length > 0) {

        $("#minimap ul li a").each(
            function (intIndex) {

                // mouseover
                $(this).bind(
                "mouseenter",
                function () {
                    ShowMap(intIndex + 1);
                });

                // mouseout
                $(this).bind(
                "mouseleave",
                function () {
                    ResetMap();
                });

            }
        ) // end each
    } // end if


    if (isIE6()) {
        // hover for ie6    
        $("ul#main-nav li").hover(function () {
            $(this).addClass("hover");
        }, function () { $(this).removeClass("hover"); });

        $('#site').supersleight();
    }

    $("#map_link").fancybox({
        'type': 'swf',
        'swf': { wmode: 'transparent' },
        'href': '/themes/default/flash/pre-load.swf',
        'showCloseButton': true,
        'padding': 0,
        'width': 900,
        'height': 570
    });

    $("#dictionary-tab").fancybox({
        'type': 'iframe',
        'href': '/themes/default/iframe/dictionary.aspx',
        'showCloseButton': true,
        'width': 620,
        'height': 215
    });
    if ($.cookie('dictionary') == "session" || $.cookie('dictionary') == location.pathname) {
        buildDictionary(true);
    }

});

function getDictionaryCookieValue() {    
    return $.cookie('dictionary');
}

// ---------------------------
// function to prevent email being spidered
function noEspam(theName, theDomain, theLink, theExtras) {

    if (theLink.length == 0) {
        theLink = theName + '@' + theDomain
    }
    document.write('<a href="mailto:' + theName + '@' + theDomain + theExtras + '">' + theLink + '</a>');

}

// ---------------------------
// minimap display (on homepage)

function ShowMap(ii) {
    var offset = (ii * 290) * -1;
    $("#minimap").css("background-position", offset + "px 0px");
}

function ResetMap() {
    $("#minimap").css("background-position", "0 0");
}



// ---------------------------
// Site search functions
function SiteSearch(param) {

    var zoom_query = "";
    
    if ($('#search-text').val().length >= 3 && $('#search-text').val() != "Search this website") {        
        zoom_query = $('#search-text').val();
    }

    if (zoom_query.length >= 3) {        
        location.href = "/search/results.html?zoom_per_page=10&zoom_and=1&zoom_query=" + encodeURIComponent(zoom_query) + param;
    }

    return false;
}

// --------------------------------
// function to retrieve qs param (used in search)
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}


/*
* jQuery JavaScript plugin
*
* URLDecode/Encode
* (used in search)
*
* http://0061276.netsolhost.com/tony/testurl.html
*
*/

$.extend({ URLEncode: function (c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        }
    } return o;
},
    URLDecode: function (s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});


// ---------------------------
// Returns true if the browser is Internet Explorer, false otherwise.
function isIE6() {
    return ($.browser.msie && $.browser.version == 6);
}


// ---------------------------
// Video code to help show popup stuff
function showVideo(videoId) {

    showPop();
    $('#the-content').load("/common/handlers/get-video.ashx?videoId=" + videoId);

    return false;
}

function showPop() {
    $("#pop-overlay").css("height", $(document).height() + "px");
    $("#pop-overlay").css("width", $(document).width() + "px");

    var scrollTop = $(window).scrollTop();
    var marginTop = ($(window).height() - 480) / 2;
    $("#pop-holder").css("margin-top", (marginTop + scrollTop) + "px");

    var marginLeft = ($(window).width() - 725) / 2;
    $("#pop-holder").css("margin-left", marginLeft + "px");

    $('#pop-overlay').css('filter', 'alpha(opacity=85)');
    $('#pop-overlay').fadeIn(200, function () {
        $("#pop-holder").css("display", "block");
    });   
}

function insertPopDivs() {    
    var html = "<div id=\"pop-overlay\"></div>";
        html += "<div id=\"pop-holder\">";
            html += "<div id=\"pop-body\">";
            html += "<img src=\"/themes/default/images/icons/pop-close.gif\" alt=\"Close\" id=\"closePop\" />";
                html += "<div id=\"the-content\"></div>";
            html += "</div>";
    html += "</div>";
    $("body").prepend(html);
    
    $("#closePop").click(function () {

        $("#pop-holder").css("display", "none");
        $("#pop-overlay").fadeOut("fast");
    });
}

var popped = false;
function showInteractiveMap()
{
    // 900x570
    if (!popped)
        insertMapDivs();

    popped = true;

    $("#map-overlay").css("height", $(document).height() + "px");
    $("#map-overlay").css("width", $(document).width() + "px");

    var scrollTop = $(window).scrollTop();
    
    var marginTop = ($(window).height() - 570) / 2;
    $("#map-holder").css("margin-top", (marginTop + scrollTop) + "px");

    var marginLeft = ($(window).width() - 900) / 2;
    $("#map-holder").css("margin-left", marginLeft + "px");

    $('#map-overlay').css('filter', 'alpha(opacity=85)');
    $('#map-overlay').fadeIn(200, function () {
        $("#map-holder").css("display", "block");
    });    
}

function insertMapDivs() {
    var html = "<div id=\"map-overlay\"></div>";
        html += "<div id=\"map-holder\">";
            html += "<div id=\"map-body\">";
                html += "<div id=\"the-content\"></div>";
            html += "</div>";
    html += "</div>";
    $("body").prepend(html);
    
    swfobject.embedSWF("/themes/default/flash/pre-load.swf", "the-content", "900", "570", "9.0.0", false, {}, { wmode: "transparent" }, { id: "flashwidget" });
}

function hideMap() {
    hideFancyBox();
}

function hideFancyBox() {
    $.fancybox.close();
}

var dictionary_current = null;
var dictionary_timeout = null;
function showDefinition() {

    var $this = $(dictionary_current);
    $.getJSON("/common/handlers/dictionary.ashx?term=" + encodeURI($(dictionary_current).text()), function (data) {
        $("#dictionary-box .info-body").html(data.Definition);
        $("#dictionary-box").css("display", "block");
        var pos = $this.offset();
        $("#dictionary-box").css("left", (pos.left - 266) + "px");
        $("#dictionary-box").css("top", (pos.top - 8) + "px");
    });
}

function closeDictionary() {
    $("#dictionary-box").css("display", "none");
    dictionary_current = null;
    dictionary_timeout = null;
    clearTimeout(dictionary_timeout);
}

var dictionary_built = false;
function buildDictionary(show) {
    
    if (!dictionary_built && show) {
        var dictionary = eval($("#hfDictionary").val());
        $(dictionary).each(function (index, obj) {
            $(".primary").highlight(obj);
        });

        $("span.highlight").hover(function () {
            dictionary_current = this;
            dictionary_timeout = setTimeout(showDefinition, 500);
        }, function () {
            clearTimeout(dictionary_timeout); dictionary_timeout = null;
        });
        dictionary_built = true;
    }
    else if(dictionary_built && !show){
        $(".primary").removeHighlight();
        dictionary_built = false;
    }

}
