$(function () {
    var currentURL = window.location;
    var baseURL = "http://www.lcvs.org.uk";
    jQuery.expr[':'].Contains = function (a, i, m) { return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; };

    
    $(".calendar_container *").hide();
    $(".calendar").parent().parent().parent().parent().removeAttr("style").wrap("<div class=\"calendar_container\"></div>");
    //Body Load
    if ((BrowserDetect.browser == "Explorer") && (BrowserDetect.version <= "8")) { } else {
        $(".Phuse_PanelContainingDiv td").each(function () { $(this).css("width", $(this).parent().parent().parent().attr("width") + "px"); });
    }
    if ($.cookie("font_size")) {
        $("#font_size").attr("href", baseURL + "/res/css/" + $.cookie("font_size") + ".css");
    }
    if ($.cookie("accessibility")) {
        $("#accessibility").attr("href", baseURL + "/res/css/" + $.cookie("accessibility") + ".css");
    }
    if ($.cookie("accessibility") == "high_contrast") {
        $("#high_contrast").text("Normal Contrast").attr("id", "normal_contrast");
    } else if ($.cookie("accessibility") == "text_only") {
        $("#high_contrast").text("Reset Graphics").attr("id", "reset_graphics");
    }
    if (currentURL.toString().indexOf("my-lcvs") > 0) { $("#mylcvs").addClass("mylcvs"); }
    if ($("#lblLink").text() == "") { $("#download_header,#download_list").remove(); }
    $(".MMDataCaptureForm_SubmitButton").val("");
    //$("#container").css("height",$(document).height()+"px");
    $("#breadcrumb").each(function () {
        var charReplace = $(this).html();
        charReplace = charReplace.replace("&gt;", "/");
        charReplace = charReplace.replace("&gt;", "/");
        charReplace = charReplace.replace("&gt;", "/");
        charReplace = charReplace.replace("&gt;", "/");
        charReplace = charReplace.replace("&gt;", "/");
        $(this).html(charReplace);
    });
    $(".MMDataCaptureForm_Table").not("#contact_form .MMDataCaptureForm_Table").each(function () {
        var thisTable = $(this);
        var thisError = $(".MMDataCaptureForm_ErrorTD").text();
        if (thisError != "") { $("#C21").html("<p class='error'>" + thisError + "</p>"); }
        thisTable.hide().after('<div class="mm_form"></div>');
        var inputs = new Array();
        for (var i = 0; i < $(".MMDataCaptureForm_FieldTD", thisTable).length; i++) {
            inputs[i] = "<div class=\"MMDataCaptureForm_FieldTD\">" + $(".MMDataCaptureForm_FieldTD:eq(" + i + ")", thisTable).html() + "</div>";
        }
        var result = inputs.toString().replace(",", "").replace(",", "");
        $(".mm_form").html(result + " " + $(".MMDataCaptureForm_ButtonTD", thisTable).html());
        thisTable.remove();
    });
    //$(".full_news_items:first,.full_events_items:first").hide();
    $(".full_news_container").pagination();
    $(".full_news_container").each(function () {
       var pagingItems1 = Math.ceil(eval($(".full_news_items").length - 5) / 5);
       var countItems = 1;
       while (countItems <= pagingItems1 + 1) {
            $(" .paging").append("<span id=\"" + countItems + "\">" + countItems + "</span>");
           countItems++;
        }
        $(".paging span:first").addClass("active");
        $(".paging span").click(function () {
          $(".paging span").removeClass("active");
          $(this).addClass("active");
          var pageNumber = $(this).attr("id") - 1;
          $("#lblListEvents").animate({ "top": "-" + eval(pageNumber * 700) + "px" });
        });
    });

  //$(".latest_news_items:first,.full_events_items:first").hide();
  $(".latest_news_container").pagination();
  $(".latest_news_container").each(function () {
      var pagingItems1 = Math.ceil(eval($(".full_news_items").length - 5) / 5);
      var countItems = 1;
      while (countItems <= pagingItems1 + 1) {
          $(" .paging").append("<span id=\"" + countItems + "\">" + countItems + "</span>");
          countItems++;
      }
      $(".paging span:first").addClass("active");
      $(".paging span").click(function () {
          $(".paging span").removeClass("active");
          $(this).addClass("active");
          var pageNumber = $(this).attr("id") - 1;
          $("#lblListEvents").animate({ "top": "-" + eval(pageNumber * 700) + "px" });
      });
  });

    $("#contact_form .MMDataCaptureForm_Table").vertTable();

    $(".calendar_container *").removeAttr("style").removeAttr("bgcolor");
    $(".calendar tr").each(function () {
        $("td:first", this).css("border-left", "0px");
        $("td:last", this).css("border-right", "0px");
    });
    $(".calendar_container table:eq(0) tr:gt(2)").addClass("days");
    $(".days").each(function () {
        $("td:first", this).css("border-left", "1px solid #d7dcdd");
    });
    $(".days:last td").css("border-bottom", "1px solid #d7dcdd");
    $(".calendar_othermonth").empty();
    if ($("#contact_form .content").html() == "") { $("#contact_form").remove(); }

    $(".event_results .search_result_item a").hide();
    $(".event_results .search_result_item a[href^='" + baseURL + "/events/']").show();
    $(".news_results .search_result_item a").hide();
    $(".news_results .search_result_item a[href^='" + baseURL + "/news/']").show();
    $(".item_filter").each(function () {
        var thisObj = $(this);
        $("li", thisObj).each(function () {
            $(this).replaceWith("<option value='" + $(this).text() + "'>" + $(this).text() + "</option>");
        });
        $("ul", thisObj).replaceWith("<select id='category_filter'><option value='all'>Choose a category</option>" + thisObj.html() + "</select>");
    });
    if ($("#search_check").text() == "") { $("#search_result_details").hide(); }

    //User Interaction
    $(".with_event").hover(function () {
        $(this).next("div").fadeIn();
    }, function () {
        $(this).next("div").fadeOut();
    });
    $("#category_filter").change(function () {
        var thisCategory = $("option:selected", this).val();
        $(".full_news_items").hide();
        $(".full_news_items .item_category:Contains('" + thisCategory.slice(0, -1) + "')").parent().parent().fadeIn();
        if (thisCategory == "all") { $(".full_news_items").fadeIn(); }
    });
    $(".proposition,.download,.event_list_item").fullLink();
    $("#social_bookmarks ul li a").hide().live("click", function () { return false; });
    $("#delicious").live("click", function () { location.href = "http://stumbleupon.com/submit?url=" + currentURL; });
    $("#digg").live("click", function () { location.href = "http://digg.com/submit?url=" + currentURL; });
    $("#facebook").live("click", function () { location.href = "http://facebook.com/sharer.php?u=" + currentURL; });
    $("#reddit").live("click", function () { location.href = "http://reddit.com/submit?url=" + currentURL; });
    $("#stumble").live("click", function () { location.href = "http://stumbleupon.com/submit?url=" + currentURL; });
    $("#twitter").live("click", function () { location.href = "http://twitter.com/home?status=Currently reading " + currentURL; });
    $("#email").attr("href", "").live("click", function () { location.href = "mailto:?subject=LCVS | United Way&body=" + currentURL; return false; });
    $("#small_text").live("click", function () {
        $.cookie("font_size", null, { path: "/" });
        $("#font_size").attr("href", baseURL + "/res/css/small.css");
        $.cookie("font_size", "small", { path: "/" });
    });
    $("#normal_text").live("click", function () {
        $.cookie("font_size", null, { path: "/" });
        $("#font_size").attr("href", "");
    });
    $("#large_text").live("click", function () {
        $.cookie("font_size", null, { path: "/" });
        $("#font_size").attr("href", baseURL + "/res/css/large.css");
        $.cookie("font_size", "large", { path: "/" });
    });
    $("#high_contrast").live("click", function () {
        $.cookie("accessibility", null, { path: "/" });
        $("#accessibility").attr("href", baseURL + "/res/css/high_contrast.css");
        $.cookie("accessibility", "high_contrast", { path: "/" });
        $(this).text("Normal Contrast").attr("id", "normal_contrast");
        $("#text_only").text("Text Only").attr("id", "text_only");
    });
    $("#normal_contrast").live("click", function () {
        $.cookie("accessibility", null, { path: "/" });
        $("#accessibility").attr("href", "");
        $(this).text("High Contrast").attr("id", "high_contrast");
    });
    $("#text_only").live("click", function () {
        $.cookie("accessibility", null, { path: "/" });
        $("#accessibility").attr("href", baseURL + "/res/css/text_only.css");
        $.cookie("accessibility", "text_only", { path: "/" });
        $(this).text("Reset Graphics").attr("id", "reset_graphics");
        $("#high_contrast").text("High Contrast").attr("id", "high_contrast");
    });
    $("#reset_graphics").live("click", function () {
        $.cookie("accessibility", null, { path: "/" });
        $("#accessibility").attr("href", "");
        $(this).text("Text Only").attr("id", "text_only");
    });
    $("#txtEmailAddress,#txtNewBalance,#txtSearch").each(function () {
        var thisVal = $(this).val();
        $(this).bind("focus", function () {
            if ($(this).val() == thisVal) { $(this).val(""); }
        }).bind("blur", function () {
            if ($(this).val() == "") { $(this).val(thisVal); }
        });
    });

    $(".Phuse_Breadcrumb_Link").each(function () {
        var Phuse_Breadcrumb_Link = $(this).attr("href");
        $(this).attr("href", "" + Phuse_Breadcrumb_Link + "default.phuse");
    });

    $(".calendar_container").fadeIn();
    $(".calendar").fadeIn();
    $(".calendar_othermonth").fadeIn();
    $(".calendar_weekday").fadeIn();
    $(".calendar_day").fadeIn();
    $(".calendar_weekendday").fadeIn();

    $(".paging span:nth-child(1)").addClass("page_show");
    $(".paging span:nth-child(2)").addClass("page_show");
    $(".paging span:nth-child(3)").addClass("page_show");
    $(".paging span:nth-child(4)").addClass("page_show");
    $(".paging span:nth-child(5)").addClass("page_show");
    $(".paging span:nth-child(6)").addClass("page_show");
    $(".paging span:nth-child(7)").addClass("page_show");
    $(".paging span:nth-child(8)").addClass("page_show");
    $(".paging span:nth-child(9)").addClass("page_show");
    $(".paging span:nth-child(10)").addClass("page_show");
    $(".paging span:nth-child(11)").addClass("page_show");
    $(".paging span:nth-child(12)").addClass("page_show");
    $(".paging span:nth-child(13)").addClass("page_show");
    $(".paging span:nth-child(14)").addClass("page_show");
    $(".paging span:nth-child(15)").addClass("page_show");
    $(".paging span:nth-child(16)").addClass("page_show");
});
