;$(document).ready(function()
{
    $('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
    $(":submit,a").focus(function(){$(this).blur()});
    $(".search input.text").defaultValue('zadejte hledaný text ...');
    $(".usernavi input[name=username]").defaultValue('přihlašovací jméno');
    $('<input type="text" value="heslo">').addClass('fakepass text').click(function(e){
        if (typeof this.realinput == 'undefined')
        {
            this.realinput = $(".usernavi input[name=password]")[0];
            this.realinput.fakeinput = this;
            $(this.realinput).blur(function(){
                $(this).hide();
                $(this.fakeinput).show();
            });
        }        
        $(this).hide();
        $(this.realinput).show().focus();
    }).insertBefore($(".usernavi input[name=password]").hide());
    
    /*$(window).scroll(function () {
        if ($(window).scrollTop())  
            var offset = ($(window).height()-200)+$(document).scrollTop()+"px";
        else
            var offset = $(window).height();
        //$('span.updown').animate({top:offset},{duration:500,queue:false});
        if ($(window).scrollTop() > 0 && ! $('span.updown').is(':visible'))
        {
            $('span.updown').fadeIn(800);//animate({top:offset},{duration:500,queue:false});    
        }
        else if ($(window).scrollTop() == 0 && $('span.updown').is(':visible'))
        {
            $('span.updown').fadeOut(800);
        }
        //$('span.updown').animate({top:offset},{duration:500,queue:false});    
    });*/  
    
    $('span.mail').each(function(){
        text = $(this).text().replace(/ \(a\) /, '@').replace(/ tecka /, '.');
        $(this).html('<a href="mailto:'+text+'" title="napsat email">'+text+'</a>');
    });
});

function wMail(login, domain, link, name)
{
    if (link)
        document.write('<a href="mailto:'+login+'@'+domain+'">'+((name)?name:login+'@'+domain)+'</a>');
    else
        document.write(login+'@'+domain);
}

function trimRatio(x1, y1, x2, y2, over)
{
    if (typeof(over)==undefined)
        over = false;
    if (!x1 || !x2)
        return {x:false, y:false, width:false, height:false};
    
    if (x1>x2 || over)
    {
        X = x2;
        Y = (y1/x1)*x2;
        
        if (Y>y2 && !over)
        {
            Y = y2;
            X = (x1/y1)*y2;
        }
        else if (over && Y<y2)
        {
            Y = y2;
            X = (x1/y1)*y2;
        }
        if (X<x2 && over)
        {
            X = x2;
            y = (y1/x1)*X;
        }
    }
    else if (y1>y2)
    {
        Y = y2;
        X = (x1/y1)*y2;
    }
    else
    {
        X = x1;
        Y = y1;
    }
    X = Math.floor(X);
    Y = Math.floor(Y);
    return {
        x : X,
        y : Y,
        width : X,
        height : Y
    };
}

function base_url()
{
    return resolveUrl(document.location.href);
}

var Vanilla = new function()
{
    var scriptTags = document.getElementsByTagName("script");
    for(var i=0;i<scriptTags.length;i++) {
        if(scriptTags[i].src && scriptTags[i].src.match(/js\/jquery-1\.4\.2\.min\.js$/))
        {
            this.baseURL = scriptTags[i].src.replace(/js\/jquery-1\.4\.2\.min\.js$/,'');
            break;
        }
    }
    this.webRoot = this.baseURL.replace(/^http(s)?:\/\/[^\/]+/, '');
    this.httpMethod = document.URL.replace(/^(http|https)(:\/\/).*$/, "$1$2");
    if (this.webRoot == this.baseURL)
    {
        this.baseURL = this.httpMethod+document.domain+this.webRoot;
    }
    return this;
};

var notify = function(message, options)
{
	var block = jQuery('#notifications');
	
	// If ready
	if (block.length > 0)
	{
		var settings = jQuery.extend({}, notify.defaults, options);
		
		if (settings.closeOld)
		{
            block.find('li').fadeAndRemove();
        }
		
		// Append message
		var closeButton = settings.closeButton ? '<span class="close-bt"></span>' : '';
		if (typeof settings.type != 'undefined')
		{
            message = '<span class="' + settings.type + '">' + message + '</span>';
        }
		var element = jQuery('#notifications').append('<li>'+message+closeButton+'</li>').children(':last-child');
		
		// Effect
		element.expand();
		
		// If closing
		if (settings.autoClose)
		{
			// Timer
			var timeoutId = setTimeout(function() { element.fadeAndRemove(); }, settings.closeDelay);
			
			// Prevent closing when hover
			element.hover(function()
			{
				clearTimeout(timeoutId);
				
			}, function()
			{
				timeoutId = setTimeout(function() { element.fadeAndRemove(); }, settings.closeDelay);
			});
		}
	}
	else
	{
		// Not ready, delay action
		setTimeout(function() { notify(message, options); }, 40);
	}
};

// Defaults values for the notify method
notify.defaults = {
	closeButton:   true,               // Add a close button to the message
	autoClose:     true,               // Message will close after (closeDelay) ms
	closeDelay:    4000,               // Delay before message closes
	closeOld:      false
};

/*
 * Expand an element
 * @param string|int duration a string (fast, normal or slow) or a number of millisecond. Default: 'normal'. - optional
 * @param function callback any function to call at the end of the effect. Default: none. - optional
 */
$.fn.expand = function(duration, callback)
{
	this.each(function(i)
	{
		// Init
		var element = $(this);
		element.css('display', 'block');
		
		// Reset and get values
		element.stop(true).css({
			'overflow': '',
			'height': '',
			'paddingTop': '',
			'paddingBottom': '',
			'borderTopWidth': '',
			'borderBottomWidth': '',
			'marginTop': '',
			'marginBottom': ''
		});
		var height = element.height();
		var paddingTop = parseInt(element.css('padding-top'));
		var paddingBottom = parseInt(element.css('padding-bottom'));
		var marginTop = parseInt(element.css('margin-top'));
		var marginBottom = parseInt(element.css('margin-bottom'));
		
		// Initial and target values
		var css = {
			'overflow': 'hidden',
			'height': 0,
			'paddingTop': 0,
			'paddingBottom': 0
		};
		var anim = {
			'height': height,
			'paddingTop': paddingTop,
			'paddingBottom': paddingBottom
		};
		
		// IE8 and lower do not understand border-xx-width
		// http://forum.jquery.com/topic/ie-invalid-argument
		if (!$.browser.msie || $.browser.version > 8)
		{
			var borderTopWidth = parseInt(element.css('border-top-width'));
			var borderBottomWidth = parseInt(element.css('border-bottom-width'));
			
			// Border width is not set to 0 because it does not allow fluid movement 
			css.borderTopWidth = '1px';
			css.borderBottomWidth = '1px';
			anim.borderTopWidth = borderTopWidth;
			anim.borderBottomWidth = borderBottomWidth;
		}
		
		// Detection of elements sticking to their predecessor
		var prev = element.prev();
		if (prev.length === 0 && parseInt(element.css('margin-bottom'))+marginTop !== 0)
		{
			css.marginTop = Math.min(0, marginTop);
			css.marginBottom = Math.min(0, marginBottom);
			anim.marginTop = marginTop;
			anim.marginBottom = marginBottom;
		}
		
		// Effect
		element.stop(true).css(css).animate(anim, {
			'duration': duration,
			'complete': function()
			{
				// Reset properties
				$(this).css({
					'display': '',
					'overflow': '',
					'height': '',
					'paddingTop': '',
					'paddingBottom': '',
					'borderTopWidth': '',
					'borderBottomWidth': '',
					'marginTop': '',
					'marginBottom': ''
				});
				
				// Callback function
				if (callback)
				{
					callback.apply(this);
				}
				
				// Required for IE7 - don't ask me why...
				if ($.browser.msie && $.browser.version < 8)
				{
					$(this).css('zoom', 1);
				}
			}
		});
	});
	
	return this;
};

/**
 * Remove an element with fading then folding effect
 * @param string|int duration a string (fast, normal or slow) or a number of millisecond. Default: 'normal'. - optional
 * @param function callback any function to call at the end of the effect. Default: none. - optional
 */
$.fn.fadeAndRemove = function(duration, callback)
{
	this.animate({'opacity': 0}, {
		'duration': duration,
		'complete': function()
		{
			// No folding required if the element has position: absolute (not in the elements flow)
			if ($(this).css('position') == 'absolute')
			{
				// Callback function
				if (callback)
				{
					callback.apply(this);
				}
				
				$(this).remove();
			}
			else
			{
				$(this).slideUp(duration, function()
				{
					// Callback function
					if (callback)
					{
						callback.apply(this);
					}
					
					$(this).remove();
				});
			}
		}
	});
	
	return this;
};
