$(document).ready(function(){
			
			$(".newwindow").attr('target','_blank');

			$("#navigation a").each(function(){
					var d=document.location.href;
					var t = (d.substr(d.length-1,1)=='/')?d+'index.htm':d;
					if (t == this.href) $(this).parent('li').addClass('current');
			});

			$("form.linkreplace").each(function(){
					var submitText = $(this).children('input [type="submit"]').attr('value');												
												
					$(document.createElement('a')).
						attr('href','#').                  
						text(submitText.length?submitText:$(this).text()).
						attr('class',this.className).
						click(function(){$(this).siblings('form').submit();}).
						insertBefore(this);
					$(this).hide();						
			});

			$("form.download_message").submit(function(){alert ($(this).find('#download_message').attr('value'));return true;});
	
			$('.bgreplace').each(function(){
										  
				 var bgimg = $('.page').css('background-image');
				if (bgimg){
					bgregex = bgimg.match(/url\(([^\)]*)\)/);
					if (bgregex&&bgregex.length>=2) fgimg=bgregex[1];
					if (fgimg){
						$(this).css('background-image','none');
						_img = $(document.createElement ('img')).addClass('scale').attr('src',fgimg).attr('alt','replaced background image').css('position','absolute').css('top',0);				
						$(this).prepend(_img);
					}	
				}				
			 }).addClass('scale');
	
});
$(window).load(function(){
	$('.scale').each(function(){resizeToContainer(this)});
	/*$('.page').css('width',window.innerWidth).css('height',window.innerHeight).each(function(){	
		resizeToContainer(this,$(this).children('img')[0]);								 
	}).css('opacity',0).css('visibility','visible').animate({opacity:100},5000);*/
	$('.page').css('opacity',0).css('visibility','visible').animate({opacity:100},5000);

}).unload(function(){
/*	if ($('#global_volume_mute').hasClass('active')){
		$.cookie('volume',$('#global_volume_mute').attr('lastvolume'));
		$.cookie('mute',true);
	} else {
		$.cookie('volume',$('#global_volume').slider('value'));
		$.cookie('mute','');	
	}*/
}).resize(function(){
		/*$('.page').css('width',window.innerWidth).css('height',window.innerHeight).each(function(){
			resizeToContainer(this,$(this).children('img')[0]);								 
		});*/
		$('.scale').each(function(){resizeToContainer(this)});
});
	
function resizeToContainer(child, parent){
		
		if (!parent) parent = $(child).parents(':not[class="scale"]');
		alert (parent.innerHTML);
		pw = parent.width?parent.width:$(parent).css('width').replace('px','');
		ph = parent.height?parent.height:$(parent).css('height').replace('px','');
		cw = child.width?child.width:$(child).css('width').replace('px','');
		ch = child.height?child.height:$(child).css('height').replace('px','');

		//alert (child.width +" / " +child.height +" / " +parent.width +" / " +parent.height);
		
		var childImgRatio = cw/ch;
		var parentContainerRatio = pw/ph;
		
		if (childImgRatio < parentContainerRatio){
			//we have a tall thin image and a short wide container.
			//scale the height of the image to the height of the container
			//scale width of the image in line with the ratio
			//width = ;
			child.height = ph;
			child.width = ph*(cw/ch);
			//$(child).css('width',parent.width);
			//height= width*childImgRatio;
		} else {
			//we have short fat image and a tall thin container
			//scale the width of the image to the width of the page
			//and the height of the image in line with the ratio
			child.width = pw;
			child.height = pw*(ch/cw);
			//alert ('b');
			//$(child).css('height',parent.height);
			//width = height*childImgRatio;
		}
		var _left = pw-child.width>0?(pw-child.width)/2:0;
		$(child).css('left',_left);
		//alert (childImgRatio+" / "+parentContainerRatio+" / "+width+"/"+height);
			
}
/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/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
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        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;
    }
};