jQuery.noConflict();

function stop(e) {
	if (!e) e = window.event;
	(e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}

function prevDef(e) {
	if (!e) e = window.event;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}

function log(message) {
	if (typeof console != 'undefined' && typeof console.log != 'undefined') {
		console.log(message);
	}
};

jQuery(document).ready(function () {
	//Remove noJs bodyclass
	jQuery("body").addClass("activeJs").removeClass("noJs");
	if (jQuery('#head-menu').length) { initMainnav(); }
	if (jQuery('#head-top .dropdown').length) { initIntnav(); }
	if (jQuery('.search .dropdown').length) { initSearchnav(); }
	if (jQuery('#sub-menu').length) { initSubMenu(); }
	if (jQuery('#tabs').length) { initTabs(); }
	if (jQuery('#head-crumble').length) { initCrumble(); }
	if (jQuery('.innerlabel', '#head-center').length) { jQuery('.innerlabel', '#head-center').inputLabel(); }
	if (jQuery('.innerlabel', '#knowledge-search').length) { jQuery('.innerlabel', '#knowledge-search').inputLabel(); }

	if (jQuery('div.wide').length != 0) { slideShow(950); }
	else if (jQuery('div.narrow').length != 0) { slideShow(760); }

	if (jQuery('.searchfield').length != 0) { autoComplete(); }
	if (jQuery('#popup').length != 0) { setPopup() }

	if (!jQuery("#rightCol>*:first-child").hasClass("contact")) {
		jQuery("#rightCol").addClass("extraPadding");
	}

    if (jQuery('#fund-search').length != 0) { fundsColumnsTopFix(); } /*remove top padding on first container in left column on an funds page*/

	jQuery("#rightCol>*:first-child").addClass("noTopBorder");

	if (jQuery('.scWebEditFrame').length != 0) {
		jQuery("html").addClass("editmode");
		jQuery("#footer-top").attr({ id: "footer-edit-top" });
		jQuery("#footer-top").attr({ id: "footer-edit-top" });
		jQuery("#subscription .heading").removeClass("heading").addClass("heading-edit");
    }

    if (jQuery('.richtext p img').length != 0) { fixImageLayout() }

});

function fundsColumnsTopFix() {
    jQuery("#leftCol>*:first-child").addClass("noTopPadding");
    jQuery("#rightCol").removeClass("extraPadding");
}

function fixImageLayout() {
    var ps = jQuery('.richtext p > img');

    ps.each(function () {
        if (jQuery.trim(jQuery(this).parent().text()) == "") {
            jQuery(this).parent().addClass("imageOnly");
        }
    })
}

function slideShow(_slideWidth) {

	var slideWidth = _slideWidth;

	if (jQuery("#slideShow")) {

		if (jQuery("#slideShow").length == 0) {
			return false;
		}
		//log("blaat");
		var canvasWidth = -(jQuery(".slide", "#slideShow").length - 1) * slideWidth;
		var slideTime = 2050;
		var waitTime = 6100;
		var slideTimer = null;
		var opacityTimer = null;

		if (jQuery("#sliderSlideTime").val()) {
			slideTime = jQuery("#sliderSlideTime").val() * 1;
		}

		if (jQuery("#sliderWaitTime").val()) {
			waitTime = jQuery("#sliderWaitTime").val() * 1;
		}

		if (slideTime >= waitTime) { slideTime = waitTime }

		jQuery(".slide", "#slideShow").each(function (i) {
		   jQuery(".directaccess ul").append("<li><a href='#'>" + i + "</a></li>");
		})

	   if (jQuery("#slideShow").length == 0) {
		   return false;
	   }
	   if (jQuery(".slide","#slideShow").length == 0) {
		   return false;
	   }

		var firstSlide = jQuery(".slide:first", "#slideShow").clone();
		jQuery("#slideShow .canvas").append(firstSlide).addClass("first");

		var tempPos = jQuery(".directaccess li:first", "#slideShow").position().left;
		jQuery(".currentslide", "#slideShow").css("left", tempPos - 14).css("margin-left", 14);

		jQuery(".directaccess a").bind("click", function (e) {
			stop(e);
			var position = jQuery(".directaccess a").index(jQuery(this));

			//set opacity to 0 while sliding
			jQuery(".directaccess").fadeOut();

			jQuery(".canvas", "#slideShow").stop().animate({ "margin-left": -position * slideWidth }, slideTime / 3, function () { setOpacity() });
			jQuery(".currentslide", "#slideShow").stop().animate({ "margin-left": (position + 1) * 14 }, slideTime / 3);

			clearInterval(slideTimer);
			slideTimer = setInterval(function () { nextSlide() }, waitTime)
		})

		function setOpacity() {
			//set opacity to 1 after sliding
			jQuery(".directaccess").fadeIn();
		}

		function nextSlide() {
			var endCanvas = jQuery(".canvas", "#slideShow").css("margin-left").split("px")[0] * 1;

			//set opacity to 0 while sliding
			jQuery(".directaccess").fadeOut();

			if (endCanvas <= canvasWidth) {
				jQuery(".canvas", "#slideShow").animate({ "margin-left": "-=" + slideWidth }, slideTime, function () {
					setOpacity()
					jQuery(".canvas", "#slideShow").css("margin-left", 0);
				});
				jQuery(".currentslide", "#slideShow").animate({ "margin-left": 14 }, slideTime);
			} else {
				jQuery(".canvas", "#slideShow").animate({ "margin-left": "-=" + slideWidth }, slideTime, function () { setOpacity() });
				jQuery(".currentslide", "#slideShow").animate({ "margin-left": "+=" + 14 }, slideTime);
			}
		}

		clearInterval(slideTimer);
		slideTimer = setInterval(function () { nextSlide() }, waitTime)
	}

	//alert(jQuery(slideShow));

	jQuery(".mask", "#slideShow").mouseenter(function () {
		clearInterval(slideTimer);
	});

	jQuery(".mask", "#slideShow").mouseleave(function () {
		clearInterval(slideTimer);
		slideTimer = setInterval(function () { nextSlide() }, waitTime)
	});
}



function autoComplete() {
	var searchFields = jQuery(".searchfield");
	searchFields.autocomplete({
		source: "/Handlers/AutoComplete.ashx",
		minLength: 3
	});

	// close all autocomplete dropdowns
	jQuery("form").submit(function() {
		searchFields.autocomplete("close");
	});
}


function initCrumble() {

	var totalWidth = 0;
	var childIndex = 3;
	var maxWidth = 750;

	setTotalWidth();

	while (totalWidth > maxWidth) {
		jQuery("#head-crumble li:nth-child(" + childIndex + ") a").text("...");
		if (jQuery("#head-crumble li:nth-child(" + childIndex + ")") != jQuery("#head-crumble li:last-child")) {
			childIndex++;
			setTotalWidth();
		}
	}

	function setTotalWidth() {
		totalWidth = 0;
		jQuery('#head-crumble li').each(function () {
			totalWidth += jQuery(this).width();
		});
	}
}

var photoVideoInited = false;
var plattegrondInited = false;

function initTabs() {
    jQuery("#tabs").tabs({
        select: function (event, ui) {
            // alter the hash of the page, so switching to tabs usings links work
            var tabName = ui.panel.id.replace('tab-content', 'tab-index');
            window.location.hash = '#' + tabName;
        },
        show: function (event, ui) {
            // check if tab is 'photovideo' 
            if (ui.tab.href.indexOf('#tab-content3') >= 0) {
                initPhotoCarousel('#tab-fotos');
                if (!photoVideoInited) {
                    initEnlargedPhotoVideo('#tab-fotos');
                    jQuery("#tab-fotos .jcarousel-first a").click();    // Active Shadowbox  
                    photoVideoInited = true;
                }
            }
            // check if tab is 'plattegrond' 
            if (ui.tab.href.indexOf('#tab-content4') >= 0) {
                initPhotoCarousel('#tab-plattegronden');
                if (!plattegrondInited) {
                    initEnlargedPhotoVideo('#tab-plattegronden');
                    jQuery("#tab-plattegronden .jcarousel-first a").click();    // Active Shadowbox 
                    plattegrondInited = true;
                }
            }
            // check if tab is 'kaart' 
            if (ui.tab.href.indexOf('#tab-content6') >= 0) {
                doInitMapInPage();
                //initializeGoogleMap(52.6199858119411, 4.74858283996582, "map_canvas");
            }
        }
    });

    var $tabs = jQuery('#tabs').tabs(); // first tab selected
}

function initSubMenu() {
	jQuery("#sub-menu ul li ul").toggleClass("open").hide();
	jQuery("#sub-menu ul li.current ul").toggleClass("open").slideDown();

	jQuery(".matching-filters ul li ul.closed").removeClass("open").hide().prev().removeClass("down");

	jQuery(".matching-filters ul li div").click(function () {
		jQuery(this).toggleClass("down").next().toggleClass("open").slideToggle();
	});
}

function initMainnav() {
	jQuery("#head-menu li.mainitem").mouseenter(function () {
		jQuery(this).css({ 'overflow': 'visible' }).addClass("dropdown-open");
	});
	jQuery("#head-menu li.mainitem").mouseleave(function () {
		jQuery(this).css({ 'overflow': 'hidden' }).removeClass("dropdown-open");

	});
}

function initIntnav() {
	jQuery("#head-top li").mouseenter(function () {
		jQuery(this).find("ul").css({ 'visibility': 'visible' });
	});
	jQuery("#head-top li").mouseleave(function () {
		jQuery(this).find("ul").css({ 'visibility': 'hidden' });
	});
}

function initSearchnav() {
	jQuery(".search .dropdown").mouseenter(function () {
		jQuery(this).parent().find("ul").css({ 'visibility': 'visible' });
	});
	jQuery(".search .dropdown-container").mouseleave(function () {
		jQuery(this).find("ul").css({ 'visibility': 'hidden' });
	});
}



//POPUP
var popupStatus = 0;
//loading popup
function loadPopup() {
	//loads popup only if it is disabled
	if (popupStatus == 0) {
		jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
		jQuery("#popup-background").css({
			"opacity": "0.7"
		});
		jQuery("#popup-background").fadeIn("slow");
		jQuery("#popup").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup
function disablePopup() {
	//disables popup only if it is enabled
	if (popupStatus == 1) {
		jQuery("#popup-background").fadeOut("slow");
		jQuery("#popup").fadeOut("slow");
		popupStatus = 0;
	}
}

function setPopup() {
	//Click the button
	jQuery("#disclaimer, ").click(function () {
		//load popup
		loadPopup();
	});

	jQuery(".videoList .mask a").live('click', loadPopup);

	//Click the X
	jQuery("#popup-close").click(function () {
		disablePopup();
	});
	//Click outside
	jQuery("#popup-background").click(function () {
		disablePopup();
	});
	//Press Escape
	jQuery(document).keypress(function (e) {
		if (e.keyCode == 27 && popupStatus == 1) {
			disablePopup();
		}
	});
}

