$(function(){


	// Store submit button
	var submitBtnElm = $('#mailform input:submit');
	submitBtnElm.replaceWith('<a href="#" id="Submit">' + submitBtnElm.attr('value') + '</a>');
	
	$('label').labelOver('over-apply');
	
	var scrollAway = function(target, time) {
	
		$.scrollTo(target, {duration: time});
	
	};
	

	if($.browser.webkit) {
		
		$('body').addClass('webkit');
	
	}


	activateItem($('.item').first());
	
	$('.item').each(function(){
	
		var $item = $(this);
		var $nextLink = $item.find(".next");
		var $prevLink = $item.find(".prev");
		var $nextItem = $item.next();
		var $prevItem = $item.prev();
		var $eLink = $item.find(".E");
		var $navLinks = $item.find(".FloorNumbers").find("a");
		
		$item.bind('mouseover', function(e){
		
			activateItem($item);
		
		});
		
		$nextLink.bind('click', function(e){
		
			e.preventDefault();
			scrollAway($nextItem, 300);
			
			activateItem($nextItem);
		
		});
		
		$prevLink.bind('click', function(e){
		
			e.preventDefault();
			scrollAway($prevItem, 750);
		
		});
		
		$eLink.bind('click', function(e){
		
			e.preventDefault();
			
			if ($(this).parent().hasClass("eText")){
			
				$.scrollTo('#Entrance', {
								duration: 5200,
								onAfter: function() { activateItem($('#Entrance')); }
				});
			
				//scrollAway("#Entrance", 5200);
			
			} else {
			
				//scrollAway("#Entrance", 750);
				
				$.scrollTo('#Entrance', {
								duration: 750,
								onAfter: function() { activateItem($('#Entrance')); }
				});
			
			}
			
			//alert(1);
			
			

		});
		
		$navLinks.bind('click', function(e){
		
			e.preventDefault();
			
			var href = $(this).attr('href');
			
			$.scrollTo(href,
				{
					duration: 750,
					onAfter: function() {
						
						activateItem($(href));
					
					}
				});
			
		});
	
	});
	
	/**
	*
	*/
	function activateItem(itemElm) {
	
		$('.Active').removeClass('Active');
		itemElm.addClass('Active');
		
		indexOfActive = $('.item').index(itemElm);
		// $('.item').index($('#Entrance'));
	
	}
	
	//Keyboard-functions
	$('*').bind('keyup', function(e) {
	
	e.preventDefault();
	
	var code = (e.keyCode ? e.keyCode : e.which);
 		if(code == 40) { //Enter keycode
   			
   			var $active = $(".item.Active");
   			var $next = $active.next(".item");
   			
   			if ($next){
   				
				$.scrollTo($next,
					{
						duration: 750,
						onAfter: function() {
							
							activateItem($next);
						
						}
					});

   			}
   			
 		} else if (code == 69) {
 			
			$.scrollTo($('#Entrance'),
				{
					duration: 750,
					onAfter: function() {
						
						activateItem($next);
					
					}
				}); 			
 		
 		}
	
	});

	
	//ContentHideShow
	
	$("#About h3").next('.AboutText').hide();
	$("#About h3").next('.AboutText').children().css("opacity", "0");
	$("#About h3").toggle( 
	
	function(){
		var $title = $(this);
		var offset =  $title.offset();
		
		
		
		$("*").stop(true, true);
		
		$(this).next('.AboutText').slideDown('fast', function(){
			
			//scrollAway(offset.top - 20, 300);
			$title.next('.AboutText').children().animate({opacity:1}, 0, function(){
			
			$title.addClass('Open');
			
			});
		
		});
	
	},
	function(){
		
		var $title = $(this);
		$("*").stop(true, true);
		
		
		$title.next('.AboutText').children().animate({opacity:0}, 300, function(){
			
			$title.next('.AboutText').slideUp('fast', function(){
			
				$title.removeClass('Open');
			
			});
		
		});
		
		
		
		
		
	
	}
	);
	
	var contentCssAttributeToChange = 'min-height';

	// If we are running IE lt 7
	if($.browser.msie && $.browser.version < 7) {
	
		// This is to prevent the form to jump when the
		// parent item gets the Active-class
		$('#Entrance').addClass('touched');
	
		// IE6 dont get min-height so pass height to it instead since IE6 
		// thinks that height is min-height (taking stupidity to new heights...)
		contentCssAttributeToChange = 'height';
	
	}
	
	// To avoid having the page jumping up and down when flipping throught tabs
	resizeEntrance();
	
	$(window).bind('resize', function() {
	
		resizeEntrance();
	
	});
	
	/**
	* Added by BF
	*/
	function resizeEntrance() {
	
		$('#Content').css(contentCssAttributeToChange, $(window).height());
		$('#Entrance .itemContent').css(contentCssAttributeToChange, parseInt($('#Entrance').height()));
	
	}

	//jQuery Tabs for content
	$("#Content").tabs({
	
		selected: -1,
		collapsible: true,
		show: function(event, ui) { 
		
			// If the user has not scolled down enough not to miss
			// the tabbed content.
			if( $(window).scrollTop() <  $('#House').offset().top ) {
		
				scrollAway($("#House").offset().top - 25, 300);
			
			}
		
		}
	
	});
	

	//Dialog
	
	/**
	* FB*-fix 4 IE by BF@FFC in M-Ö .SE
	* * = FancyBox, !Facebook
	*/
	
	if($.browser.msie) {	
		
		var fbIeFixFullPath = window.location.toString();
		var fbIeFixExtra = 'styles/'
		
		// Loop all elements in .fancybox-ie
		$('.fancybox-ie').find('*').each(function(i) {
		
			// Cache
			var elm = $(this);
			
			// Get the filter for the element
			var curFilter = elm.css('filter');
			
			// If element has a filter
			if(curFilter !== '') {
			
				//alert(curFilter);
				
				// Get the value of src in the filter.
				// A reg exp would be nice here....
				var src = curFilter.substring(curFilter.indexOf("'")+1, curFilter.lastIndexOf(",")-1);
				
				//alert('Before: ' + curFilter);
				//alert('SRC: ' + src);
				
				// Fix the src value of the filter
				elm.css('filter', curFilter.replace(src, fbIeFixFullPath + fbIeFixExtra + src))
				
				//alert('After: ' + $(cssSelector).css('filter'));
				
			}
		
		});

		// Set the height if the fancybox overlay to the same as the document
		$('#fancybox-overlay').height(parseInt($(document).height()) + parseInt($(window).height()));
							
	}	
	
	/**
	* E.o. fancyboxfix for IE
	*/
	
	var fbTransition = 'elastic';
	
	if($.browser.msie) {
	
		fbTransition = 'fade';
	
	}
	
	$(".ImageGallery .modalLink").fancybox({
								titleShow: false,
								overlayShow: true,
								overlayOpacity: 0.6,
								overlayColor: '#000',
								titlePosition: fbTransition,
								transitionIn: fbTransition, 
								transitionOut: "fade"
							});
								
								
	//Mailform 
		
		$("#Submit").bind('click', function(e){
			
			e.preventDefault();
			$("#mailform").trigger('submit');
		
		});
		
		// Clear outcome when element gets focus
		$("#mailform .email").bind('focus', function() {
		
			if($('#mailform .signupOutcome').text() === $('#textSignupSuccess').text()) {
			
				$('#mailform .signupOutcome').html('');
				
			}
		
		});
		
		


		$("#rent").click(function(){
		
				if ($(this).is(":checked")){
				
				$("#phoneLabel").fadeIn('fast');
				
				} else {
				
				$("#phoneLabel").fadeOut('fast');
				
				}
				
		
		});
		
		$("#phoneLabel").hide();

		$("#mailform").validate({
			submitHandler: function(form) {
			
				$('.signupOutcome', form).text($('#textSendingSignupForm').text());
			
				$(form).ajaxSubmit({
				
					//target: $('.signupOutcome', form),
					data: {
						invalidAddressMsg: $('#textRequiredEmail').text(),
						successMsg: $('#textSignupSuccess').text()
					},
					success: function(returnText) {
						//$('.email', form).attr('value', '');
						$('.signupOutcome', form).text(returnText);
					}
					
				});
			},
			rules: {
    			email: {
    				required: true,
    				email: true
    			}
  			},
  			messages: {
  				email: {
  					required: $('#textRequiredEmail').text(),
  					email: $('#textRequiredEmail').text()
  				}
  			},
  			errorPlacement: function(error, element) {

				error.appendTo($('.signupOutcome'));

			}

		});
		

		
		
	
	//png-fix	
	$("em").addClass("png");
	
	//scrollhint 
	
	/*
$(window).scroll(function(){
	
		$("#item1").find(".nextText, .eText").find(".bouncer").effect("bounce", { direction:'up', times:4, distance: 50 }, 300);
	
	});
*/								
	
	/**
	* SOm ekind of fix for avoiding having the ocntent jimping up and down on resize
	*/
	
	// Get the current scroll position in percent of the documents height
	var p = $(window).scrollTop() / $(document).height();
	
	// Bind a function to the resize-event
	$(window).bind('resize', function() {	
		
		// Catch the new percent value
		$(document).scrollTop(p * $(document).height());
	
	});
	
	$(window).bind('scroll', function() {

		p = $(document).scrollTop() / $(document).height();
	
	});

});
