function setCompareBar() {
    var height = $(window).height();
    var scrollTop = $(window).scrollTop();
    var totalHeight = $("#container").outerHeight();

    if (scrollTop > (totalHeight - height - 35)) {
        $("#balk_float").removeClass("balk_float");
    }
    else {
        $("#balk_float").addClass("balk_float");
    }
}

function IsEmpty(string) {
    //first remove all spaces using the following regex
    string = string.replace(/^\s+|\s+$/, '');

    //then we check for the length of the string if its 0 or not
    if (string.length == 0)
        return true;
    else
        return false;
}


$(document).ready(function () {
    $("li.submenu").hover(
		function () {
		    var ct = $(this).children('ul').children('li').size();
		    var ct = ct * 20;
		    var ct = ct + 41;
		    $(this).children('ul').css('display', 'block');
		    $(this).css('height', ct + 'px');
		}
	);
    $("li.submenu").mouseleave(
		function () {
		    $(this).children('ul').css('display', 'none');
		    $(this).css('height', '33px');
		}
	);

    $("#provider-table").tablesorter({
        textExtraction: function (node) {
            return $(node).find("span.hidden").text();
        }

    });

    $("#SubscriptionSelect select").each(function () {
        $(this).change(function () {
            $("#SubscriptionSelect").submit();
        });
    });

    $("#btn_opties").click(function () {

        $("#more_options_div").animate({ "height": "92px" }, function () {

        });
        $("#buttons").animate({ "margin-top": "98px" }, function () {

        });
        return false;
    });

    $('#provider_select').change(function () {
        providerID = "";

        if (!IsEmpty($("#provider_select").val())) {
            providerID = $("#provider_select").val();
        }

        $('#subscription_select').load('/informatie/subscriptions/' + providerID);
    });

    if ($('#subscription_select').val() == "") {
        providerID = "";
        if (!IsEmpty($("#provider_select").val())) {
            providerID = $("#provider_select").val();
        }
        $('#subscription_select').load('/informatie/subscriptions/' + providerID);
    }

    $("#add_review").click(function () {
        $("#review_error").css("display", "none");

        valid = true;
        if (IsEmpty($("#name").val())) {
            valid = false;
        }
        if (IsEmpty($("#email").val())) {
            valid = false;
        }
        if (IsEmpty($("#review").val())) {
            valid = false;
        }
        if (IsEmpty($("#provider_select").val())) {
            valid = false;
        }
        if (IsEmpty($("#subscription_select").val())) {
            valid = false;
        }

        if (valid != true) {
            $("#review_error").html("Naam, email, beoordeling en pakket zijn verplichte velden.");
            $("#review_error").css("display", "block");
        }
        return valid;
    });

    setCompareBar();

    $('.hstar').rating({
        focus: function (value, link) {
            var parent = $(this).parent();
            var obj = $(parent).children(".hover_text");
            obj.html(link.title || 'value: ' + value);
        },
        blur: function (value, link) {
            var parent = $(this).parent();
            var obj = $(parent).children(".hover_text");
            obj.html('' || '');
        }
    });
});

window.onscroll = function(e) {
    setCompareBar();
}

