function pokazCookie(nazwa)
{
    if (document.cookie!="") 
    {
        var toCookie=document.cookie.split("; ");
        for (i=0; i<toCookie.length; i++)
        {
            var nazwaCookie=toCookie[i].split("=")[0];
            var wartoscCookie=toCookie[i].split("=")[1];
            if (nazwaCookie==nazwa) return unescape(wartoscCookie)
        }
    }
}


$(document).ready(function() {

         var new_map='<span class="map"><span class="s1" /><span class="s2" /><span class="s3" /><span class="s4" /><span class="s5" /><span class="s6" /><span class="s7" /><span class="s8" /><span class="s9" /></span>';
         var new_bg=$("<span>");
         new_bg.addClass("bg");
         $("#polska li a").append(new_map);
         $("#polska li a").append(new_bg);

         $("#polska li a").hover(function(){
             $('#state-name').html($(this).html());
         });

        $('#top').flash({ swf: 'public/images/default/'+style+'/top.swf', height: 351, width: 951 });

        $("a.popup").fancybox({
            'titleShow': false
        });

        $("a[rel=popup]").fancybox({
            'titleShow': false
        });

        $('.product a').tooltip({
                showURL: false
        });


        $('.menu > li').each(function(i){

            if(pokazCookie($(this).attr('id')) != 'undefined' && pokazCookie($(this).attr('id')) != 'null')
            {
                if($('#'+pokazCookie($(this).attr('id'))).children('ul').css('display') == 'none')
                {
                    $('#'+pokazCookie($(this).attr('id'))).children('a').addClass('active');
                    $('#'+pokazCookie($(this).attr('id'))).children('ul').show();
                }
                else
                {
                    $('#'+pokazCookie($(this).attr('id'))).children('a').removeClass('active');
                    $('#'+pokazCookie($(this).attr('id'))).children('ul').hide();
                }
            }
            
        });


        $('.menu > li > ul > li').each(function(i){

            if(pokazCookie($(this).attr('id')) != 'undefined' && pokazCookie($(this).attr('id')) != 'null')
            {
                if($('#'+pokazCookie($(this).attr('id'))).children('ul').css('display') == 'none')
                {
                    $('#'+pokazCookie($(this).attr('id'))).children('ul').show();
                }
                else
                {
                    $('#'+pokazCookie($(this).attr('id'))).children('ul').hide();
                }
            }

        });

        $('.menu > li.submenu > a').click(function () {

            var submenu = $(this).parent().children('ul');

            if(submenu.css('display') == 'none')
            {
                submenu.slideDown();
                $(this).parent().children('a').addClass('active');
                document.cookie = $(this).parent().attr('id')+"="+escape($(this).parent().attr('id'))+"; path=/";
            }
            else
            {
                submenu.slideUp();
                $(this).parent().children('a').removeClass('active');
                document.cookie = $(this).parent().attr('id')+"=null; path=/";
            }
            
            return false;
        });

        $('.menu > li > ul > li.submenu > a').click(function () {

            var submenu = $(this).parent().find('ul');

            if(submenu.css('display') == 'none')
            {
                submenu.slideDown();
                document.cookie = $(this).parent().attr('id')+"="+escape($(this).parent().attr('id'))+"; path=/";
            }
            else
            {
                submenu.slideUp();
                document.cookie = $(this).parent().attr('id')+"=null; path=/";
            }

            return false;
        });

        $('#newsletter-email').click(function () {
            $(this).val('');
        });

        $('#newsletter-signin').click(function () {
            $(this).attr('disabled', true);

            $.ajax({
                      url: lang+"/newsletter/wpisz.html",
                      async: true,
                      dataType: "html",
                      type: "POST",
                      data: $('#newsletter-email').serialize(),
                      beforeSend: function () {
                          $('#newsletter-loader').show().delay(500);
                      },
                      success: function (response) {
                            $('#newsletter-loader').hide();
                            $.fancybox('<div style="margin: 20px; font-weight: bold; color: #000;">'+response+'</div>');

                            $('#newsletter-signin').attr('disabled', false);
                      },
                      error: function () {
                          $('#newsletter-signin').attr('disabled', false);
                      }
               });
        });

        $('#newsletter-signout').click(function () {
            $(this).attr('disabled', true);

            $.ajax({
                      url: lang+"/newsletter/wypisz.html",
                      async: true,
                      dataType: "html",
                      type: "POST",
                      data: $('#newsletter-email').serialize(),
                      beforeSend: function () {
                          $('#newsletter-loader').show().delay(500);
                      },
                      success: function (response) {
                            $('#newsletter-loader').hide();
                            $.fancybox('<div style="margin: 20px; font-weight: bold; color: #000;">'+response+'</div>');

                            $('#newsletter-signout').attr('disabled', false);
                      },
                      error: function () {
                          $('#newsletter-signout').attr('disabled', false);
                      }
               });
        });

        $('#search-box').click(function () {
            $(this).val('');
        });

        $('#search-submit').click(function () {
            $('#search').submit();
        });
        
});
