 $(document).ready(function(){
     
     $("form.printrecipe").append("<input type='button' class='buttonlink' value='Print this Recipe' />").click(function() {
         window.print();
         return false;
     });
     
     function toggleContactForm() {
         $(".header .contactme .form").slideToggle('slow');    
     }
     function clearContactForm() {
         $("#email").val("").removeClass("fail");
         $("#yourname").val("").removeClass("fail");
         $("#message").val("").removeClass("fail");
         toggleContactForm();
     }
     $("#quickform").submit(function() {    
        var noval = '';
        var yourname = $(this).find("input[name='yourname']");
        var email = $(this).find("input[name='email']");
        var message = $(this).find("textarea[name='message']");
        $(this).find('textarea').removeClass('fail');
        $(this).find('input').removeClass('fail');
        var error = $(this).find('.error');
        $(error).hide();
    
           if ($(yourname).val() == "") {
            noval = 'something missing';
            $(yourname).addClass('fail').focus();
            $(error).show().fadeOut(5000);
        }
        if ($(email).val() == "") {
            noval = 'something missing';
            $(email).addClass('fail').focus();
            $(error).show().fadeOut(5000);
            
        }
        if ($(message).val() == "") {
            noval = 'something missing';
            $(message).addClass('fail').focus();
            $(error).show().fadeOut(5000);
        }
        if (noval == 'something missing') {
            return false;
        }
        return (true);
    
    });
    $(".header .contactme h6").click(toggleContactForm);
     $(".header .contactme div.cancel").click(clearContactForm);
     
    $(".embedrecipe").click(popupEmbed);
    
    function popupEmbed() {
        var iframelink = $(this).attr("href");
        $("body").append("<div id='embedlinkwin'><span class='close x'>Close</span><h4>Embed this Recipe</h4><p>Copy the text in the area below and paste directly into the HTML of your blog post or web page. Adjust the <span class='code'>height='450'</span> to a higher or lower number to suit your recipe height and page space.</p><label>Copy this:</label></div>");
        var iframe = "<textarea><iframe src ='http://www.omnomrecipes.com" + iframelink + "' width='100%' height='450'><p>Your browser does not support iframes.</p></iframe></textarea>";
        $("#embedlinkwin").append(iframe);
        var closebutton = "<span class='close buttonlink'>Done</span>"
        $("#embedlinkwin").append(closebutton);
        bindCloseAction();
        
        return false;
    }
    
    function bindCloseAction() {
        $("#embedlinkwin .close").click(function() {
            $("#embedlinkwin").remove();
        });    
    }
     
     $(".ingredients .iingred").focus(checkLastRow);
     
     function checkLastRow() {
         checkName = $(this).attr('name');
         lastChild = $(".ingredients li:last input.iingred");
         if ($(lastChild).attr('name') == checkName) {
             newIngredientRow();
         }    
     }
     
     function newIngredientRow() {
         var count = $(".ingredients li").length;
        var countnew = count + 1;
        var lastIngredient = "input[name='iingred" + count + "']";
        var newli = '<li style="display: none;" id="line' + countnew +'"><input type="text" value="" name="iquant' + count +'" class="iquant" />\n';
        newli += '<input type="text" value="" name="iunit' + count +'" class="iunit" />\n';
        newli += '<input type="text" value="" name="iingred' + count +'" class="iingred" /></li>';
            $(".ingredients").append(newli);            
            $(".ingredients input[name='iingred"+count+"']").focus(checkLastRow);
            $(".countingred").val(countnew);
        $("#line"+countnew).slideDown('slow');
     }
     
    $("#addingred").click(newIngredientRow);
    
    $(".catlist ul").hover(function() {
        $(".catlist ul.categories").css("display", "block");
    }, function() {
        $(".catlist ul.categories").css("display", "none");
    });
    
    $("#recipeform label").each(function() {
        title = $(this).attr("title");
        if (title != "") {
            if (($(this).siblings("input:text").val() == "") || ($(this).siblings("input:text").val() == title)) {
                $(this).siblings("input:text").val(title).css("color", "#aaa").addClass("hint").focus(removeHints).blur(addHints);
            }
            if (($(this).siblings("textarea").html() == "") || ($(this).siblings("textarea").html() == title)) {
                $(this).siblings("textarea").html(title).css("color", "#aaa").addClass("hint").focus(removeHints).blur(addHints);
            }
        }
    });
    
    $("#recipeform").submit(function() {
        $("#recipeform .hint").each(function() {
            $(this).val("").html("");
        });
    });
    
    function removeHints() {
        if ($(this).hasClass("hint") == true) {
            $(this).val("").html("").css("color", "#000");
        }
    }    
    function addHints() {
        if ($(this).val() == "") {
            var title= $(this).siblings("label").attr("title");
            $(this).val(title).html(title).css("color", "#aaa");
        }    
        else {
            $(this).removeClass("hint");
        }
    }
    $("#recipeform .thumbs li").click(toggleImageDelete);
    
    function toggleImageDelete() {
        var src = $(this).children("img:first").attr("src");
        var preImgNum = parseInt(src.lastIndexOf("=")) + 1;
        var imgNum = src.substr(preImgNum);
        if ($(this).hasClass("deleteit") == false) {
            $(this).children("input:first").val("1");
            $(this).addClass("deleteit");
        }
        else {
            $(this).removeClass("deleteit");
            $(this).children("input:first").val("0");
        }
    }
    
    function swapPhoto() {
        imgSrc = $(this).children("img").attr("src");
        $(".photocontainer .photo img").attr("src", imgSrc);
    }
    $(".photocontainer .thumbs li").click(swapPhoto).hover(function() {
          $(this).animate({
            opacity: 0.5
          }, 500, function() {$(this).animate({opacity: 1.0}, 500); });
      });
      
      $(".addphoto").click(function() {
          var piclist = $(this).siblings("ul");
          var count = $(piclist).children("li").length;
          var newcount = parseInt(count) + 1;
          var newListItem = '<li style="display:none;"><input type="file" name="img' + count + '" /></li>';
          $(piclist).append(newListItem);
          $(piclist).children('li:last').slideDown('slow');
          $(".countphoto").val(newcount);
      });
    
    
 });

