﻿$(document).ready(function() {

    // bodge to remove empty child menus
    $("ul.subMenu:empty").hide();

    // initialize scrollable with mousewheel support
    $("#homeFeature .scrollable").scrollable({ vertical: true, mousewheel: false, circular: true }).navigator().autoscroll(7000);

    // Highlight the first item in the home feature box
    $("#homeFeature li:first").addClass("active");

    // region tooltip
    //    $("a[rel]").overlay({ effect: 'apple' });
    $("#changeregion").tooltip({ effect: 'slide', position: "bottom center" });
    $("#changecookie").tooltip({ effect: 'slide', position: "bottom center" });

    // unhighlighted home link and add class
    $("nav#tabs li:first a").html("&nbsp;&nbsp;").attr("accesskey", "1")
    $("nav#tabs li:first").removeClass("selected").addClass("home");

    // swap image
    // usage: <img src="[PATH]" class="swapimage" data-swapimage="[PATH]" />
    $(".swapimage").hover(
		function() {
		    originalImage = ($(this).attr("src"));
		    $(this).attr("src", $(this).data("swapimage"));
		},
		function() {
		    $(this).attr("src", originalImage);
		}
    );

    // swap image
    // usage: <a class="hovertext {hovertext:'...';orignaltext:'...'}" />
    $(".hovertext").hover(
		function() {
		    $(this).text($(this).data("hovertext").replace("<p>", "").replace("</p>", "").replace("<br>", "").replace("<br />", ""));
		},
		function() {
		    $(this).text($(this).data("originaltext"));
		}
    );

    // the adx editor will use class="quote" for quote content
    // we need to add a div to display the second bg image
    $(".quote").each(function(index) {
        var html = $(this).html();
        $(this).html("<div>" + html + "</div>");
    });

    // If there's 2 pagers then space them out
    $(".paging:eq(1)").addClass("pagingLower");

    // hr styling is rubbish so replace with div
    $(".membership_panel hr").replaceWith("<div class=\"line\"></div>");


    var searchWaterMarkText = "Search";

    $("#search .textbox").val(searchWaterMarkText).focus(function() {
        if ($(this).val() == searchWaterMarkText) {
            $(this).val("");
        }
    }).blur(function() {
        if ($(this).val() == "") {
            $(this).val(searchWaterMarkText);
        }
    });

    // Gallery lightbox
    $("a[rel*=lightbox]").lightbox();

    // datepicker from JQuery UI
    if ($("input.date").length) {
        $("input.date").mask("99/99/9999").datepicker({
            dateFormat: "dd/mm/yy",
            showOn: "button",
            buttonImage: "/Library/Images/calendar.png",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true,
            yearRange: '-80:+2'
        });
    }

    // http://updatepanel.net/2009/04/19/jquery-aspnet-validator-callout-plugin/
    $.updnValidatorCallout.attachAll({
        calloutCssClass: "updnValidatorCallout",
        pointerCssClass: "updnValidatorCalloutPointer",
        errorInputCssClass: "updnValidationErrorInput",
        errorLabelCssClass: "updnValidationErrorLabel",
        my: "left top",
        at: "right top",
        collision: "fit fit",
        offset: "10 -3"
    });

    // Add stripes to tables
    $("table.mpstable tbody tr:even:has(td)").addClass("alt");

    // member check enter to submit
    $(".membercheck .textbox").keydown(function(event) {
        if (event.keyCode == '13') {
            $(".membercheck .button").click();
            return false;
        }
    });

    // member check service
    $(".membercheck .button").click(function() {
        var url = $(this).data("url");
        var guid = $(this).data("guid");
        var membernumber = $(".memno" + guid).val();

        $.ajax({
            type: "POST",
            url: "/Services/MemberCheck.asmx/IsValid",
            data: "{'membershipNumber':'" + membernumber + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                if (msg.d) {
                    loadPage(url);
                }
                else {
                    $(".error" + guid).fadeIn().delay(10000).fadeOut('slow');
                }
            },
            error: function(xhr) {
                // do nothing
            }
        });
    });

    // don't submit if box says Search
    $("#search .imagebutton").click(function() {
        if ($("#search .textbox").val() == "Search") {
            return false;
        }
    });

    // search highlight
    //    var searchTerm = $(".searchForm .textbox").val();
    //    $("ul.searchResults li").highlight(searchTerm);

    // expandable content
    $("h2.expand").prepend("<span class=\"ui-icon ui-icon-circle-plus\"></span>").click(function() {
        $(this).next("p").slideToggle(500);
        var html = $(this).html();
        if (html.indexOf("plus") != -1) {
            $(this).html($(this).html().replace("plus", "minus"));
        }
        else {
            $(this).html($(this).html().replace("minus", "plus"));
        }
    });

    // adds google adsense to links
    $(".AdxAttachment a, .AdxAttachmentLink").click(function() {
        var href = $(this).attr("href");
        var lastequalpos = href.lastIndexOf("=");
        var filename = href.substring(lastequalpos + 1);
        _gaq.push(['_trackPageview', filename]);
    });

    // adds the icon to external links
    $('.usefulLinks a[href^="http"]:not([href*="medicalprotection.org"])')
        .not($("div.footerLinks a"))
        .not($("div.social a"))
        .each(function() {
            $(this).append("<a href=\"" + $(this).attr("href") + "\" target=\"_blank\"><img src=\"/library/images/external.png\" class=\"external\" title=\"(Opens new window)\" /></a>");
        });

});

$(function() {
    $("div.pod").addClass("iesux");
})

function loadPage(url) { window.location.href = url; }
