/**
 * JavaScrits
 */

$(document).ready(function()
{
    $('a.fancybox').fancybox({
        titlePosition:'over'
    });
    
    showErrorMessage('');
    
    $('.tooltips').tipsy({gravity: 'n'});
    
    jQuery('.content_main a[href^=#]:not(.content_main a.fancybox)').bind('click', function(event) {            
        event.preventDefault();
        var name = $(this).attr('href').substr(1);
        
        if(!name)
        {
            return false;
        }
        
        if($('a[name='+name+']').length)
        {
            jQuery.scrollTo('a[name='+name+']', 800, {offset: {top: -15}});
        }
        else if($('#'+name).length)
        {
            jQuery.scrollTo('#'+name, 800, {offset: {top: -15}});
        }
    });
});
//---------------------------------------------------------------

/*
	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');
        }
    }

}
//----------------------------------------------------------

function validateSearchForm()
{
    var jField = $('#qSearchField');
    var value = $.trim(jField.val());
    var defaultValue = $.trim(jField.attr('title'));
    
    if ((defaultValue) && (value == defaultValue))
    {
        value = '';
    }
    $('#qSearchButton').blur();
    
    return (value) ? true : false;
}
//----------------------------------------------------------

/*
	Opens popup window, whitch is centered in screen
	Param:
		id string (element id to get content)
		width int (window width)
		height int (window height)
*/
function openPrintPopup(id,width,height) {
	var rnd = (Math.round((Math.random()*999)+1));
    var html = $('#'+id).html();
    
	scrollbars = 'yes';
	
	var top = (screen.height) ? (screen.height-height)/2 : 0;
	var left = (screen.width) ? (screen.width-width)/2 : 0;
    
	my_window = window.open("", "w"+rnd, "top="+top+",left="+left+",width="+width+",height="+height+",buttons=no,scrollbars="+scrollbars+",location=no,menubar=no,resizable=no,status=no,directories=no,toolbar=no");
	my_window.document.writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
    my_window.document.writeln("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
	my_window.document.writeln("<head>");
	my_window.document.writeln("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
	my_window.document.writeln("<meta http-equiv=\"Content-Script-Type\" ","content=\"text/javascript\">");
    my_window.document.writeln("<title>Print document</title>");
    my_window.document.writeln("<style type=\"text/css\">");
    my_window.document.writeln(".printHide { display:none; }");
    my_window.document.writeln("<\/style>");
    my_window.document.writeln("<\/head>");
	my_window.document.writeln("<body onload=\"window.print()\" style='margin:15px;padding:0px;'>");
	my_window.document.writeln(html);
	my_window.document.writeln("<\/body><\/html>");
	my_window.document.close();
	
	return false;
}
//---------------------------------------------------------------

