var prodPages = new Array();
prodPages[0] = 'informacje-ogolne';

$(document).ready(

    function() {
        $('#prod-menu').children('.position').each(
            //
            function(i) {
                $(this).click (
                    function () {
                        $(this).blur ();
                        $('#prod-menu').children('.position').each(
                            function(i) { 
                                $(this).removeClass('select'); 
                                $(this).children('a').blur();
                            }
                        )
                        
                        $('#prod-content').children('div').each( function(i) { $(this).hide(); } )
                        $('#prod-load').show();
                        
                        //$(this).addClass('select');
                        hash   = $(this).children('a').attr('href');
                        tmpId  = $(this).attr('id');
                        tmpId  = tmpId.split('-');
                        thisId = tmpId[1];
                        location.hash = hash;
                        hash = hash.replace("#", "");
                       
                        if (prodPages[thisId]!= null) {
                            $('#subpage-' + thisId + '').addClass('select');
                            $('#prod-load').hide();
                            $('#prodcont-' + thisId + '').show();
                            return false;
                        }
                        
                        urlToLoad = location.href;
                        urlToLoad = urlToLoad.replace(".html", "");
                        urlToLoad = urlToLoad.replace("#", "/");
                        
                        $.get(
                            urlToLoad, 'ajax=1',
                            function(data){
                                //alert("Data Loaded: " + data);
                                if (data.search('error') != -1) {
                                    location.hash = '#' + prodPages[0] + '';
                                    $('#subpage-0').addClass('select');
                                    $('#prod-load').hide();
                                    $('#prodcont-0').show();
                                    
                                    return false;
                                } else {
                                    $('#prod-content').append(data);
                                    prodPages[thisId] = hash;
                                    $('#subpage-' + thisId + '').addClass('select');
                                    $('#prod-load').hide();
                                    $('#prodcont-' + thisId + '').show();
                                    set_option_meth()
                                }
                            }
                        );
                        
                        /**/
                        return false;
                    }
                )
            }
        )
        
        if  (location.hash != '') {
            hash   = location.hash;
            thisId = 0;           
            $('#prod-menu').children('.position').each(
                function(i) { 
                    mHash   = $(this).children('a').attr('href');
                    mTmpId  = $(this).attr('id');
                    mTmpId  = mTmpId.split('-');
                    mThisId = mTmpId[1];
                    if (mHash == hash) { thisId = mThisId; }
                }
            )            
            hash = hash.replace("#", "");
            
            if (hash == prodPages[0]) {
                $('#subpage-0').addClass('select');
                $('#prod-load').hide();
                $('#prodcont-0').show();
            } else {
                urlToLoad = location.href;
                urlToLoad = urlToLoad.replace(".html", "");
                urlToLoad = urlToLoad.replace("#", "/");                        
                $.get(
                    urlToLoad, 'ajax=1',
                    function(data){
                        if (data.search('error') != -1) {
                            location.hash = '#' + prodPages[0] + '';
                            $('#subpage-0').addClass('select');
                            $('#prod-load').hide();
                            $('#prodcont-0').show();
                            return false;
                        } else {
                            $('#prod-content').append(data);
                            prodPages[thisId] = hash;
                            $('#subpage-' + thisId + '').addClass('select');
                            $('#prod-load').hide();
                            $('#prodcont-' + thisId + '').show();
                            set_option_meth();
                        }
                    }
                );
            }
        } else {
            //location.hash = '#' + prodPages[0] + '';
            $('#subpage-0').addClass('select');
            $('#prod-load').hide();
            $('#prodcont-0').show();
        }       
        
    }
    
);


function set_option_meth() {
    $('.top').each (
        function (i) {            
            $(this).click (
                function () {
                    $('body').scrollTo(1, { axis:'y', duration:500 } );
                    return false;
                }
            )
        }
    )
    
    $('.print').each (
        function (i) {            
            $(this).click (
                function () {
                    print();
                    return false;
                }
            )
        }
    )
    $('.bookmark').jFav();
}

