var resized = null;
// percentages
function fitBackground()
{
	var bodyW = $(document).width();
	var bodyH = $(document).height();
	var bodyRatio = bodyW/bodyH;

	var imageW = $('#fullscreenBgImage').width();
	var imageH = $('#fullscreenBgImage').height();
	var imageRatio = imageW/imageH;

	//alert('image h:'+imageH + ' image w:' + imageW+ ' body h:'+bodyH + ' body w:'+bodyW);
	//alert('image h:'+imageH + ' image w:' + imageW+ ' body h:'+bodyH + ' body w:'+bodyW);
	if (imageH == 0 || imageW == 0)
	{	
		//alert('image h:'+imageH + ' image w:' + imageW+ ' body h:'+bodyH + ' body w:'+bodyW);
		return false;
	}
	
	$('#fullscreenBgImage').css('top',0);
	$('#fullscreenBgImage').css('left',0);
	if (bodyRatio == imageRatio)
	{
		$('#fullscreenBgImage').width(bodyW);
		$('#fullscreenBgImage').height(bodyH);
	}
	else if (bodyRatio > imageRatio)
	{
		$('#fullscreenBgImage').width(bodyW);
		//alert('puvodni rozmery: '+imageW+'*'imageH);
		//$('#fullscreenBgImage').height(bodyH);
		var newW = bodyH/imageH*imageW; // pomer zvetseni * puvodni sirka obrazku
		//$('#fullscreenBgImage').width(newW);
		//var offset = (newW-bodyW)/2;
		var offset = 0;
		$('#fullscreenBgImage').css('left',-offset);
	}
	else
	{
		//alert('uzsi');
		$('#fullscreenBgImage').height(bodyH);
		var newW = bodyH/imageH*imageW;
		var offset = (newW-bodyW);
		$('#fullscreenBgImage').css('left',-offset);
	}
	//$('#fullscreenBgImage').fadeIn();
	//alert(bodyRatio +' '+imageRatio);
	return true;
}

function fontSize(){
	this.COOKIE_NAME = 'fontSize';
	this.applyTo = 'content'; // dom element id
	
	this.currentSize = 100;
	this.maxSize = 150;
	this.minSize = 80;
	this.step = 10;
	
	this.Down = function()
	{
		try
		{
			this._setSize(this.currentSize-this.step);
		} catch (e) {}
	}
	
	this.Up = function()
	{
		try
		{
			this._setSize(this.currentSize+this.step);
		} catch (e) {}
	}
	
	this._setSize = function(newFontSize)
	{
		if (newFontSize >= this.minSize && newFontSize <= this.maxSize)
		{
			this.currentSize = newFontSize;
			this.applyToElement();
			this.save();
		}
		else
		{
			throw "New size is out of range";
		}
	}
	
	this.getCurrentSize = function()
	{
		return this.currentSize;
	}
	
	this.applyToElement = function()
	{
		$('#'+this.applyTo).css('font-size',this.currentSize+'%');
		$('#'+this.applyTo).change();
	}
	
	this.save = function()
	{
		$.cookie(this.COOKIE_NAME,this.currentSize);
	}

	this.load = function()
	{
		var sizeFromCookie = parseInt($.cookie(this.COOKIE_NAME));
		try {
			this._setSize(sizeFromCookie);
		} catch (e){}
	}
}

var fontSize = new fontSize();
$(document).ready(function(){fontSize.load();});

/**
 * @see main html head, there are start and loadDelay defined
 */
var SKIN_COOKIE_NAME = 'skin_cookie';
var resizeTimer = null;

$('#fullscreenBgImage').ready(function() {
	$('#fullscreenBgImage').load(function() {
		fitBackground();
	});
	$('#fullscreenBgImage').css('visibility','visible');
	fitBackground();
	timestamp = new Date().getTime();
	if (typeof(start) !== 'undefined') {
		loadDelay = timestamp - start;
		var lastSkin = $.cookie(SKIN_COOKIE_NAME);
		if (loadDelay < 300 && lastSkin != currentSkin)
		{
			$('#main').css('opacity','0');
			var timer = setTimeout( function(){$('#main').animate({opacity:1})},1200);
		}
		$.cookie(SKIN_COOKIE_NAME, currentSkin);
	}
});

$(window).resize(function(){
	if (resizeTimer) clearTimeout(resizeTimer);
	resizeTimer = setTimeout(fitBackground, 50); // check for window size changes
});
