$(document).ready(function() {
	
	if($("#multiImage").length > 0) {
		$("#multiImage").scrollerScroll();
	}
	$("#switch_intro").live('click', function(e) {
		e.preventDefault();
		$("#multiImage").slideToggle(function(){
			if($("#multiImage").is(':visible')) {
				$("#switch_intro").text('sluit intro');
				if ($(".visual_content").is(':visible')) {
					$(".visual_content").slideUp();
				}
			} else {
				$("#switch_intro").text('laad intro');
			}
		});
	});
	

	
	
	// visual slider
	$(".visual_menu a").live('click', function(e) {
		e.preventDefault();
		
		myHref = $(this).attr("href");
		myTarget = $(".visual_content");
		
		if(myTarget.is(":visible")) {
			myTarget.slideUp();
		} else {
			if ( $("#multiImage").is(':visible')) {
				$("#multiImage").slideUp();
				$("#switch_intro").text('laad intro');
			};
			myTarget.load(myHref + " #fetch", function(response, status, xhr) {
				if (status == "error") {
					myTarget.html("error: " + xhr.status + " " + xhr.statusText);
				}
				if($.browser.msie && $.browser.version < 7) {
					myTarget.fadeIn();
				} else {
					myTarget.slideDown();
				}
			});
		}
	});
	
	$(".visual_refresh").live('click', function(e) {
		e.preventDefault();
		
		myHref = $(this).attr("href");
		myTarget = $(".visual_content");
		
		if(myTarget.is(":visible")) {
			myTarget.slideUp();
			myTarget.load(myHref + " #fetch", function(response, status, xhr) {
				if (status == "error") {
					myTarget.html("error: " + xhr.status + " " + xhr.statusText);
				}
				if($.browser.msie && $.browser.version < 7) {
					myTarget.fadeIn();
				} else {
					myTarget.slideDown();
				}
			});
		}
	});
	
	$(".visual_box").live('click', function(e) {
		e.preventDefault();
		$(".visual_boxes").hide();
		myRel = $(this).attr("rel");
		$("#visual_" + myRel).show();
	});
	$(".visual_close").live('click', function(e) {
		e.preventDefault();
		$(".visual_boxes").hide();
	});
	
	// slideBig voor het hoofdmenu
	$(".mainMenuSlideBig").live('click', function(e) {
		e.preventDefault();
		
		$(".visual_content").slideUp();
		$("#multiImage").slideUp();
		
		if($(this).parent("li").hasClass(activePage)) {
			$(this).parent("li").addClass("active");
			return false;
		}
		
		if($(this).parent("li").hasClass("active")) {
			$("a.close").click();
			$("." + activePage).addClass("active");
			return false;
		}
		var slider = $("#sliderBig");
		var menuThis = $(this).parent("li");
		var menuAll = $("ul.menu li");
		
		menuAll.removeClass("active");
		menuThis.addClass("active");
		loadContent(slider, $(this).attr("href"), "fetch");
	});
	
	// content van kaart niveau 4 - POI naar de grote slider
	$("area.slide").click(function(e){
		e.preventDefault();
		setFavData($(this).attr("href"), $(this).attr("alt"));
		loadContent($("#sliderBig"), $(this).attr("href"), "poifetch");
	});
	$("a.poiSlide").click(function(e){
		e.preventDefault();
		setFavData($(this).attr("href"), $(this).attr("alt"));
		loadContent($("#sliderBig"), $(this).attr("href"), "poifetch");
	});
	
	// content van submenu naar de kleine slider
	$(".subMenuSlideSmall").click(function(e){
		e.preventDefault();
		
		if($(this).parent().hasClass("active")) {
			$("a.closeSmall").click();
			return false;
		}
		$("ul.submenu li").removeClass("active");
		$(this).parent().addClass("active");
		loadContent($("#sliderSmall"), $(this).attr("href"), "fetchInner");
		if($.browser.msie && $.browser.version < 7) {
			$("#sliderSmallWrapper").fadeIn(1000);
		} else {
			$("#sliderSmallWrapper").slideDown();
		}
	});
	
	$(".subMenuSlideSmallSingle").click(function(e){
		e.preventDefault();
		
		if($(this).hasClass("active")) {
			$("a.closeSmall").click();
			$(this).removeClass("active");
			return false;
		}
		$("ul.submenu li").removeClass("active");
		$(this).addClass("active");
		loadContent($("#sliderSmall"), $(this).attr("href"), "fetchInner");
		if($.browser.msie && $.browser.version < 7) {
			$("#sliderSmallWrapper").fadeIn(1000);
		} else {
			$("#sliderSmallWrapper").slideDown();
		}
	});
	
	// content van submenu naar de kleine slider
	$(".subMenuSlideSmallFetch").click(function(e){
		e.preventDefault();
		loadContent($("#sliderSmall"), $(this).attr("href"), "fetch");
		if($.browser.msie && $.browser.version < 7) {
			$("#sliderSmallWrapper").fadeIn(1000);
		} else {
			$("#sliderSmallWrapper").slideDown();
		}
	});
	
	$(".showTlt").live('click', function(e) {
		$(".tlt").hide();
		$(".closeSmall").click();
		$("#tlt_" + $(this).attr("rel")).show();
	});
	
	
	// content fetchInnerMap naar fetchInnerMap (de gehele kaart vervangen)
	// plus de legende erbij
	$(".subMenuFetchInnerMap").click(function(e){
		e.preventDefault();
		
		$(this).parent().parent().find("li").removeClass("active");
		$(this).parent().addClass("active");
		
		loadContent($("#fetchInnerMap"), $(this).attr("href"), "fetchInnerMap");
		loadContent($("#subMenuFetchInnerLegende"), $(this).attr("href"), "subMenuFetchInnerLegende");
	});
	
	// content van ingeladen menu naar de grote slider
	$("ul.map_menu a").live('click', function(e) {
		if($(this).hasClass("normal")) {
			return true;
		}
		e.preventDefault();
		$("a").removeClass("active");
		$(this).addClass("active");
		
		if($("#sliderBig #contentRight").length) {
			loadContent($("#sliderBig #contentRight"), $(this).attr("href"), "fetchInner");
		} else {
			loadContent($("#fetchInner"), $(this).attr("href"), "fetchInner");
		}
	});
	
	$(".menu li").hover(function() {
		$(this).addClass("hactive");
		$(this).children("ul").show();
		$(this).css("z-index", 100);
		$(this).children("ul").css("z-index", 100);
		
	}, function() {
		$(this).removeClass("hactive");
		$(this).children("ul").hide();
	});
	
	// accordion menu
	$(".container ul.submenu").hide();
	
	$('.container ul.map_menu').children("li").children("a").click(function(e){
		e.preventDefault();
		if($(this).hasClass('subMenuSlideSmall')) {
			
		} else {
			if($(this).hasClass('subMenuFetchInnerMap')){
				
			} else {
				$('.container ul.map_menu').each(function(ch){
					$(this).find('li ul').slideUp();
					$(this).find('li div.submenu3').slideUp();
				});
				if($.browser.msie && $.browser.version < 7) {
					$(this).next().fadeIn(1000);
				} else {
					$(this).next().slideDown();
				}
				$(this).parent().find("ul li a:first").click().parent().addClass("active");
			}
		}
	});
	
	// accordion menu inner
	$('.container #fetch ul.map_menu li a.accordion').live('click', function(e) {
		e.preventDefault();
		$(this).next().slideToggle('normal');
		return false;
	});
	
	// sluit de grote slider
	$("a.close").live('click', function(e) {
		e.preventDefault();
		$("#sliderBig").slideUp();
		$("#sliderSmall").slideUp();
		$("ul.menu li").removeClass("active");
		$("." + activePage).addClass("active");
	});
	// sluit de klein slider
	$("a.closeSmall").live('click', function(e) {
		e.preventDefault();
		$("ul.submenu li").removeClass("active");
		$("#sliderSmallWrapper").slideUp();
	});
	
	// fotobox bij POI
	$(".show_fotobox").live('click', function(event) {
		event.preventDefault();
		if($.browser.msie && $.browser.version < 7) {
			$("#fotobox").fadeIn(1000);
		} else {
			$("#fotobox").slideDown();
		}
	});
	$(".close_fotobox").live('click', function(event) {
		event.preventDefault();
		$("#fotobox").slideUp();
	});
	$(".close_mediabox").live('click', function(event) {
		event.preventDefault();
		$("#flashobj").html("");
		$("#mediabox").slideUp();
	});
	$(".fotobox_click").live('click', function(event) {
		event.preventDefault();
		$(".fotobox_bullet").attr("src", "/gfx/bullet-grey.png");
		$(this).find('img').attr("src", "/gfx/bullet-orange.png");
		$(".fotobox_foto").attr("src", $(this).attr("href"));
		$(".fotobox_foto_href").attr("href", $(this).attr("href"));
		$(".fotobox_text").hide();
		$("#fotobox_text_" + $(this).attr("rel")).show();
	});
	// POI tekst
	$(".show_description").live('click', function(event) {
		event.preventDefault();
		
		$("div#descriptions div").hide();
		$("#text_" + $(this).attr("rel")).show();
	});
	
	$(".makeFav").live('click', function(e) {
		e.preventDefault();
		//alert("'" + favLocation + "?addFav=" + favUrl + "&favTitle=" + favTitle + "'");
		document.location = favLocation + "?addFav=" + favUrl + "&favTitle=" + favTitle;
	});
	
	
	
	$("#openimages").click(function() {
		$("#nimages").hide();
		$("#images").show();
	});
	$("#closeimages").click(function() {
		$("#images").hide();
		$("#nimages").show();
		closeImage();
	});
	$(".imagelist a").click(function() {
		openImage();
	});

    $(".infoinfo").live("mouseenter", function(evt) {
		if($(this).attr("rel") =="info") {
			v = $("#tip" + $(this).attr("rel"));
		} else {
        	v = $("#tip" + $(this).attr("rel"));
			$(v).css("top", (evt.pageY - 140) + "px");
			$(v).css("left", (evt.pageX - 100) + "px");
		}

        $(v).show();
    });
    
    $(".infoinfo").live("mouseleave", function(evt) {
		v = $("#tip" + $(this).attr("rel"));
        $(v).hide();
    });
    
    
    $("area.slide").live("mouseenter", function(evt) {
    	v = $("#tip" + $(this).attr("rel"));
		$(v).css("top", (evt.pageY - 140) + "px");
		$(v).css("left", (evt.pageX - 100) + "px");
        $(v).show();
    });
    $("area.slide").live("mousemove", function(evt) {
		$(".mapmap .info").show();

    	v = $("#tip" + $(this).attr("rel"));
		$(v).css("top", (evt.pageY - 140) + "px");
		$(v).css("left", (evt.pageX - 100) + "px");
	
    });

    $("area.slide").live("mouseout", function(evt) {
	   	$(".mapmap .info").hide();
	    v = $("#tip" + $(this).attr("rel"));
	    $(v).hide();
    });

    
    $(".mediapop").live("click", function(event) {
    	event.preventDefault();
    	$(".mediainfo").hide();
    	r = $(this).attr("rel");
    	$("#" + r).show();
    	var flv = this.href;
    	var bgimage = $(this).attr("image");
    	var mediatype = $(this).attr("mediatype");
    	var myType = "video";
    	
    	if(mediatype == "mp3"){
    		myType = "audio";
    	}
    	
    	if(mediatype == "mp3" && bgimage == ""){
    		bgimage = "/gfx/flowplayer_audio_bg_" + activeLanguage + ".gif";
    	}
    	
    	$("#mediabox").slideDown(500, function() {
    		flowplayer("flashobj", {
				src: "/flash/flowplayer-3.2.3.swf",
				version: [9, 115],
				onFail: function()  {
					document.getElementById("info").innerHTML =
						"You need the latest Flash version to view MP4 movies. " +
						"Your version is " + this.getVersion()
					;
				}
			}, {
				clip:{
					url:flv
					,type:myType
				}
				, canvas: {
					backgroundImage: 'url(' + bgimage + ')'
				}
			});
    	});
    });
    
    submitForm("search");
    
	initMapHilite()
	$('a.lightbox').lightBox();
	
	if($(".scrolhor").length > 0) {
		if($(".scrolhor").find("div .list").length > 1) {
			$(".scrolhor").scrollable({ vertical: true});
		} else {
			$("#actions").hide();
		}
	}
	
	$('.hilightlink').mouseover(function(e) {
		$(".tooltip").hide();
		$('#' + $(this).attr("rel")).mouseover();
		$(".tooltip").hide();
	}).mouseout(function(e) {
		$('#' + $(this).attr("rel")).mouseout();
	}).click(function(e) {
		e.preventDefault();
	});
	
	$('.hilightlink_deelkaart').mouseover(function(e) {
		$(".tooltip").hide();
		$('#' + $(this).attr("rel")).mouseover();
		$(".tooltip").hide();
	}).mouseout(function(e) {
		$('#' + $(this).attr("rel")).mouseout();
	}).click(function(e) {
		document.location.href=$(this).attr("href");
	});
	
	init();
});



function loadContent(sliderTarget, uri, divId) {
	
	$(document).scrollTop(0);
	if($(sliderTarget).is(":visible")) {
		sliderTarget.slideUp(500, function() {
			sliderTarget.load(uri + "?ajax=1 #" + divId, function(response, status, xhr) {
				if (status == "error") {
					sliderTarget.html("error: " + xhr.status + " " + xhr.statusText);
				}
				if($.browser.msie && $.browser.version < 7) {
					sliderTarget.fadeIn(1000);
				} else {
					sliderTarget.slideDown();
				}
				initMapHilite();
			});
		});
	} else {
		sliderTarget.load(uri + " #" + divId, function(response, status, xhr) {
			if (status == "error") {
				sliderTarget.html("error: " + xhr.status + " " + xhr.statusText);
			}
			if($.browser.msie && $.browser.version < 7) {
				sliderTarget.fadeIn(1000);
			} else {
				sliderTarget.slideDown();
			}
			initMapHilite();
		});
	}
}

function log(v) {
	if(typeof(console) !== 'undefined' && console != null) {
		console.log(v);
	}
}



function openImage() {
	$("#images .info").show();
	$("#images").css("top","-600px");
	$("#images").css("height","732px");
} 
function closeImage() {
	$("#images .info").hide();
	$("#images").css("top","0px");
	$("#images").css("height","132px");
}

function initMapHilite() {
		$('.map').maphilight.defaults = {
		fill: true,
		fillColor: '0094FF',
		fillOpacity: 0.2,
		stroke: true,
		strokeColor: 'FF0000',
		strokeOpacity: 1,
		strokeWidth: 1,
		fade: false,
		alwaysOn: false,
		neverOn: false,
		groupBy: false
	}
	$('.map').maphilight();
}

function init() {
	
	$("a.block").live("click", function(ev) {
		ev.preventDefault();
		loadImage(this);
	});
	
	
}
function loadImage(w) {
		
		$(".multiimage").hide();
		$("a.block").removeClass("active");
		$(w).addClass("active");
		
		activeImage = $(w).attr("numindex")-1;
		
		if($(w).attr("blockid") == ""){
			blockId = 1;
		} else {
			blockId = $(w).attr("blockid");	
		}
		
		var flow = false
		
		$("#pic" + blockId).fadeIn();
		if($("#pic" + blockId).find(".player").length > 0) {
			$("#pic" + blockId).find(".player").each(function() {
				flow = flowplayer($(this).attr("id"), "/flash/flowplayer.commercial-3.1.5.swf", {api:true,key: flowplayerKey});
			});
		} else {
			if(flow != false) {
				flow.close();
				flow = false;
			}
		}
		return false;
	}	


var submitForm = function(formName){
	$("#form_" + formName).live("submit",function(e){
		$.ajax({
			type: "POST",
			url: $(this).attr("action"),
			data: $(this).serialize(),
			success: function(result){
				var splitted = result.split("::");
				log(splitted[0]);
				if(splitted[0] == "true") {
					$("#form_" + formName).slideUp();
				}
				$("#response_" + formName).html(splitted[1]);
			}
		});
		e.preventDefault();
		return false;
	});
}
