
$(document).ready(function() {
	$('TABLE TH:first-child').addClass('first-child');
	$('TABLE TR:first-child').addClass('first-child');
	$('TABLE TD:first-child').addClass('first-child');
	$('TABLE TH:last-child').addClass('last-child');
	$('TABLE TR:last-child').addClass('last-child');
	$('TABLE TD:last-child').addClass('last-child');
	$('TABLE TH:nth-child(odd)').addClass('nth-child-odd');
	$('TABLE TR:nth-child(odd)').addClass('nth-child-odd');
	$('TABLE TD:nth-child(odd)').addClass('nth-child-odd');
	$('TABLE TH:nth-child(even)').addClass('nth-child-even');
	$('TABLE TR:nth-child(even)').addClass('nth-child-even');
	$('TABLE TD:nth-child(even)').addClass('nth-child-even');

	$('TABLE.contact *')
	.removeClass('first-child')
	.removeClass('last-child')
	.removeClass('nth-child-odd')
	.removeClass('nth-child-even');

	$('TABLE.math *')
	.removeClass('first-child')
	.removeClass('last-child')
	.removeClass('nth-child-odd')
	.removeClass('nth-child-even');

	var s = $.cookie('fontsize');
	if (s) $('#main').css('fontSize', s);

	$('#mail_this_page').each(function() { this.href += encodeURIComponent(document.location.href.replace(/^http:\/\/(www\.)?suominen\.fi\//, '')); });
});

function print_preview(lang) {
	var print_preview = true;
	$('HEAD LINK[rel=stylesheet][href~=/print.css]').attr('media', 'screen');
	var href = document.location.href.replace(/#.+$/, '');
	document.location.href = href + '#content';
	$('.top .frame').not(':last').hide();  // show only 1st frame in frontpage animation
	while (print_preview) {
		window.print();
		print_preview = window.confirm(lang == 'fi' ? 'Tulosta lisää?' : 'Print more?');
	}
	$('HEAD LINK[rel=stylesheet][href~=/print.css]').attr('media', 'print');
	document.location.href = document.location.href.replace(/#.+$/, '#header');
	$('.top .frame').not(':last').show();
}

function rotateTable(source, target) {
        var cols = $('#'+source+' TABLE TR:first-child TD').length;
        
        for (var i = 0; i < cols; i++) {
                var cells = $('#'+source+' TABLE TR TD:nth-child(1)');
                var row = document.createElement('TR');
                $(row).append(cells);
                $('#'+target+' TABLE TBODY').append(row);
        }
}

function font_size(value) {
	var current = $.cookie('fontsize') ? parseInt($('#main').css('fontSize')) : 100;
	var body_value = value ? value + current : 100;
	$('#main').css('fontSize', body_value + '%');

	// hack to prevent top navigation items overlapping with logo
	//var header_value = value ? current - value: 100;
	//$('#header').css('fontSize', header_value + '%');

	$.cookie('fontsize', $('#main').css('fontSize'), { path: '/', expires: 30 });
}

/* jQuery Cookie Plugin - Copyright (c) 2006 Klaus Hartl (stilbuero.de) */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
