$.fn.lightbox.defaults.fileLoadingImage = "/javascripts/lightbox/images/loading.gif";
$.fn.lightbox.defaults.fileBottomNavCloseImage = "/javascripts/lightbox/images/closelabel.gif";
$.fn.lightbox.defaults.fitToScreen = true;
$.fn.mailEncrypt = function() {
	return $(this).each(function() {
		var email_ecrypted = $(this).attr('rel');
		var email_decrypted = $.base64Decode( email_ecrypted );
		$(this)
		.text( email_decrypted )
		.attr( { href : 'mailto:' + email_decrypted } );
	});
}

function mailTo( email ) {
	location.href = 'mailto:' + $.base64Decode( email );
}
function map( c1, c2, id ) {
	var p = new GLatLng( c1 , c2);
	var m = new GMap2( document.getElementById( id ) );
	m.addControl(new GSmallZoomControl3D());
	m.setCenter(p, 13);
	var mk = new GMarker(p);
	m.addOverlay(mk);
	m.getDefaultUI();
}
function select_area(obj) {
	var show_all = $('select#or option:first').text();
	var district = $('select#di').val();
	var area = 'all';
	if ( typeof(q) == 'object' ) {
		area = q.or;
	}
	var options = '';
	options += '<option value="all"' + (area=='all' ? ' selected' : '') + '>' + show_all + '</option>\n';
	if ( typeof(ar[ district ]) != "undefined" ) {
		jQuery.each(ar[ district ], function(city, areas) {
			options += '<optgroup label="' + city + '">\n';
			options += '<option value="city;' + city + '"' + (area=='city;' + city ? ' selected' : '') + '>' + show_all + ' (' + city + ')</option>\n';
			jQuery.each(areas, function() {
				options += '<option value="area;' + this.name + '"' + (area=='area;' + this.name ? ' selected' : '') + '>' + this.name + '</option>\n';
			});
			options += '</optgroup>';
		});
	}
	obj.html(options);
}

$(function() {
	
	$('select#or').each(function() {
		var obj = $(this);
		$('select#di').change(function() {
			select_area(obj);
		});
		select_area(obj);
	});
	
	$('a.mailto').mailEncrypt();
	
	$('#so')
	.clone(false)
	.replaceAll("#Sortlist")
	.change(function() {
		location.href = sortlist_url( $(this).find('option:selected').attr('value') );
	})
	.addClass('phide');
	
	$('.images a:has(img)')
	.lightbox();
	
	$('div.print')
	.html( document.createElement('a') )
	.find('a')
	.attr({
		href: '#',
		title: 'Skriv ut'
	})
	.addClass('button_grey phide')
	.text('Skriv ut')
	.click(function() {
		window.print();
		return false;
	});
	
	if ( typeof(offices) != undefined && typeof(contacts) != undefined ) {
		var Map = $('#Map');
		var Office = $('#Office');
		var Contacts = $('#Contacts');
		var Card = function() {
			return 	$('<div/>')
					.attr('id', 'Card')
					.insertAfter(Contacts);
		}
		Contacts.each(function () {
			$(this)
			.find('.data td:first-child a')
			.click(function() {
				Card().load( $(this).attr('href') + ' #Card', {}, function(resp, status) {
					if (status == 'success') {
						var self = this;
						Map.hide();
						Office.hide();
						Contacts.hide();
						$(this).prepend(
							$('<p/>').prepend(
								$('<a/>')
								.attr('href', '#')
								.text('« Visa alla medarbetare i regionen')
								.click(function() {
									$(self).remove();
									Map.show();
									Office.show();
									Contacts.show();
									return false;
								})
							)
						)
						.find('a.mailto').mailEncrypt();;
						
					}
				});
				return false;
			});
		});
	}
	
	if ( typeof(q) == 'object' ) {
		$('a.cache_save')
		.click(function() {
			var id = $(this).attr('rel');
			var href = $(this).attr('href');
			$.get('/cache', {queries: $.param(q), id:id}, function() {
				location.href = href;
			});
			return false;
		});
	}
	
	$('#Services').each(function() {
		$('.slidebox .text', this).each(function() {
			$(this).height( $(this).height() )
			.hide();
		});
		$('.slidebox h2', this).each(function(i) {
			$(this).wrapInner(
				$('<a />')
					.addClass('slidebutton')
					.attr({
						href: '#'
					})
					.click(function() {
						$('#Service' + i + ' .text').slideToggle("slow");
						return false;
					})
			);
		});
	});
	
	if ( ! ( $.browser.msie && $.browser.version.substr(0,1)<=6 ) ) {
		var r = 5;
		var aa = true;
		var ap = false;
		$('.box').filter(':not(.norc)').each(function() {
			$(this).corner({
				tl: { radius: r },
				tr: { radius: r },
				bl: { radius: r },
				br: { radius: r },
				antiAlias: aa,
				autoPad: ap
			});
			if ( $(this).hasClass('thinbox') ) {
				$(this)
				.css({
					padding: '5px',
					backgroundPosition: "center center"
				});
			} else {
				$(this)
				.css({
					padding: '20px'
				});
			}
		});
	}
	
});









