function generateCssPngImage (elm, src) {
	if (jQuery.browser.msie) {
		elm.css ('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + src + ', sizingMethod="scale")');
	} else {
		elm.css ('background-image', 'url(' + src + ')');
	}
}

$('input[default],textarea[default]').ready (function () {
	$('input[default],textarea[default]').each (function () {
		if (this.value == '') {
			this.value = $(this).attr ('default');
		}
	});

	$('input[default],textarea[default]').focus (function () {
		if (this.value == $(this).attr ('default')) {
			this.value = '';
		}
	});

	$('input[default],textarea[default]').blur (function () {
		if (this.value == '') {
			this.value = $(this).attr ('default');
		}
	});
});

$('ul.programimgs').ready (function () {
	$('ul.programimgs span.text').css ('position', 'relative');
	$('ul.programimgs span.text').each (function (i) {
		$('ul.programimgs span.text').eq (i).css ('top', ($('ul.programimgs span.square').eq (i).height () - $('ul.programimgs span.text').eq (i).height ()) + 'px');
	})
});

$('ul.daynav').ready (function () {
	$('ul.daynav img').mouseenter (function () {
		if ($(this).attr('today') != 'today') {
			this._past = (this.src.indexOf ('_past.') != -1) ? true : false;
			this.src = this.src.replace (/(.*?_)default|past(\.*)/, '$1hover$2');
		}
	});

	$('ul.daynav img').mouseleave (function () {
		if ($(this).attr('today') != 'today') {
			var src = (this._past) ? 'past' : 'default';
			this.src = this.src.replace (/(.*?_)hover(\.*)/, '$1' + src + '$2');
		}
	});
});

$('ul.classnav').ready (function () {
	$('ul.classnav li div.class div.margins').mouseenter (function () {
		$(this).addClass ('hover');
	});

	$('ul.classnav li div.class div.margins').mouseleave (function () {
		$(this).removeClass ('hover');
	});

	$('ul.classnav input:checkbox').wrap ('<span class="checkbox"></span>');
	$('ul.classnav input:checkbox').css ('visibility', 'hidden');

	$('ul.classnav span.checkbox').click (function () {
		$(this).parent ().parent ().parent ().children ('ul').children('li').children('div').children('div').children('span').addClass('checkbox_checked').children('input').attr('checked', true);
		var elm = $(this).children ('input:checkbox');
		elm.get (0).checked = !(elm.get (0).checked);
		$(this).children ('input:checkbox').change ();
		return false;
	});

	$('ul.classnav label').click (function () {
		$(this).parent ().children ('span.checkbox').click ();
		return false;
	});

	$('ul.classnav input:checkbox').change (function () {
		if (this.checked) {
			$(this).parent ().addClass ('checkbox_checked');
			$(this).parent ().parent ().parent ().parent ().children ('ul').show ('fast');
		} else {
			$(this).parent ().removeClass ('checkbox_checked');
			$(this).parent ().parent ().parent ().parent ().children ('ul').children('li').children('div').children('div').children('span').removeClass('checkbox_checked').children('input').attr('checked', false);
			$(this).parent ().parent ().parent ().parent ().children ('ul').hide ('fast');
			$(this).attr('checked', false);
		}
	});

	$('ul.classnav input:checkbox').each (function () {
		if (this.checked) {
			$(this).change ();
		}
	});
});

/**
 *	Important
 **/

var important = {
	activate: function (link, elm) {
		if (elm.get (0) != null) {
			if (elm.children ('div.content').length <= 0) {
				elm.wrapInner ('<div class="margins"></div>');
				elm.wrapInner ('<div class="borders"></div>');
				elm.wrapInner ('<div class="content"></div>');
				elm.children ('div.content').before ('<div class="important-shadow">&nbsp;</div>');
				elm.children ('div.content').before ('<div class="important-arrow">&nbsp;</div>');

				elm.children ('div.content').append ('<a href="javascript://" class="important-close">&nbsp;</a>');
				elm.children ('div.content').children ('a.important-close').click (function () {
					important.deactivate (link, elm);
				});
			}

			elm.css ('display', 'block');
			elm.css ('visibility', 'hidden');

			elm.children ('div.important-shadow').css ('width', (elm.children ('div.content').width () + 20) + 'px');
			elm.children ('div.important-shadow').css ('height', (elm.children ('div.content').height () + 20) + 'px');
			elm.children ('div.important-shadow').css ({left: '-10px', top: '-10px'});

			var pos = {h: '', v: ''};

			if ($(link).offset ().left - $(document).scrollLeft () < $(window).width () / 2) {
				pos.h = 'right';
				elm.css ('left', ($(link).offset ().left + $(link).width () + 20) + 'px');
			} else {
				pos.h = 'left';
				elm.css ('left', ($(link).offset ().left - elm.children ('div.content').width () - 25) + 'px');
			}

			if ($(link).offset ().top - $(document).scrollTop () < $(window).height () / 2) {
				pos.v = 'below';
				elm.css ('top', ($(link).offset ().top + $(link).height () - 10) + 'px');
			} else {
				pos.v = 'above';
				elm.css ('top', ($(link).offset ().top - elm.children ('div.content').height () + 10) + 'px');
			}

			var arrow = elm.children ('div.important-arrow');
			switch (pos.h + pos.v) {
				case 'rightbelow':
					generateCssPngImage (arrow, '/important.arrow.left.top.png');
					arrow.css ({width: '54px', height: '53px', left: '-54px', top: '20px'});
					break;
				case 'rightabove':
					generateCssPngImage (arrow, '/important.arrow.left.bottom.png');
					arrow.css ({width: '54px', height: '53px', left: '-54px', top: (elm.children ('div.content').height () - 73) + 'px'});
					break;
				case 'leftbelow':
					generateCssPngImage (arrow, '/important.arrow.right.top.png');
					arrow.css ({width: '54px', height: '53px', left: elm.children ('div.content').width () + 'px', top: '20px'});
					break;
				case 'leftabove':
					generateCssPngImage (arrow, '/important.arrow.right.bottom.png');
					arrow.css ({width: '54px', height: '53px', left: elm.children ('div.content').width () + 'px', top: (elm.children ('div.content').height () - 73) + 'px'});
					break;
			}

			elm.css ('visibility', 'visible');
		}
	},
	deactivate: function (link, elm) {
		if (elm.get (0) != null) {
			elm.css ('display', 'none');
		}
	}
}

$('*[important-hover]').ready (function () {
	$('*[important-hover]').mouseenter (function () {
		important.activate (this, $('#' + $(this).attr ('important-hover')));
	});
});

$('*[important-click]').ready (function () {
	$('*[important-click]').click (function () {
		important.activate (this, $('#' + $(this).attr ('important-click')));
		return false;
	});
});

/**
 *	Imageset
 **/

var imagesetcollections = new Array ();

function Imageset (name) {
	var _this = this;
	var _images = new Array ();
	var _id = 'imageviewer-' + $('div.imageviewer').length;
	var _name = name;
	var _activated = false;
	var _index = 0;

	$('body').append (
		'<div id="' + _id + '" class="imageviewer">' +
			'<div class="background">&nbsp;</div>' +
			'<div class="image">' +
				'<a href="javascript://" class="close">Stäng</a>' +
				'<span>&nbsp;</span>' +
				'<a href="javascript://" class="prev">« Föregående</a>' +
				'<a href="javascript://" class="next">Nästa »</a>' +
			'</div>' +
		'</div>'
	);

	var _content = $('#' + _id);

	_content.children ('div.image').children ('a.close').click (function () {
		_this.deactivate ();
	});

	_content.children ('div.image').children ('a.prev').click (function () {
		_this.prev ();
	});

	_content.children ('div.image').children ('a.next').click (function () {
		_this.next ();
	});

	$(window).resize (function () {
		_this.center ();
	});

	function _setEvents (elm) {
		$(elm).mouseenter (function () {
			$(elm).addClass ('hover');
		});

		$(elm).mouseleave (function () {
			$(elm).removeClass ('hover');
		});

		$(elm).click (function () {
			var index = 0;

			for (var i = 0, l = _images.length; i < l; i++) {
				if (elm === _images [i].elm) {
					index = i;
					break;
				}
			}

			_this.activate (index, elm);
		});
	}

	this.center = function () {
		var image = _content.children ('div.image').children ('span').children ('img');
		var heightMargin = 90;
		var widthMargin = 20;

		var imageProportions = image.width() / image.height();

		if (image.width () > $('#page').width () - widthMargin) {
			image.width ($('#page').width () - widthMargin);
			image.height (image.width() / imageProportions);
		}
		if (image.height () > $(window).height() - heightMargin) {
			image.height ($(window).height() - heightMargin);
			image.width (image.height() * imageProportions);
		}

		_content.children ('div.background').css ('width', _content.children ('div.image').width () + 'px');
		_content.children ('div.background').css ('height', _content.children ('div.image').height () + 'px');

		if (_activated) {
			_content.animate ({
				top: (($(window).height () / 2 - _content.children ('div.image').height () / 2) + $(document).scrollTop ()) + 'px',
				left: (($(window).width () / 2 - _content.children ('div.image').width () / 2) + $(document).scrollLeft ()) + 'px'
			}, 'fast');
		}
	}

	this.add = function (elm, src) {
		_images.push ({elm: elm, src: src});
		_setEvents (elm);
	}

	this.activate = function () {
		var index = (arguments.length > 0) ? arguments [0] : 0;
		var elm = (arguments.length > 1) ? arguments [1] : null;

		if (!_activated) {
			_content.fadeOut (0);

			if (elm != null) {
				_content.css ('top', $(elm).offset ().top + 'px');
				_content.css ('left', $(elm).offset ().left + 'px');
			}

			this.center ();
			_content.fadeIn ('fast');

			_activated = true;
		}

		this.image (index);
	}

	this.deactivate = function () {
		_content.width (1);
		_content.height (1);
		_content.fadeOut ();
		_activated = false;
	}

	this.prev = function () {
		_index--;
		if (_index < 0) {
			_index = _images.length - 1;
		}
		this.image (_index);
	}

	this.next = function () {
		_index++;
		if (_index >= _images.length) {
			_index = 0;
		}
		this.image (_index);
	}

	this.image = function (index) {
		_index = index;
		_content.children ('div.image').children ('span').html ('<img src="' + _images [index].src + '" onload="imagesetcollections[\'' + _name + '\'].center();"" />');
	}
}

$('img[imageset]').ready (function () {
	$('img[imageset]').each (function () {
		if (imagesetcollections [$(this).attr ('imageset')] == null) {
			imagesetcollections [$(this).attr ('imageset')] = new Imageset ($(this).attr ('imageset'));
		}

		if ($(this).attr ('imageset-src') != null) {
			imagesetcollections [$(this).attr ('imageset')].add (this, $(this).attr ('imageset-src'));
		} else {
			imagesetcollections [$(this).attr ('imageset')].add (this. $(this).attr ('src'));
		}
	});
});
