// psearch box
$(function() {
    var psearch = $('#psearch');
    psearch.focus(function(e) {
        if (psearch.val() == 'product search') {
            psearch.val('');
        }
    }).blur(function(e) {
        if (psearch.val() == '') {
            psearch.val('product search');
        }
    });
    
    
    // Engraving copying
    $('#engraving_dupe').click(function(e) {
        // confirm and dupe
        if (confirm('Are you sure you want to copy all entries from the main engraving to all unique engravings?')) {
            $('.engraving_main').each(function(k, v) {
                var parts = $(v).attr("id").split('_');
                                
                $('.engraving').each(function(k2, v2) {
                    var parts2 = $(v2).attr("id").split('_');                    
                    if (parts[2] == parts2[2]) {
                        $(v2).val($(v).val());
                    }
                });
            });
        }
    });
       
    
    // Product images
    
    var pimages = $('#product_thumbnails a[rel*=lightbox]');   
    if (pimages.length == 0) {
        pimages = $('#product_image_container a');            
        var target = "#product_image_zoom a";
    }
    else {
        var target = "#product_image_container a, #product_image_zoom a";
    }
    
    var src;
    $("#product_thumbnails img").mouseenter(function(){
        src = $(this).attr("src");
        src = src.replace("/thumb/", "/small/");
        $("#product_image_container img").attr("src", src);
    });
    
    pimages.lightBox();
    
    $(target).click(function(e){        
        e.preventDefault()
        
        var found = false;
        if (src) {
            pimages.each(function(k, v){                
                var href = $(v).attr("href")
                href = href.replace("/large/", "/small/");                
                if (href == src) {
                    pimages[k].click();
                    found = true;
                    return;
                }
            });   
        }
        
        if (!found) {
            pimages[0].click();
        }
    });
});

