
	function getInternetExplorerVersion() {
	
		var rv = -1; // Return value assumes failure.
		if (navigator.appName == 'Microsoft Internet Explorer') {

			var ua = navigator.userAgent;
			var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
		
		}
		return rv;
		
	}
	
	function checkVersion() {

		var msg = "You're not using Internet Explorer.";
		var ver = getInternetExplorerVersion();

		if ( ver > -1 ) {

			if ( ver > 6.0 ) msg = "You're using a recent copy of Internet Explorer."
			else msg = 1;
		
		}
	
		return msg;
	
	}
					
	$(window).load(function() {
		
		/***********************************************
		*** SEARCH SELECT FIX
		***********************************************/
	
		if(checkVersion() != 1) {
						
			var i = 0;
			$('#search > form > .jqTransformSelectWrapper').each(function() {
								
				if(i==0) {
					$(this).css({'width':'221px'});
					$(this).children('ul').css({'width':'219px'});
				}
				else if(i==1) {
					$(this).css({'width':'221px'});
					$(this).children('ul').css({'width':'219px'});
				}
				else if(i==2) {
					$(this).css({'width':'129px'});
					$(this).children('ul').css({'width':'127px'});
				}
				i++;
						
			});
			
		}
		
		$('ul > li > a').each(function() {
			
			if($(this).html() == 'Vuokrakohteet') {
				
				$(this).click(function() {

					$('.price').parent('div').hide();
					$('.price').val('');

				});


			}
			else {
				
				$(this).click(function() {

					$('.price').parent('div').show();

				});

			}

			if($(this).html() == 'Tontti' || $(this).html() == 'Muu') {
			
				$(this).click(function() {
	
					$('.m2').parent('div').hide();
					$('.m2').val('');

				});
				
			}
			else {
	
				$(this).click(function() {
	
					$('.m2').parent('div').show(); 

				});

			}
			
		});
		
		
		var shows = 0; 
		$('.showing > .item').each(function() { 
		
			shows++; 
			
		});
		
		$('.showall > a').click(function() {
			
			var i = shows;
			
			if(i > 0) { 
				
				i = i * 32; 
				if($('.showing').hasClass('open')) { 
					
					$('.showing').animate({'height':'32px'},500).removeClass('open'); 
					$(this).html('Näytä kaikki'); 
				
				} 
				else { 
				
					$('.showing').animate({'height': i + 'px'},500).addClass('open'); 
					$(this).html('Piilota'); 
					
				} 
			
			}
			
		});
			
		/***********************************************
		*** DESTINATION ITEMS LIST
		***********************************************/

		$('.itemContainer').attr('pos','0');
					
		var i = 0;
			
		$('.itemContainer > .item').each(function() {
							
			i++;
							
		});
						
		i = Math.ceil(i / 6);
						
		if(i==1) $('.browseNext').css({'display':'none'});
		
					
		var k = 0;
		while(k<i) {
						
			if(k==0) $('.dots').append('<img src="/themes/site/images/dot-active.png" pos="' + k + '">');
			else $('.dots').append('<img src="/themes/site/images/dot-inactive.png" pos="' + k + '">');
			k++;
		
		}
						
		$('.dots > img').click(function() {
							
			$('.dots > img').attr('src','/themes/site/images/dot-inactive.png');
			$(this).attr('src','/themes/site/images/dot-active.png');
							
			var thisPos = parseInt($(this).attr('pos'));
			var items = Math.ceil(parseInt($('.itemContainer > .item').length) / 6);
												
			if(items == (thisPos+1)) $('.browseNext').fadeOut();
			else $('.browseNext').fadeIn();
			if(thisPos > 0) $('.browsePrevious').fadeIn();
			else $('.browsePrevious').fadeOut();

			$('.itemContainer').fadeOut('slow', function() { $(this).css({'top': '-' + (thisPos*439) + 'px'})}).fadeIn('slow').attr('pos',thisPos);
								
		});
		
		/***********************************************
		*** SINGLE DESTINATION
		***********************************************/
		
		if($('#destination').attr('active') == 'yes') {
		
			var street = $('#destination').attr('street');
			var postcode = $('#destination').attr('postcode');
		
			geocoder = new google.maps.Geocoder();
			initMap(street, postcode);
	
			var i = 0;
						
			$('.imageContainer > img').each(function() {
							
				if(i == 0) $(this).css({'display':'block'});
				else $(this).css({'display':'none'});
				$(this).attr('pos',i);
				i++;
							
			});				
					
			var k = 0;
						
			while(k<i) {
						
				if(k==0) $('.dots').append('<img src="/themes/site/images/dot-active.png" pos="' + k + '" class="dot">');
				else $('.dots').append('<img src="/themes/site/images/dot-inactive.png" pos="' + k + '" class="dot">');
				k++;
							
			}
						
			$('.dots > .dot').click(function() {
					
				clearInterval($('.imageContainer').attr('interval'));
											
				$('.dots > img').attr('src','/themes/site/images/dot-inactive.png');
				$(this).attr('src','/themes/site/images/dot-active.png');
							
				var pos = parseInt($(this).attr('pos'));
	
				$('.imageContainer > img').fadeOut('slow', function() { 
							
					$(this).each(function() {
								
						if($(this).attr('pos') == pos) {
						
							if($(this).width() < $(this).height()) $(this).parent('div').css({'height':'585px'});
							else $(this).parent('div').css({'height':'330px'});
							$(this).fadeIn('slow').addClass('show');
							
						}
						else $(this).removeClass('show');
					
					});
								
				});						
				
			});
						
			theRotator();
					
		}
		
		/***********************************************
		*** MAP PAGE
		***********************************************/
		
		if($('#container').attr('type') == 'map') {
			
			geocoder = new google.maps.Geocoder();
			init('Rovaniemi');
			
		}
						
	});

	/***********************************************
	*** DESTINATION PAGE PICTURES
	***********************************************/

	function previous() {
					
		var pos = parseInt($('.itemContainer').css('top').replace("px",""))+439;
		var count = parseInt($('.itemContainer').attr('pos'))-1;
		var items = parseInt($('.itemContainer > .item').length) / 6;

		if(pos == 0) $('.browsePrevious').fadeOut();
		if((count+1) > 0) $('.browseNext').fadeIn();
					
		$('.itemContainer').fadeOut('slow', function() { $(this).css({'top': pos + 'px'})}).fadeIn('slow').attr('pos',count);
		$('.dots > img').each(function() {

			if($(this).attr('pos') == count) $(this).attr('src','/themes/site/images/dot-active.png');
			else $(this).attr('src','/themes/site/images/dot-inactive.png');
						
		});
											
	}
	
	function next() {
					
		var pos = parseInt($('.itemContainer').css('top').replace("px",""))-439;
		var count = parseInt($('.itemContainer').attr('pos'))+1;
		var items = parseInt($('.itemContainer > .item').length) / 6;
		
		if((items-1) < count) $('.browseNext').fadeOut();
		if(pos < 0) $('.browsePrevious').fadeIn();
							
		$('.itemContainer').fadeOut('slow', function() { $(this).css({'top': pos + 'px'})} ).fadeIn('slow').attr('pos',count);
		$('.dots > img').each(function() {

			if($(this).attr('pos') == count) $(this).attr('src','/themes/site/images/dot-active.png');
			else $(this).attr('src','/themes/site/images/dot-inactive.png');
					
		});
												
	}
					
	function move(pos) {
					
		$('.itemContainer').fadeOut('slow', function() { $(this).css({'top': pos + 'px'})}).fadeIn('slow');
					
	}
					
	function theRotator() {

		if($('.dots > img').length > 1) {
			
			var interval = setInterval('rotate()',5000);
			$('.imageContainer').attr('interval',interval);
						
		}
						
	}

	function rotate() {	

		var current = ($('.imageContainer img.show')?  $('.imageContainer img.show') : $('.imageContainer img.first'));
			
		var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('.imageContainer img.first') :current.next()) : $('.imageContainer img.first'));	
						
		if(next.width() < next.height()) {
			
			next.css({'display':'none'})
			.addClass('show')
			.fadeIn(1000)
			.parent('div').css({'height':'585px'});
			
		}
		else {
			
			next.css({'display':'none'})
			.addClass('show')
			.fadeIn(1000)
			.parent('div').css({'height':'330px'});
			
		}
						
		var nextPos = next.attr('pos');
						
		$('.dots > img').attr('src','/themes/site/images/dot-inactive.png');
							
		$('.dots > img').each(function() {
						
			if($(this).attr('pos') == nextPos) $(this).attr('src','/themes/site/images/dot-active.png');
						
		});
	
		current.fadeOut(1000)
		.removeClass('show');
	
	}			
	
	/***********************************************
	*** GOOGLE MAP
	***********************************************/


	var geocoder;
	var map;
	var marker;				

	function initMap(street,postcode) {
		
		address = street + ', ' + postcode;
				
	    geocoder.geocode( { 'address': address }, function(results, status) {
		
			if(status == google.maps.GeocoderStatus.OK) {
								
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
								
				addMarker(lat,lng,'');					
								
			}
    	
		});
						
	}	
									
	function init(address) {
	
	    geocoder.geocode( { 'address': address }, function(results, status) {
	
	       	if(status == google.maps.GeocoderStatus.OK) {
				
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
							
				showMap(lat,lng);					
							
			}

		});
					
	}	
				
	function showCity(address,zoom) {
				
		$('#cities > .cityContent > ul > li > a').each(function() {
					
			var html = $(this).html();
			html = html.replace(" &gt;","");
					
			$(this).removeClass('active').html(html);
					
		});
	
	    geocoder.geocode( { 'address': address }, function(results, status) {
	
	       	if(status == google.maps.GeocoderStatus.OK) {
						
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
							
				changeLocation(lat,lng,zoom);
							
			}

		});
				
	}	
				
	function changeLocation(lat,lng,zoom) {
					
		var latlng = new google.maps.LatLng(lat,lng);
		map.setCenter(latlng);
		map.setZoom(zoom);
																			
	}
				
	function fetch(id,address,title) {
				
	    geocoder.geocode( { 'address': address }, function(results, status) {
	
			if(status == google.maps.GeocoderStatus.OK) {
							
				var lat = results[0].geometry.location.lat();
				var lng = results[0].geometry.location.lng();
							
				addMarker(lat,lng,title);				
							
				$.post('?action=setCoordinate&coordinate=' + lat + ',' + lng + '&id=' + id,function(data) {});

        	}
			
		});
					
	}
	
	function showMap(lat,lng) {
				
		var latlng = new google.maps.LatLng(lat,lng);
		var options = {
			
			zoom: 12,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
			
		};
		map = new google.maps.Map(document.getElementById("map"), options);
		
		setMarkers();
									
	}
				
	function addMarker(lat,lng,title) {
				
		var latlng = new google.maps.LatLng(lat,lng);
		var image = new google.maps.MarkerImage('/themes/site/images/gmap_pin.png',
					new google.maps.Size(28, 39),
					new google.maps.Point(0,0),
					new google.maps.Point(12, 32)
		);
		
		if($('#destination').attr('active') == 'yes') {

			var options = {
				
				zoom: 12,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
				
			};
			map = new google.maps.Map(document.getElementById("map"), options);
	
		}
		

		var contentString = title;
		var infowindow = new google.maps.InfoWindow({
		
			content: contentString
		
		});
						
		var marker  = new google.maps.Marker({
        
			position: latlng,
			title: '',
			icon: image
        	
		});
					 
		marker.setMap(map);
		
		if($('#container').attr('type') == 'map') {

			google.maps.event.addListener(marker, 'click', function() {
		
				infowindow.open(map,marker);
			
			});
			
		}
					
	}
