function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);

    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];

        if (elm.type == 'radio') {
            if (re.test(elm.id)) {
                elm.checked = false;
            }
        }
    }

    current.checked = true;
}


function AutoFill(selector) {
    $(selector).each(function() {
        var v = $("label[for='" + $(this).attr("id") + "']").hide().text();
        $(this).val(v).focus(function() {
            if ($(this).val() == v) {
                $(this).val("");
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).val(v);
            }
        });
    });
};

function ExpandClickArea(id) {
    $(id).each(function(i) {
        if ($(this).find("a").attr("href")) {
            $(this).hover(function() {
                $(this).css({ cursor: 'pointer' });
            }, function() {
                $(this).css({ cursor: 'default' });
            }).click(function() { window.location = $(this).find("a").attr("href"); });
        }
    });
}

function CreateSlider(div, steps) {
    $(div + " *").hide();
    $("<div class=\"slider-outer\"><div class=\"slider-selected\"></div><button value=\"1\" id=\"btnVoteNow\"></button></div>").prependTo(div);

    var sliderOuter = $(div + " .slider-outer");

    var minX = 0;
    var maxX = $(div + " .slider-outer").width();
    var stepDistance = Math.round(maxX / steps);

    MoveSlider($(div + " input[type=\"text\"]").val() * stepDistance);

    sliderOuter.one("mousedown", function(e) {
        $("body").one("mouseup", function(e) {
            sliderOuter.unbind("mousemove");
            var rating = Math.round(GetSliderPos() / stepDistance);
            var hotOrNotContentId = $(".rating input[type=\"hidden\"]").val();

            $(div + " input[type=\"text\"]").val(rating);
            $(div + " p").show().text("You rated " + rating + "/10! " + $(div + " p").text());


            $.ajax({
                type: "POST",
                url: "http://" + window.location.host + "/api/HotOrNot.aspx/Vote",
                data: "{'hotOrNotContentId' : " + hotOrNotContentId + ", 'vote' : " + rating + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) { },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert("Error: " + xhr.status + "\n" + xhr.responseText);
                }
            });
        });
        MoveSlider(CalculateSliderPos(e, this));
        $(this).mousemove(function(e) { MoveSlider(CalculateSliderPos(e, this)); });
    });

    function CalculateSliderPos(e, container) {
        var x = e.pageX - container.offsetLeft;
        if (x % stepDistance != 0) x = Math.round(x / stepDistance) * stepDistance;
        if (x >= minX && x <= maxX && x % stepDistance == 0) { return x; }
    };

    function GetSliderPos() {
        return $(div + " button")[0].offsetLeft + 3;
    };

    function MoveSlider(x) {
        $(div + " .slider-selected").css("width", x + "px");
        $(div + " button").css("left", (x - 3) + "px");
    };
};

function keypressSubmit(me, e, submitButtonId) {
    try {
        if (e.keyCode == 13) {
            $("#" + submitButtonId).click();
            return false;
        }
    } catch (e) { }
}

function Trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function Omniture_CleanPageName(str) {
    return str.replace(/\s+/g, "-").replace(/\'/g, "").toLowerCase();
}

function trim(str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

function loading(finished, button, loading) {
    if (finished) {
        // show the button and hide the loading icon
        button.style.display = "inline";
        loading.style.display = "none";
    }
    else {
        if (Page_IsValid) {
            // hide the button and display the loading icon
            button.style.display = "none";
            loading.style.display = "inline";
        }
    }
}

function setBackground(section, subsection, bgStyle, clearFooterBg, startDate, endDate, previewCode) {
    var preview = false;
    var now = new Date();
    var skinCode = (typeof (spacQS) == "undefined") ? null : spacQS.getQueryStringParam("skin");
       
    section = (typeof(section) == "undefined" || section == null) ? "" : section;
    subsection = (typeof (subsection) == "undefined" || subsection == null) ? "" : subsection;
    clearFooterBg = (typeof(clearFooterBg) == "undefined") ? true : clearFooterBg;
    startDate = (typeof (startDate) == "undefined" || startDate == null) ? null : startDate;
    endDate = (typeof (endDate) == "undefined" || endDate == null) ? null : endDate;
    previewCode = (typeof (previewCode) == "undefined" || previewCode == null) ? "" : previewCode.toLowerCase();
    
    _nmsnSection = (typeof (_nmsnSection) == "undefined") ? "" : _nmsnSection;
    _nmsnSubSection = (typeof (_nmsnSubSection) == "undefined") ? "" : _nmsnSubSection;

    skinCode = (skinCode == null) ? null : skinCode.toLowerCase();
    preview = (skinCode == previewCode)

    if (preview || ((startDate == null || startDate <= now) && (endDate == null || endDate >= now))) {
        if ((section == _nmsnSection || section.length == 0) && (subsection == _nmsnSubSection || subsection.length == 0)) {           
            if (clearFooterBg)
                window.onload = clearFooterBackground;

            // set the background
            document.body.style.background = bgStyle;
        }
    }
}

function clearFooterBackground() {
    // clear the footers background
    if (document.getElementById("footer_container") != null)
        document.getElementById("footer_container").style.background = "none";
}

function loadScript(fullUrl) {
    var e = document.createElement("script");
    e.src = fullUrl;
    e.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}

$(document).ready(function() {
    AutoFill("#branding .search ul .text input");
    AutoFill(".bbrnav .search ul .text input");
    AutoFill(".login_module .form .text input");
    AutoFill(".membership_module .log_in .form .text input");
    ExpandClickArea(".profile .other_profiles li");
    CreateSlider(".hot_or_not .rating", 10);
});
