
var ste;
application = {
  products: new Array(),
  categories: new Array(),
  bind_ajax_products: function(){
    jQuery("h3").not(".selected").next("ul.level2").hide();
    jQuery("ul.level1 li h3 a").click(function(e){
      var clicked_link=jQuery(this);
      jQuery("h3.selected").not(jQuery(this).parent()).removeClass("selected").next("ul.level2").slideUp();
      jQuery(this).parent().addClass("selected").next("ul.level2").slideDown(function(){
        var category_id=parseInt(clicked_link.attr("id").replace(/category_/,''));
        if(!application.categories[category_id]) {
          jQuery("#right").html('<img src="/public/images/ajax-loader.gif" alt="Ładuję..." class="ajax-loader" />');
          jQuery.get(jQuery(clicked_link).attr('href')+"?method=ajax",function(d) {
            application.categories[category_id]=d;
            jQuery("#right").html(application.categories[category_id]);
          });
        }
        else {
          jQuery("#right").html(application.categories[category_id]);
        }
      });
      e.preventDefault();
    });
    jQuery("ul.level2 li a").click(function(e){
      var product_id=parseInt(jQuery(this).attr("id").replace(/product_/,''));
      if(!application.products[product_id]) {
        jQuery("#right").html('<img src="/public/images/ajax-loader.gif" alt="Ładuję..." class="ajax-loader" />');
        jQuery.get(jQuery(this).attr('href')+"?method=ajax",function(d) {
          application.products[product_id]=d;
          jQuery("#right").html(application.products[product_id]);
        });
      }
      else {
        jQuery("#right").html(application.products[product_id]);
      }
      e.preventDefault();
    });
  },
  canchange: true,
  bind_promotions: function(){
    jQuery("#promotions-chooser a:first").addClass("current");
    setInterval(function(){
      application.canchange=false;
      jQuery("#promotions a.promotion:first").fadeOut(500,function(){
        jQuery(this).clone(true).insertBefore(jQuery("#promotions-chooser"));
        jQuery(this).remove();
        jQuery("#promotions a.promotion:last").css({display:"inline",opacity:"1",zIndex:-1});
        jQuery("#promotions a.promotion").each(function(){
          jQuery(this).css("z-index",parseInt(jQuery(this).css("z-index"))+1);
        });
        jQuery("#promotions-chooser a.current").removeClass("current").parent("li").next("li").find("a").addClass("current");
        if(jQuery("#promotions-chooser a.current").size()==0)
          jQuery("#promotions-chooser a:first").addClass("current");
        application.canchange=true;
      });
    },5000);
    jQuery("#promotions-chooser a").click(function(){
      if(application.canchange){
        application.canchange=false;
        var lastimg=jQuery("#promotions a.promotion:last").get(0);
        jQuery("#promotions a#promotion-"+jQuery(this).attr('id').replace('show-promotion-','')).prevAll("a").each(function(){
          jQuery(this).clone(true).insertAfter(jQuery(lastimg));
          jQuery(this).remove();
        });
        jQuery("#promotions a.promotion").each(function(i){
          jQuery(this).css("z-index",jQuery("#promotions a.promotion").size()-i);
        });
        jQuery("#promotions-chooser a.current").removeClass("current");
        jQuery(this).addClass("current");
        application.canchange=true;
      }
    });
  },
  bind_gallery:function(){
    jQuery("#gallery #photos a").lightBox();
    if(jQuery("#gallery #photos a").size()>5) {
      jQuery("#gallery #photos a").css({opacity:1});
      setInterval(function() {
        jQuery("#gallery #photos a").slice(5,6).css({opacity:0}).animate({opacity:1},1000);
        jQuery("#gallery #photos a:first").animate({opacity: 0},1000);
        jQuery("#gallery #photos").animate({left: -154},1000,function() {
          jQuery("#gallery #photos a:first").clone(true).appendTo(jQuery("#gallery #photos"));            
          jQuery("#gallery #photos a:first").remove();
          jQuery("#gallery #photos").css({left: 0}).find("a:last").css({opacity:1});
        });
      },10000);
    }
  },
  bind_ste:function(){
    ste = new SimpleTextEditor("description", "ste");
    ste.init();
    jQuery(".submit input").click(function() {
      ste.submit();
    });
  }
}
jQuery(function(){
  jQuery(document).pngFix();
});
