/* CUFON
-------------------------*/
Cufon.replace('h1, h2, h4', { fontFamily: 'Avenir LT Std' });
Cufon.replace('h3', { fontFamily: 'Angelina' });

/* SHADOWBOX
-------------------------*/
Shadowbox.init({
	language: "nl",
	overlayColor: "#000",
	overlayOpacity: "0.7",
	handleOversize: "none",
	displayNav: "false",
	viewportPadding: "0",
	players: ['html', 'iframe']
	});


/* TOGGLE (MEER INFORMATIE)
-------------------------*/
$(function(){
	$(".toggle_container").hide();
	
	$("span.trigger").click(function(){
		if ($(this).attr("active") == "true") {
			$(this).removeAttr("active");
			$(this).removeClass("active").next(".toggle_container").slideUp("slow");
		} else {
			$("span.trigger").removeClass("active").removeAttr("active");	
			$(this).attr("active", "true");
			$(".toggle_container").slideUp();
			$(this).addClass("active").next(".toggle_container").slideToggle("slow");
		}
	});
	//$("span.trigger").eq(0).click();
});

function initCarousel(carousel) {
	carousel.scroll(1);
}

/* TABS
-------------------------*/
$(function(){
	$('#tabs > div').hide();
	if ($('#tabsMenu > li.current').length <= 0) {
		$('#tabsMenu > li:eq(0)').addClass("current");
	}
	$('#tabsMenu > li').each(function(index) {
		if ($(this).hasClass("current")) {
			$('#tabs > div:eq(' + index + ')').show();
			$(this).addClass('active');
		}
	});
});

$(function() {
	$('#tabs ul#tabsMenu li a').hover(function() {
		$(this).stop().animate({height: 40}, 250);
	}, function() {
		if (!$(this).parent().hasClass("active")) {
			$(this).stop().animate({height: 34}, 250);
		} else {
			$(this).stop().animate({height: 40}, 250);
		}		
	});

	$('#tabs ul#tabsMenu li a').click(function() {

		if ($(this).parents("#toggleBig").length > 0) {
			if (!$(this).parent().hasClass("active")) {
				$('#tabs ul#tabsMenu li.active a').animate({height: 34}, 250);
				$('#tabs ul#tabsMenu li').removeClass("active");
				$(this).parent().addClass('active'); 
				var currentTab = $(this).attr('href');
				gotoTab(currentTab);
				initEasySlider();
					
				$("#tabs>div:visible").find(".jcarousel:not(.jcarousel-list)").jcarousel({
					vertical: true,
					scroll: 1,
					initCallback: initCarousel
				});
			}
			return false;
		} else {
			window.location.href = $(this).attr("href");
		}
		
	});

	var indexHekje = window.location.href.indexOf('#');
	if (indexHekje != -1) {
		gotoTab(window.location.href.substring(indexHekje));
		scrollUp();
	}
	
	if ($("#tabsMenu").size() > 0 && 
			$("#toggleBig").length == 0) {
		
		var id = $("#tabs").find("div[id^=tab]").show().attr("id");
		var section = id.substr(3);
		// Alle tabs inactief zetten (verlagen)
		$('#tabs ul#tabsMenu li').removeClass("active current");
		if (section == "Algemeen") {
			// Geen enkele tab moet hier active zijn.
		} else {
			// Zet de huidige tab actief (verhogen)
			$('#tabs ul#tabsMenu li.btn' + section).addClass("active").stop().animate({height: 34}, 250);
		}
	}
	
	
	var anchor = location.toString().split('#')[1];
	var isTab = $('li a[href=#' + anchor + ']').size() == 1;
	if (anchor && isTab) {
		$('li a[href=#' + anchor + ']').click();
		scrollUp();
	}
});

function gotoTab(sTab) {
	if (sTab.lastIndexOf('/') > -1) {
		sTab = sTab.substring(sTab.lastIndexOf('/') + 1);
	}
	$('#tabs > div').hide();
	$(sTab).show();
}

function scrollUp() {
	$.scrollTo(0);
	setTimeout(function() {
		// Again for IE, but delayed
		$.scrollTo(0);
	}, 500);
}

/* NOTES - DETAILPAGINA
-------------------------*/
$(function(){
	$('#notes > div').hide();
	if ($('#notesMenu > li.current').length <= 0) {
		$('#notesMenu > li:eq(0)').addClass("current");
	}
	$('#notesMenu > li').each(function(index) {
		if ($(this).hasClass("current")) {
			$('#notes > div:eq(' + index + ')').show();
			$(this).addClass('active');
		}
	});
});

$(function() {
	$('#notes ul#notesMenu li a').hover(function() {
		$(this).stop().animate({height: 23}, 250);
		$('#notes ul#notesMenu li a span').stop().animate({height: 23}, 250);
	}, function() {
		if (!$(this).parent().hasClass("active")) {
			$(this).stop().animate({height: 22}, 250);
		} else {
			$(this).stop().animate({height: 23}, 250);
		}		
	});

	$('#notes ul#notesMenu li a').click(function() { 
		if (!$(this).parent().hasClass("active")) {
			$('#notes ul#notesMenu li.active a').animate({height: 22}, 250);
			$('#notes ul#notesMenu li').removeClass("active");
			$(this).parent().addClass('active'); 
			var currentTab = $(this).attr('href'); 
			$('#notes > div').hide();
			$(currentTab).show();
		}
		if ($(this).attr("href") == "#tab5") {
			resetObjectMap();
		}
		return false;
	});
	$("#googleMapSmallLink").click(function(){
		$('#notes ul#notesMenu li a[href=#tab5]').click();
		return false;
	});
	$("#canIPayHypotheek").click(function(){
		$('#notes ul#notesMenu li a[href=#tab7]').click();
		return false;
	});
	
	function resetObjectMap() {
		var frame = $("#tab5").find("iframe");
		var src = frame.attr("src"); 
		frame.attr("src", src);
	}
});

/* WOONDOSSIER
-------------------------*/
$(function() {
	$('#woondossierGroot').hover(function() {
		$(this).stop().animate({width: 55}, 250);
		}, function() {
			$(this).stop().animate({width: 40}, 250);
		});
	});

$(function() {
	$('div#body #woondossierKlein').hover(function() {
		$(this).stop().animate({top: 120, height: 35}, 250);
		}, function() {
			$(this).stop().animate({top: 115, height: 40}, 250);
		});
	});
$(function() {
	$('div#body.page-home #woondossierKlein, div#body.page-vestiging #woondossierKlein, div#body.page-actie #woondossierKlein').hover(function() {
		$(this).stop().animate({top: 250, height: 35}, 250);
		}, function() {
			$(this).stop().animate({top: 245, height: 40}, 250);
		});
	});


/* DOCK
-------------------------*/
$(function(){
	$('#dock').Fisheye( {
		maxWidth: 40,
		items: 'a',
		itemsText: 'span',
		container: '.dock-container',
		itemWidth: 45,
		proximity: 40,
		alignment : 'left',
		valign: 'bottom',
		halign : 'center'
	});

	// Reposition proximity on document resize
	$(document).data("height", $(document).height());
	$(document).data("init", true);

	$(document).bind("mousemove click", function() {
		if ($(this).height() != $(this).data("height")) {
			$(this).data("height", $(this).height());
			if (!$(this).data("init")) {
				$(window).triggerHandler("resize");
			} else {
				$(this).data("init", false);
			}
		}
	});

});

/* SLIDER
-------------------------*/
$(function(){
	initEasySlider();

	$("#main .slider").easySlider({
		auto: true, 
		continuous: true
	});
});	

function initEasySlider() {
	$("div#tabs>div:visible:not(.easySlider)").each(function() {
		$(this).addClass("easySlider");
		$(this).find(".slider").easySlider({
			auto: true, 
			continuous: true
		});
	});
}

/* BROWSERWIDTH
-------------------------*/
function getBrowserWidth(){
	if (window.innerWidth){
		return window.innerWidth;}	
	else if (document.documentElement && document.documentElement.clientWidth != 0){
		return document.documentElement.clientWidth;	}
	else if (document.body){return document.body.clientWidth;}		
		return 0;}

function determineStyle(){
	
	var browserWidth = getBrowserWidth();
	var i, a, main;
	
	if (browserWidth <= 1025) {
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")) {
				a.disabled = true;
				if(a.getAttribute("title") == "klein") a.disabled = false;
			}
		}
	}
	
	if (browserWidth > 1025) {
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1
			&& a.getAttribute("title")) {
				a.disabled = true;
				if(a.getAttribute("title") == "groot") a.disabled = false;
			}
		}
	}
}

function doOnResize () {determineStyle();}
function doOnLoad () {determineStyle();}

$(window).bind("resize", doOnResize);
$(window).bind("load", doOnLoad);

/* ACTIES
-------------------------*/
var lowestActie;
var highestActie;
var iTimer;

$(function(){
	var delay=18000;

	$("div#acties ul").children().each(function(i, oElem) {
		var sId = oElem.getAttribute('id');
		if (!sId) {
			return true;
		}
		var iSub = sId.substring(5) - 0;
		$(oElem).hoverIntent(function(e) {
			showItem(iSub);
		}, function() {});
		if (!lowestActie || lowestActie > iSub) {
			lowestActie = iSub;
		}
		if (!highestActie || highestActie < iSub) {
			highestActie = iSub;
		}
	});
	iTimer = highestActie;
	autoRotate();
});

var curr_no=0;
var iTimerInterval = 18000;

function showItem(no){
	if (curr_no == 0) {
		$("#header_img"+no).show();
	}
	if(curr_no!=no){

		resetItems();
		$("#header_img"+curr_no).stop().fadeOut(300, function() {
			$("#header_img"+no).stop().fadeIn(300);
		});

		$("#actie"+no).each(function() {
			$("a, a>div", $(this)).css("background","#042e6f");
		});
		curr_no=no;
	}
}

function resetItems() {
	//$("#header_img"+curr_no).fadeOut(800);
	$("#actie"+curr_no + " a, #actie"+curr_no + " a>div").css("background","#b0bccd");
}

function autoRotate(){
	showItem(iTimer);
	iTimer++;
	if (iTimer > highestActie) {
		iTimer = lowestActie;
	}
	setTimeout("autoRotate()", iTimerInterval);
}

/* TOOLTIP
-------------------------*/
$(document).ready(function() {
	$(".mapDot").tooltip({ 
		effect: 'toggle',
		delay: 80,
		offset: [5, 88],
		relative: true
	}); 
});

/* JCAROUSEL
-------------------------*/
 $(function() {
	$("#tabs>div:visible").find(".jcarousel").jcarousel({
		vertical: true,
		scroll: 1
	});
});