var tabPane;
var tabUi;

// added to prevent older browsers from messing up the content
var no_tabs = 1;
//if (! jQuery.support.noCloneEvent && jQuery.support.objectAll)
if ($.browser.msie && parseInt($.browser.version) <= 6.0)
{
	no_tabs = 1;
}

function position_footer()
{
	var footer = $('#block-footer');

	if (footer.offset())
	{
		var bottom = footer.offset().top + footer.height();
		var win_h = $(window).height();

		var absolute = footer.css('position') == 'absolute';

		if (absolute)
		{
			if (bottom < win_h)
			{
				footer.css('position', 'relative');
			}
			else
			{
				footer.css('position', 'absolute');
			}
		}
		else
		{
			if (bottom > win_h)
			{
				footer.css('position', 'relative');
			}
			else
			{
				footer.css('position', 'absolute');
			}

		}
	}
}

$(function(){
		$(window).resize(function(){
			position_footer();
		});

		position_footer();

		if (! no_tabs) $('.root-panel').hide();

        $(document).pngFix();
        $('#block-popup').bgiframe();

        if (no_tabs)
        {
        	tabPane = $('#block-main > ul.notabs');
        }
        else
        {
			tabPane = $('#block-main > ul');
        }

        tabPane.tabs({
            //selected:null,
            cache:false,
            ajaxOptions:{cache:false},
            spinner:'Učitavanje...',
            fx:{ opacity: 'toggle', duration: 'fast' },
            select:function(e, ui){
                /*$("#image-header").fadeTo(1, 0.0, function()
                {
                $("#image-header").attr({ src: ui.tab.rel });
                $("#image-header").fadeTo(1500, 1.0);
                });*/

                $('#image-header').attr({src:ui.tab.rel});
                if (ui.tab.rev)
                {
                	$('#block-header').css('background-color', ui.tab.rev);
                }
                else
                {
                	$('#block-header').css('background-color', 'transparent');
                }
            },
            load:function(e,ui){
            	tabUi = ui;
                //if (ui.tab.attr('class') == 'active') alert(ui.tab.id);
                attach_submenu_links();
            }
        });

        if (no_tabs) attach_submenu_links();

        $('#search_form').submit(function(e){
        	var url = $(this).attr('action');
        	url += '?q=' + escape($('input[name=q]').val());
			load_popup_content(url);
		    $('#block-popup').fadeIn('fast');
        	e.preventDefault();
        });

        // preload content in all tabs
        //for (i = 0; i < tabPane.tabs('length'); i++) tabPane.tabs('load', i);

        // preload header images (found in rel="" on all menu links)
        var sell = $('#block-main > ul li a');
        if (no_tabs) sell = $('#block-main > ul.notabs li a');

        sell.each(function(index, item){
            $('<img>').attr('src', item.rel)
        });

        $('#popup-close').click(function(){
                $('#block-popup').fadeOut('fast');
        });

        $('#lang-choose').change(function(){
                window.location = '/'+$(this).val();
        });

        //tabPane.tabs('select', 2);
        
        $('.news-ticker').liScroll({travelocity:0.07});
        
});

function attach_submenu_links()
{
	$('.popup-open').click(function(){
		var href = $(this).attr('href');
		var rel = $(this).attr('rel');
		if (! href)
		{
			href = rel;
		}

		load_popup_content(href);
	    $('#block-popup').fadeIn('fast');
	    return false;
	});
}

function attach_form_in_popup()
{
	$('.form-in-popup form').submit(function(e){
		var act = $(this).attr('action');
		var data = $(this).serialize();

		load_popup_content_post(act, data);
		$('#block-popup').fadeIn('fast');

		e.preventDefault();
	});
}


function load_popup_content(url)
{
	popup = $('#popup-content');
    loading = $('#popup-loading');

	popup.html('');
    loading.show();

	$.get(url, function(data, status){
        loading.hide();
		popup.html(data);
	});
}

function load_popup_content_post(url, args)
{
	popup = $('#popup-content');
    loading = $('#popup-loading');

	popup.html('');
    loading.show();

	$.post(url, args, function(data, status){
        loading.hide();
		popup.html(data);
	});
}

