$(document).ready(function() {

  /* Auto flash */
  var flash_id = 1;
  $(".flash").each(function() {
    $(this).html("Brak wtyczki Flash Player.");
    var attributes = $(this).attr("title").split(" ");
    var id = "flash"+flash_id;
    $(this).attr("id", id);
    var so = new SWFObject("layouts/wl2010/"+attributes[0], "flash", attributes[1], attributes[2], "8", "#ffffff");
    so.addParam("allowscriptaccess", "always");
	so.addParam("wMode", "transparent");
    so.write(id);
    $(this).removeAttr("title").removeClass("flash");
    flash_id++;
  });
  
  /* Zdjecie produktu jako link */
  $(".items li").css("cursor", "pointer").click(function() {
    location.href = $(this).find("a").attr("href");
  });
  
   $("li.item").css("cursor", "pointer").click(function() {
    location.href = $(this).find("a").attr("href");
  });

  /* Sugestie wyszukiwarki */
  $(".search form").append("<ul class=\"ajax_search\"></ul>");
  $(".search input[type=text]").attr("autocomplete", "off");
  var timer;
  $(".search input[type=text]").keypress(function() {
    clearTimeout(timer);
    if($(this).val() != '') {
      timer = setTimeout("getAjaxResults();", 2000);
    }
  });
  var mouseoversearch = false;
  $(".ajax_search").mouseenter(function() {
    mouseoversearch = true;
  });
  $(".ajax_search").mouseleave(function() {
    mouseoversearch = false;
  });
  $(".search input[type=text]").blur(function() {
    if(mouseoversearch) {
      //przypisac zdarzenie do zmiennej, jezeli wyjdzie poza obszar to je skillowac i ukryc ajax_search
    } else setTimeout("$(\".ajax_search\").css(\"display\", \"none\")", 100); // a tu chyba wtedy nie potrzeba settimeout
  });
  
  /* Press news jako link */
  $(".press_link").each(function() {
    $(this).parent().css("cursor", "pointer").click(function() {
      location.href = $(this).find("a").attr("href");
    });
  });
  
   /* Newsy na glownej jako linki */
   
   /*
  $(".news li").css("cursor", "pointer").click(function() {
    location.href = $(this).find(".title a").attr("href");
  });
  */

  $(".news li .title a").each(function() {
    var text = $(this).html();
    text = text.replace(/prima moda/i, '<img src="images/logo-text-white.png" alt="Prima Moda" />');
    $(this).html(text);
  });

  $(".news_standalone li .title a").each(function() {
    var text = $(this).html();
    text = text.replace(/prima moda/i, '<img class="nomargin" src="images/logo-text.png" alt="Prima Moda" />');
    $(this).html(text);
  });

  $("#NewsPostDetailContent p").each(function() {
    var text = $(this).html();
    text = text.replace(/prima moda/i, '<img src="images/logo-text-small.png" alt="Prima Moda" />');
    $(this).html(text);
  });

   $(".posttitle").each(function() {
    var text = $(this).html();
    text = text.replace(/prima moda/i, '<img src="images/logo-text-big.png" alt="Prima Moda" />');
    $(this).html(text);
  });
  
  
  /* catalog 2011 */
  var citem = 0;
  $("#next").fadeIn(1);
  
   $("#next").click(function() {
      citem = c_next(citem);
   });
   
   $("#prev").click(function() {
      citem = c_prev(citem);
   });
   
   function c_next(citem) {
     $("#catalog2011 li.item"+citem).fadeOut();
     $("#catalog2011 li.item"+(++citem)).fadeIn();
     if(citem > 0){ $("#prev").fadeIn(1); }
     if(citem > 9){ $("#next").fadeOut(1); }
     return citem;
   }
   
   function c_prev(citem) {
      $("#catalog2011 li.item"+citem).fadeOut();
      $("#catalog2011 li.item"+(--citem)).fadeIn();
      if(citem < 10){ $("#next").fadeIn(1); }
      if(citem < 1){ $("#prev").fadeOut(1); }
      return citem;
   }
  

  
  /* Color picker */
  
  $(".advs .color div input[type=radio]").click(function(){
    $(".advs .color div input[type=radio]").parent().find("label").css({border: '2px solid #eee'});
    $(this).parent().find("label").css({border: '2px solid #ff0000'});
   });
   
  /* Unused colors disabler */
  
  $(".advs .color div label[for=fbrp__33_1_8]").parent().css({display: 'none'});
  $(".advs .color div label[for=fbrp__33_1_3]").parent().css({display: 'none'});
  $(".advs .color div label[for=fbrp__33_1_16]").parent().css({display: 'none'});
  $(".advs .color div label[for=fbrp__33_1_6]").parent().css({display: 'none'});
   
   /* Advances search buttons */
   
  $("#close-advs").click(function() { $("#advs-wrapper").fadeOut('slow'); });
  $(".advs_open").click(function() { $("#advs-wrapper").fadeIn('slow'); });
  
  
  
/* Poll */

  $(".poll .submit").hide();
  $(".poll").each(function() {
    $("#flash1").hide();
  });
  $(".poll input").click(function() {
    $(".poll form").submit();
    $(".poll").hide();
    $("#flash1").show();
  });
  $(".poll form").ajaxForm(function() {});
  $(".poll").append('<div class="close"></div>').show();
  $(".poll .close").click(function() {
    $(".poll").hide();
    $("#flash1").show();
    var id = $(".poll").attr("id");
    set_cookie(id, '1', 2020, 1, 1);
  });
});

function getAjaxResults() {
  var query = $(".search input[type=text]").val();
  $.ajax({
   type: "GET",
   url: "ajax-search.html",
   data: "query="+query,
   success: function(msg){
     $(".ajax_search li").unbind('click');
     $(".ajax_search").html(msg).css("display", "block");
     $(".ajax_search li").click(function() {
       var val = $(this).html();
       $(".search input[type=text]").val(val);
       $(".search form").submit();
     });
   }
 });
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {
  var cookie_string = name + "=" + escape ( value );
  if ( exp_y ) {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }
  if(path)
    cookie_string += "; path=" + escape ( path );
  if(domain)
    cookie_string += "; domain=" + escape ( domain );
  if(secure)
    cookie_string += "; secure";
  document.cookie = cookie_string;
}

/*catalog*/

function catalog2011(){
 

}


/* rotate */

function rotate(){
  count = $('.banners').children().size();
  var imgs = '';
  var j = 0;
  var k = 0;
  var s;

  s_interval(count,j,k,s);

}

function switch_img(thisone, nextone) {
    if(thisone != nextone)
    {
      $(".item"+thisone).fadeOut(1500);
      $(".item"+nextone).fadeIn(1500);
    }
  }

  function s_interval(count,j,k,s) {
    s = setInterval(function() {
      thisone = k%count;
      nextone = (k+1)%count;
      k++;
      switch_img(thisone, nextone);
    }, 4500);
  }

  function c_interval() {
    clearInterval(s);
  }


