﻿$(document).ready(function(){
	
	
	
	//Function to update content min height when window size change
	$(window).resize(function(){
		if(!$.browser.msie)
		{
			adjustRootWrapperHeight();
		}
	});
	//Adjust content wrapper min height
	adjustRootWrapperHeight = function(){
		var docHeight = $(document).height();
		$('#rootWrapper').css({'min-height':docHeight});
	}
	
	//TOP MENU HOVER FUNCTIONS
	$('#topMenuTipsButton').hover(function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'-4px'},'fast',function(){
			$(this).animate({top:'0px'},'fast')
		});
	},function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'0px'},'fast');
	});
	$('#topMenuOffertButton').hover(function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'-6px'},'fast',function(){
			$(this).animate({top:'0px'},'fast')
		});
	},function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'0px'},'fast');
	});
	$('#topMenuKontaktButton').hover(function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'-4px'},'fast',function(){
			$(this).animate({top:'0px'},'fast')
		});
	},function() {
		$(this).children(':first').stop();
		$(this).children(':first').animate({top:'0px'},'fast');
	});
	//Preload heavy images before PLUGG animation
	startLoadingIMGForOneAnimation = function()
	{
		if($('#pluggOneContentWrapper').length)
		{
			var preloadIMG1 = $('<img />')
				.attr('src', '/images/template/truck_large.png')
				.load(function(){
					startPluggOneAnimation();
			});
		}
	}
	//Plugg One Animation
	startPluggOneAnimation = function(){
		$('#pluggOneTextRow1').delay(800).animate({top:'95px'},600);
		$('#pluggOneTextRow2').delay(1200).animate({top:'115px'},600);
		$('#pluggOneTextRow3').delay(1600).animate({top:'135px'},600);
		$('#pluggOneTextRow4').delay(2000).animate({top:'155px'},600);
		$('#pluggOneTextRow5').delay(2400).animate({top:'175px'},600,function(){
			if($.browser.msie)
			{
				setTimeout(function(){
					$('#pluggOneRedCircle').show();
					$('#pluggOneButton').show();
				}, 500);
			}
			else
			{
				$('#pluggOneRedCircle').delay(500).fadeIn(800);
				$('#pluggOneButton').delay(500).fadeIn(800);
			}
		});
	}
	//When animation has run once, just show the content on reload
	showPluggWithoutAnimation = function(){
		$('#pluggOneTextRow1').css({top:'95px'});
		$('#pluggOneTextRow2').css({top:'115px'});
		$('#pluggOneTextRow3').css({top:'135px'});
		$('#pluggOneTextRow4').css({top:'155px'});
		$('#pluggOneTextRow5').css({top:'175px'});
		$('#pluggOneRedCircle').show();
		$('#pluggOneButton').show();
	}
	//Plugg One Needle on Map
	$('#pluggOneGoteborgHover').hover(function(){
		$('#pluggOneGoteborgNeedle').show();
	},function(){
		$('#pluggOneGoteborgNeedle').hide();
	});
	$('#pluggOneKungalvAleHover').hover(function(){
		$('#pluggOneAleNeedle').show();
		$('#pluggOneKungalvNeedle').show();
	},function(){
		$('#pluggOneAleNeedle').hide();
		$('#pluggOneKungalvNeedle').hide();
	});
	$('#pluggOneUddevallaHover').hover(function(){
		$('#pluggOneUddevallaNeedle').show();
	},function(){
		$('#pluggOneUddevallaNeedle').hide();
	});
	$('#pluggOneMellerudHover').hover(function(){
		$('#pluggOneMellerudNeedle').show();
	},function(){
		$('#pluggOneMellerudNeedle').hide();
	});
	//Fix target in a links
	$('a.newWindow').attr({target:'_new'});
	//Adjust root wrapper height when page loads
	adjustRootWrapperHeight();
	//Start animation for plugg one
	startLoadingIMGForOneAnimation();
	
	
	
	
	//Offert
	$('#offertStartText').show();
	$('#offertButtonPrivat').fadeIn();
	$('#offertButtonForetag').fadeIn();
	$('#offertButtonStad').fadeIn();
	
	$('#offertButtonPrivat').click(function(){
		showOffertForm('privat');
	});
	$('#offertButtonForetag').click(function(){
		showOffertForm('foretag');
	});
	$('#offertButtonStad').click(function(){
		showOffertForm('stad');
	});
	
	showOffertForm = function(formType){
		$('#offertStartText').hide();
		$('#formOffertType').val(formType);
		$('#offertButtonPrivat').unbind('click').css({cursor:'auto'});
		$('#offertButtonForetag').unbind('click').css({cursor:'auto'});
		$('#offertButtonStad').unbind('click').css({cursor:'auto'});
		//Hide menu items not choosen
		if(formType!='privat')
		{
			$('#offertButtonPrivat').animate({height:0});
		}
		if(formType!='foretag')
		{
			$('#offertButtonForetag').animate({height:0});
		}
		if(formType!='stad')
		{
			$('#offertButtonStad').animate({height:0});
		}
		//Change page header
		if(formType=='privat')
		{
			$('#offertFormHeader').text('Offert för "Privatflytt"');
		}
		else if(formType=='foretag')
		{
			$('#offertFormHeader').text('Offert för "Företagsflytt"');
		}
		else if(formType=='stad')
		{
			$('#offertFormHeader').text('Offert för "Flyttstäd"');
		}
		//Add datepickers
		$('#formOffertMoveInDate').datepicker();
		$('#formOffertMoveOutDate').datepicker();
		$('#formOffertCleaningDate').datepicker();
		//Hide form inputs not used
		if(formType=='privat')
		{
			$('#jsFormOffertOrgNumb').hide();
			$('#jsFormOffertCompanyName').hide();
			$('#jsFormOffertCompanyRef').hide();
			$('#jsFormOffertCleaningHeader').hide();
			$('#jsFormOffertCleaningDate').hide();
			$('#jsFormOffertMoveOutCompanyArea').hide();
			$('#jsFormOffertMoveOutWorkstations').hide();
			$('#jsFormOffertMoveInCompanyArea').hide();
			$('#jsFormOffertMoveInWorkStations').hide();
			$('#jsFormOffertCleaningRooms').hide();
			$('#jsFormOffertCleaningArea').hide();
			$('#jsFormOffertMoveInAttic').hide();
			$('#jsFormOffertMoveInBasement').hide();
		}
		else if(formType=='foretag')
		{
			$('#jsFormOffertFirstName').hide();
			$('#jsFormOffertLastName').hide();
			$('#jsFormOffertCleaningHeader').hide();
			$('#jsFormOffertCleaningDate').hide();
			$('#jsFormOffertMoveOutArea').hide();
			$('#jsFormOffertMoveOutAttic').hide();
			$('#jsFormOffertMoveOutBasement').hide();
			$('#jsFormOffertCleaningRooms').hide();
			$('#jsFormOffertCleaningArea').hide();
			$('#jsFormOffertMoveInAttic').hide();
			$('#jsFormOffertMoveInBasement').hide();
		}
		else if(formType=='stad')
		{
			$('#jsFormOffertOrgNumb').hide();
			$('#jsFormOffertCompanyName').hide();
			$('#jsFormOffertCompanyRef').hide();
			$('#jsFormOffertMore1Header').hide();
			$('#jsFormOffertMore1TextArea').hide();
			$('#jsFormOffertMoveOutHeader').hide();
			$('#jsFormOffertMoveOutDate').hide();
			$('#jsFormOffertMoveOutAdress').hide();
			$('#jsFormOffertMoveOutPostalAdress').hide();
			$('#jsFormOffertMoveOutFloorElevator').hide();
			$('#jsFormOffertMoveOutArea').hide();
			$('#jsFormOffertMoveOutCompanyArea').hide();
			$('#jsFormOffertMoveOutWorkstations').hide();
			$('#jsFormOffertMoveInHeader').hide();
			$('#jsFormOffertMoveInCompanyArea').hide();
			$('#jsFormOffertMoveInWorkStations').hide();
			$('#jsFormOffertMoveInDate').hide();
			$('#jsFormOffertMoveOutAttic').hide();
			$('#jsFormOffertMoveOutBasement').hide();
			$('#jsFormOffertMoveOutHelpToPack').hide();
			$('#jsFormOffertMoveOutCleaningHelp').hide();
		}
		//
		$('#formOffertSubmitButton').click(function(){
			sendOffertToServer();
		});
		
		
		
		//Show form / different depending on browser
		if($.browser.msie)
		{
			$('#offertFormWrapper').show();
		}
		else
		{
			$('#offertFormWrapper').fadeIn();	
		}
	
		
		
	}
	
	
	
	
	sendOffertToServer = function(){
		
		
		
		var formData = $('#offertForm').serialize();
		var ajaxResponse = '';
		
		$.ajax({
   			type: 'POST',
   			url: '/offert.cfc',
   			data: {method:'saveOffertData','formData':formData},
			async: false,
			dataType: 'json',
   			success: function(msg){
				ajaxResponse = msg;
			}
		});
		
		if(typeof ajaxResponse!='object')
		{ alert('Ett oväntat fel uppstod!\nOm felet kvarstår är vi mer än tacksamma om du gör oss uppmärksamma på det.\n\nTack!'); }
		else
		{
			if(ajaxResponse.SUCCESS==true)
			{
				if($.browser.msie)
				{
					$('#offertFormWrapper').hide(function(){ $('#offertSuccessText').show(); });
				}
				else
				{
					$('#offertFormWrapper').fadeOut( function(){ $('#offertSuccessText').fadeIn(); } );
				}	
			}
			else
			{
				//Reset red color fiedls
				$('.offertFormRedBorder').removeClass('offertFormRedBorder');
				
				if(ajaxResponse.ERRORFIELDS.length>0)
				{
					for(i=0;i<ajaxResponse.ERRORFIELDS.length;i++)
					{
						$('#' + ajaxResponse.ERRORFIELDS[i] ).addClass('offertFormRedBorder');
					}	
				}
				if(ajaxResponse.MESSAGE.length>0)
				{
					alert(ajaxResponse.MESSAGE);	
				}
			}
		}
		
	}
	
});
