	jQuery(document).ready(function(){
		$j("form.jqtransform").jqTransform();
		$j('.options').toggle();
		$j('.searchResult').toggle();

		$j(".optionSwitch").click(function() {
			 $j('.options').slideToggle("fast")
			 return false;
		});

		var animating = false;
		var paikallisetCache = [];

		if ($j.browser.msie && Math.floor($j.browser.version) == 7) {
			$j('.submenu').each(function() {
				$j(this).css('left', $j(this).parent().position().left + 'px');
			});
		}

		$j(".paikallisetMenuItem").mouseenter(function(event) {
			$j(".paikallisetMenuItem").removeClass("hover");
			$j(event.target).addClass("hover");
			if (!paikallisetCache[$j(event.target).attr('tpk')]) {
				$j.ajax({
				  url: '/toimipaikkaosoite?tpk='+$j(event.target).attr('tpk'),
				  success: function(data) {
					paikallisetCache[$j(event.target).attr('tpk')] = data;
					$j('.paikallisetShortContact').html(paikallisetCache[$j(event.target).attr('tpk')]);
				  }
				});
			}
			else {
				$j('.paikallisetShortContact').html(paikallisetCache[$j(event.target).attr('tpk')]);
			}
			return false;
		});
	    
		$j(".submitSearch").click(function(event) {
		    $j('.searchTarget').slideUp('fast');
			params = $j('#hakuform').serialize() + '&haku[kohde]=' + $j(event.target).attr('name');
			$j.ajax({
				type: "GET",
				url: "/haku",
				data: params,
			
			  success: function(data) {
			    $j('.closeOnSearch').slideUp('fast');
			    $j('.searchResult').html(data).slideDown('slow');
			  }
			});
			return false;
		});

		$j(".submitSearchOsto").click(function(event) {
		    $j('.searchTarget').slideUp('fast');
			formparams = $j('#hakuform').serialize();

			params = 'cgidata_Tyyppi_toimeksiantoJulkisuus=0' +  '&cgidata_Tyyppi_active=0' + '&cgidata_Tyyppi_kaupunki=0' + '&cgidata_Tyyppi_alue=0' + '&cgidata_Tyyppi_asuintilaMin=' + '&cgidata_Tyyppi_asuintilaMax=' + '&cgidata_Tyyppi_hintaMin=' + $j('#minField').val() + '&cgidata_Tyyppi_hintaMax=' + $j('#maxField').val() + '&cgidata_Tyyppi_hakusana=' + $j('#citySelect').val() + '&folder=Pub';

			if ($j('#huoneita').val() == '1') {
				params = params + '&yksioFoo=on';
			}
			else if ($j('#huoneita').val() == '2') {
				params = params + '&kaksioFoo=on';
			}
			else if ($j('#huoneita').val() == '3') {
				params = params + '&kolmioFoo=on&nelioFoo=on';
			}
			else if ($j('#huoneita').val() == '') {
				params = params + '&yksioFoo=&kaksioFoo=&kolmioFoo=&nelioFoo=';
			}

			$j.ajax({
				type: "POST",
				url: "/fi/AsuntoListausWeb.php",
				data: params,
			
			  success: function(data) {
			    $j('.closeOnSearch').slideUp('fast');
			    $j('.searchResult').html(data).slideDown('slow');
			  }
			});
			return false;
		});

		$j('#searchField').keypress(function(event) {
			if (event.which == '13') {
				$j(".submitSearch").click();
			}
		});

		$j('.quickSearch').click(function(event) {
			$j('#searchField').val($j(event.target).html());
			$j("#searchField").change();
			$j(".submitSearch").click();
		});

	    
		$j(".browseLeftButton").click(function() {
			if (!animating) {
				animating = true;
				$j('.browserInner').animate({
				    left: '+=300',
				    queue: false
				  }, 'slow', function() {
						$j('.browserInner').prepend($j('.browserItem').last().detach());
						$j('.browserInner').css('left', '-300px');
						animating = false;
				  });
				return false;
			}
		});
	
		$j(".browseRightButton").click(function() {
			if (!animating) {
				animating = true;
				$j('.browserInner').append($j('.browserItem').first().detach());
				$j('.browserInner').css('left', '0px');
				$j('.browserInner').animate({
				    left: '-=300',
					queue: false
				  }, 'slow', function() {
					animating = false;
				  });
				return false;
			}
		});
	

		$j('.paikallisetShortContact').ajaxStart(function(){
			//$j(this).prepend('ajax');
		});


		$j(document).ajaxStart(function(){
			//$j("#ajaxIndicator").show();
			$j("body").css("cursor", "progress");
		});
	
		$j(document).ajaxStop(function(){
			//$j("#ajaxIndicator").hide();
			$j("body").css("cursor", "");
		});


		$j(".paikallisetMenuItem").first().mouseenter();





		/* menu */

		var menuTimeout	= 500;
		var menuCloseTimer;
		var menuItem;
		var menuItemOld;

		function menuClose() {
			if(menuItem) {
				menuItem.slideUp('fast');
			}
		}

		function menuCloseOld() {
			if(menuItemOld) {
				//menuItem.css('display', 'none');
				menuItemOld.slideUp('fast');
			}
		}

		function menuTimer() {
			menuCloseTimer = window.setTimeout(menuClose, menuTimeout);
		}

		function menuCanceltimer() {
			if(menuCloseTimer) {
				window.clearTimeout(menuCloseTimer);
				menuCloseTimer = null;
			}
		}

		$j('.mainmenu').mouseover(function(){
			menuItem = $j('.submenu', this);
			menuCanceltimer();
		
			if (menuItemOld && menuItem.attr('id') != menuItemOld.attr('id')) {
			   	menuCloseOld();
			}
			menuItem.slideDown('fast');
			menuItemOld = menuItem;
		});

		$j('.mainmenu').mouseout(function(){
			menuTimer();
		});

		document.onclick = menuClose;

		$j('#menu').css({zIndex: 1000});





		$j('.overlayClose').click(function() {
			$j('.overlay').fadeOut('fast', function() {
				scrollBack();
			});
		});


		$j("#searchField").change(function(event) {
			if ($j(event.target).value != '') {
				$j('.searchFieldInput').removeClass('searchFieldWrapper');
			}
			else {
				$j('.searchFieldInput').addClass('searchFieldWrapper');
			}
		});
	    
		$j("#minField").change(function(event) {
			if ($j(event.target).value != '') {
				$j('.minFieldInput').removeClass('minFieldWrapper');
			}
			else {
				$j('.minFieldInput').addClass('minFieldWrapper');
			}
		});
	    
		$j("#maxField").change(function(event) {
			if ($j(event.target).value != '') {
				$j('.maxFieldInput').removeClass('maxFieldWrapper');
			}
			else {
				$j('.maxFieldInput').addClass('maxFieldWrapper');
			}
		});
	    

	});

	function asunto(asuntoID) {
		params = 'asunto=' + asuntoID;
		$j.ajax({
			type: "GET",
			url: "/asunto",
			data: params,
		
			success: function(data) {
				$j('.searchResult').slideUp('fast');
				$j('.searchTarget').html(data).slideDown('fast', function() {
					scrollObj('.searchTarget');
			});
		  }
		});
		return false;
	}


	function myyntiasunto(asuntoID, toimeksiantoID) {
		params = 'myyntiAsuntoId=' + asuntoID + '&toimeksiAntoId=' + toimeksiantoID + '&julkisuus=0';

		$j.ajax({
			type: "GET",
			url: "/fi/EsiteWeb.php",
			data: params,
		
			success: function(data) {
				$j('.searchResult').slideUp('fast');
				$j('.searchTarget').html(data).slideDown('fast', function() {
					scrollObj('.searchTarget');
			});
		  }
		});
		return false;
	}


	function returnToResults() {
		$j('.searchTarget').slideUp('fast');
		$j('.searchResult').slideDown('fast', function() {
			scrollBack();
		});
		return false;
	}

	function overlay(url) {
		$j('.overlayContent').hide();
		$j('.overlayIframe').attr('src', '');
		$j('.overlayIframe').height(0);
		baseurl = '';
		$j('.overlayIframe').attr('src', baseurl + url);
		$j('.overlayIframe').show();
		$j('.overlay').fadeIn('fast', function() {
			scrollObj('.overlay');
		});
		return false;
	}

	function overlayAjax(url, params) {
		$j('.overlayIframe').hide();
	    	$j('.overlayContent').html('');
		$j.ajax({
			type: "GET",
			url: url,
			data: params,
		  success: function(data) {
		    $j('.overlayContent').html(data);
		  }
		});
		$j('.overlayContent').show();
		$j('.overlay').fadeIn('fast', function() {
			scrollObj('.overlay');
		});
		return false;
	}

	function scrollObj(elem) {
		scrollPos.push($j('html, body').scrollTop());
		if (!$j(elem)) {
			elem = 'body';
			
		}
		pos = $j(elem).offset().top - 50;
		$j('html, body').animate({
			scrollTop: pos
		}, 'fast');
	}

	var scrollPos = [0];

	function scrollBack() {
		$j('html, body').animate({
			scrollTop: scrollPos.pop()
		}, 'fast');
	}

	function getDocumentHeight(doc) {
		var mdoc = doc || document;
		if (mdoc.compatMode=='CSS1Compat') {       
			return mdoc.body.offsetHeight;
		}
		else {
			if ($j.browser.msie) {
				return mdoc.body.scrollHeight;
			}
			else  {
				return Math.max($j(mdoc).height(), $j(mdoc.body).height());
			}
		}
	}


	function resizeiframe() {
		$j('.overlay').fadeIn('fast');
		$j('.overlayIframe').contents().find("body").css('padding-bottom', '20px');
		height = getDocumentHeight($j('.overlayIframe').contents().get(0));
		$j('.overlayIframe').height(height);
	}



