/**
 * JavaScrits
 */

window.onresize = initResizes;
window.onload = initResizes;
//---------------------------------------------------------------

$(document).ready(function()
{
    showErrorMessage('');
    
    $('#meniu li a.container').click(function(){
        var parent = $(this).parent();
        var submenu = parent.find('ul:first-of-type');
        
        if(submenu.length)
        {
            if(submenu.is(':visible'))
            {
                submenu.slideUp(500);
            }
            else
            {
                submenu.slideDown(500);
            }
        }
        
        $(this).blur();
        
        return false;
    });
});
//---------------------------------------------------------------

function initResizes() {
    var bodyId = $('body').attr('id');
    if (bodyId == 'module_general')
    {
        resizeSliderBlock();
        resizeMainImages();
    }
}
//---------------------------------------------------------------

function resizeSliderBlock()
{
    var bodyHeight = $('body').height();
    //var wrapperHeight = $('#wrapper').height();
    var wrapperHeight = 0;
    
    var contentInnerObj = $('#slider_outer');
    
    var contentH = Math.max(bodyHeight, wrapperHeight);
    
    if(contentH < 500) {
        contentH = 500;
    }
    
    contentInnerObj.css({'height':contentH+'px'})
}
//---------------------------------------------------------------

function resizeMainImages()
{
    var index = 0;
    var outerH = $('#slider_outer').height();
    
    $('#slider img.main_image').each(function(){
        
        var currentImage = $(this);
        
        var objImagePreloader = new Image();
        objImagePreloader.onload = function()
        {
            if(outerH >= objImagePreloader.height)
            {
                currentImage.css({'height':'auto'});
            }
            else
            {
                currentImage.css({'height':'100%'});
            }
        };
        objImagePreloader.src = currentImage.attr('src');
    });
}
//---------------------------------------------------------------

/*
	Strips spaces from start and end of string
	Param:
		str string (text)
*/
function trimString (str)
{
	while (str.charAt(0) == ' ')
		str = str.substring(1);
	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);

	return str;
}
//---------------------------------------------------------------

function showErrorMessage(text)
{
    if (text)
    {
        $('#error-message').html(text);
        $('#error-message-container a').trigger('click');
    }
    else
    {
        var html = '';
        
        var errMsgContainer = $('.page-messages-container');
        if (errMsgContainer.length)
        {
            if (errMsgContainer.length > 1)
            {
                errMsgContainer.each(function(){
                    html += $.trim($(this).html());
                });
            }
            else
            {
                html = $.trim(errMsgContainer.html());
            }
        }
        
        if(html)
        {
            $('#error-message').html(html);
            $('#error-message-container a').trigger('click');
        }
    }

}
//----------------------------------------------------------

