/* OSCAR WIDGET */

(function(){
    jQuery.fn.add_oscar_item = function() {
        var old_item = this.parents(".widget-content").find(".item_complete:last");
        var new_item = old_item.clone();
        var count = this.parents(".widget-content").find(".item_complete").length;
        var footer = this.parents(".widget-content > div").attr("id");
        var index = 0;
        
        new_item.find(".item_label").attr("name", footer+"_item["+count+"][label]").val("");
        new_item.find(".item_link").attr("name", footer+"_item["+count+"][link]").val("");
        new_item.find(".button").show();
        new_item.insertAfter(old_item);
        
        var items = this.parents(".widget-content").find(".item_complete");
        
        items.each(function() {
            jQuery(this).find(".item_label").attr("name", footer+"_item["+index+"][label]");
            jQuery(this).find(".item_link").attr("name", footer+"_item["+index+"][link]");
            index++;
        });
    };
})(jQuery);

(function(){
    jQuery.fn.remove_oscar_item = function() {
    	var footer = this.parents(".widget-content > div").attr("id");
    	var index = 0;
    	
        this.parents(".item_complete").remove();
        
        var items = this.parents(".widget-content").find(".item_complete");
        
        items.each(function() {
        	jQuery(this).find(".item_label").attr("name", footer+"_item["+index+"][label]");
            jQuery(this).find(".item_link").attr("name", footer+"_item["+index+"][link]");
            index++;
        });
    };
})(jQuery);

/* OSCAR MENU */

(function(){
    jQuery.fn.show_add_product = function() {
        this.find(".add-product").show();
    };
})(jQuery);

(function(){
    jQuery.fn.hide_add_product = function() {
        this.find(".add-product").hide();
    };
})(jQuery);

(function(){
    jQuery.fn.show_remove_product = function() {
        this.find(".remove-product").show();
    };
})(jQuery);

(function(){
    jQuery.fn.hide_remove_product = function() {
        this.find(".remove-product").hide();
    };
})(jQuery);

(function(){
    jQuery.fn.add_product_to_menu = function() {
        var new_entry = this.parents("li").clone();
        var count = jQuery("#oscar-home-products-menu-full li").length;
        var already = false;

        new_entry.find(".add-product").hide();
        new_entry.bind("mouseover", function() {
            jQuery(this).show_remove_product();
        });
        new_entry.bind("mouseout", function() {
            jQuery(this).hide_remove_product();
        });
        new_entry.find(".oscar_product_menu_order").val(count+1);
        
        new_entry.find(".oscar_product_menu_separator").attr("name", "oscar_product_menu["+(count+1)+"][separator]");
        new_entry.find(".oscar_product_menu_order").attr("name", "oscar_product_menu["+(count+1)+"][order]");
        new_entry.find(".oscar_product_menu_post_id").attr("name", "oscar_product_menu["+(count+1)+"][post_id]");

        jQuery("#oscar-home-products-menu-full ul").find(".oscar_product_menu_post_id").each(function() {
            if (jQuery(this).val() == new_entry.find(".oscar_product_menu_post_id").val() && new_entry.find(".oscar_product_menu_separator").val() != 1)
                already = true;
        });

        if (!already) {
            jQuery("#oscar-home-products-menu-full ul").append(new_entry);
        }
        else {
            alert("Cet élément a déjà été ajouté !");
        }
    };
})(jQuery);

(function(){
    jQuery.fn.remove_product_from_menu = function() {
        this.parents("li").fadeOut(250, function(){
            jQuery(this).remove();
        });
    };
})(jQuery);



/* OSCAR EMPLOYEES */

(function(){
    jQuery.fn.oscar_new_employee = function() {
        var employees_list = jQuery("#oscar-employees-list");
        var new_employee = employees_list.find("#oscar-new-employee").clone();

        new_employee.hide();
        new_employee.attr("id", "");
        new_employee.insertBefore(employees_list.find(".postbox:first"));
        new_employee.slideDown(500, function(){
            new_employee.find(".oscar-employee-upload").fadeIn(250);
            new_employee.find("form .button-primary").fadeOut(250);
            new_employee.find("form .button-secondary").fadeOut(250, function(){
                new_employee.find(".oscar-button-submit").fadeIn(250);
                new_employee.find(".oscar-employee-upload .button-secondary:first").fadeIn(250);
                new_employee.find(".oscar-button-cancel-add").fadeIn(250);
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_upload = function() {
        var employee_upload = this.parent(".oscar-employee-upload");

        employee_upload.find(".button-secondary").fadeOut(250, function(){
            employee_upload.find(".button-primary").fadeIn(250);
            employee_upload.find(".button-upload").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_deletepict = function() {
        var employee_upload = this.parent(".oscar-employee-upload");
        var employee_form = this.parents("form");

        this.fadeOut(250);
        employee_form.find(".oscar-employee-picture").animate({"opacity": 0}, 250);
        setTimeout(function(){
            employee_upload.find(".employee-picture").val("");
            employee_form.find(".oscar-employee-picture-save").html(employee_form.find(".oscar-employee-picture").html());
            employee_form.find(".oscar-employee-picture .employee-picture-display").hide();
            employee_form.find(".oscar-employee-picture").html('<div class="picture-empty">Aucune image</div>');
            employee_form.find(".oscar-employee-picture").animate({"opacity": 1}, 250);
        }, 250);

        employee_form.find(".employee-picture-delete").val(1);
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_changepict = function() {
        var s_url = this.val().split('.');
        var ext_url = s_url[s_url.length - 1].toLowerCase();
        var ext = ["jpg", "png", "jpeg"];

        if (ext.join().indexOf(ext_url) == -1) {
            this.parent().find(".employee-error").show();
        }
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_cancelupload = function() {
        var employee_upload = this.parent(".oscar-employee-upload");

        employee_upload.find(".button-upload").fadeOut(250, function(){
            jQuery(this).val("");
        });
        employee_upload.find(".employee-error").fadeOut(250);
        employee_upload.find(".button-primary").fadeOut(250, function(){
            employee_upload.find(".button-secondary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_modify = function() {
        var employee_modify = this.parent(".manage-button");
        var employee_form = this.parents("form");
        
        employee_modify.find(".button-secondary").fadeOut(250, function(){
            employee_modify.find(".oscar-button-submit").fadeIn(250);
            employee_modify.find(".oscar-button-cancel-modify").fadeIn(250);
            employee_form.find(".oscar-employee-upload").fadeIn(250);
            employee_form.find("input[type='text']").attr("disabled", "").removeClass("no-edit");
            employee_form.find(".employee-info").each(function(){
                jQuery(this).parent(".oscar-employee-detail").find(".employee-save").text(jQuery(this).val());
            });
            employee_form.find(".oscar-employee-picture-save").html(employee_form.find(".oscar-employee-picture").html());
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_cancelmodify = function() {
        var employee_modify = this.parent(".manage-button");
        var employee_form = this.parents("form");

        employee_form.find(".oscar-employee-upload").fadeOut(250);
        employee_form.find(".employee-error").fadeOut(250);
        employee_modify.find(".oscar-button-submit").fadeOut(250);
        this.fadeOut(250, function(){
            employee_form.find(".button-secondary").fadeIn(250);
            employee_form.find("input[type='text']").attr("disabled", "disabled").addClass("no-edit");
            employee_form.find(".employee-save").each(function(){
                jQuery(this).parent(".oscar-employee-detail").find(".employee-info").val(jQuery(this).text());
                jQuery(this).empty();
            });
            employee_form.find(".employee-picture").val("");
        });

        employee_form.find(".oscar-employee-picture").animate({"opacity": 0}, 250);
        setTimeout(function(){
            employee_form.find(".oscar-employee-picture").html(employee_form.find(".oscar-employee-picture-save").html());
            employee_form.find(".oscar-employee-picture-save").empty();
            employee_form.find(".oscar-employee-picture").animate({"opacity": 1}, 250);
        }, 250);

        employee_form.find(".employee-picture-delete").val(0);
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_delete = function() {
        var employee_delete = this.parent(".manage-button");

        employee_delete.find(".employee-delete").val(1);
        
        employee_delete.find(".button-secondary").fadeOut(250, function(){
            employee_delete.find(".oscar-button-confirm").fadeIn(250);
            employee_delete.find(".oscar-button-cancel-delete").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_confirmdelete = function() {
        var employee_postbox = this.parents(".postbox");

        employee_postbox.fadeOut(500, function() {
            jQuery(this).find("form").trigger("submit");
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_canceldelete = function() {
        var employee_delete = this.parent(".manage-button");

        employee_delete.find(".employee-delete").val(0);
        employee_delete.find(".oscar-button-confirm").fadeOut(250);
        this.fadeOut(250, function(){
            employee_delete.find(".button-secondary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_canceladd = function() {
        var employee_postbox = this.parents(".postbox");

        employee_postbox.fadeOut(500, function() {
            jQuery(this).remove();
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_employee_check = function() {
        var employee_form = this;
        var error = false;
        var picture_input = employee_form.find(".oscar-employee-upload input[type='file']");

        employee_form.find(".employee-error").hide();
        employee_form.find('.ajax-loading').css('visibility', 'visible');

        employee_form.find(".employee-info").each(function() {
            if (jQuery(this).val() == "") {
                jQuery(this).parents(".oscar-employee-detail").find(".employee-error").show();
                error = true;
            }
        });

        var s_url = picture_input.val().split('.');
        var ext_url = s_url[s_url.length - 1].toLowerCase();
        var ext = ["jpg", "png", "jpeg"];

        if (ext.join().indexOf(ext_url) == -1) {
            picture_input.parent().find(".employee-error").show();
            error = true;
        }

        if (error) {
            employee_form.find('.ajax-loading').css('visibility', 'hidden');
            return false;
        }
        else {
            employee_form.trigger("submit");
            return true;
        }
    };
})(jQuery);


/* OSCAR CLIENT */

(function(){
    jQuery.fn.oscar_new_client = function() {
        var client_list = this.parents(".oscar-client-list");
        var new_client = client_list.find(".oscar-new-client").clone();

        new_client.hide();
        new_client.removeClass("oscar-new-client");
        new_client.insertBefore(client_list.find(".postbox:first"));
        new_client.slideDown(500, function(){
            new_client.find(".oscar-client-upload").fadeIn(250);
            new_client.find("form .button-primary").fadeOut(250);
            new_client.find("form .button-secondary").fadeOut(250, function(){
                new_client.find(".oscar-button-submit").fadeIn(250);
                new_client.find(".oscar-client-upload .button-secondary:first").fadeIn(250);
                new_client.find(".oscar-button-cancel-add").fadeIn(250);
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_upload = function() {
        var client_upload = this.parent(".oscar-client-upload");
        var client_form = this.parents("form");

        client_form.find(".oscar-client-infos:first label").text("Fichier à uploader");
        client_form.find(".oscar-client-infos:first .oscar-client-detail .client-info").fadeOut(250, function() {
            client_form.find(".oscar-client-infos:first .oscar-client-detail .button-upload").fadeIn(250);
        });
        
        client_upload.find(".button-secondary").fadeOut(250, function(){
            client_upload.find(".button-primary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_changefile = function() {
        var client_form = this.parents("form");
        var client_upload = client_form.find(".oscar-client-upload");

        var s_url = this.val().split('.');
        var ext_url = s_url[s_url.length - 1].toLowerCase();
        var ext = ["pdf", "doc"];

        if (ext.join().indexOf(ext_url) == -1) {
            this.parents(".oscar-client-detail").find(".client-error").text("Mauvais type de fichier !").show();
        }
        else {
            client_form.find(".oscar-client-infos:first .oscar-client-detail .button-upload").fadeOut(250, function() {
                client_form.find(".oscar-client-infos:first label").text("Titre");
                client_form.find(".oscar-client-infos:first .oscar-client-detail .client-info").fadeIn(250);
            });

            this.parents(".oscar-client-detail").find(".client-error").fadeOut(250);

            client_upload.find(".button-primary").fadeOut(250, function(){
                client_upload.find(".button-secondary").fadeIn(250);
            });
        }
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_cancelupload = function() {
        var client_upload = this.parent(".oscar-client-upload");
        var client_form = this.parents("form");

        client_form.find(".oscar-client-infos:first label").text("Titre");
        client_form.find(".oscar-client-infos:first .oscar-client-detail .button-upload").fadeOut(250, function() {
            client_form.find(".oscar-client-infos:first .oscar-client-detail .client-info").fadeIn(250);
        });

        client_form.find(".client-file").val("");
        client_form.find(".client-error").fadeOut(250);

        client_upload.find(".button-upload").fadeOut(250);
        client_upload.find(".button-primary").fadeOut(250, function(){
            client_upload.find(".button-secondary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_modify = function() {
        var client_modify = this.parent(".manage-button");
        var client_form = this.parents("form");
        
        client_modify.find(".button-secondary").fadeOut(250, function(){
            client_modify.find(".oscar-button-submit").fadeIn(250);
            client_modify.find(".oscar-button-cancel-modify").fadeIn(250);
            client_form.find(".oscar-client-upload").fadeIn(250);
            client_form.find("input[type='text']").attr("disabled", "").removeClass("no-edit");
            client_form.find(".client-info").each(function(){
                jQuery(this).parent(".oscar-client-detail").find(".client-save").text(jQuery(this).val());
            });
            client_form.find(".oscar-client-picture-save").html(client_form.find(".oscar-client-picture").html());
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_cancelmodify = function() {
        var client_form = this.parents("form");

        client_form.find(".button-primary").fadeOut(250);
        client_form.find(".oscar-client-upload").fadeOut(250);
        
        client_form.find(".oscar-client-infos:first label").text("Titre");
        client_form.find(".oscar-client-infos:first .oscar-client-detail .button-upload").fadeOut(250, function() {
            client_form.find(".oscar-client-infos:first .oscar-client-detail .client-info").fadeIn(250);
        });

        this.fadeOut(250, function(){
            client_form.find(".button-secondary").fadeIn(250);
            client_form.find("input[type='text']").attr("disabled", "disabled").addClass("no-edit");
            client_form.find(".client-save").each(function(){
                jQuery(this).parent(".oscar-client-detail").find(".client-info").val(jQuery(this).text());
                jQuery(this).empty();
            });
            client_form.find(".client-file").val("");
            client_form.find(".client-error").fadeOut(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_delete = function() {
        var client_delete = this.parent(".manage-button");

        client_delete.find(".client-delete").val(1);
        
        client_delete.find(".button-secondary").fadeOut(250, function(){
            client_delete.find(".oscar-button-confirm").fadeIn(250);
            client_delete.find(".oscar-button-cancel-delete").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_confirmdelete = function() {
        var client_postbox = this.parents(".postbox");

        client_postbox.fadeOut(500, function() {
            jQuery(this).find("form").trigger("submit");
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_canceldelete = function() {
        var client_delete = this.parent(".manage-button");

        client_delete.find(".client-delete").val(0);
        client_delete.find(".oscar-button-confirm").fadeOut(250);
        this.fadeOut(250, function(){
            client_delete.find(".button-secondary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_canceladd = function() {
        var client_postbox = this.parents(".postbox");

        client_postbox.fadeOut(500, function() {
            jQuery(this).remove();
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_client_check = function() {
        var client_form = this;
        var error = false;
        var picture_input = client_form.find(".client-file");

        client_form.find(".client-error").hide();
        client_form.find('.ajax-loading').css('visibility', 'visible');

        client_form.find(".client-info").each(function() {
            if (jQuery(this).val() == "") {
                jQuery(this).parents(".oscar-client-detail").find(".client-error").text("Vous devez renseigner un titre !").show();
                error = true;
            }
        });

        var s_url = picture_input.val().split('.');
        var ext_url = s_url[s_url.length - 1].toLowerCase();
        var ext = ["pdf", "doc"];

        if (ext.join().indexOf(ext_url) == -1) {
            picture_input.parents(".oscar-client-detail").find(".client-error").text("Mauvais type de fichier !").show();
            error = true;
        }

        if (error) {
            client_form.find('.ajax-loading').css('visibility', 'hidden');
            return false;
        }
        else {
            client_form.trigger("submit");
            return true;
        }
    };
})(jQuery);


/* OSCAR CONTACT */

(function(){
    jQuery.fn.oscar_contact_modify = function() {
        var contact_modify = this.parent(".manage-button");
        var contact_form = this.parents("form");

        contact_modify.find(".button-secondary").fadeOut(250, function(){
            contact_modify.find(".oscar-button-submit").fadeIn(250);
            contact_modify.find(".oscar-button-cancel-modify").fadeIn(250);
            contact_form.find("input[type='text'], textarea").attr("disabled", "").removeClass("no-edit");
            contact_form.find(".contact-info").each(function(){
                jQuery(this).parent(".oscar-contact-detail").find(".contact-save").text(jQuery(this).val());
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_contact_cancelmodify = function() {
        var contact_modify = this.parent(".manage-button");
        var contact_form = this.parents("form");

        contact_modify.find(".oscar-button-submit").fadeOut(250);
        contact_form.find(".contact-error").fadeOut(250);
        this.fadeOut(250, function(){
            contact_form.find(".button-secondary").fadeIn(250);
            contact_form.find("input[type='text'], textarea").attr("disabled", "disabled").addClass("no-edit");
            contact_form.find(".contact-save").each(function(){
                jQuery(this).parent(".oscar-contact-detail").find(".contact-info").val(jQuery(this).text());
                jQuery(this).empty();
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_contact_check = function() {
        var contact_form = this;
        var error = false;

        contact_form.find(".contact-error").hide();
        contact_form.find('.ajax-loading').css('visibility', 'visible');

        contact_form.find(".contact-info").each(function() {
            if (jQuery(this).val() == "") {
                jQuery(this).parents(".oscar-contact-detail").find(".contact-error").show();
                error = true;
            }
        });
        
        if (error) {
            contact_form.find('.ajax-loading').css('visibility', 'hidden');
            return false;
        }
        else {
            contact_form.trigger("submit");
            return true;
        }
    };
})(jQuery);

/* OSCAR PRODUCTS */

    (function(){
        jQuery.fn.oscar_new_product = function() {
            var products = jQuery("#oscar-products");
            var new_product = products.find("#oscar-new-product").clone();
            
            new_product.hide();
            new_product.attr("id", "");
            new_product.insertBefore(products.find(".oscar-products-list:first"));
            new_product.slideDown(500);
            new_product.find(".product-description .product-info:first").attr("disabled", "");
            new_product.find(".product-description .product-info:first").removeClass("no-edit");
            new_product.find(".manage-button .oscar-button-modify").trigger("click");
            new_product.find(".manage-button .oscar-button-cancel-add").fadeIn(250);
            new_product.find(".manage-button .oscar-button-cancel-modify").remove();
        };
    })(jQuery);

    /* OSCAR PRODUCT LIST */

        (function(){
            jQuery.fn.oscar_product_delete = function() {
                var product_delete = this.parent(".manage-button");

                product_delete.find(".product-delete").val(1);

                product_delete.find(".button-secondary").fadeOut(250, function(){
                    product_delete.find(".oscar-button-confirm").fadeIn(250);
                    product_delete.find(".oscar-button-cancel-delete").fadeIn(250);
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_confirmdelete = function() {
                var product_postbox = this.parents(".postbox");

                product_postbox.find(".product-description .product-info:first").attr("disabled", "");

                product_postbox.fadeOut(500, function() {
                    jQuery(this).find("form").trigger("submit");
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_canceldelete = function() {
                var product_delete = this.parent(".manage-button");

                product_delete.find(".product-delete").val(0);

                product_delete.find(".oscar-button-confirm").fadeOut(250);
                this.fadeOut(250, function(){
                    product_delete.find(".button-secondary").fadeIn(250);
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_canceladd = function() {
                var products_list = this.parents(".oscar-products-list");
                
                products_list.fadeOut(500, function() {
                    jQuery(this).remove();
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_modify = function() {
                var product_modify = this.parent(".manage-button");
                var product_form = this.parents("form");
                var products_list = product_form.parents(".oscar-products-list");

                product_modify.find(".button-secondary").fadeOut(250, function(){
                    products_list.find(".oscar-product-insert").fadeIn(250);
                    product_modify.find(".oscar-button-submit").fadeIn(250);
                    product_modify.find(".oscar-button-cancel-modify").fadeIn(250);
                    products_list.find(".product-description .product-info").attr("disabled", "");
                    products_list.find(".product-description .product-info").removeClass("no-edit");
                    products_list.find(".product-description .oscar-product-detail").each(function() {
                        jQuery(this).find(".product-save").text(jQuery(this).find(".product-info").val());
                    });
                });

                products_list.find(".oscar-products-toolbox").animate({"right": "-26%"}, 250);
                products_list.animate({"width": "75%"}, 500);
                setTimeout(function(){
                    products_list.find(".oscar-products-toolbox").fadeIn(500);
                }, 500);
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_cancelmodify = function() {
                var product_modify = this.parent(".manage-button");
                var product_form = this.parents("form");
                var products_list = this.parents(".oscar-products-list");
                var products_toolbox = products_list.find(".oscar-products-toolbox");

                product_form.find(".product-error").fadeOut(500);

                products_list.find(".oscar-product-insertion").fadeOut(500, function(){
                    products_list.find(".oscar-product-insert .button-primary").val("Afficher les couleurs");
                    jQuery(this).css("display", "none");
                    products_list.find(".oscar-product-infos").animate({"width": "96%"}, 500);
                });

                setTimeout(function(){
                    products_list.find(".oscar-products-toolbox").animate({"right": "0%"}, 250);
                }, 500);

                products_list.find(".oscar-product-insertion").fadeOut(500, function(){
                    products_list.find(".oscar-product-insert .button-primary").val("Afficher les couleurs");
                    jQuery(this).css("display", "none");
                    products_list.find(".oscar-product-infos").animate({"width": "96%"}, 500);
                    products_toolbox.find(".button-secondary").attr("disabled", "");
                });

                products_list.find(".oscar-product-field").fadeOut(250);
                setTimeout(function() {
                    products_list.find(".product-description").fadeIn(250);
                    products_list.find(".oscar-product-insert").fadeOut(250);
                }, 500);

                products_toolbox.find(".manage-button .button-primary").fadeOut(250);
                
                products_list.find(".product-description .product-info").attr("disabled", "disabled");
                products_list.find(".product-description .product-info").addClass("no-edit");

                product_modify.find(".oscar-button-submit").fadeOut(250);
                this.fadeOut(250, function()
                {
                    product_form.find(".button-secondary").fadeIn(250);
                    products_list.find(".product-description .oscar-product-detail").each(function() {
                        jQuery(this).find(".product-info").val(jQuery(this).find(".product-save").text());
                    });
                    products_list.find(".product-description .product-save").empty();
                    
                    products_list.find(".product-files .product-files-part").each(function() {
                        jQuery(this).find(".product-files-choice option:first").attr("selected", "selected");
                        var files_copy = jQuery(this).find(".product-save").html();
                        jQuery(this).find(".product-files-list").html(files_copy);
                        jQuery(this).find(".product-save").empty();
                    });

                    products_list.find(".product-illustration .product-info").val("");

                    var first_picture = products_list.find(".product-diaporama .oscar-product-detail:first");
                    first_picture.find(".product-info").val("");
                    products_list.find(".product-diaporama .oscar-product-detail").not(first_picture).remove();

                    products_list.find(".product-moreinfo .product-info").text(products_list.find(".product-moreinfo .product-save").html());
                    products_list.find(".product-moreinfo .product-save").empty();
                });

                products_list.find(".oscar-products-toolbox").fadeOut(500, function(){
                    products_list.animate({"width": "100%"}, 500);
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_check = function() {
                var product_form = this;
                var error = false;

                product_form.find(".product-error").hide();
                product_form.find('.ajax-loading').css('visibility', 'visible');

                product_form.find(".product-description .product-info").each(function() {
                    if (jQuery(this).val() == "") {
                        jQuery(this).parents(".oscar-product-detail").find(".product-error").show();
                        error = true;
                    }
                });

                if (error) {
                    product_form.find('.ajax-loading').css('visibility', 'hidden');
                    return false;
                }
                else {
                    product_form.trigger("submit");
                    return true;
                }
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_show_insert = function() {
                var products_list = this.parents(".oscar-products-list");

                products_list.find(".oscar-product-infos").animate({"width": "77%"}, 500);
                setTimeout(function(){
                    products_list.find(".oscar-product-insertion").fadeIn(500);
                }, 500);

                this.val("Masquer la sélection");
                this.one("click", function() {
                    jQuery(this).val("Sélection des couleurs");
                    products_list.find(".oscar-product-insertion").fadeOut(500, function(){
                        jQuery(this).css("display", "none");
                        products_list.find(".oscar-product-infos").animate({"width": "96%"}, 500);
                    });
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_addfile = function() {
                var product_file = this.parent(".product-files-part").find(".product-files-choice option:selected");
                var product_files_list = this.parent(".product-files-part").find(".product-files-list");
                var count = product_files_list.find(".product-files-chosen").length;
                var type = this.attr("title");
                
                product_files_list.append('<li class="product-files-chosen"></li>');
                product_files_list.find("li:last").append('<input type="hidden" value="'+product_file.val()+'" name="oscar_product['+type+']['+count+'][id]" />');
                product_files_list.find("li:last").append('<input type="hidden" value="'+product_file.html()+'" name="oscar_product['+type+']['+count+'][name]" />');
                product_files_list.find("li:last").append(product_file.html());
                product_files_list.find("li:last").append(' <input type="button" class="button-secondary" value="supprimer" onclick="jQuery(this).oscar_product_removefile();" />');
                this.attr("disabled", "disabled");
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_removefile = function() {
                var product_file = this.parents(".product-files-part").find(".product-files-choice option:selected");
                var product_file_add = this.parents(".product-files-part").find("input:first");

                if (this.parent(".product-files-chosen").find("input:first").val() == product_file.val()) {
                    product_file_add.attr("disabled", "");
                }
                this.parent(".product-files-chosen").remove();
            };
        })(jQuery);
        
        (function(){
            jQuery.fn.oscar_product_checkfile = function() {
                var product_file = this.find("option:selected");
                var product_files_list = this.parent(".product-files-part").find(".product-files-list");
                var exists = false;

                product_files_list.find(".product-files-chosen").each(function() {
                    if (jQuery(this).find("input").val() == product_file.val())
                        exists = true;
                });

                if (exists)
                    this.parent(".product-files-part").find(".button-primary").attr("disabled", "disabled");
                else
                    this.parent(".product-files-part").find(".button-primary").attr("disabled", "");
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_diaporamaleft = function() {
                var diaporama = this.parent(".product-diaporama-display").find(".product-diaporama-pictures");
                var left = diaporama.find(".picture-current").prev().css("left");

                if (left) {
                    diaporama.animate({left: "-"+left}, 500);
                    diaporama.find(".picture-current").prev().addClass("picture-current");
                    diaporama.find(".picture-current:last").removeClass("picture-current");
                }
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_diaporamaright = function() {
                var diaporama = this.parent(".product-diaporama-display").find(".product-diaporama-pictures");
                var left = diaporama.find(".picture-current").next().css("left");

                if (left) {
                    diaporama.animate({left: "-"+left}, 500);
                    diaporama.find(".picture-current").next().addClass("picture-current");
                    diaporama.find(".picture-current:first").removeClass("picture-current");
                }
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_diaporamamore = function() {
                var new_picture = this.parents(".product-diaporama").find(".oscar-new-detail").clone();
                var i = 0;

                if (this.parents(".product-diaporama").find(".oscar-product-detail").length < 5) {
                    new_picture.val("").removeClass("oscar-new-detail").addClass("oscar-product-detail");
                    new_picture.find(".product-save").before('<input type="button" class="button-secondary" value="supprimer" onclick="jQuery(this).oscar_product_diaporamaless();" />');
                    new_picture.insertBefore(this.parents(".product-diaporama").find(".oscar-new-detail")).slideDown(250);
                    if (this.parents(".product-diaporama").find(".oscar-product-detail").length >= 5) {
                        this.find("input").attr("disabled", "disabled");
                    }
                }

                this.parents(".product-diaporama").find(".oscar-product-detail").each(function() {
                    jQuery(this).find("input").attr("name", "oscar_product_diaporama["+i+"]")
                    i++;
                });
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_product_diaporamaless = function() {
                var product_diaporama = this.parents(".product-diaporama");
                var i = 0;

                this.parent(".oscar-product-detail").fadeOut(250, function() {
                    jQuery(this).remove();
                    product_diaporama.find(".oscar-product-detail").each(function() {
                        jQuery(this).find("input").attr("name", "oscar_product_diaporama["+i+"]")
                        i++;
                    });
                });

                
            };
        })(jQuery);

    /* OSCAR PRODUCT TOOLBOX */

        (function(){
            jQuery.fn.oscar_toolbox_load = function(insertion, box) {
                var products_toolbox = this.parents(".oscar-products-toolbox");
                var products_content = this.parents(".oscar-products-content");
                var target = "."+this.attr("title");

                products_content.find(".oscar-product-field").not(target).fadeOut(250);
                setTimeout(function() {
                    products_content.find(target).fadeIn(250);
                }, 500);

                products_toolbox.find(".manage-button .button-primary").fadeIn(250);
                if (insertion) {
                    products_content.find(".oscar-product-insert").fadeIn(250);
                }
                else {
                    products_content.find(".oscar-product-insert").fadeOut(250);
                }

                products_toolbox.find(".button-secondary").attr("disabled", "disabled");
                products_toolbox.find(".oscar-toolbox-cancel").attr("title", this.attr("title"));
                
                if (this.attr("title") == "product-files") {
                    products_content.find(target + " .product-files-part").each(function() {
                        var files_copy = jQuery(this).find(".product-files-list").html();
                        jQuery(this).find(".product-save").html(files_copy);
                    });
                }
                else if (this.attr("title") == "product-moreinfo") {
                    products_content.find(target + " .product-save").html(products_content.find(target + " .product-info").text());
                }
            };
        })(jQuery);
        
        (function(){
            jQuery.fn.oscar_toolbox_confirm = function() {
                var products_toolbox = this.parents(".oscar-products-toolbox");
                var products_content = this.parents(".oscar-products-content");

                products_content.find(".oscar-product-insertion").fadeOut(500, function(){
                    products_content.find(".oscar-product-insert .button-primary").val("Afficher les couleurs");
                    jQuery(this).css("display", "none");
                    products_content.find(".oscar-product-infos").animate({"width": "96%"}, 500);
                    products_toolbox.find(".button-secondary").attr("disabled", "");
                });

                products_toolbox.find(".manage-button .ajax-loading").css("visibility", "visible");
                setTimeout(function() {
                    products_toolbox.find(".manage-button .button-primary").fadeOut(250);
                    products_toolbox.find(".manage-button .ajax-loading").css("visibility", "hidden");
                    products_content.find(".oscar-product-insert").fadeOut(250);
                    products_content.find(".oscar-product-field").fadeOut(250);
                    setTimeout(function() {
                        products_content.find(".product-description").fadeIn(250);
                        products_content.find(".oscar-product-insert").fadeIn(250);
                    }, 500);
                }, 500);
            };
        })(jQuery);

        (function(){
            jQuery.fn.oscar_toolbox_cancel = function(box) {
                var products_toolbox = this.parents(".oscar-products-toolbox");
                var products_content = this.parents(".oscar-products-content");
                var target = "."+this.attr("title");
                
                products_content.find(".oscar-product-insertion").fadeOut(500, function(){
                    products_content.find(".oscar-product-insert .button-primary").val("Afficher les couleurs");
                    jQuery(this).css("display", "none");
                    products_content.find(".oscar-product-infos").animate({"width": "96%"}, 500);
                    products_toolbox.find(".button-secondary").attr("disabled", "");
                });

                products_content.find(".oscar-product-field").fadeOut(250);
                setTimeout(function() {
                    products_content.find(".product-description").fadeIn(250);
                    products_content.find(".oscar-product-insert").fadeIn(250);
                }, 500);

                products_toolbox.find(".manage-button .button-primary").fadeOut(250);

                if (this.attr("title") == "product-files") {
                    products_content.find(target + " .product-files-part").each(function() {
                        jQuery(this).find(".product-files-choice option:first").attr("selected", "selected");
                        var files_copy = jQuery(this).find(".product-save").html();
                        jQuery(this).find(".product-files-list").html(files_copy);
                        jQuery(this).find(".product-save").empty();
                    });
                }
                else if (this.attr("title") == "product-illustration") {
                    products_content.find(target + " .product-info").val("");
                }
                else if (this.attr("title") == "product-diaporama") {
                    var first_picture = products_content.find(target + " .oscar-product-detail:first");
                    first_picture.find(".product-info").val("");
                    products_content.find(target + " .oscar-product-detail").not(first_picture).remove();
                }
                else if (this.attr("title") == "product-moreinfo") {
                    products_content.find(target + " .product-info").text(products_content.find(target + " .product-save").html());
                    products_content.find(target + " .product-save").empty();
                }

                this.attr("title", "");
            };
        })(jQuery);

/* OSCAR COLORS */

(function(){
    jQuery.fn.oscar_new_color = function() {
        var colors_list = jQuery("#oscar-colors-list");
        var new_color = colors_list.find("#oscar-new-color").clone();

        new_color.hide();
        new_color.attr("id", "");
        new_color.insertBefore(colors_list.find(".postbox:first"));
        new_color.slideDown(500, function(){
            new_color.find("form .button-primary").fadeOut(250);
            new_color.find("form .button-secondary").fadeOut(250, function(){
                new_color.find(".oscar-button-submit").fadeIn(250);
                new_color.find(".oscar-button-cancel-add").fadeIn(250);
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_modify = function() {
        var color_modify = this.parent(".manage-button");
        var color_form = this.parents("form");

        color_modify.find(".button-secondary").fadeOut(250, function(){
            color_modify.find(".oscar-button-submit").fadeIn(250);
            color_modify.find(".oscar-button-cancel-modify").fadeIn(250);
            color_form.find("input[type='text']").attr("disabled", "").removeClass("no-edit");
            color_form.find(".color-info").each(function(){
                jQuery(this).parent(".oscar-color-detail").find(".color-save").text(jQuery(this).val());
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_change = function() {
        var color_item = this.parents(".oscar-color-item");
        var color_code = this.val();

        if (this.val().length == 6) {
            color_item.find(".oscar-color-preview").css("background-color", "#"+color_code);
        }
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_cancelmodify = function() {
        var color_modify = this.parent(".manage-button");
        var color_form = this.parents("form");

        color_form.find(".color-error").fadeOut(250);
        color_modify.find(".oscar-button-submit").fadeOut(250);
        this.fadeOut(250, function(){
            color_form.find(".button-secondary").fadeIn(250);
            color_form.find("input[type='text']").attr("disabled", "disabled").addClass("no-edit");
            color_form.find(".color-save").each(function(){
                jQuery(this).parent(".oscar-color-detail").find(".color-info").val(jQuery(this).text());
                jQuery(this).empty();
            });
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_delete = function() {
        var color_delete = this.parent(".manage-button");

        color_delete.find(".color-delete").val(1);

        color_delete.find(".button-secondary").fadeOut(250, function(){
            color_delete.find(".oscar-button-confirm").fadeIn(250);
            color_delete.find(".oscar-button-cancel-delete").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_confirmdelete = function() {
        var color_postbox = this.parents(".postbox");

        color_postbox.fadeOut(500, function() {
            jQuery(this).find("form").trigger("submit");
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_canceldelete = function() {
        var color_delete = this.parent(".manage-button");

        color_delete.find(".color-delete").val(0);
        color_delete.find(".oscar-button-confirm").fadeOut(250);
        this.fadeOut(250, function(){
            color_delete.find(".button-secondary").fadeIn(250);
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_canceladd = function() {
        var color_postbox = this.parents(".postbox");

        color_postbox.fadeOut(500, function() {
            jQuery(this).remove();
        });
    };
})(jQuery);

(function(){
    jQuery.fn.oscar_color_check = function() {
        var color_form = this;
        var error = false;

        color_form.find(".color-error").hide();
        color_form.find('.ajax-loading').css('visibility', 'visible');

        color_form.find(".color-info").each(function() {
            if (jQuery(this).val() == "") {
                jQuery(this).parents(".oscar-color-detail").find(".color-error").show();
                error = true;
            }
        });
        
        if (error) {
            color_form.find('.ajax-loading').css('visibility', 'hidden');
            return false;
        }
        else {
            color_form.trigger("submit");
            return true;
        }
    };
})(jQuery);

/* OSCAR FILES */

(function(){
    jQuery.fn.oscar_file_deletion = function() {
        
    	file_form = this.parents('form');
    	
    	if (!confirm("Confirmez-vous la suppression de ce fichier ?")) {
    		
    		return false;
    		
    	}
    	else {
    		
    		file_form.trigger('submit');
    		return true;
    		
    	}
    	
    };
})(jQuery);
