$(document).ready( function(){
    var searchText = $("input.sText").val();
    var img = new Array();
    img.push('gfx/layout/default/top/bg_img1.png');
    img.push('gfx/layout/default/top/bg_img2.png');
    img.push('gfx/layout/default/top/bg_img3.png');
    img.push('gfx/layout/default/top/bg_img4.png');
    img.push('gfx/layout/default/top/bg_img5.png');
    img.push('gfx/layout/default/top/bg_img6.png');
    img.push('gfx/layout/default/top/bg_img10.png');
    img.push('gfx/layout/default/pl/menu/1.png');
    img.push('gfx/layout/default/pl/menu/2.png');
    img.push('gfx/layout/default/pl/menu/3.png');
    img.push('gfx/layout/default/pl/menu/4.png');
    img.push('gfx/layout/default/pl/menu/5.png');
    img.push('gfx/layout/default/pl/menu/6.png');
    img.push('gfx/layout/default/pl/menu/10.png');
    img.push('gfx/layout/default/en/menu/1.png');
    img.push('gfx/layout/default/en/menu/2.png');
    img.push('gfx/layout/default/en/menu/3.png');
    img.push('gfx/layout/default/en/menu/4.png');
    img.push('gfx/layout/default/en/menu/5.png');
    img.push('gfx/layout/default/en/menu/6.png');
    img.push('gfx/layout/default/en/menu/10.png');
    img.push('gfx/layout/default/ru/menu/1.png');
    img.push('gfx/layout/default/ru/menu/2.png');
    img.push('gfx/layout/default/ru/menu/3.png');
    img.push('gfx/layout/default/ru/menu/4.png');
    img.push('gfx/layout/default/ru/menu/5.png');
    img.push('gfx/layout/default/ru/menu/6.png');
    img.push('gfx/layout/default/ru/menu/10.png');
    img.push('gfx/layout/default/de/menu/1.png');
    img.push('gfx/layout/default/de/menu/2.png');
    img.push('gfx/layout/default/de/menu/3.png');
    img.push('gfx/layout/default/de/menu/4.png');
    img.push('gfx/layout/default/de/menu/5.png');
    img.push('gfx/layout/default/de/menu/6.png');
    img.push('gfx/layout/default/de/menu/10.png');

    for(i=0; i<img.length; i++)
    {
        $.get(img[i], function(data){});
    }


    $("input.sText").focus(function(){
        if( $(this).val()==searchText )
        {
            $(this).val('');
        }
    }).blur(function(){
        if( $(this).val()=='' )
        {
            $(this).val(searchText);
        }
    });

    $('#logo').click(function(){
        window.location.href='index.php';
    });

    $('li.li1').each(function(){
        var c1 = $('#topBanner').attr('class');
        var c2 = $('#mNav').attr('class');
        $(this).mouseenter(function(){
            var id = $(this).attr('id').substring(2);
            $('#topBanner').removeClass().addClass('nav_link'+id);
            $('#mNav').removeClass().addClass('mNav nav_link'+id);
        }).mouseleave(function()
        {
            $('#topBanner').attr('class', c1);
            $('#mNav').attr('class', c2);
        })
    });

    $('li.li1:has(ul)').each(function(){

        $(this).click(function(event){
            event.preventDefault();
            var target = $(this).children('ul');
            $("ul[id^=nav_link][id$=_sub]:visible").not(target).slideUp();
            $("ul.ul1 li.li1").removeClass('active');
            target.slideToggle(function(){
                $(this).parent('li').addClass('active');
                $(this).children('li').children('a').each(function(){
                    $(this).click(function(){
                        window.location.href = $(this).attr('href');
                    })
                })
            })
        })
    });

    $('div.cRight a[id!="locked"]:has(img)').attr('rel', 'prettyPhoto[gallery]').prettyPhoto();

    $('#sendOrder').click(function(){
        var q = window.prompt('Podaj swój adres email:');
        if(q)
        {
            window.location.href = 'index.php?s=sendOrder&mail='+q;
        }
    });
});

function setLinks()
{
    $('a[id^=nav_link]').each(function(){
        $(this).unbind('click');
        $(this).click(function(event){
            event.preventDefault();
            var stop = false;
            $(this).parent().has('ul').each(function(){
                stop = true;
            });
            if(stop == true)
            {
                return;
            }
            id = $(this).attr('id').substring(0, 9);
            $('#topBanner').fadeOut("slow", function(){
                $(this).attr('class', id);
            }).fadeIn("slow");
            $('div.mNav').fadeOut("slow", function(){
                $(this).attr('class', 'mNav '+id);
            }).fadeIn("slow");


            $.ajax({
                url: $(this).attr('href'),
                dataType: 'json',
                success: function(data){
                    $('#cLeft').fadeOut("slow", function(){
                        $(this).html(data.cLeft);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                    $('#cRight').fadeOut("slow", function(){
                        $(this).html(data.cRight);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                    $('#cPath').fadeOut("slow", function(){
                        $(this).replaceWith(data.cPath);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                }
            });
        })
    });
    
    $('a.nav_link_sub,a[id^=nb2_],a.main_b,a[id^=mlink],a[id^=blink]').each(function(){
        $(this).unbind('click');
        $(this).click(function(event){
            event.preventDefault();
            id = $(this).parent().parent().attr('id').substring(0, 9);
            $('#topBanner').fadeOut("slow", function(){
                $(this).attr('class', id);
            }).fadeIn("slow");
            $('div.mNav').fadeOut("slow", function(){
                $(this).attr('class', 'mNav '+id);
            }).fadeIn("slow");

            $.ajax({
                url: $(this).attr('href'),
                dataType: 'json',
                success: function(data){
                    $('#cLeft').fadeOut("slow", function(){
                        $(this).html(data.cLeft);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                    $('#cRight').fadeOut("slow", function(){
                        $(this).html(data.cRight);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                    $('#cPath').fadeOut("slow", function(){
                        $(this).replaceWith(data.cPath);
                        $(this).fadeIn("slow");
                    }).ajaxComplete(function(){
                        setTimeout('setLinks()', 1000);
                    });
                }
            });
        })
    });
}
