﻿$.fx.speeds._default = 1000;

$(function () {
    $("#dialog:ui-dialog").dialog("destroy");

    $("#error").dialog({
        autoOpen: false,
        show: "blind",
        hide: "blind"
    });

    $("#dialog_phone").dialog({
        autoOpen: false,
        height: 250,
        width: 350,
        modal: true,
        buttons: {
            "OK": function () {
                var order = "phone_submit=phone_submit";
                order += "&phone_company=" + document.getElementById("phone_company").value;
                order += "&phone_name=" + document.getElementById("phone_name").value;
                order += "&phone_firstname=" + document.getElementById("phone_firstname").value;
                order += "&phone_phone=" + document.getElementById("phone_phone").value;
                order += "&phone_message=" + document.getElementById("phone_message").value;
                $.ajax({
                    url: 'default.aspx',
                    async: true,
                    cache: false,
                    data: order,
                    dataTypeString: 'html',
                    type: 'post'
                });
                $(this).dialog("close");
                $("#confirm").html("Votre demande a été envoyé");
                $("#confirm").dialog({ title: 'OK' });
                $("#confirm").dialog({ width: 400 });
                $("#confirm").dialog("open");
                setTimeout(function () { $("#confirm").dialog("close"); }, 2000);
            },
            "Annuler": function () {
                $(this).dialog("close");
            }
        }
    });

    $("#header_contact")
			.click(function () {
			    $("#dialog_phone").dialog("open");
			});

	$("#right_contact_phone")
	.click(function () {
		$("#dialog_phone").dialog("open");
	});

    $("#confirm").dialog({
        autoOpen: false,
        show: "blind",
        hide: "blind"
    });
    
    $('#header_carousel').slidy({
        children: 'a',
        menu: true,
        pause: true,
        speed: 400,
        time: 6000,
        animation: 'fade',
        width: 980,
        height: 200
    });

    $('#center_carousel').slidy({
        children: 'a',
        menu: false,
        pause: false,
        speed: 400,
        time: 4000,
        animation: 'fade',
        width: 710,
        height: 200
    });

    $('#left_carousel').slidy({
        children: 'a',
        menu: false,
        pause: false,
        speed: 900,
        time: 4000,
        animation: 'fade',
        width: 160,
        height: 200
    });

    $('#right_carousel').slidy({
        children: 'a',
        menu: false,
        pause: false,
        speed: 900,
        time: 4000,
        animation: 'fade',
        width: 150,
        height: 145
    });
    
    if($('#slider').size() > 0)
    {
	$('#slider').nivoSlider({
	    controlNavThumbs: true,
	    pauseTime: 5000
	});
    }
    
    var cache = {};
    $("#search_value").autocomplete({
        source: function (request, response) {
            if (cache.term == request.term && cache.content) {
                response(cache.content);
            }
            if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response($.grep(cache.content, function (value) {
                    return matcher.test(value.value)
                }));
            }
            $.ajax({
                url: "default.aspx/ajaxframe/search/autocomplete/" + document.getElementById("search_value").value + "",
                dataType: "json",
                data: request,
                success: function (data) {
                    cache.term = request.term;
                    cache.content = data;
                    response(data);
                }
            });
        },
        minLength: 2
    });
});

function ajax_loading(url, img) {
    document.getElementById('mod_body').innerHTML = '<img class="ajax_loader" src="' + img + '" alt="loader" />';
    $.ajax({
        url: url,
        async: false,
        cache: false,
        success: function (data) {
            $('#mod_body').html(data);
        }
    });
}

function delete_confirm() {
    $("#error").dialog({
        resizable: false,
        height: 140,
        modal: true,
        buttons: {
            "Delete all items": function () {
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });
}

function dialog_phone() {
    $("#dialog_phone").dialog("open");
}

function catalog_subzone(url) {
    $.ajax({
        url: url,
        async: false,
        cache: false,
        success: function (data) {
            $('#catalog_subzone').html(data);
        }
    });
}

function search(url, element, idCatalog, idParent) {
    var value = document.getElementById(element).value;
    var order = element + "=" + value + "&idCatalog=" + idCatalog + "&idParent=" + idParent;
    $.ajax({
        url: url,
        async: false,
        cache: false,
        type: 'post',
        data: order,
        success: function (data) {
            $('#center_catalog_table').html(data);
        }
    });
}

function DialogPhoneArticle(Id) {
    $("#dialog_phone").dialog({
        autoOpen: true,
        height: 250,
        width: 350,
        modal: true,
        buttons: {
            "OK": function () {
                var order = "phone_submit=phone_submit";
                order += "&phone_company=" + document.getElementById("phone_company").value;
                order += "&phone_name=" + document.getElementById("phone_name").value;
                order += "&phone_firstname=" + document.getElementById("phone_firstname").value;
                order += "&phone_phone=" + document.getElementById("phone_phone").value;
                order += "&phone_message=" + document.getElementById("phone_message").value;
                order += "&ArticleId=" + Id;
                $.ajax({
                    url: 'default.aspx',
                    async: true,
                    cache: false,
                    data: order,
                    dataTypeString: 'html',
                    type: 'post'
                });
                $(this).dialog("close");
                $("#confirm").html("Votre demande a été envoyé");
                $("#confirm").dialog({ title: 'OK' });
                $("#confirm").dialog({ width: 400 });
                $("#confirm").dialog("open");
                setTimeout(function () { $("#confirm").dialog("close"); }, 2000);
            },
            "Annuler": function () {
                $(this).dialog("close");
            }
        }
    });
}

