﻿/* Namespace for all OpinionAssurances js script */
if (!window.OpinionAssurances) { window.OpinionAssurances = {}; }

// Vote btn
(function($) {
    $.fn.voteBtn = function(userId, action, eltType, eltId) {
        return this.each(function() {
            $(this).click(function() {
                // Remove click behavior
                $(this).unbind("click");
                $(this).addClass("oa_voteButtonDisabled");
                // Increment value
                var $child = $($(this).find("div"));
                $child.html(parseInt($child.html()) + 1);
                $child.fadeOut().fadeIn();
                // Save on server
                $.ajax(
                {
                    type: "POST",
                    url: "/WebServices/OpinionAssurancesResources.asmx/Vote",
                    data: "{ userId: " + userId + ", action: " + action + ", eltType: " + eltType + ", eltId: " + eltId + " }",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(result) {
                        // Nothing to do ...
                        /*if (result.d)
                            alert("success")
                        else
                            alert("error");*/
                    }
                });
            });
        });
    };
})(jQuery);

// Carrier / product dropdownlist
(function($) {
    $.fn.carrierDropDownList = function(ddlId) {
        return this.each(function() {
            if ($(this).val() == "")
                $("#" + ddlId).attr("disabled", true);
            $("#" + ddlId).data("cid", $(this).val());

            var load = function(sender) {
                if ($("#" + ddlId).data("cid") != $(sender).val()) {
                    $("#" + ddlId).data("cid", $(sender).val());
                    if ($(sender).val() == "") {
                        // Disable child dropdownlist
                        $("#" + ddlId).attr("disabled", true);
                        var ddl = $("#" + ddlId).get(0);
                        ddl.options.length = 0;
                        ddl.options[0] = new Option("-- Sélectionnez --", "");
                    } else {
                        // Update child dropdownlist
                        $.ajax(
                        {
                            type: "POST",
                            url: "/WebServices/OpinionAssurancesResources.asmx/GetProductsByCarrier",
                            data: "{ carrierId: " + $(sender).val() + " }",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function(result) {
                                $("#" + ddlId).removeAttr("disabled");
                                var ddl = $("#" + ddlId).get(0);
                                ddl.options.length = 0;
                                var j = 0;
                                //if (result.d.length > 1)
                                ddl.options[j++] = new Option("-- Sélectionnez --", "");
                                for (var i = 0; i < result.d.length; i++) {
                                    ddl.options[j++] = new Option(result.d[i].Value, result.d[i].Key);
                                }
                            }
                        });
                    }
                }
            };
            $(this).change(function() {
                load(this);
            });
            $(this).blur(function() {
                // Change event is not fired in some specific case
                // Load list on blur event required
                load(this);
            });
        });
    };
})(jQuery);

// Avatar picker
// TODO : sélectionner le picto courant ?
// TODO : supprimer attribut "imgid" pour validation W3C ?
(function($) {
    $.fn.avatarPicker = function() {
        return this.each(function() {
            var $that = $(this);
            var nbImg = $that.find(".oa_scrollable img").length;
            // Get validator
            var val = $that.find(".oa_error").get(0);
            // Hide picture div
            $that.find(".oa_avatars").hide();
            // Add scroll
            var beforeSeek = function(event, i) {
                if (i + 4 > nbImg) return false;
            };
            $that.find(".oa_scrollable").scrollable({ prev: ".oa_prev", next: ".oa_next", onBeforeSeek: beforeSeek });
            // Add div button opening/closing code
            $that.find(".alf_box a").click(function() {
                $that.find(".oa_avatars").fadeToggle();
                return false;
            });
            // Hide div when someone clicks outside of it
            $that.find(".oa_avatars").bind("clickoutside", function(event) {
                $(this).fadeOut();
            });
            // Add image selection click
            $that.find(".oa_scrollable img").click(function() {
                // Update selected image value
                $that.find("input").val($(this).attr("imgid"));
                // Update displayed image
                var src = $(this).attr("src");
                var cssClass = $(this).attr("class");
                $that.find(".oa_current img").fadeOut(500, function() { $(this).attr("src", src).attr("class", cssClass).fadeIn(); });
                $that.find(".oa_avatars").fadeOut();
                // Clear validator
                if (val) {
                    val.isvalid = true;
                    ValidatorUpdateDisplay(val);
                    ALF_ValidatorPictoExtenderEvaluationFunctionValid(val);
                }
            });
        });
    };
})(jQuery);

// TODO : Généraliser dans ALF avec option ?
// Podium Hp animation
OpinionAssurances.HpPodiumAnimation = function(selector, speed_ms) {
    // Element size in pixel
    var size = 377;
    // Number of elements in animation
    var items = 3;
    // Animation speed AND waiting time between animation
    var speed = speed_ms;
    // Current first element index
    var index = 0;
    // Animation container element
    var $elt = $(selector);

    // Update div content style
    $elt.find('.oa_item').addClass("oa_content");

    // Go next
    var next = function() {
        $elt.find('.oa_items').animate({ "left": "-=" + size }, speed, afterNext);
    };
    // Do after next
    var afterNext = function() {
        index += 1;
        // Clone previous element to end of line
        // Keep ghost to avoid bad visual effect ?
        $elt.find('.oa_content:first').clone().appendTo($elt.find('.oa_items'));
        $elt.find('.oa_content:first').html("&nbsp;");
        $elt.find('.oa_content:first').addClass("oa_ghost")
        $elt.find('.oa_content:first').removeClass("oa_content");
        if (index == (items + 1)) {
            // Return to initial state
            $elt.find('.oa_ghost').remove();
            $elt.find('.oa_items').css("left", "0");
            index = 0;
        }
        // Loop
        setTimeout(next, speed);
    };
    // Start animation
    this.start = function() {
        setTimeout(next, speed);
    };
};
