function Carmin(site, className)
{
    var this_ref = this;
    
    this.site = site;
    this.className = className;
    this.prettyPhoto = 
    {
        theme: "light_square", 
        social_tools: "", 
        autoplay: false, 
        overlay_gallery: false, 
        show_title: false, 
        autoplay_slideshow: false
    }
    this.config = 
    {
        
    };
    
    this.status = 
    {
    
    }
    this.menu = function(menu)
    {
        var self = this;
        this.flag = false;
        this.menu = $('#'+menu+'-over');
        this.trigger = $('#'+menu);
        this.to = 20;

        this.initHideFn = function()
        {
          self.flag=false;
          setTimeout(self.hideFn, self.to);
        };

        this.initShowFn = function()
        {
          self.flag = true;
        };

        this.hideFn = function()
        {
          if(!self.flag)
          {
            self.trigger.find("a").removeClass('hover');
            self.menu.slideUp(100);
          }
        };

        this.showFn = function()
        {
          self.initShowFn();
          self.trigger.find("a").addClass('hover');
          self.menu.slideDown(300);
        };

        this.init = function()
        {
           if(self.trigger.hasClass("inactive") && self.menu.children().length){
              self.trigger.mouseenter(this.showFn).mouseleave(this.initHideFn);
              self.menu.mouseenter(this.initShowFn).mouseleave(this.initHideFn);
           }
        }
        
        this.init();
    }
    this.slideshow = function(obj, nav)
    {
   
        var cycle, go, stop, interval, active, next, can=true, interval_sec = 5000;
        
        var _navUpdate = function(el)
        {
            if(nav)
            {
                nav.find('li').removeClass('active');
                
                obj.children().each(function(i,e)
                {
                    if(e==el[0])
                    {
                        nav.find('li:eq('+i+')').addClass('active');
                    }
                    
                });
            }
        }
        
        this.cycle = cycle = function(next)
        {   
            active = obj.find('li.active');
            
            next =  (next && next.length) ? 
                    next : 
                    (active.next().length ? 
                            active.next() : 
                            obj.find('li:first')
            );
                
            if(!can || next[0]==active[0])
                return false;
            
            can = false;
            //next.css(alert(obj));
            active.addClass('last-active');
            next.css({opacity: 0.0})
                .addClass('active')
                .animate({opacity: 1.0}, 200, function() {
                    active.removeClass('active last-active');
                    can=true;
                });
            _navUpdate(next);
         }
         
         this.go = go = function(num)
         {
            stop();
            cycle(typeof num == "object" ? num : obj.find('li').eq(num));
         }
          
         this.run = function()
         {
             if(!obj.find('li.active').length)
             {
                 obj.find('li').removeClass('active');
                 obj.find('li:first').addClass('active')
             }
             if(nav && !nav.find('li.active').length)
             {
                nav.find('li').removeClass('active');
                nav.find('li:first').addClass("active");
             }
             stop();
             interval = setInterval(cycle, interval_sec);
         }
         
         this.stop = stop = function()
         {
             if(interval)
                 clearInterval(interval);
         }
         
         if(nav)
         {
             nav.find('li').each(function(i)
             {
                 $(this).click(function(){
                     go(i);
                     return false;
                 })
             })
         }
    };
    
    this.error = function(msg, title)
    {
        $("<div>", {"title": title}).html(msg).dialog(
        {
            modal: true,
            buttons: {
                "Zamknij": function(){$(this).dialog("close");}
            }
        });
    }
    
    this._prepareForm = function()
    {
        $("form.normal-form").submit(function()
        {
           
            var data = $(this).serialize();
            var error = [];
            
            if($("[name='email_name']").val()=="")
            {
                error.push("Podaj swoje imię i nazwisko");
            }

            if(!/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i.test($("[name='email_email']").val()))
            {
                error.push("Zły email");
            }

            if($("[name='email_msg']").val()=="")
            {
                error.push("Wpisz wiadomość");
            }

            if(error.length>0)
            {
                this_ref.error(error.join("<br/>"), "Bład danych");
                return false;
            }
            
            $("#msg-loading").addClass("yes");
            $.ajax({
                url: "/ajax-public/kontakt",
                data: data,
                type: "post"
            }).
            done(function(resp)
            {
                if(resp=="success")
                {
                    $("#msg-loading").
                        removeClass("yes").
                        text("Wiadomość wysłana").
                        fadeOut(500).
                        fadeIn(500).
                        fadeOut(500).
                        fadeIn(500).
                        fadeOut(500).
                        fadeIn(500).
                        fadeOut(500, function(){
                            $(this).
                                text("").
                                show().
                                parents("form")[0].reset();
                        });
                }
                else{
                    this_ref.error(resp, "Błąd transmisji");
                }
            });
            return false;
        });
    }
    
    this.run = function()
    {
        this_ref.global();
        var _method = this_ref[this_ref.site];
        if(typeof _method == 'function')
        {
            _method();
        }
        _method = this_ref[this_ref.className];
        if(typeof _method == 'function')
        {
            _method();
        }
    }
    
    this.index = function()
    {
       new this_ref.slideshow($('.slide-slide'), $('.slide-nav')).run();
    }
    
    this['o-firmie'] = function()
    {    
        $("[rel^='prettyPhoto']").prettyPhoto(this_ref.prettyPhoto);
    }
    
    this['o-firmie_kontakt'] = function()
    {
         this_ref._prepareForm();
    }
    
    this['o-firmie_index'] = function()
    {
          new this_ref.slideshow($('#carmin_slideshow'), $('#carmin_slideshow_nav')).run();
    }
    
    this.oferta_product = function()
    {
        
        $("[rel^='prettyPhoto']").prettyPhoto(this_ref.prettyPhoto);
        $("#gallery-wrapper ul a").mouseenter(function(){
            var t = $(this).attr("href");
            $("#gallery-wrapper .gallery-big").attr("href", t.replace("/max/", "/full/")).find("img").attr("src", t).css("width", "auto").css("height", "auto");
        }).click(function(){
            var t = $(this).attr("href").replace("/max/", "/full/");
           $("#gallery-hidden a[href='"+t+"']").click();
           return false;
        });
        
        $("#gallery-wrapper .gallery-big").click(function(){
           var t = $(this).attr("href");
           $("#gallery-hidden a[href='"+t+"']").click();
           return false;
        });
        $(".more a[title], .qtip").qtip({
            style:
            {
              classes: 'ui-tooltip-tipsy'
            },
            position:{
                my: "top center",
                at: "bottom center"
            }
        });
        
        $(".sidebar a img").each(function()
        {
            var t = $(this)
            var full  = t.attr("src").replace("/micro/", "/min/");
            var content = $('<a />', 
                  { 
                     href: $(this).parents("a").attr('href'),
                     target: '_parent' // This is just so the links work in the demos section, you can take it out! :)
                  })
                  .append( $('<img />', {src: full, "class": "block"}).css("padding", "4px 0px") );
                  
            $(this).qtip(
            {
                content: content,
                style:
                {
                  classes: 'ui-tooltip-light'
                },
                position:{
                    my: "center",
                    at: "center",
                    viewport: $(window)
                },
                hide: {
                    fixed: true
                }
            })
        });
        
        $(".structs a").each(function()
        {
            var content = $(this).find("img").addClass("block").css("padding", "4px 0");
                  
            $(this).qtip(
            {
                content: content,
                style:
                {
                  classes: 'ui-tooltip-tipsy'
                },
                position:{
                    my: "bottom center",
                    at: "top center",
                    viewport: $(window)
                },
                hide: {
                    fixed: true
                }
            })
        });
        this_ref._prepareForm();
    }
    this.global = function()
    {
       
       var dostawcy = new this_ref.slideshow($('#footer-right ul'));

       dostawcy.run();
       
       $("#footer-right a.next").click(function(){
           var next = $('#footer-right ul li.active');
           next  = next.next().length ? next.next() : $('#footer-right ul li:first');
           dostawcy.go(next);
           return false;
       });
       
       $("#footer-right a.prev").click(function(){
           var prev = $('#footer-right ul li.active');
           prev  = prev.prev().length ? prev.prev() : $('#footer-right ul li:last');
           dostawcy.go(prev);
           return false;
       });
       $("a[href='#null']").click(function(){return false});
       $("a._blank").attr("target", "_blank");
        $("#q").autocomplete({
			minLength: 1,
                        appendTo: "#search_ac_container",
                        //autoFocus: true,
                        delay: 200,
			source: function(req, resp){
                            $.getJSON("/ajax-public/ac/?q=" + encodeURIComponent(req.term), resp);
                        },
			select: function( event, ui )
                        {
                            document.location.href = ui.item.url;
                            return false; 
                        },
                        focus: function( event, ui ) {
				$( "#q" ).val( ui.item.product_name );
				return false;
			}
                })
                .data( "autocomplete" )._renderItem = function( ul, item )
                {
                       ul.attr("id", "search_ac")
                       if(item.type==1)
                       {
                        
                        var image   = $("<img/>").attr("src", (
                            item.picture_id_fk ? 
                                "/products/min/"+item.picture_id_fk+".jpg" :
                                "/gfx/def/product-75.jpg"
                            )
                        );
                        
                        var h6      = $("<h6>").text(item.product_name);
                        var h7      = $("<h7>").text(item.category_title);
                        var clr     = $("<div>").addClass("clr");
                        var a       = $("<a>").attr("href", item.url + item.product_id)
                        
                        a.append(image).append(h6).append(h7).append(clr);
                        
                        return $( "<li></li>" ).
                            data( "item.autocomplete", item ).
                            append(a).
                            appendTo( ul );
                       }
                       else{
                           if(item.count > 0)
                           {
                               return $( "<li></li>" ).
                                        data( "item.autocomplete", item ).
                                        addClass("more center").
                                        append(
                                            $("<a>").
                                                attr("href", item.url).
                                                text("Zobacz wszystkie wyniki wyszukiwania ("
                                                    +item.count+") »")
                                       ).
                                       appendTo( ul );
                           }
                       }
		};
       
       new this_ref.menu('sub-1');
       new this_ref.menu('sub-2');
       new this_ref.menu('sub-3');
       new this_ref.menu('sub-4');
    }
    
}

$(function()
{
    window.CarminCB = {};
    new Carmin($('body').attr('id'), $('body').attr('class')).run();
});

