$(document).ready(function() {

});

var krulib = {
  connected : {
    init : function() {
      $("#connect").hide();
      $("#fb-user").show();
      $("#create").show();
    }
  },
  not_connected : {
    init : function() {
      $("#fb-user").hide();
      $("#create").hide();
      $("#connect").show();
    }
  },
  get_images : {
    init : function() {
      var widget_div = document.getElementById("profilePics");
       FB.ensureInit(function () {
         FB.Facebook.get_sessionState().waitUntilReady(function() {
           FB.Facebook.apiClient.friends_get(null, function(result) {
             var markup = "";
             var max_friends = 5000;
             var num_friends = result ? Math.min(max_friends, result.length) : 0;
             if (num_friends > 0) {
               for (var i=0; i<num_friends; i++) {
                 markup +=
                   "<div class='kb-fb-image-wrapper'><div class='kb-fb-image'><fb:profile-pic size='normal' uid='"+result[i]+"' linked='false' facebook-logo='false'></fb:profile-pic></div></div>";
               }
             }
             widget_div.innerHTML = markup;
             FB.XFBML.Host.parseDomElement(widget_div);
           });
         });
       });
    }
  },
  friend_submit : {
    init : function() {
      $('#friend_submit').click(function() {
        var pics = [];
        var fbName = $("#fb-name").text();
        $("#profilePics img").each(function (i) {
          pics.push( $(this).attr("src") );
        });
        $("input[name='friend[friend_urls]']").val((pics.join(' ')));
        $("input[name='friend[facebook_name]']").val(fbName);
      });
    }
  },
  new_page : {
    init : function() {

      var profilePic = $("#profilePics img");

      profilePic.live("mouseover", function() {
          $(this).animate({"opacity": "0.2"}, "slow");
          $(this).closest(".kb-fb-image-wrapper").css("background", "url('/images/delete.png') no-repeat");
          $(this).click(function() {
            $(this).remove();
          });
      });
      profilePic.live("mouseout", function () {
        $(this).animate({"opacity": "1"}, "slow");
        $(this).closest(".kb-fb-image-wrapper").css("background", "none");
      });
      
      /*var ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf('chrome/') != -1){
      } else if (ua.indexOf('safari/') != -1){
      $("#safari-bug").show();
      } else {   
      };*/
    }
  },
  repeat_images : {
    init : function () {
      var posterImages = $('#poster #images').children();
      var posterImageCount = posterImages.size();

      while (posterImageCount >= 1 && posterImageCount < 900) {

        posterImages.each(
          function() {
            if (posterImageCount < 900) {
              $(this).clone().appendTo('#poster #images');
              posterImageCount++;
            } else {
            }
          }
        );
      };

    }
  },
  show_page : {
    init : function () {
      $("#zoom").click( function(){
        var zoomToggle = $("#content .toggle");
        zoomToggle.toggleClass("small");
        $("#zoom span").toggle();
      });
    }
  },
  order_page : {
    init : function () {
      $("#repeat_information").click(function() {
        if ($("#repeat_information").is(":checked")) {
          $("#shipping_fields input", ":visible", document.body).each(function(i) {
            $(this).val($("#billing_fields input").eq(i).val());
          });
          var billing_state = $("#order_billing_state").val();
          $("#order_shipping_state").val(billing_state);
        } else {
          $("#shipping_fields input", ":visible", document.body).each(function(i) {
            $(this).val("");
          });
          $("#order_shipping_state").val("");
        }
      });
      $('#qty').keyup( function() {
        var qty = $("#qty").val();
        var price = 9.95;
        var total = "$" + (qty * price).toFixed( 2 );
        $("#total").html("Total: " + total)        
      });
      $(".num-only").keypress(function (e){
        var charCode = (e.which) ? e.which : e.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
          return false;
        }
      });
      
    }
  } 
}

