var offerPages = new Array();
offerPages[0] = 'informacje-ogolne';

$(document).ready(

    function() {
        $('#offer-menu').children('.position').each(
            function(i) {
                $(this).click (
                    function () {                        
                        $('#offer-menu').children('.position').each(
                            function(i) { 
                                $(this).removeClass('select'); 
                                $(this).children('a').blur();
                            }
                        )
                        $('#offer-content').children('div').each( function(i) { $(this).hide(); } )
                        $('#offer-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 (offerPages[thisId]!= null) {
                            $('#subpage-' + thisId + '').addClass('select');
                            $('#offer-load').hide();
                            $('#offercont-' + 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 = '#' + offerPages[0] + '';
                                    $('#subpage-0').addClass('select');
                                    $('#offer-load').hide();
                                    $('#offercont-0').show();
                                    
                                    return false;
                                } else {
                                    $('#offer-content').append(data);
                                    offerPages[thisId] = hash;
                                    add_select();
                                    $('#subpage-' + thisId + '').addClass('select');
                                    $('#offer-load').hide();
                                    $('#offercont-' + thisId + '').show();
                                }
                            }
                        );
                        return false;                        
                    }
                )
            }
        
        )

        
        if  (location.hash != '') {
            hash   = location.hash;
            thisId = 0;           
            $('#offer-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 == offerPages[0]) {
                $('#subpage-0').addClass('select');
                $('#offer-load').hide();
                $('#offercont-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 = '#' + offerPages[0] + '';
                            $('#subpage-0').addClass('select');
                            $('#offer-load').hide();
                            $('#offercont-0').show();
                            return false;
                        } else {
                            $('#offer-content').append(data);
                            offerPages[thisId] = hash;
                            $('#subpage-' + thisId + '').addClass('select');
                            add_select();
                            $('#offer-load').hide();
                            $('#offercont-' + thisId + '').show();
                        }
                    }
                );
            }
        } else {
            //location.hash = '#' + offerPages[0] + '';
            $('#subpage-0').addClass('select');
            $('#offer-load').hide();
            $('#offercont-0').show();
        }       
        
    }

);

function add_select() {
    $('.sub-prod').children('.position').each(
        function (i) {
            $(this).mouseenter (
                function () {
                    $(this).addClass('select');
                }
            );
            $(this).mouseleave (
                function () {
                    $(this).removeClass('select');
                }
            );
            $(this).click (
                function () {
                    location.href = $(this).children('.prev').children('.more').children('a').attr('href');
                }                
            );
        }
    );
}

