﻿$(document).ready(function () {
    if ($("#twitter-feed").length > 0) {
        $("#twitter-feed").TwitterFeed("__adm/twitter.ashx");
    }

    if ($("#news-items").length > 0) {
        $("#news-items").newsPaging();
    }

    if ($(".split-data").length > 0) {
        $(".split-data").tabLinks();
    }

    $(".big-button").wrapInner('<span><span></span></span>');




    $(".textbox-background,.transparent").css("opacity", "0.6");

    $(".sign-in-open").click(function (e) {
        e.preventDefault();

        addModalBackground();

        $("#sign-in").css("margin-left", "-209px");
        $("#sign-in").css("margin-top", "-150px");

        OpenModal();

        setTimeout(function () {
            $("#sign-in").show();
            $("#sign-in input[type=text]").focus();
        }, 300);
    });

    $(".cLink").live("click", function () {
        var id = $(this).attr("id").substring(3);
        $.post("__adm/cnt.ashx", { id: id }, function (txt) { });
    });

    if ($(".cStatic").length == 1) {
        var id = $(".cStatic:first").attr("id").substring(3);
        $.post("__adm/cnt.ashx", { id: id }, function (txt) { });        
    }

    $(".close-sign-in").click(function (e) {
        e.preventDefault();
        $("#sign-in").hide();
        $("#sign-in input[type=text],#sign-in input[type=text]").val("");
        CloseModal();
    });

    if ($("#article-image-holder").length > 0) {
        $("#article-image-holder").articleImages();
    }

    $("#main #col-rhs .section .archive-cntl ul li a.year").click(function (e) {
        e.preventDefault();
        var el = $(this),
            ul = el.parent().find("ul"),
            li = ul.find("li:first");

        if (li.is(":visible")) {
            ul.find("li").css("display", "none");
            el.parent().removeClass("selected");
        }
        else {
            ul.find("li").css("display", "block");
            el.parent().addClass("selected");
        }

    });

    var path = location.href;

    if (path.indexOf("#comments") > -1) {
        var scrollElement = $("#comments-section");
        if (scrollElement.length == 1) {
            setTimeout(function () {
                var scrollPos = scrollElement.offset().top;
                $('html,body')
                .animate({ scrollTop: scrollPos }, 600);
            }, 200);
        }
    }

    $("#comments-section a.post").click(function (e) {
        e.preventDefault();
        var el = $(this), comment = $("#add-comment textarea").val();

        if (comment != "") {
            $.post("__adm/postcomment.ashx", { comment: comment, id: el.attr("id") }, function (txt) {
                if (txt != "error") {
                    var div = $(txt);
                    div.insertBefore(".leave-comment");
                    div.show();

                    var h = $("#comments-section h5:first");

                    if (h.is(":visible")) {
                        var current = parseInt(h.find("span:first").text());
                        if (current == 1) {
                            h.find("span:last").text("s").show();
                        }

                        h.find("span:first").text(current + 1);
                    }
                    else {
                        h.show();
                    }

                    $("#add-comment textarea").val("");
                }
            });
        }
    });

    $("#comments-section .remove-item").live("mouseover", function () {
        $(this).addClass("hover");
    });

    $("#comments-section .remove-item").live("mouseout", function () {
        $(this).removeClass("hover");
    });

    $("#comments-section a.delete").live("click", function (e) {
        e.preventDefault();
        var el = $(this);
        $.post("__adm/postcomment.ashx", { id: el.attr("id") }, function (txt) {
            if (txt != "error") {
                el.parent().remove();
                var h = $("#comments-section h5:first"),
                    count = parseInt($("span:first", h).text());
                count--;

                if (count <= 0) {
                    h.hide();
                    h.find("span:first").text("1");
                    h.find("span:last").hide();
                }
                else if (count == 1) {
                    h.find("span:first").text("1");
                    h.find("span:last").hide();
                }
                else {
                    h.find("span:first").text(count);
                }
            }
        });
    });

    // polls
    $(".poll-button a:not(a.blue)").click(function (e) {
        e.preventDefault();
        var el = $(this);
        var q = el.parent().parent();
        var id = $("input[type=radio]:checked", q).val();

        if (id > -1) {

            el.hide();
            $.post("__adm/pollvote.ashx", { id: id, w: q.width() }, function (txt) {
                $(".options", q).remove();
                $(".results", q).html($(txt));
            });
        }
    });


    var initial = "Search Arsenal Land Articles";
    $("#searchBox input[type=text]").val(initial);

    $("#searchBox input[type=text]").focus(function () {
        var st = $(this);
        if (st.val() == initial) {
            st.val("");
            st.addClass("selected");
        }
    });

    $("#searchBox input[type=text]").blur(function () {
        var st = $(this);
        if (st.val() == "") {
            st.val(initial);
            st.removeClass("selected");
        }
    });

    $("#searchBox input[type=text]").keyup(function (e) {

        var val = $(this).val();
        if (e.keyCode == 13) {
            e.preventDefault();
            if (val != "" && val != initial) {
                window.location = $("base").attr("href") + "search/?st=" + val;
            }
            else {
                window.location = $("base").attr("href") + "search/";
            }
        }
    });

    $("#searchBox a").click(function (e) {
        e.preventDefault();
        var val = $("#searchBox input[type=text]").val();
        if (val != "" && val != initial) {
            window.location = $("base").attr("href") + "search/?st=" + val;
        }
        else {
            window.location = $("base").attr("href") + "search/";
        }

    });









    function addModalBackground() {
        if ($("body #modal-background").length == 0) {
            $("body").append($("<div id=\"modal-background\"/>"));
            // get the window size
            bgResize();
            // if the user changes the window size, then the BG must change
            $(window).bind("resize.backgroundScale", function () {
                bgResize();
            })
        }
    }

    // open function
    function OpenModal() {
        $("body #modal-background").fadeTo(250, 0.6);
    }
    // close function
    function CloseModal() {
        $("body #modal-background").fadeOut(250);
    }

    // gte the BG size
    function bgResize() {

        var bg = $("body #modal-background"),
            _docHeight = 0,
            _windowHeight = 0,
            theHeight = 0;

        _docHeight = document.height || document.body.offsetHeight;
        _windowHeight = document.documentElement.clientHeight
        if (_docHeight > _windowHeight) {
            theHeight = _docHeight;
        }
        else {
            theHeight = _windowHeight;
        }

        if ($.browser.msie && $.browser.version == "6.0") {
            var myWidth = 0, myHeight = 0;
            if (typeof (window.innerWidth) == 'number') {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
            }
            bg.css("height", (myHeight) + "px");
        }
        else {
            bg.css("height", (theHeight + 15) + "px");
        }
    }

});

$.fn.tabLinks = function () {
    return this.each(function () {
        var holder = $(this),
            uls = $(".split-data-item", holder),
            div = $("<div class=\"split-data-links\"/>"),
            h3 = $(".title h3", holder);

        var a = $("<a href=\"#\"/>");

        var count = 0;

        uls.each(function () {
            var b = a.clone();
            b.text($(this).attr("title"));

            b.bind("click", function (e) {
                e.preventDefault();
                var el = $(this);
                var id = $(el, div).index();
                displayUL(id);
            });

            div.append(b);

            if (count < uls.length-1) {
                div.append(" | ");
            }

            count++;
        });

        div.insertBefore($(".split-data-item:first", holder)).show();

        displayUL(0);

        function displayUL(pn) {
            var elToDisplay = uls.eq(pn);
            h3.text(elToDisplay.attr("title"));
            uls.hide();
            $("a", div).removeClass("selected");
            $("a", div).eq(pn).addClass("selected");
            elToDisplay.show();

        }
    })
}
