﻿/* resize vertical by slice
sliceHeight         : height for each slice
minHeight,maxHeight : range for no height change
target              : secondary target to resize
targetHeightGap     : gap height for secondary target
*/
$.fn.ResizeVertical = function (target) {
    $(this).ResizeVerticalBySlice(100, 0, 0, target, null);
};

/* resize vertical by slice
sliceHeight         : height for each slice
minHeight,maxHeight : range for no height change
target              : secondary target to resize
targetHeightGap     : gap height for secondary target
*/
$.fn.ResizeVerticalBySlice = function (sliceHeight, minHeight, maxHeight, target, targetHeightGap) {
    var height = $(this).height();
    var modulo = height % sliceHeight;
    var count = parseInt(height / sliceHeight);
    var newHeight;
    if (minHeight > 0)
        if ((modulo > minHeight) || (modulo < maxHeight))
            newHeight = height;
        else
            newHeight = (count * sliceHeight) + minHeight;
else
    newHeight = (count + 1) * sliceHeight;

$(this).css('height', newHeight);
/* secondary resize */
if (target != null && $(target).height() < newHeight) {
    $(target).css('height', newHeight + targetHeightGap);
}
};

function SetMaxHeight(elements,additionnalHeights) {
    var maxSize = 0;
    for (var i = 0; i < elements.length; i++) {
        var size = elements[i].height() + additionnalHeights[i];
        if (size > maxSize) maxSize = size;
    }
    for (var i = 0; i < elements.length; i++) {
        elements[i].css("height", maxSize);
    }

};

/* force vertical centering */
$.fn.VerticalAlign = function (marginTop, maxHeight) {
    var initDisplay = (this).parent().parent().css('display');
    $(this).parent().parent().css('display', 'block');
    var currentHeight = $(this).height();
    if (maxHeight == 0) maxHeight = $(this).parent().height();
    marginTop += (maxHeight - currentHeight) / 2;
    $(this).css('margin-top', marginTop);
    $(this).parent().parent().css('display', initDisplay);
};


/* Article internal menu displayer */
$.fn.ArticleInternalDisplay = function () {
    var menuName = $(this).attr('ID');
    if (menuName != null) {
        var contentName = "ArticleInternalContent" + menuName.substr(19);
        /* unselect other items*/
        $('.ArticleInternalContent').children().not(contentName).hide('fast');
        $('.ArticleInternalMenu .Body').children().not(menuName).attr('class', 'Item');
        /* select current item */
        $('#' + contentName).show('fast');
        $(this).attr('class', 'Item Selected');
    }
};

/* Boutton scroll top animation */
$.fn.TopPage = function () {
    if ($(window).scrollTop() == 0) {
        $('#TopPage').hide().animate({ opacity: 0, display: 'none' }, { queue: false, duration: 200 });

    } else {
        $('#TopPage').show().animate({ opacity: 1 }, { queue: false, duration: 200 });
    }


};

/*function scrollToTop*/
$('#TopPage a').live('click', function (event) {
    event.preventDefault();
    $('html,body').animate({ scrollTop: 0 }, 'slow');
});



$(document).ready(function () {
    // init first item
    $('.ArticleInternalMenu .Body').children(':first-child').ArticleInternalDisplay();
    $('.ArticleInternalMenu .Body').children().click(function () { $(this).ArticleInternalDisplay() });
    $(window).scroll(function () { $(this).TopPage(); });



    //    $("#prevBtn").hover(function () {
    //        $(this).parent().find("#prevBtn").animate({ left: '-=3' }, "fast");
    //    }, function () {
    //        $(this).parent().find("#prevBtn").animate({ left: '+=3' }, "fast");
    //    });
    //    $("#nextBtn").hover(function () {
    //        $(this).parent().find("#nextBtn").animate({ left: '+=3' }, "fast");
    //    }, function () {
    //        $(this).parent().find("#nextBtn").animate({ left: '-=3' }, "fast");
    //    });

});

$.fn.AddQuantitySubmit = function (id) {
    var quantity = $(this).parent().find("input[type=text]").val().replace(",", ".");
    var priceID = $(this).parent().find("select").val();
    $.ajax({
        url: "/fr/Product/AddQuantityToOrder?productID=" + parseInt(id) + "&quantity=" + parseFloat(quantity) + "&priceID=" + parseInt(priceID),
        data: { id: $(this).val(), priceID: priceID },
        cache: false,
        type: "POST",
        dataType: "html",

        success: function (data, textStatus, XMLHttpRequest) {
            $("#OrderResumeUpdate").html(data); // HTML DOM replace
            $.prettyPhoto.close();
        }
    });

};

$.fn.AnimateSliderButtons = function (move) {
    $(this).parent().find("#prevBtn").hover(function () {
        $(this).animate({ left: '-=3' }, "fast");
    }, function () {
        $(this).animate({ left: '+=3' }, "fast");
    });
    $(this).parent().find("#nextBtn").hover(function () {
        $(this).animate({ left: '+=3' }, "fast");
    }, function () {
        $(this).animate({ left: '-=3' }, "fast");
    });
};

$.fn.wait = function (time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function () {
        var self = this;
        setTimeout(function () {
            $(self).dequeue();
        }, time);
    });
};
$.fn.SlideShowMeltingInitialize = function (startDuration, duration) {
    var s = $(this).children().length;
    if (s > 1) {
        var first = $(this).children(":first");
        var offset = $(this).offset();
        var l = offset.left;
        var t = offset.top;
        $(this).children().css("position", "absolute").css("display", "block").css("left", l).css("top", t);
        $(this).children().not(":first").hide();
        $(this).children(":first").show();
        setInterval("SlideShowMeltingSwitch('" + $(this).selector + "')", duration);
    }
};
$.fn.SlideShowMeltingPositionRelativeInitialize = function (startDuration, duration) {
    var s = $(this).children().length;
    if (s > 1) {
        var first = $(this).children(":first");
        $(this).children().css("position", "relative").css("display", "block");
        $(this).children().not(":first").hide();
        $(this).children(":first").show();
        setInterval("SlideShowMeltingSwitch('" + $(this).selector + "')", duration);
    }
};
function SlideShowMeltingSwitch(obj) {

    var current = $(obj).children(":visible");
    current.hide();
    var next = current.next();
    if (next.length == 0)
        next = $(obj).children(":first");
    next.show();
}


$.fn.InfiniteDropDownListChange = function (maxDepth, values, texts) {
    var id = $(this).context.name;
    var tb = id.split('_');
    LoadSelect(maxDepth, values, texts, tb[0], parseInt(tb[1]));
};
function LoadSelect(maxDepth, values, texts, prefixID, currentDepth) {
    var parent = $("select#" + prefixID + "_" + (currentDepth));
    var dest = $("select#" + prefixID + "_" + (currentDepth + 1));
    var currentValue = $(parent).val();
    $(dest).ClearSelectOption();
    if (currentValue != "") {
        for (i = 0; i < values.length; i++) {
            var itemValue = values[i];
            if (itemValue.indexOf(currentValue) == 0 && (itemValue != currentValue) && (itemValue.length == currentValue.length + 3))
                $(dest).AddSelectOption(texts[i], itemValue);
        }
    }
    $(dest).SetSelectVisibility();
    if (currentDepth < maxDepth)
        LoadSelect(maxDepth, values, texts, prefixID, currentDepth + 1);
}
$.fn.SetSelectVisibility = function () {
    if ($(this).find("option").size() > 1)
        $(this).show();
    else
        $(this).hide();
};
$.fn.ClearSelectOption = function () {
    $(this).find("option:not(option:first)").remove();
};
$.fn.AddSelectOption = function (text, value) {
    $(this).append("<option value=\"" + value + "\">" + text + "</option>");
};


function DatePickerInit() {
    $.datepicker.regional['fr'] = {
        closeText: 'Fermer',
        prevText: '&#x3c;Préc',
        nextText: 'Suiv&#x3e;',
        currentText: 'Courant',
        monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
		'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
        monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun',
		'Jul', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
        dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
        weekHeader: 'Sm',
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: '',
        showTime: true
    };
    $.datepicker.setDefaults($.datepicker.regional['fr']);
}

$(document).ready(function () {
    //DatePickerInit();
    //$("INPUT[type=text].Date").datepicker();
    //$("INPUT[type=text].DateTime").datetimepicker();
    //$("INPUT[type=text].Monetary").numeric({ allow: "." });

    $(".CheckBoxImage input[type=checkbox]:checked").each(function () {
        var img = $(this).parent();
        img.attr('class', 'CheckBoxImage Selected');
    });
    $(".CheckBoxImage").click(function () {
        var ch = $(this).find("INPUT[type=checkbox]");
        var checked = ch.attr('checked');
        if (checked) {
            ch.attr('checked', false);
            $(this).attr('class', 'CheckBoxImage');
        }
        else {
            ch.attr('checked', true);
            $(this).attr('class', 'CheckBoxImage Selected');
        }
    });

    $(".AdvancedSearch #ArticleCategories INPUT[type=checkbox]").click(function () { $(this).CheckBoxSelectChild(); });
    $(".AdvancedSearch #ProductCategories INPUT[type=checkbox]").click(function () { $(this).CheckBoxSelectChild(); });
    $(".AdvancedSearch #EtablishmentCategories INPUT[type=checkbox]").click(function () { $(this).CheckBoxSelectChild(); });
});

$.fn.CheckBoxSelectChild = function (items) {
    var checked = $(this).attr('checked');
    $(this).parent().parent().find("INPUT[type=checkbox]").not($(this)).attr('checked', checked);
};

/*Sliding texte left to right*/
$.fn.SlidingText = function (speed) {
    var widthText = $(this).width();
    var parentWidth = $(this).parent().width();
    var duration = widthText * speed;

    $(this).css('left', parentWidth);
    $(this).SlidingTextAnimate(-widthText, duration);
};

$.fn.SlidingTextAnimate = function (widthText, duration) {

    $(this).animate({ left: widthText }, { duration: duration, specialEasing: { left: 'linear' }, complete: function () { $(this).css('left', $(this).parent().width()); $(this).SlidingTextAnimate(widthText, duration) } });
    /* */
};


$.fn.SlideText = function (speed, vertical, repeat) {
    var gap = vertical ? $(this).height() - $(this).parent().height() : $(this).width() - $(this).parent().width();
    if (gap > 0) {
        $(this).parent().attr("style", "overflow:hidden");
        $(this).attr("style", "position:relative");
        var duration = gap * speed;
        $(this).SlideTextAnimate(-gap, duration, vertical, repeat);
        $(this).hover(
        function () { $(this).stop(); },
        function () { $(this).SlideTextAnimate(-gap, duration, vertical, repeat); }
    );
    };
};

$.fn.SlideTextAnimate = function (newPosition, duration, vertical, repeat) {
    if (vertical)
        $(this).animate({ top: newPosition }, { duration: duration, specialEasing: { top: 'linear' }, complete: function () { if (repeat) { $(this).css('top', "0"); $(this).SlideTextAnimate(newPosition, duration, vertical, repeat); } } });
    else
        $(this).animate({ left: newPosition }, { duration: duration, specialEasing: { left: 'linear' }, complete: function () { if (repeat) { $(this).css('left', "0"); $(this).SlideTextAnimate(newPosition, duration, vertical, repeat); } } });
};



$.fn.CssAltern = function (itemCss, alternCss) {
    var type = 0;
    $(this).children().each(function () {
        if (type == 0) {
            $(this).attr("class", itemCss);
            type = 1;
        } else {
            $(this).attr("class", alternCss);
            type = 0;
        }
    });
};


function DatePickerInit() {
    $.datepicker.regional['fr'] = {
        closeText: 'Fermer',
        prevText: '&#x3c;Préc',
        nextText: 'Suiv&#x3e;',
        currentText: 'Courant',
        monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin',
		'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
        monthNamesShort: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun',
		'Jul', 'Aoû', 'Sep', 'Oct', 'Nov', 'Déc'],
        dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
        dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
        weekHeader: 'Sm',
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: '',
        showTime: true
    };
    $.datepicker.setDefaults($.datepicker.regional['fr']);
}


// Slider 1 initialize (type ACN)
$.fn.Slider1Initialize = function () {
    $(this).children().each(function () {
        $(this).click(function () {
            Slider1TabSelect(this);
        });
    });
    Slider1NextTab(this);
    setInterval("Slider1NextTab('" + $(this).selector + "')", 5000);
};
// Slider 1 loop
function Slider1NextTab(tabs) {
    var tab = $(tabs).children(".Selected").next();
    if (tab.length == 0) tab = $(tabs).children(":first");
    Slider1TabSelect(tab);
};
// Slider 1 tab select
function Slider1TabSelect(tab) {
    var pos = $(tab).prevAll().length;
    $(tab).parent().children(".Selected").not(tab).removeClass("Selected");
    $(tab).addClass("Selected");
    $(tab).parent().parent().find(".Content .Item:visible").hide('slow');
    $(tab).parent().parent().find(".Content .Item:eq(" + pos + ")").show('slow');
}

