
$(document).ready(function() {

	if (! modern_browser()) {

		// Fixes for non-CSS3 browsers

		$(':first-child').addClass('first-child');
		$(':last-child').addClass('last-child');
		$('INPUT[type=text]').addClass('text');
		$('.column:empty').addClass('column-empty');

		// Extra fixes for really old browser

		if ($.browser.msie && parseFloat($.browser.version) < 7) {
			$('.group LI').hover(
				function() { $(this).addClass('hover'); },
				function() { $(this).removeClass('hover'); }
			);

			$('UL.navi-tree > LI.open > A')
				.css('background', 'url(/files/oriocc/img/arrow_down.png) left center no-repeat');
			
			$('UL.navi-tree LI.open > A, UL.navi-tree LI.current > A').css('color', '#9a8737');

			$('UL.navi-tree > LI > UL > LI.open > A')
				.css('display', 'inline')
				.css('padding-right', '1em')
				.css('background', 'url(/files/oriocc/img/arrow_down.png) right center no-repeat');

			$('UL.navi-tree UL UL LI.current > A').css('font-weight', 'bold');
		}

	}

	$('A[href$=.pdf]').addClass('pdf');

	$('INPUT.autoclear, TEXTAREA.autoclear').each(function() {
		$(this).attr('title', $(this).val());
	});

	$('INPUT.autoclear, TEXTAREA.autoclear').focus(function() {
		if ($(this).val() == $(this).attr('title')) $(this).val('');
	});

	$('INPUT.autoclear, TEXTAREA.autoclear').blur(function() {
		if ($(this).val() == '') $(this).val($(this).attr('title'));
	});

	$('FORM.validate').submit(function() {
		$(':input', this).removeClass('invalid');
		$('FIELDSET.required :input[value=]', this).addClass('invalid');
		$('.validate-as-email', this).not('[value*=@]').addClass('invalid');
		var invalid = $('.invalid', this);
		if (invalid.length > 0) {
			window.alert('Ole hyvä ja täytä kaikki vaadittavat kentät.');
			invalid.get(0).focus();
			return false;
		}
		return true;
	});

	$('FORM').submit(function() {
		$('.autoclear', this).each(function() {
			if ($(this).val() == $(this).attr('title')) $(this).val('');
		});
		return true;
	});

	$('INPUT.initial-focus:first').focus();
	
	$('.group LI IMG').error(function() {
		$(this).attr('src', '/files/oriocc/img/ei_kuvaa.png').attr('alt', 'Ei kuvaa');
	});

	$('A[href^=mailto:]').each(function() {
		if ($(this).attr('href').indexOf('mailto:etunimi.sukunimi') == 0) {
			var user = $(this).attr('title').toLowerCase().replace(' ', '.');
			var href = $(this).attr('href');
			$(this).attr('href', href.replace('mailto:etunimi.sukunimi', 'mailto:' + user));
			if ($(this).text().indexOf('etunimi.sukunimi') == 0) {
				var text = $(this).text();
				$(this).text(text.replace('etunimi.sukunimi', user));
			}
			$(this).addClass('replaced');
		}
	});

	$('A[href^=mailto:]').mouseover(function() {
		if (! $(this).hasClass('replaced')) {
			var h = $(this).attr('href');
			$(this).attr('href', 'mailto:' + h.substring(7, h.length - 12).split('').reverse().join(''))
				.addClass('replaced');
		}
	});

	$('A[href$=.jpg], A[href$=.png], A[href$=.gif]').addClass('popup');

	$('A.zoomable').click(function(e) {
		e.preventDefault();
		var original = $('IMG', this);
		var popup = $(document.createElement('DIV'))
			.width(original.width() + 78)
			.height(original.height() + 78)
			.css('position', 'absolute')
			.css('top', original.offset().top - 40)
			.css('left', original.offset().left - 40)
			.css('zIndex', '100')
			.css('background', '#f0f0f0')
			.css('opacity', '0.8')
			.css('border', '1px solid #ccc')
			.appendTo('BODY');
		var img = original.clone()
			.css('position', 'absolute')
			.css('zIndex', '200')
			.css('top', original.offset().top + 1)
			.css('left', original.offset().left + 1)
			.appendTo('BODY');
		var f_close = function() {
			popup.hide();
			img.hide();
		}
		var close = $(document.createElement('SPAN'))
			.addClass('clickable link block')
			.text('Sulje')
			.css('position', 'absolute')
			.css('top', '10px')
			.css('right', '10px')
			.css('zIndex', '200')
			.css('color', '#666')
			.css('fontSize', '.75em')
			.click(function() { f_close() })
			.appendTo(popup);
		img.click(f_close).attr('title', 'Sulje').css('cursor', 'pointer');
		$(document).keydown(function(e) {
			if (e.which == 27) {  // close by esc
				f_close();
			}
		});
	});
	
	/*$('BODY.front .lifts A.popup').click(function(e) {
		e.preventDefault();
		var popup = new Popup($('.popup-content', this.parentNode).clone().show());
		popup.show();
	});*/

});

function modern_browser() {
	return ! (
		// IE 8+
		($.browser.msie && parseFloat($.browser.version) < 8) ||

		// FF 3+
		($.browser.mozilla && parseFloat(jQuery.browser.version.substr(0,3)) < 1.9) ||

		// Safari 3+
		($.browser.safari && parseInt($.browser.version) < 522) ||

		// Opera 9+
		($.browser.opera && parseFloat($.browser.version) < 9) ||

		// Konqueror 3.4+
		(navigator.userAgent.toLowerCase().indexOf('khtml') != -1 && parseFloat($.browser.version) < 3.4)
	);
}



/* Popup object */

Popup = function(element) {
	this.create();
	this.append(element);
}

Popup.prototype.close = function() {
	this.popup.hide();
	this.shadow.hide();
}

Popup.prototype.create = function() {

	var shadow = $(document.createElement('div'))
		.addClass('popup-shadow')
		.height($(document).height())
		.appendTo(document.body);

	var popup = $(document.createElement('div'))
		.addClass('popup-box')
		.appendTo('BODY');

	popup.css('left', $('BODY').width()/2 - popup.width()/2);
	
	var _this = this;

	var close = $(document.createElement('div'))
		.text('Sulje')
		.addClass('close clickable link')
		.click(function() { _this.close() })
		.appendTo(popup);
		
	$(document).keydown(function(e) {
		if (e.which == 27) _this.close();  // close by esc
	});
	
	this.popup = popup;
	this.shadow = shadow;
}

Popup.prototype.show = function() {
	this.popup.show();
	this.shadow.show();
}

Popup.prototype.append = function(elements) {
	this.popup.append(elements);
}

