$(document).ready(function () {
    SetDivWrapperHeight();
    $("#dvMenuHoofd").corner("bottom, left");
    $("#dvMain").corner();
    $("#dvFooter").corner("top");
    $(".dvPromotieHome, .dvBestsellerHome").corner("top");
    SetHomeBoxHeight();
    $(".aImgPrevFB").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });

    $(".dvArtikelBox").hover(function () {
        $(this).css("cursor", "hand");
        $(this).find("a:first").css("text-decoration", "underline");
    }, function () {
        $(this).find("a:first").css("text-decoration", "none");
    });

    $(".dvWinkelMandje, .dvPromotie, .dvWinkelMandje, .dvBestseller, .dvPromotieHome, .dvBestsellerHome").hover(function () {
        $(this).css("cursor", "hand");
        $(this).find("span:first").css("text-decoration", "underline");
    }, function () {
        $(this).find("span:first").css("text-decoration", "none");
    });

    $(".dvArtikelBox, .dvWinkelMandje, .dvPromotie, .dvWinkelMandje, .dvBestseller, .dvPromotieHome, .dvBestsellerHome").click(function () {
        window.location = $(this).find("a:first").attr("href");
    });

    SetHomeBoxHeight();

    if ($(".ZoekRblLst").length > 0) {
        CheckZoekGebruikValue($("table[id$='tblRbZoekGebruik'] :checked").val(), false);
        $("table[id$='tblRbZoekGebruik'] :radio").click(function () {
            CheckZoekGebruikValue($("table[id$='tblRbZoekGebruik'] :checked").val(), true);
        });
    }

    $(".tdProductOverdracht").click(function () {
        $(".tdProductOverdracht :radio").removeAttr("checked");
        $(this).find(":radio").attr("checked", "checked");
    });

    if ($("input[id$='chkLeveringsFacturatieAdres']").length > 0) {
        ValidatorEnable($("span[id$='reqValLeveringsAdresStraatNr']")[0], false);
        ValidatorEnable($("span[id$='reqValLeveringSadresPostcode']")[0], false);
        ValidatorEnable($("span[id$='reqValLeveringsAdresGemeente']")[0], false);
        $(".trLeveringsAdres").hide();
    }
    $("input[id$='chkLeveringsFacturatieAdres']").click(function () {
        if ($(this).is(":checked")) {
            ValidatorEnable($("span[id$='reqValLeveringsAdresStraatNr']")[0], false);
            ValidatorEnable($("span[id$='reqValLeveringSadresPostcode']")[0], false);
            ValidatorEnable($("span[id$='reqValLeveringsAdresGemeente']")[0], false);
            $(".trLeveringsAdres").hide();
        }
        else {
            ValidatorEnable($("span[id$='reqValLeveringsAdresStraatNr']")[0], true);
            ValidatorEnable($("span[id$='reqValLeveringSadresPostcode']")[0], true);
            ValidatorEnable($("span[id$='reqValLeveringsAdresGemeente']")[0], true);
            $(".trLeveringsAdres").show();
        }
    });

    $(".selectRow").click(function () {
        if ($(this).find(":radio").length > 0) {
            $(this).find(":radio").attr("checked", "checked");
        }
        else if ($(this).is("td")) {
            if ($(this).parent().find(":checked").length > 0) {
                $(this).parent().find(":checkbox").removeAttr("checked");
            }
            else {
                $(this).parent().find(":checkbox").attr("checked", "checked");
            }
        }
    });

    $(".dvInhuizingTrapTypeImg").click(function () {
        $(".dvInhuizingTrapTypeImg :radio").removeAttr("checked");
        $(this).find(":radio").attr("checked", "checked");
    });

    $(".tdProductOverdracht, .selectRow, .dvInhuizingTrapTypeImg").hover(function () { $(this).css("cursor", "hand"); }, function () { });

    //Filter changes
    $("select[id$='ddlFilterWaartegenBeschermen']").change(function () {
        $("input[id$='hdnFilterWaartegenBeschermen']").val($("select[id$='ddlFilterWaartegenBeschermen']").val());
    });

    $(".txtWinkelmandjeAantal").focus(function () {
        $(this).attr("oldValue", $(this).val());
    });

    //Title
    if (document.URL.toString().toLocaleLowerCase().indexOf("artikel.aspx") != -1 || document.URL.toString().toLocaleLowerCase().indexOf("artikelgroep.aspx") != -1) {
        document.title = $("span[id$='lblTitle']").text();
    }

    if ($("input[id$='ChkAlgemeneVoorwaarden']").length > 0) {
        $("#dvBetalingCheckOutOgone").hide();
    }
    $("input[id$='ChkAlgemeneVoorwaarden']").click(function () {
        if ($(this).is(":checked")) {
            $("#dvBetalingCheckOutOgone").show();
        }
        else {
            $("#dvBetalingCheckOutOgone").hide();
        }
    });
});

function SetHomeBoxHeight()
{
	var height = 0;
	var length = $(".tblPromotieHome, .tblBestsellerHome").length;
	if (length == 2)
	{
		$(".tblPromotieHome, .tblBestsellerHome").each(function()
		{
			if ($(this).height() > height)
			{
				height = $(this).height();
			}
		});
		if (height > 0)
		{
			$(".tblPromotieHome, .tblBestsellerHome").height(height);
		}
	}
	else if (length == 3)
	{
		if ($(".tblPromotieHome, .tblBestsellerHome")[0].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[0].clientHeight;
		}
		if ($(".tblPromotieHome, .tblBestsellerHome")[1].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[1].clientHeight;
		}
		if (height > 0)
		{
			$(".tblPromotieHome, .tblBestsellerHome")[0].style.height = height;
			$(".tblPromotieHome, .tblBestsellerHome")[1].style.height = height;
		}
	}
	else if (length == 4)
	{
		if ($(".tblPromotieHome, .tblBestsellerHome")[0].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[0].clientHeight;
		}
		if ($(".tblPromotieHome, .tblBestsellerHome")[1].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[1].clientHeight;
		}
		if (height > 0)
		{
			$(".tblPromotieHome, .tblBestsellerHome")[0].style.height = height;
			$(".tblPromotieHome, .tblBestsellerHome")[1].style.height = height;
		}
		height = 0;
		
		if ($(".tblPromotieHome, .tblBestsellerHome")[2].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[2].clientHeight;
		}
		if ($(".tblPromotieHome, .tblBestsellerHome")[3].clientHeight > height)
		{
			height = $(".tblPromotieHome, .tblBestsellerHome")[3].clientHeight;
		}
		if (height > 0)
		{
			$(".tblPromotieHome, .tblBestsellerHome")[2].style.height = height;
			$(".tblPromotieHome, .tblBestsellerHome")[3].style.height = height;
		}
	}
}

function CheckZoekGebruikValue(selectedValue, clear)
{
	if (clear)
	{
		$("table[id$='tblWaardeBergingPrivaat'] :radio, table[id$='tblWaardeBergingZakelijk'] :radio").removeAttr("checked");
		$("table[id$='tblWaardeBergingPrivaat'], table[id$='tblWaardeBergingZakelijk']").hide();
	}
	if (selectedValue == "1")
	{
		$("table[id$='tblWaardeBergingPrivaat']").show();
		if ($("table[id$='tblWaardeBergingPrivaat'] :checked").length == 0)
		{
			$("input[id$='rdblListPrivaatGeenKeuze']").attr("checked", "checked");
		}
	}
	else if (selectedValue == "2")
	{
		$("table[id$='tblWaardeBergingZakelijk']").show();
		if ($("table[id$='tblWaardeBergingZakelijk'] :checked").length == 0)
		{
			$("input[id$='rdblListZakelijkGeenKeuze']").attr("checked", "checked");
		}
	}
	else
	{
		$("input[id$='rbGebruikPrivaat']").attr("checked", "checked");
		$("table[id$='tblWaardeBergingPrivaat']").show();
		if ($("table[id$='tblWaardeBergingPrivaat'] :checked").length == 0)
		{
			$("input[id$='rdblListPrivaatGeenKeuze']").attr("checked", "checked");
		}
	}
}

$(window).resize(function()
{
	SetDivWrapperHeight();
});

function SetDivWrapperHeight()
{
	var height = window.innerHeight;
	if (height = 'undefined') { height = document.documentElement.clientHeight; }
	var width = window.innerWidth;
	if (width = 'undefined') { width = document.documentElement.clientWidth; }
	if ($("#dvMain").height() < (height - 218))
	{
		$("#dvMain").height((height - 218));
	}
}

function RemoveItemFromBasket(guid)
{
	if (!confirm($("span[id$='lblVerwijderenArtikelZeker']").text())) { return false; }

	var pagePath = window.location.pathname;
	//Call the page method
	$.ajax({
		type: "POST",
		url: pagePath + "/RemoveItemFromBasket",
		data: "{ basketArtikelID : '" + guid + "' }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			if (!data.d)
			{
				alert("Could not delete article!");
			}
			else
			{
				window.location = window.location;
			}
		}
	});
}


function hasAttribute(id, attr)
{
	if ($("*[id='" + id + "'][" + attr + "]").length > 0)
	{
		return true;
	}
	return false;
}

function EditAantalArticleExtraFromBasket(guid, extraid, element)
{
	if (!Page_IsValid) { return false; }
	var ele = $("#" + element.id);
	if (ele.length > 0)
	{
		if (ele.attr("oldValue") == element.value) 
		{
			return false;
		}
	}
	
	var pagePath = window.location.pathname;
	//Call the page method
	$.ajax({
		type: "POST",
		url: pagePath + "/EditAantalExtraFromBasket",
		data: "{ aantal : '" + element.value + "', basketArtikelID : '" + guid + "', extraID : '" + extraid + "' }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			ResetWinkelMandjeItemTotal(guid, element.id);
			ResetWinkelMandjeTotal();
		}
	});
}

function EditAantalArticleFromBasket(guid, element)
{
		if (!Page_IsValid) { return false; }
		var ele = $("#" + element.id);
		if (ele.length > 0)
		{
			if (ele.attr("oldValue") == element.value) 
			{
				return false;
			}
		}
		
		if ("#"+ element.id)

		var pagePath = window.location.pathname;
		//Call the page method
	$.ajax({
		type: "POST",
		url: pagePath + "/EditAantalFromBasket",
		data: "{ aantal : '" + element.value + "', basketArtikelID : '" + guid + "' }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			ResetAantallenExtras(guid);
			ResetWinkelMandjeItemTotal(guid, element.id);
			ResetWinkelMandjeTotal();
		}
	});
}

function ResetAantallenExtras(guid)
{
	var pagePath = window.location.pathname;
	var els = $("div[basketitemid='" + guid + "'] input[extraid]");
	var lbls = $("div[basketitemid='" + guid + "'] span[extraid]");
	for (var i = 0; i < els.length; i++)
	{
		$.ajax({
			type: "POST",
			async: false,
			url: pagePath + "/GetWinkelMandjeItemExtraAantal",
			data: "{ basketArtikelID : '" + guid + "', extraID : '" + $("#" + els[i].id).attr("extraid") + "' }",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(data)
			{
				els[i].value = data.d;
			}
		});
	}

	for (var i = 0; i < lbls.length; i++)
	{
		$.ajax({
			type: "POST",
			async: false,
			url: pagePath + "/GetWinkelMandjeItemExtraAantal",
			data: "{ basketArtikelID : '" + guid + "', extraID : '" + $("#" + lbls[i].id).attr("extraid") + "' }",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(data)
			{
				lbls[i].innerHTML = data.d + "x";
			}
		});
	}
}

function ResetWinkelMandjeItemTotal(guid, elementid)
{
	var pagePath = window.location.pathname;
	//Call the page method
	$.ajax({
		type: "POST",
		async: false,
		url: pagePath + "/GetBaskedItemTotal",
		data: "{ basketArtikelID : '" + guid + "' }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			$("#" + elementid).parents("div[id$='pnlWinkelMandjeItem']").find("span[id$='lblWinkelMandjeItemPrijs']").text(data.d);
		}
	});
}

function ResetWinkelMandjeTotal()
{
	var pagePath = window.location.pathname;
	//Call the page method
	$.ajax({
		type: "POST",
		url: pagePath + "/GetBaskedTotal",
		async: false,
		data: "{ }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			$("span[id$='lblWinkelMandjeTotaalPrijs']").text(data.d);
		}
	});
}

function RemoveItemExtraFromBasket(basketArtikelID, extraArtikelNav)
{
	if (!confirm($("span[id$='lblVerwijderenArtikelExtraZeker']").text())) { return false; }

	var pagePath = window.location.pathname;
	//Call the page method
	$.ajax({
		type: "POST",
		url: pagePath + "/RemoveItemExtraFromBasket",
		data: "{ basketArtikelID : '" + basketArtikelID + "', extraArtikelNav : '" + extraArtikelNav + "' }",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(data)
		{
			if (!data.d)
			{
				alert("Could not delete article extra!");
			}
			else
			{
				window.location = window.location;
			}
		}
	});	
}
