$(document)
		.ready(
				function() {
					
					$("#menu li:not(.subMenu li)").hover(
						function(){
							$(this).addClass("over");
						},
						function(){
							$(this).removeClass("over");
						}
					);
					
					/*$("#submenuMore").click(function(e){
						
						e.preventDefault();
						
						$("#submenuMore").hide();
						
						$("#submenuContainer").animate({height: $("#submenuContainerInner").height()}, 600, function(){
							$("#submenuLess").css({"display" : "block"});
						});
						
						return false;
					});
					
					$("#submenuLess").click(function(e){
						
						e.preventDefault();
						
						$("#submenuLess").hide();
						
						$("#submenuContainer").animate({height: 57}, 600, function(){
							$("#submenuMore").show();
						});
												
						return false;
					});*/
					
					$("#submenu").hover(function(){
						
						$("#submenuContainer").stop().animate({height: $("#submenuContainerInner").height()}, 600, function(){
							$("#submenuMore").hide();
							$("#submenuLess").css({"display" : "block"});
						});
					}, function(){
						$("#submenuContainer").stop().animate({height: 66}, 600, function(){
							$("#submenuLess").hide();
							$("#submenuMore").show();
						});
					});
					
					$("#branche-select").change(
							function() {
								$("#phone-number").html(
										$("#branche-select option:selected")
												.val())
							});

					$('#callme').focus(function() {
						if ($(this).val() == 'Uw telefoonnummer...')
							$(this).val('');
					});
					$('#callme').blur(function() {
						if ($(this).val() == '')
							$(this).val('Uw telefoonnummer...');
					});
					
					$('#btn-callme').click(function(e){
						e.preventDefault();
						
						$.ajax({
						
							type: 'POST', 
							url: '/klantenservice/bel-mij-terug', 
							data: {"action" : "sendCallMeBackForm", "txtName": "Anoniem", "txtPhone": $("#callme").val() },
							success: function(response){
								$("#frmCallMeBack").html(response);
								_gaq.push(['_trackPageview', '/bel-mij-terug-verzonden']);
							}
						});
						
						return false;
					});
					
					$('#apply').focus(function() {
						if ($(this).val() == 'Uw e-mailadres')
							$(this).val('');
					});
					$('#apply').blur(function() {
						if ($(this).val() == '')
							$(this).val('Uw e-mailadres');
					});
					
					$('#contact-banner .btn-contact').click(function(e){
						
						e.preventDefault();
						
						$(this).fadeOut(500);
						$(this).parent().find(".cta-arrow").fadeOut(500);
						
						$(this).parent().find(".contactform-inline").show("blind", { direction: "vertical"}, 500, function(){
							
							$(this).parent().find(".close-contactform-inline").fadeIn(500);
						});
						
						return false;
					});
					
					$(".close-contactform-inline").click(function(e){
						
						e.preventDefault();
						
						$(this).fadeOut(500);
						
						$(this).parent().find(".contactform-inline").hide("blind", { direction: "vertical"}, 500, function(){
							
							$(this).parent().find(".cta-arrow").fadeIn(500);
							$(this).parent().find(".btn-contact").fadeIn(500);
						});
						
						return false;
					});
					
					/*$('.overview-items>li>a').each(function(index) {
						$(this).click(function() {
							$('#slider').tinycarousel_move(index + 1);
							return false;
						});
					});*/

					if ($('#slider').length > 0) {
						$('#slider').tinycarousel({
							axis : 'x', 
							interval : true, 
							intervaltime : 8000, 
							duration : 1000, 
							animation : true, 
							pager: true
						});
					}

					$('.faq-item').click(function() {
						$(this).parent().next().toggle();
					});

					$("#situation p").append("<span id='end-quote'></span>");

					var map_created = false;

					if ($('#accordion').length > 0) {
						$("#accordion")
								.accordion(
										{
											active : 'none',
											collapsible : true,
											autoHeight : false,
											change : function(event, ui) {
												event.preventDefault();

												if ($(this).accordion("option",
														"active") == 7) {
												}

												try {
													var stateObj = {
														foo : "bar"
													};
													history.pushState(
														stateObj,
														event.currentTarget.activeElement.attributes.id.nodeValue,
														"/klantenservice/" + event.currentTarget.activeElement.attributes.id.nodeValue
													);
												} catch (err) {
													// Handle errors here
												}
											}
										});

					}

					var create_map = function() {
						var myLatlng = new google.maps.LatLng(52.13, 5.29);
						var myOptions = {
							zoom : 8,
							center : myLatlng,
							mapTypeId : google.maps.MapTypeId.ROADMAP
						}
						var map = new google.maps.Map(document
								.getElementById("map_canvas"), myOptions);
					};

					var geolocation = function() {
						// Try W3C Geolocation (Preferred)
						if (navigator.geolocation) {
							browserSupportFlag = true;
							navigator.geolocation.getCurrentPosition(function(
									position) {
								initialLocation = new google.maps.LatLng(
										position.coords.latitude,
										position.coords.longitude);
								map.setCenter(initialLocation);
							}, function() {
								handleNoGeolocation(browserSupportFlag);
							});
							// Try Google Gears Geolocation
						} else if (google.gears) {
							browserSupportFlag = true;
							var geo = google.gears.factory
									.create('beta.geolocation');
							geo.getCurrentPosition(function(position) {
								initialLocation = new google.maps.LatLng(
										position.latitude, position.longitude);
								map.setCenter(initialLocation);
							}, function() {
								handleNoGeoLocation(browserSupportFlag);
							});
							// Browser doesn't support Geolocation
						} else {
							browserSupportFlag = false;
							handleNoGeolocation(browserSupportFlag);
						}
						function handleNoGeolocation(errorFlag) {
							map.setCenter(initialLocation);
						}
					};

					$("#btn-call-me-back").click(
							function() {
								$("#contact-content #contact-slide-block")
										.slideToggle('2000');
							});

					$(".block .places a").hover(function() {
						$(this).find("span").fadeIn();
					}, function() {
						$(this).find("span").fadeOut();
					});

					// When page loads...
					$(".tab-container div.panel").hide(); // Hide all content
					$("ul.tab-buttons li:first").addClass("active").show(); // Activate
					// first tab
					$(".tab-container div.panel:first").show(); // Show first
																// tab
					// content

					// On Click Event
					$("ul.tab-buttons li").click(function(event) {
						event.preventDefault();
						$("ul.tab-buttons li").removeClass("active"); // Remove
																		// any
						// "active"
						// class
						$(this).addClass("active"); // Add "active" class to
													// selected
						                            // tab
						
						// scroll to tabs
						// var dest = $("#tabs").offset().top - 25;
						// $("html:not(:animated),body:not(:animated)").animate({ scrollTop: dest}, 700, function() { });
						
						$(".tab-container div.panel").hide(); // Hide all tab
																// content

						var url = jQuery(location).attr('href');

						href = $(this).find("a").attr("href").split('/');
						id = href[href.length - 1];

						var activeTab = '#' + id; // Find the href attribute
													// value to
						// identify the active tab + content
						$(activeTab).fadeIn(); // Fade in the active ID content
						
						
						/*
						 * if ( !$.browser.msie ) { var stateObj = { foo: "bar" };
						 * url = url.split('/'); url[url.length - 2] = id; var
						 * goodUrl = url.join("/");
						 * 
						 * history.pushState(stateObj, id, goodUrl
						 * );//$(this).find("a").attr("href")); }
						 */
						try {
							var stateObj = { foo: "bar" };
							
							url = url.split('/');
							url[url.length - 1] = id;

							var goodUrl = url.join("/");
	
							history.pushState(stateObj, id, goodUrl );
						} catch (err) {
							// Handle Errors
						}
						
						
						
						return false;
					});

					$('ul.tree li span.daddy a.blue').click(function() {
						if ($(this).parent().next('ul.tree').is(":visible")) {
							$(this).parent().siblings('ul.tree').slideUp();

							$(this).parent('span').attr('class', 'daddy');
						} else {

							$(this).parent().siblings('ul.tree').slideDown();

							$(this).parent('span').attr('class', 'expanded');
						}

						return false;
					});

					$("#callMeBackForm #txtDay").change(function() {
						if ($(this).val() == "Vrijdag") {
							$("#callMeBackForm #txtHour option:last").css({
								'display' : 'none'
							});
						} else {
							$("#callMeBackForm #txtHour option:last").css({
								'display' : 'block'
							});
						}
					});

				});

$(window).load(function(){
	
	$(".subMenu").each(function(){
		
		var smOffset = $(this).parent().offset();
		var smWidth = $(this).outerWidth();
		var wOffset = $("#wrapper").offset();
		var wWidth = $("#wrapper").outerWidth();
		
		if ((smOffset.left - wOffset.left) + smWidth > wWidth)
		{			
			var left = wWidth - ((smOffset.left - wOffset.left) + smWidth);
			
			$(this).css({"left" : left});
		}
		
	});
});
