var doResetMenuState = true;
var resetMenuTimer = 750;

$(document).ready(function() {

    $('#submenu-c').html($('#submenu-placeholder').html());
    $('#submenu-placeholder').html('&nbsp;');

    $('#navigation').append($('#tertiairmenu-content').html());

    $("#search").click(function() { $(this).addClass("active"); })
						.blur(function() { if ($("#search").attr("value") == "") { $(this).removeClass("active"); }; })
						.attr("value", "");
    $("#btn-search").click(function() { submit_onclick("frm_search", true); return false; });

    // Display helptext in the keywords input field in the search control.
    $("INPUT#keywords").focus(
							function() {
							    if ($(this).val() == "keywords...") {
							        $(this).val("");
							        $(this).removeClass("help");
							    }
							})
						.blur(
							function() {
							    if ($(this).val() == "") {
							        $(this).addClass("help");
							        $(this).val("keywords...");
							    }
							});

    doTransforms();
    doPostTransforms();

    // Top menu mouse-overs.
    $("#menu A").mouseover(function() { $(this).addClass("active"); })
						.mouseout(function() { if (!$(this).hasClass("current")) $(this).removeClass("active"); });

    // Reset menu state when the mouse leaves the menu to reflect your current location in the navigation menu.
    $("#navigation").mouseover(function() { doResetMenuState = false; })
						.mouseout(function() { doResetMenuState = true; setTimeout(function() { resetMenuState(); }, resetMenuTimer); });

    // Tertiair menu mouse-overs.
    $("#tertiairmenu LI").hover(
        function() {
            var sub = $(this).find("UL");

            $(this).addClass("active");
            sub.show();
            if (sub.length == 1 && sub.hasClass("tertiairmenu-bg")) {
                $(this).addClass("sprite tertiairmenu-tab");
            }
        },
         function() {
             var sub = $(this).find("UL");

             if (!$(this).hasClass("current"))
                 $(this).removeClass("active");
             sub.hide();
             if (sub.hasClass("tertiairmenu-bg")) {
                 $(this).removeClass("sprite tertiairmenu-tab");
             }
         });

    // Choose your language dropdown.
    $(".languagebar").mouseover(function() { $(this).addClass("active"); $("#languages").removeClass("hidden"); })
						.mouseout(function() { $(this).removeClass("active"); $("#languages").addClass("hidden"); });

    $(".nav .tab").mouseover(function() { $(this).addClass("hover"); })
						.mouseout(function() { $(this).removeClass("hover"); });

    //default tekst van tab activeren wanneer er een subcontainer is geactiveerd.
    //    $(".nav .tab.active").click(function() {
    //        if (($("#details-body-intro").length > 0 && !$("#details-body-intro").hasClass("hidden")) || ($("#product-details-intro").length > 0 && !$("#product-details-intro").hasClass("hidden")) || $("#product-details-default").length > 0) return;
    //        $(this).parent().parent().parent().find(".content").find(".container").addClass("hidden");

    //        if ($("#details-body-intro").length > 0) $("#details-body-intro").removeClass("hidden");
    //        if ($("#product-details-intro").length > 0) $("#product-details-intro").removeClass("hidden");
    //    })

    // Request more info pops a highslide with contact information.
    $(".moodboard .request-more-info").mouseover(function() { $(this).addClass("active"); })
									  .mouseout(function() { $(this).removeClass("active"); });

    // Handle product buttons.
    $(".buttons DIV").mouseover(function() { if (!$(this).hasClass("current")) $(this).addClass("active"); })
						.mouseout(function() { if (!$(this).hasClass("current")) $(this).removeClass("active"); })
						.click(function() {

						    if ($(this).hasClass("unclickable")) return;
						    // toggle button
						    $(this).parent().find("DIV").removeClass("current active");
						    $(this).addClass("current active");

						    // toggle container
						    var container = $("#" + $(this).attr("id").replace("btn-", ""));
						    $(this).parent().parent().find(".container").addClass("hidden");
						    //$(this).parent().parent().find(".container").addClass("invisible");
						    $(container).removeClass("hidden");
						    //$(container).removeClass("invisible");
						});


    //
    $(".textlinks a").click(function() {
        //reset current textlinks
        $(this).parent().parent().find('a').each(function() {
            $(this).removeClass('active');
        })
        // toggle container
        var container = $("#" + $(this).attr("id").replace("btn-", ""));
        var c = $(this).parent().parent().parent().parent().attr("id");
        $("#" + c + " > .container").addClass("hidden");
        $(container).removeClass("hidden");
        $(this).addClass('active');
        return false;
    });


    // Handle the mouse-over and click events of the thumbnails on the product details page.
    $("#product-viewer .thumbs IMG").mouseover(function() { if (!$(this).hasClass("current")) $(this).addClass("active"); })
									.mouseout(function() { if (!$(this).hasClass("current")) $(this).removeClass("active"); })
									.click(function() {
									    $("#product-viewer .container").addClass("hidden");
									    $("#product-viewer .buttons DIV").removeClass("current active");
									    $("#product-viewer .bigimage").css("background-image", "url('" + $(this).attr("longdesc") + "')");
									});

    // Downloads listing - Harmonica effect
    $("#downloads-listing LI").click(function() {
        var sender = $(this);
        var isSenderActive = $(sender).hasClass("active");
        var dontclose = $(sender).hasClass("dontclose");

        // close other items
        $("#downloads-listing LI").each(function() {
            if (!dontclose) {
                $(this).removeClass("active");
                $(this).find(".btn").removeClass("active");
                $(this).find(".details").slideUp();
            }
        });

        // open selected item
        if (!isSenderActive) {
            $(this).find(".btn").addClass('active');
            $(this).find(".details").slideDown();
            $(this).addClass("active")
        }

    });

    // (De)activate picture download buttons
    $("#downloads-listing LI LI").mouseover(function() { $(this).addClass("active").find(".btn").addClass("active"); })
									.mouseout(function() { $(this).removeClass("active").find(".btn").removeClass("active"); });

    // Homepage banners - Vertical Sliding
    //    $('.boxgrid.slideup').hover(function() {
    //        $(".cover", this).stop().animate({ bottom: '0px' }, { queue: false, duration: 100 });
    //    }, function() {
    //        $(".cover", this).stop().animate({ bottom: '-45px' }, { queue: false, duration: 400 });
    //    });

    // Set the width of the UL that holds the images.
    $("#homepage_banners UL").css("width", ($("#homepage_banners UL LI").length + 1) / 2 * (140 + 22) - 22);

    // Enable horizontal scrolling of the banners.
    $(".scroll-pane").jScrollHorizontalPane({
        showArrows: true,
        scrollbarMargin: 0,
        scrollbarHeight: 15,
        arrowSize: 20,
        dragMinWidth: 490,
        dragMaxWidth: 490,
        wheelSpeed: 100
    });

    //Printen
    $('.btn-print').click(function() {
        printme();
        return false;
    })
});

function doTransforms() {
	// jqTransform makes the form controls looks nice.
	$('#footer').jqTransform({imgPath:PROJECT_URL + 'js/img/'});
	$("#product-finder").jqTransform({imgPath:PROJECT_URL + 'js/img/'});
	$("#downloads-search").jqTransform({imgPath:PROJECT_URL + 'js/img/'});
	$("#frmRequestMoreInfo").jqTransform({imgPath:PROJECT_URL + 'js/img/'});
	$("#news-events").jqTransform({imgPath:PROJECT_URL + 'js/img/'});
}

function doPostTransforms() {
	$(".postTransform").css("visibility", "visible").addClass("hidden");
}

function showTertiairMenu(sender) {
	var subId = $(sender).parent().attr("id").split("-")[1];
	var tertiairId = $(sender).parent().attr("id").split("-")[2];

	// deactivate all submenu items
	$(sender).parent().parent().find("LI").each(function() { $(this).removeClass("active"); $(this).find("IMG").removeClass("active"); });
	$("UL[id^=tertiairmenu-]").each(function() { $(this).addClass("hidden"); });

	// activate submenu item
	$(sender).parent().addClass("active").find("IMG").addClass("active");
	$("UL#tertiairmenu-" + subId + "-" + tertiairId).removeClass("hidden");
}

function resetMenuState() {
	if (!doResetMenuState)
		return;

	var currentSubmenuId, currentTertiairmenuId;

	// activate current submenu
	$(".submenu LI").each(function() {
		if (!$(this).hasClass("current") || $(this).parent().hasClass("hidden")) {
			$(this).removeClass("active");
			$(this).find("IMG").removeClass("active");
		} else {
			$(this).addClass("active");
			$(this).find("IMG").addClass("active");
			currentSubmenuId = $(this).attr("id").split("-")[1];
			currentTertiairmenuId = $(this).attr("id").split("-")[2];
		}
	});

	// show current tertiairmenu
	$("UL[id^=tertiairmenu]").each(function() {
		var subId = $(this).attr("id").split("-")[1];
		var tertiairId = $(this).attr("id").split("-")[2];
		if (subId == currentSubmenuId && tertiairId == currentTertiairmenuId) {
			$(this).removeClass("hidden");
		} else {
			$(this).addClass("hidden");
		}
	});

	// activate the current menu item in the tertiairmenu
	$("#tertiairmenu LI").each(function() {
		if (!$(this).hasClass("current")) {
			$(this).removeClass("sprite tertiairmenu-tab active");
		} else {
			$(this).addClass("active");
		}
	});

	// menu is eligible for a reset again
	doResetMenuState = true;
}

function showTab(li, startId, activeId) {
	// deactivate all tabs
	$(".nav .tab").each(function() { $(this).removeClass("current active"); });
	$("DIV[id^=" + startId + "]").each(function() { $(this).addClass("hidden"); });

	// activate selected tab
	$("DIV[id^=" + startId + activeId + "]").removeClass("hidden");
	$(li).addClass("current active");
	doTransforms();
}

function showListingDiv(sender, divStartId, div) {
	// deactivate listing items
	$(sender).parent().parent().find("A").each(function() { $(this).removeClass("active"); });
	$("DIV[id^=" + divStartId + "]").each(function() { $(this).addClass("hidden"); });

	// activate selected item
	$(sender).addClass("active");
	$("#" + divStartId + div).removeClass("hidden");
}

function showContactDetails(select) {
	$("DIV[id^=contact-details-]").addClass("hidden");
	$("#contact-details-" + select.value).removeClass("hidden");
}

function pagingGotoImg(sender, imageIdPrefix, imageId) {
	var activePage = $(sender).parent();
	var pagingLIs = $("#" + imageIdPrefix + "-container").find("IMG");
	var maxImages = pagingLIs.length;
	//var pagingLIs = $(sender).parent().parent().find("LI");
	//var maxImages = (pagingLIs.length - 3) / 2;

	
    
	$("IMG[id^=" + imageIdPrefix + "]").each(function() {
		if (isNaN(imageId) && !$(this).hasClass("hidden")) {
			// imageId = 'next' or 'prev'
			imageId = parseInt($(this).attr("id").replace(imageIdPrefix + "-", "")) + (imageId == "next" ? 1 : -1);
			
			// check bounds
			if (imageId < 1) imageId = maxImages; //if lower bound is reached goto upperbound
			if (imageId > maxImages) imageId = 1; //if upper bound is reached goto lowerbound

			// calculate new page
			activePage = $(pagingLIs)[imageId * 2];
		}
           
		// hide old image
		$(this).addClass("hidden");
	});

	// show new image
	$("#" + imageIdPrefix + "-" + imageId).removeClass("hidden");
	
	
	//$(pagingLIs).removeClass("active");
	//$(activePage).addClass("active");
}

function ajaxSearch(form) {

}

function display(id, arr) {
    if (arr != undefined) {
        var a = arr.split(',')
        for (i = 0; i < a.length; i++) {
            $('#' + a[i]).css('display', 'none');
        }
    }
    var style = $('#' + id).css('display');
    if (style == 'none') {
        $('#' + id).css('display', 'block');
    }
    return false
}

function showResultMailFriend() {
    display('cont-mailfriend-result', 'cont-mailfriend');
}

function doProductFinder(currentElement,oCategory, oGroup, oModel) {
    $.ajax({
        type: "POST",
        url: PROJECT_URL + LANG + "/xml/UpdateProductFinder/?productCategory=" + oCategory.val() + "&productGroup=" + oGroup.val() + "&productModel=" + oModel.val() + "&level1=" + $("#level1").val(),
        dataType: "html",
        success: function(txt) {
            $('#container-product-finder').html(txt);
            $("#product-finder form").jqTransform({ imgPath: PROJECT_URL + 'js/img/' });
        }
    });

    return false;
}

function printme() {
    var objOut = document.getElementById("toprint");
    var out = objOut.innerHTML;

    var wh = window.open("", "wh", "width=640px,height=480px,scrollbars=yes");
    wh.document.open();
    var stylesheet = '<link href="' + PROJECT_URL + 'Content/css/print.css" rel="stylesheet" type="text/css" />';
    var output = '<html><head>' + stylesheet + '</head><body>' + out.replace('<object', '<!--object').replace('/object>', '/object-->') + '</body></html>';
    output += '<' + 'script type="text/javascript">';
    output += 'setTimeout("window.print()",2000);';
    output += '</' + 'script>';
    wh.document.writeln(output);
    wh.document.close();
}

function DoAjaxPagingCall(id, action, page){
    var url = PROJECT_URL + action.replace('page=','');
    url += 'page=' + page
    $.ajax({
        type: "GET",
        url: url,
        dataType: "html",
        success: function(html) {
            $('#'+ id).html(html);
        }
    });
}

// functie voor bij tips, zodat niet dichtklapt
function gotoUrl(obj, url) {
    location.href = url;
    $(obj).parents('li').addClass('dontclose');
    return false;
}
