$(function() {
    // Handle menu hovers
    $(".nav").data("clicked", false).data("timeout", false);
    $(".nav ul li ul").each(setHeight).hide().css('visibility', 'visible');
    //$(".nav ul li").hover(menuLiMouseOver, menuLiMouseOut).click(menuLiMouseClick);
    $(".nav ul li").hover(menuLiMouseOver, menuLiMouseOut);

    $(".nav ul li ul li a").click(menuLiAMouseClick);

    // Nieuws brief openen
    $(".openNewsLetter").click(openNewsLetter);

    // Als hij scrollt kijken of hij ander logo moet tonen
    //$(window).scroll(logoCheck);
    //setInterval(logoCheck, 1000);
    //setRightLogo();

    //Zet een andere class op de container van een text area
    if ($("textarea").parent().attr("class") == "inputContainer") {
        $("textarea").parent().addClass("largeContainer");
    }
    
    if ($("select").parent().attr("class") == "inputContainer") {
        $("select").parent().addClass("selectContainer");
    }

    // Formulier valideren
    $(".newsLetterSubmit").click(function() {
        $(".error").removeClass("error");
        $(".required").removeClass("required").addClass("hasRequired");
        $(".email").removeClass("email").addClass("hasEmail");
        $(".phone").removeClass("phone").addClass("hasPhone");
        $(".newsLetter .hasRequired").removeClass("hasRequired").addClass("required");
        $(".newsLetter .hasEmail").removeClass("hasEmail").addClass("email");
        $(".newsLetter .hasPhone").removeClass("hasEmail").addClass("phone");

        if ($("#aspnetForm").valid()) {
            $(".left").find("input:hidden").val("false");
            $("#aspnetForm").submit();
        }
        else {
            $(".errors").html("");
        }
    });
    $(".button").click(function() {
        $(".error").removeClass("error");
        $(".required").removeClass("required").addClass("hasRequired");
        $(".email").removeClass("email").addClass("hasEmail");
        $(".phone").removeClass("phone").addClass("hasPhone");
        $(".left .hasRequired").removeClass("hasRequired").addClass("required");
        $(".left .hasEmail").removeClass("hasEmail").addClass("email");
        $(".left .hasPhone").removeClass("hasEmail").addClass("phone");

        if ($("#aspnetForm").valid()) {
            $(".errors").html("");
            $("#aspnetForm").submit();
        }
    });

    // form validatie
    $("#aspnetForm").validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                $(".errors").html("Het formulier is niet volledig/correct ingevuld");
            }
            else {
                $(".errors").hide();
            }
        },
        errorPlacement: function(error, element) {
            if (element.parent().attr("class") == "inputContainer") {
                element.parent().addClass("ContainerError");
            };
		},
		success: function(label) {
		}
    });

    $.extend($.validator.messages, {
        required: " ",
        email: " ",
        phone: " "
    });
    
    $(".inputContainer input").blur(function () {
        if ($(this).valid() == 1) {
            //alert($(this).parent().attr("class"));
            $(this).parent().removeClass("ContainerError");
        }
    });

    // Add >> to productoverzicht
    $(".prodOvIcons a").append("<span></span>");

    // Homepage portfolio
    if ($(".scroller").length > 0) {
        $(".scroller").photoBalls({
            slideElement: "a",
            pagerElement: ".dots span",
            pagerActive: "selected",
            startSliding: function(event, newIndex) {
                var $el = $(this).find("a").eq(newIndex);
                $(".hpMeer").attr("href", $el.attr("href"));
                if ($el.attr("domain") != null) {
                    $(".hpURL").html($el.attr("domain")).attr("href", "http://" + $el.attr("domain"));
                }
            }
        });
    }

    // Do header image scroller
    $("#featureCarousel").featureCarousel({
        autoPlay: 10000
    });
    
    // Zoek tonen
    $(".opener").mouseover(function(){$(this).next().fadeIn()});
    $(".searchOpener").mouseleave(function(){
        $(this).find(".opener").next().fadeOut();
    });
    // smartsearch
    $(".search input").keyup(handleSearchKey);
    $(".searchGlass").click(function() {
        if ($('#__zoekStr').val() != '') {
            document.location = '/z/' + $('#__zoekStr').val() + '.aspx?zoekstr=' + $('#__zoekStr').val();
            return true;
        }
    });
});

function handleSearchKey(e) {
    if (e.which == 13) {
        document.location = '/z/' + $('#__zoekStr').val() + '.aspx?zoekstr=' + $('#__zoekStr').val();
        return true;
    }
   
    // plaats hier de ajax post.
    if ($('#__zoekStr').val().length > 3) {
        //Ajax post afhandelen
        $.ajax({
            type: "POST",
            url: "/smartsearch.aspx/GetResults",
            data: "{'searchterm':'" + $('#__zoekStr').val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
                $("#searchResults").html(response.d);
            }
        });
        $(this).parent().find("div").slideDown();
    }
    else {
        $(this).parent().find("div").slideUp();
    }
}

// Menu Hovers
//function menuLiMouseOver() {
//	if ($(this).closest("ul").hasClass("navMenu")) {
//		$(this).find("a").eq(0).addClass("selected");
//		if ($(".nav").data("clicked")) {
//			clearTimeout($(".nav").data("timeout"));
//			$(this).trigger("click");
//		}
//	}
//}
//function menuLiMouseOut() {
//    if ($(this).closest("ul").hasClass("navMenu")) {
//        $(this).find("a").eq(0).removeClass("selected");
//		$(this).find("ul").eq(0).hide().css('z-index', 70);
//		$(".nav").data("timeout",setTimeout("$('.nav').data('clicked', false)", 0));
//	}
//}
//function menuLiMouseClick() {
//	if ($(this).closest("ul").hasClass("navMenu")) {
//		if ($(this).find("ul").length > 0) {
//			$(".nav").data("clicked", true);
//			$(this).find("ul").eq(0).slideToggle().css('z-index', 80);
//			return false;
//		}
//	}
//}
function menuLiMouseOver() {
	if ($(this).closest("ul").hasClass("navMenu")) {
	    $('ul.navMenu > li').css('z-index', 70);
	    $(this).css('z-index', 71);
		$(this).find('ul').stop(true, true).slideDown();
	}
}
function menuLiMouseOut() {
    if ($(this).closest("ul").hasClass("navMenu")) {
	    $(this).css('z-index', 70);
        $(this).find('ul').hide();
	}
}
function menuLiAMouseClick(e) {
	if ($(this).attr("href") == "" && $(this).attr("href") == "#") {
		e.preventDefault();
	}
}
function setHeight() {
	$(this).height($(this).find(".subCenter").height()).width($(this).find(".subCenter").innerWidth());	
    
    var meWidth = parseFloat($(this).innerWidth()) + parseFloat($(this).offset().left);
    var containerWidth = parseFloat($(".container").offset().left) + parseFloat($(".container").innerWidth());
    if (meWidth > containerWidth) {
        $(this).addClass("menuFloatRight");
    }
}

// Nieuwsbrief
function openNewsLetter() {
	if ($(this).data("open")) {
		$(this).data("open", false);
		$(this).closest("div").animate({ height: 36 });
	}
	else {
		$(this).data("open", true);
		$(this).closest("div").animate({ height: 140 });
	}
	return false;
}

// Scroll logo check
function logoCheck() {
	if ($.browser.msie && $.browser.version == "6.0") {
	    return false;
	}
		
	var pos = $(this).scrollTop();
	var $logo = $(".logo");
	
	if (pos > 100) {
		if (!$logo.hasClass("smallLogo")) {
			$logo.stop(true, false).animate({ top: -$logo.height() }, 500, function() {
				$logo.addClass("smallLogo").animate({ top: 0 }, 500);
			});
		}
	}
	else {
		if ($logo.hasClass("smallLogo")) {
			$logo.stop(true, false).animate({ top: -158 }, 500, function() {
				$logo.removeClass("smallLogo").animate({ top: 0 }, 500);
			});
		}
	}
}

function setRightLogo() {
	if ($.browser == "msie" && $.browser.version == "6.0") {
	    return false;
	}
	
	var pos = $(window).scrollTop();
	if (pos > 0) {
		$(".logo").addClass("smallLogo");
	}
}

