(function ($) {

  $(document).ready(function () {
  
    // Guestbook
    $forms = $('.guestbook form');
    
    $('input.query', $forms)
      .each(function() {
        $(this).val($('.val-' + $(this).attr('id'), $forms).val());
      })
      .focus(function() {
        if ($(this).val() == $('.val-' + $(this).attr('id'), $forms).val()) {
          $(this).removeClass('inactive')
                 .val('');
        }
      })
      .blur(function() {
        if ($(this).val() == '') {
          $(this).addClass('inactive')
                 .val($('.val-' + $(this).attr('id'), $forms).val());
        }
      });
      
    // Sites
    
    // RT90 to WGS84 extrated from script by Arnold Andreasson, 2007. http://mellifica.se/konsult
    // License: http://creativecommons.org/licenses/by-nc-sa/3.0/
    // http://latlong.mellifica.se/
    // Conversion from grid coordinates to geodetic coordinates.
    function grid_to_geodetic(x, y) {
    	var lat_lon = new Array(2);
    	if (central_meridian == null) {
    		return lat_lon;
    	}
    	// Prepare ellipsoid-based stuff.
    	var e2 = flattening * (2.0 - flattening);
    	var n = flattening / (2.0 - flattening);
    	var a_roof = axis / (1.0 + n) * (1.0 + n*n/4.0 + n*n*n*n/64.0);
    	var delta1 = n/2.0 - 2.0*n*n/3.0 + 37.0*n*n*n/96.0 - n*n*n*n/360.0;
    	var delta2 = n*n/48.0 + n*n*n/15.0 - 437.0*n*n*n*n/1440.0;
    	var delta3 = 17.0*n*n*n/480.0 - 37*n*n*n*n/840.0;
    	var delta4 = 4397.0*n*n*n*n/161280.0;
    	
    	var Astar = e2 + e2*e2 + e2*e2*e2 + e2*e2*e2*e2;
    	var Bstar = -(7.0*e2*e2 + 17.0*e2*e2*e2 + 30.0*e2*e2*e2*e2) / 6.0;
    	var Cstar = (224.0*e2*e2*e2 + 889.0*e2*e2*e2*e2) / 120.0;
    	var Dstar = -(4279.0*e2*e2*e2*e2) / 1260.0;
    
    	// Convert.
    	var deg_to_rad = Math.PI / 180;
    	var lambda_zero = central_meridian * deg_to_rad;
    	var xi = (x - false_northing) / (scale * a_roof);		
    	var eta = (y - false_easting) / (scale * a_roof);
    	var xi_prim = xi - 
    					delta1*Math.sin(2.0*xi) * math_cosh(2.0*eta) - 
    					delta2*Math.sin(4.0*xi) * math_cosh(4.0*eta) - 
    					delta3*Math.sin(6.0*xi) * math_cosh(6.0*eta) - 
    					delta4*Math.sin(8.0*xi) * math_cosh(8.0*eta);
    	var eta_prim = eta - 
    					delta1*Math.cos(2.0*xi) * math_sinh(2.0*eta) - 
    					delta2*Math.cos(4.0*xi) * math_sinh(4.0*eta) - 
    					delta3*Math.cos(6.0*xi) * math_sinh(6.0*eta) - 
    					delta4*Math.cos(8.0*xi) * math_sinh(8.0*eta);
    	var phi_star = Math.asin(Math.sin(xi_prim) / math_cosh(eta_prim));
    	var delta_lambda = Math.atan(math_sinh(eta_prim) / Math.cos(xi_prim));
    	var lon_radian = lambda_zero + delta_lambda;
    	var lat_radian = phi_star + Math.sin(phi_star) * Math.cos(phi_star) * 
    					(Astar + 
    					 Bstar*Math.pow(Math.sin(phi_star), 2) + 
    					 Cstar*Math.pow(Math.sin(phi_star), 4) + 
    					 Dstar*Math.pow(Math.sin(phi_star), 6));  	
    	lat_lon[0] = lat_radian * 180.0 / Math.PI;
    	lat_lon[1] = lon_radian * 180.0 / Math.PI;
    	return lat_lon;
    }
    
    // Missing functions in the Math library.
    function math_sinh(value) {
    	return 0.5 * (Math.exp(value) - Math.exp(-value));
    }
    function math_cosh(value) {
    	return 0.5 * (Math.exp(value) + Math.exp(-value));
    }
    
    function RT90_to_WGS84(x, y) {
      var x = parseFloat(x.replace(",", "."));
  		var y = parseFloat(y.replace(",", "."));
  		axis = 6378137.0; // GRS 80.
    	flattening = 1.0 / 298.257222101; // GRS 80.
    	central_meridian = null;
    	lat_of_origin = 0.0;
  		central_meridian = 15.0 + 48.0/60.0 + 22.624306/3600.0;
  		scale = 1.00000561024;
  		false_northing = -667.711;
  		false_easting = 1500064.274;
  		var lat_lon = grid_to_geodetic(x, y);
  		return {
  		  lat: lat_lon[0],
  		  lng: lat_lon[1]
  		};
    }
    
    function googlemaps(x, y, mapEl) {
      var coords = RT90_to_WGS84(x, y), 
          latlng = new google.maps.LatLng(coords.lat, coords.lng);
    
      var mapOptions = {
        zoom:               11,
        center:             latlng,
        mapTypeId:          google.maps.MapTypeId.ROADMAP,
        mapTypeControl:     false,
        streetViewControl:  false,
        scrollwheel:        false
      };
      
      var map = new google.maps.Map(document.getElementById('map'), mapOptions);
      
      var marker = new google.maps.Marker({
        position: latlng, 
        map:      map
      }); 
    }
    
    if ($('input[rel=RT90]').length) {
      $map = $('<div/>').attr('id', 'map');
      $('article').after($map);
      googlemaps($('input[name=RT90x]').val(), $('input[name=RT90y]').val(), $map);
    } 
    
  });
  
  $(window).load(function() {
  	$('.sponsorlist').easySlider({
  		'auto': true,
  		'continuous': true,
  		'controlsShow': false,
  		'pause': 5000,
  		'speed': 500
  	});
    
  });
  
})(jQuery);


/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).outerWidth(); 
			var h = $("li", obj).outerHeight(); 
			var clickable = true;
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);



// Author: Arnold Andreasson, 2007. http://mellifica.se/konsult
// License: http://creativecommons.org/licenses/by-nc-sa/3.0/
var lat_dd = null;
var lat_dm = null;
var lat_dms = null;
var long_dd = null;
var long_dm = null;
var long_dms = null;
var proj_rt90 = null;
var x_rt90 = null;
var y_rt90 = null;
var proj_sweref99 = null;
var n_sweref99 = null;
var e_sweref99 = null;
// Decimal degrees. Type=float.
var latitude = null;
var longitude = null;

function init() {
	lat_dd = document.getElementById("lat_dd");
	lat_dm = document.getElementById("lat_dm");
	lat_dms = document.getElementById("lat_dms");
	long_dd = document.getElementById("long_dd");
	long_dm = document.getElementById("long_dm");
	long_dms = document.getElementById("long_dms");
	proj_rt90 = document.getElementById("proj_rt90");
	x_rt90 = document.getElementById("x_rt90");
	y_rt90 = document.getElementById("y_rt90");
	proj_sweref99 = document.getElementById("proj_sweref99");
	n_sweref99 = document.getElementById("n_sweref99");
	e_sweref99 = document.getElementById("e_sweref99");
	show_rt90_meridian(proj_rt90.value); // In map.js.
	show_sweref99_meridian(proj_sweref99.value); // In map.js.
	
}

// Set and get functions for external use. Storage, map, etc.
function set_lat_long(lat, lon) {
	latitude = lat;
	longitude = lon;
	update_lat();
	update_long();
	show_map_marker(lat, lon);  // In map.js.
	update_rt90();
	update_sweref99();
}
function get_latitude() {
	return latitude;
}
function get_longitude() {
	return longitude;
}

// Should be called when a key goes up while the field has focus.
function keyup_lat_dd(event) {
	var value = lat_dd.value;
	latitude = convert_lat_from_dd(value);
	lat_dm.value = convert_lat_to_dm(latitude);
	lat_dms.value = convert_lat_to_dms(latitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_long_dd(event) {
	var value = long_dd.value;
	longitude = convert_long_from_dd(value);
	long_dm.value = convert_long_to_dm(longitude);
	long_dms.value = convert_long_to_dms(longitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_lat_dm(event) {
	var value = lat_dm.value;
	latitude = convert_lat_from_dm(value);
	lat_dd.value = convert_lat_to_dd(latitude);
	lat_dms.value = convert_lat_to_dms(latitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_long_dm(event) {
	var value = long_dm.value;
	longitude = convert_long_from_dm(value);
	long_dd.value = convert_long_to_dd(longitude);
	long_dms.value = convert_long_to_dms(longitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_lat_dms(event) {
	var value = lat_dms.value;
	latitude = convert_lat_from_dms(value);
	lat_dd.value = convert_lat_to_dd(latitude);
	lat_dm.value = convert_lat_to_dm(latitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_long_dms(event) {
	var value = long_dms.value;
	longitude = convert_long_from_dms(value);
	long_dd.value = convert_long_to_dd(longitude);
	long_dm.value = convert_long_to_dm(longitude);
	update_rt90();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_rt90(event) {
	if ((x_rt90.value == "") || (y_rt90.value == "")) {
		latitude = null;
		longitude = null;
	} else {
		var x = parseFloat(x_rt90.value.replace(",", "."));
		var y = parseFloat(y_rt90.value.replace(",", "."));
		swedish_params(proj_rt90.value);
		var lat_lon = grid_to_geodetic(x, y);
		latitude = lat_lon[0];
		longitude = lat_lon[1];
	}
	update_lat();
	update_long();
	update_sweref99();
	show_map_marker(latitude, longitude); // In map.js.	
}
function keyup_sweref99(event) {
	if ((n_sweref99.value == "") || (e_sweref99.value == "")) {
		latitude = null;
		longitude = null;
	} else {
		var n = parseFloat(n_sweref99.value.replace(",", "."));
		var e = parseFloat(e_sweref99.value.replace(",", "."));
		swedish_params(proj_sweref99.value);
		var lat_lon = grid_to_geodetic(n, e);
		latitude = lat_lon[0];
		longitude = lat_lon[1];
	}
	update_lat();
	update_long();
	update_rt90();
	show_map_marker(latitude, longitude); // In map.js.	
}

//  Should be called when the fields loses focus.  
function blur_lat(event) {
	update_lat();
}
function blur_long(event) {
	update_long();
}
function blur_rt90(event) {
//	update_rt90();
}
function blur_sweref99(event) {
//	update_rt90();
}

// Projection changes.
function select_proj_rt90(event) {
	show_rt90_meridian(proj_rt90.value); // In map.js.
	update_rt90();
}
function select_proj_sweref99(event) {
	show_sweref99_meridian(proj_sweref99.value); // In map.js.
	update_sweref99();
}

// Private functions.
function update_lat() {
	lat_dd.value = convert_lat_to_dd(latitude);
	lat_dm.value = convert_lat_to_dm(latitude);
	lat_dms.value = convert_lat_to_dms(latitude);
}
function update_long() {
	long_dd.value = convert_long_to_dd(longitude);
	long_dm.value = convert_long_to_dm(longitude);
	long_dms.value = convert_long_to_dms(longitude);
}
function update_rt90() {
	if ((latitude != null) && (longitude != null) &&
		(latitude >= -90) && (latitude <= 90) &&
		(longitude >= -180) && (longitude < 180)) {
		swedish_params(proj_rt90.value);
		var x_y = geodetic_to_grid(latitude, longitude);
		x_rt90.value = x_y[0];
		y_rt90.value = x_y[1];
	} else {
		x_rt90.value = "";
		y_rt90.value = "";
	}	
}
function update_sweref99() {
	if ((latitude != null) && (longitude != null) &&
		(latitude >= -90) && (latitude <= 90) &&
		(longitude >= -180) && (longitude < 180)) {
		swedish_params(proj_sweref99.value);
		var n_e = geodetic_to_grid(latitude, longitude);
		n_sweref99.value = n_e[0];
		e_sweref99.value = n_e[1];
	} else {
		n_sweref99.value = "";
		e_sweref99.value = "";
	}
}

// Hide/show infotext.
function toggle_info() {
	var info_button = document.getElementById("info_button");
	var wgs84_info = document.getElementById("wgs84_info");
	var rt90_info = document.getElementById("rt90_info");
	var sweref99_info = document.getElementById("sweref99_info");
	var map_info = document.getElementById("map_info");
	var about_info = document.getElementById("about_info");
	if (info_button.value == "info") {
		info_button.value = "dšlj info";
		wgs84_info.style.display = "block";
		rt90_info.style.display = "block";
		sweref99_info.style.display = "block";
		map_info.style.display = "block";
		about_info.style.display = "block";
	} else {
		info_button.value = "info";
		wgs84_info.style.display = "none";
		rt90_info.style.display = "none";
		sweref99_info.style.display = "none";
		map_info.style.display = "none";
		about_info.style.display = "none";
	}
}

// Use position if url arguments are supplied.
function parse_url_arguments(url_args) {
	var result = url_args.split(/[=,]/);
	if ((result[0] != '') && (result[0] != null) &&
		(result[1] != '') && (result[1] != null) &&
		(result[2] != '') && (result[2] != null)) {
		if (result[0] == 'latlong') {
			set_lat_long(parseFloat(result[1]), parseFloat(result[2]));
		}
		else if (result[0] == 'rt90') {
			swedish_params(proj_rt90.value);
			var lat_lon = grid_to_geodetic(parseFloat(result[1]), 
									       parseFloat(result[2]));
			set_lat_long(lat_lon[0], lat_lon[1]);			
		}
		else if (result[0] == 'sweref99tm') {
			swedish_params(proj_sweref99.value);
			var lat_lon = grid_to_geodetic(parseFloat(result[1]), 
										   parseFloat(result[2]));
			set_lat_long(lat_lon[0], lat_lon[1]);
		}
	}
}


