
String.prototype.reverse = function() { return this.split("").reverse().join(""); }

function include(url) { $("<script>", { "type": "text/javascript", "src": url}).appendTo("body"); }

function fix_ie()
{
  var table = $('<table id="contents" cellspacing="0" cellpadding="0"></table>');
  var tr = $('<tr></tr>');
  
  tr.append($('<td id="content"></td>').html($("#content").html()));
  tr.append($('<td id="sidebar"></td>').html($("#sidebar").html()));
  table.append(tr);
  $("#contents").replaceWith(table);
  
  /*
  var table = $('<table id="menu"></table>');
  var tr = $('<tr></tr>');
  
  table.addClass("typeface-js");
  $("#menu ul > li").each(function ()
  {
     tr.append($("<td></td>").html($(this).html()));
  });
  table.append(tr);
  $("#menu").replaceWith(table);  
  */
}


$(function()
{
  var noresize = false;
  if($("#contents").css("display") != "table")
  {
   fix_ie();
   noresize = true;
  }
  
 $("input.hint")
 .each(input_hint_blur)
 .focus(function()
   {
      var v = $(this).attr("value");
      var t = $(this).attr("title");
      if(v == t) $(this).attr("value", "");
   })
 .blur(input_hint_blur);

 function input_hint_blur()
 {
    var v = $(this).attr("value");
    var t = $(this).attr("title");
    if(v == "") $(this).attr("value", t);
 }
   
 $(".submenu").hide();
 $(".showmenu").mouseover(function() { $(".submenu").hide(); $(this).next(".submenu").show().one("mouseleave", function() { $(this).hide(); }); });
 
 
 $(".help").each(function() 
 {
    var h = $(this);
    h.prev("input").bind("focus", function() { h.animate({"top": "0"}, 700).addClass("active") });
    h.prev("input").bind("blur", function() { h.animate({"top": "-2.3em"}, 700).removeClass("active") });
 });
 
 
  $('form').ketchup({
    validationAttribute: 'rel',
    errorContainer: $('<div>', {
      'class': 'form-error',
      'html': '<ol></ol>'
    }),
    positionContainer: function(errorContainer, field) {},
    initialPositionContainer: function(errorContainer, field) {}
  }); 
  
  $(".more").hide();
  $(".hide").hide();
  
  $(".showmorechk").bind("change", function() { $(this).parent().next(".more").slideToggle(); })
  $(".showmore").bind("click", function() { $(this).next(".more").slideToggle(); })
  
  
  
  $(window).resize(function() 
  {
    var home = $("#home");
    c = home.find(".cut");
    c.hide();
    space = home.height() - $("#actualcontent").height();
    lh = parseInt(c.css("line-height"));
    h = Math.floor(space / (lh * 2));
    c.height(h * lh + 2);
    c.show();
    
    $("#slider").height($("#slider img:first").height());
    
  });
  
  $(window).resize();

  img = $("#slider img:first");
  
  img.one("load", function()
  {
    var h = $(this).height();

    $("#slider").height(h);
    $(window).resize();    
    
    $("#slider").cycle({
      nowrap: true,
      after: function() 
        { 
          if(!noresize) $(window).resize(); 
  
        }
      });
  });
  
  if(img.get(0) && img.get(0).complete) img.load();
  
 
  $("input.counter").each(function() 
  {
    var input = $(this);
    
    p = $("<span>", { "class": "counter_plus", text: "+"});
    m = $("<span>", { "class": "counter_minus", text: "-"});
    p.click(function() { v = parseInt(input.val(), 10) + 1; if(v >= 0) input.val(v); input.change(); });
    m.click(function() { v = parseInt(input.val(), 10) - 1; if(v >= 0) input.val(v); input.change(); });
    
    w = $("<span>", { "class": "counter_wrap"});
    input.wrap(w);
    input.before(m);
    input.before(p);
  });
  
  $("input.num").bind("keypress", function(e) { return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ; });
  
  
  $("#orders input.counter").bind("keyup change", function()
  { 
    
    var row = $(this).closest("tr");
    
    row.find(".c_sum").text(parseInt(row.find(".c_price").text(), 10) * parseInt($(this).val(), 10));
   
    var sum = 0;
    $(this).closest("table").find(".c_sum").each(function() { sum += parseInt($(this).text(), 10); });
    $(this).closest(".orders").find(".c_sumsum").text(sum);
    
    $("#orders input.submit").show();
  });
  
  $("a.email").each(function()
  {
    t = $(this).text();
    $(this).attr("href", "mailto:" + t.reverse());
    $(this).text(t);
  });
});


$(function()
{
  var s = $("#shop");

  s.find(".check-on, .check-off").mouseover(check_on_off).mouseout(check_on_off);
  function check_on_off() { $(this).toggleClass("check-on").toggleClass("check-off") ; }
  
  
  $(".productimg").click(function() 
  {
    t = $(this).find(".thumb");
    
    bw = t.closest(".body").width();
    
    t.toggleClass("large");
    
    t.one("load", function() { 
      tw = t.width();
      th = t.height();
      t.css("width", "auto");
      t.css("height", "auto");
      w = t.width(); 
      h = t.height();
      t.width(tw);
      t.height(th);
      
      mw = t.hasClass("large") ? (bw - w) / 2 : 0;
      t.animate({"width": w, "height": h, "margin-left": mw});
    })

    if(t.hasClass("large"))
    {
      $(this).next(".text").fadeOut();
      t.width(t.width());
      t.height(t.height());
      t.attr("src", t.attr("src").replace("-t.png", ".png"));
    }
    else  
    {
      t.attr("src", t.attr("src").replace(".png", "-t.png"));
      $(this).next(".text").fadeIn();
    }
    
    return false;
  })

});
