/**
 * Pokazuje/ukrywa strone glowna
 */
var isMainPage = true;
function showMainTheme(){
    var IEsmierdzi2 = ( $.browser.msie ) ? ', .contents .jScrollPaneContainer' : '';
    $('div.contents'+IEsmierdzi2).fadeOut('slow', function(){
        $('div.gallery').animate({
            'margin-left':'0px'
        }, 'slow', function(){
            isMainPage = true;
        });
    });
}
function hideMainTheme(){
    $('div.gallery').animate({
        'margin-left':'-486px'
    }, 'slow', function(){
        showContents();
        isMainPage = false;
    });
}

/**
 * Pokazuje/ukrywa podstrony tekstowe
 */
function showContents(){
    var IEsmierdzi = ( $.browser.msie ) ? ', .contents .jScrollPaneContainer' : '';
    if ( $.browser.msie ){
        $('.contents .jScrollPaneContainer').css({'display':'none'});
    }
    $('div.contents'+IEsmierdzi).fadeIn('slow', function(){
        $('div.contents .scroller').jScrollPane({
            scrollbarWidth: 3,
            dragMinHeight:96,
            dragMaxHeight: 96
        });
    });
}
function hideContents(){
    $('div.contents').fadeOut('slow');
}

/**
 * Router
 */
function route( location ){
    var href = $(location).attr('href');
    href = href.replace('#', '');

	if ( $.browser.msie ){
		var iehref = $(location).attr('href');
		var hrefparts = iehref.split('#');
		href = hrefparts[1];
	}
	
    if ( href == 'home' ){
        if ( !isMainPage ){
            showMainTheme();
        }
    } else {
        if ( isMainPage ){
            hideMainTheme();
        }

        if ( !isMainPage ){
            var IEsmierdzi = ( $.browser.msie ) ? ', .contents .jScrollPaneContainer' : '';
            $('.contents'+IEsmierdzi).fadeOut('slow', function(){
                $('.contents').html( $('#subpages .'+href).clone(true) );

                if ( $.browser.msie ){
                    $('.contents .jScrollPaneContainer').fadeOut(0);
                }

                $('.contents'+IEsmierdzi).fadeIn('slow', function(){
                    $('div.contents .scroller').jScrollPane({
                        scrollbarWidth: 3,
                        dragMinHeight:96,
                        dragMaxHeight: 96
                    });
					//tb_init('a.thickbox');
                });


            });
        } else {
            $('.contents').html( $('#subpages .'+href).clone(true) );
			//tb_init('a.thickbox');
        }
        
        

    }

}

/**
* automatycznie laduje strone z linka #link
*/
function autoLinkCheck(){
	var pathname = window.location.href;
	var urlParts = pathname.split('#');
	if ( null != urlParts[1] ){
		
		$('.navigation ul a, a.subgallery').each(function(){
		
			if ( $(this).attr('href') == '#'+urlParts[1] ){
				$(this).trigger('click');
			}
		
		});
		
	}
}


/**
 * RUN
 */
$(document).ready(function(){
    $('.navigation ul a, #logo a, a.subgallery, a.localLink').live('click', function(){
        if ( $(this).hasClass('active') ) return;
        $('.navigation ul a, #logo a, a.subgallery').removeClass('active');
        $(this).addClass('active');
        route( this );
    });
    $('.scroller').jScrollPane({
        scrollbarWidth: 3,
        dragMinHeight:96,
        dragMaxHeight: 96
    });
	autoLinkCheck();
	$('.lightbox').lightbox({fitToScreen:true});
});
