$(function() {
    $.fn.log = function(msg) {
        console.log("%s: %o", msg, this);
        return this;
    };
	
	// dropdown menu
	$('.menu').dropDownMenu({timer: 0, parentMO: 'parent-hover', childMO: 'child-hover1'});
    
	// FAQ
    $(".col").next().hide();

    $(".subTitle").click(function() {
        if ($(this).hasClass("col")) {
            $(this).removeClass("col").next().slideDown();
        }
        else {
            $(this).addClass("col").next().slideUp();
        }
    });

    // Textarea value
    $(".cadeaubonBon textarea").inputReset()
	                            .disableEnter()
	                            .maxLength({
	                                maxLength: 250,
	                                textTarget: ".cadeaubonBon div",
	                                usingModuleInputReset: true
	                            });
    // Input reset
    $(".to input, .inlogscherm input, .exisCustForm input").inputReset();

    // Foto gallery
    $(".galleryThumbs a").mouseover(showThumb);
    $(".galleryThumbs a").click(function() { return false; });

    // History toggle
    $(".hisBalk").click(toggleHistory);
    $(".hisPlus").next().hide();

    // Validate
    $("#aspnetForm").validate({
        errorPlacement: function(error, element) {
            error.appendTo(element.parent("div").next("div"));
            //			$(".formError").show();
            element.parent("div").next("div").show();
        },
        success: function(label) {
            label.parent("div").hide();
        },
        rules:
        {
            jclc_password: { minlength: 4 },
            jclc_password_check: { equalTo: "#jclc_password" },
            jclc_emailfirst_check: { equalTo: "#jclc_emailfirst" }
        }
    });

    // Bestel overzicht aanpassen tonen
    $(".verzendenNaarButton").click(function() {
        var visible = $(".verzendenNaar").is(":visible");
        $(".verzendenNaar").show();
        $(".verzendenNaarText").hide();

        if (visible) {
            $("body").each(function() {
                if ($(this).data("required")) {
                    $(this).addClass("required");
                }
            });
            $(".required").each(function() {
                $(this).data("required", true);
                if ($(this).closest(".verzendenNaar").length == 0) {
                    $(this).removeClass("required");
                }
            });

            // Validatie + submit
            submitForm();
        }
    });
    
    // Scroll Gallery
    $(".gallery a").data("index",0).data("interval", setInterval(scrollGallery, 5000));
    
	
	// Bekijk details
	if($(".hisOverBtn").length > 0)
	{
		$(".hisOverBtn").click(function(){
			$(this).closest(".hisProductEdit").find(".hisOver").show();								
		});	
		$(".hisOverClose").click(function(){
			$(this).closest(".hisProductEdit").find(".hisOver").hide();				  
		});
	}
});

function scrollGallery()
{
    var $this = $(".gallery a");
    
    // Handle index
    var oldIndex = $this.data("index");
    var newIndex = $this.data("index") + 1;    
    $this.data("index", newIndex);
    
    // Aan het eind?
    if( oldIndex == $this.find("img").length - 1 )
    {
        $this.parent().animate({scrollLeft : 0}, 500);
        $this.data("index", 0);
    }
    else
    {
        var scroll =  parseInt($this.parent().scrollLeft());
        scroll = scroll + $this.find("img").eq(oldIndex).width();
        $this.parent().animate({scrollLeft : scroll}, 500);
    }
    
}

function submitFormRequiredEdit() {
    $("body").each(function() {
        if ($(this).data("required")) {
            $(this).addClass("required");
        }
    });
    $(".required").each(function() {
        $(this).data("required", true);
        if ($(this).closest(".jbz").length == 0) {
            $(this).removeClass("required");
        }
    });
}
// Show the thumbnail
function showThumb()
{
    $(".galleryLarge img").get(0).src = $(this).attr("href");
    $(".imgAltDisplay").html($(this).attr("title"));
}

// Toggle history
function toggleHistory()
{

     $(this).toggleClass("hisMin").toggleClass("hisPlus").next().toggle(); 
     $(this).siblings(".hisBalk").removeClass("hisMin").addClass("hisPlus").next().hide();
     
     /*   
    // FF2 of lager
    var ver = parseFloat(jQuery.browser.version.substr(0,3));
    if($.browser.mozilla && ver <= 1.8)
    {
       
    }
    else
    {
    
    // Slide current
        $(this).toggleClass("hisMin").toggleClass("hisPlus").next().slideToggle(1000);    
        
        // Slide all others
        $(this).siblings(".hisBalk").removeClass("hisMin").addClass("hisPlus").next().slideUp(1000);
    }
    */
}
function submitWinkelMandjeAjax() {
    __doPostBack('ctl00$ContentPlaceHolder1$TriggerWinkelmandPanel', '');
}

function removeProductWinkelmandje(productid) {
    $("#removeProduct").val(productid);
    submitWinkelMandjeAjax();
}

function submitUrl(url) {
    document.aspnetForm.action = url;
    document.aspnetForm.submit();
}

function submitFormUrl(url) {
    if ($('#aspnetForm').valid()) {
        submitUrl(url);
    }
}

function submitForm() {
    if ($('#aspnetForm').valid()) {
        document.aspnetForm.submit();
    }
}

function submitExistingCust() {
    $(".newCustForm .required").removeClass("required").addClass("hasRequired");
    $(".newCustForm .email").removeClass("email").addClass("hasEmail");
    if ($("#aspnetForm").valid()) {
        $("#mode").val('login');
        document.aspnetForm.submit();
    }
    else {
        $(".newCustForm .hasRequired").removeClass("hasRequired").addClass("required");
        $(".newCustForm .hasEmail").removeClass("hasEmail").addClass("email");
    }
}

function submitNewCust() {
    $(".exisCustForm .required").removeClass("required").addClass("hasRequired");
    $(".exisCustForm .email").removeClass("email").addClass("hasEmail");
    if ($('#aspnetForm').valid()) {
        $("#mode").val('newcust');
        document.aspnetForm.submit();
    }
    else {
        $(".exisCustForm .hasRequired").removeClass("hasRequired").addClass("required");
        $(".exisCustForm .hasEmail").removeClass("hasEmail").addClass("email");
    }
}

function submitLostPassword() {
    $("#mode").val('lostPassword');
    submitForm();
}

function submitLostPasswordUrl(url) {
    $("#mode").val('lostPassword');
    submitFormUrl(url);
}

function submitLogin() {
    $("#mode").val('login');
    submitForm();
}

$(document).ready(function() {
    // Add the page method call as an onclick handler for the div.
    //validate
 //  $("#aspnetForm").validate();
});

function changeAddress() {
    $.ajax({
        type: "POST",
        url: "/formulieren.aspx/GetAddressPanel",
        data: "{'zipcode': '" + $('#jclc_da_zipcodepart1').val() + "','language': '" + $('#jclc_language_id').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $("#updatePanel").html(data.html);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(xhr.statusText);
            alert(thrownError);
        }
    });
}
