﻿$.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_mail=" + document.getElementById("phone_mail").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"); }
        }
    });

    $("#left_contact").click(function () { $("#dialog_phone").dialog("open"); });

	$("#click_to_call").click(function () { $("#dialog_phone").dialog("open"); });

    $("#confirm").dialog({ autoOpen: false, show: "blind", hide: "blind" });
    
    if($("#catalogcenter").size() > 0) { $("#catalogcenter").tabs({ fx: { height:'toggle', opacity:'toggle', duration:300 }}); }
    
    if($( "#accordionfaq" ).size() > 0) { $( "#accordionfaq" ).accordion({ autoHeight: false }); }

    if($( ".accordionjquery" ).size() > 0) { $( ".accordionjquery" ).accordion({ autoHeight: false }); }
    
    
    // Jcarousel
    if($('#carousel_article').size() > 0) { $('#carousel_article').jcarousel({ auto: 10, wrap: 'circular', scroll:2, initCallback: mycarousel_initCallback }); }
    
    if($("#header_carousel_content").size() > 0) {
	    $("#header_carousel_content").nivoSlider({controlNav:false,pauseTime: 10000});
    }

    if($('#center_carousel').size() > 0) {
	    $('#center_carousel').slidy({
	        children: 'a',
	        menu: false,
	        pause: false,
	        speed: 400,
	        time: 4000,
	        animation: 'fade',
	        width: 710,
	        height: 200
	    });
    }

    if($('#left_carousel').size() > 0) {
	    $('#left_carousel').slidy({
	        children: 'a',
	        menu: false,
	        pause: false,
	        speed: 900,
	        time: 4000,
	        animation: 'fade',
	        width: 160,
	        height: 200
	    });
    }

    if($('#right_carousel').size() > 0) {
	    $('#right_carousel').slidy({
	        children: 'a',
	        menu: false,
	        pause: false,
	        speed: 900,
	        time: 4000,
	        animation: 'fade',
	        width: 160,
	        height: 200
	    });
    }
    
    if($("#news_container").size() > 0) {
	    $("#news_container").easySlider({
	        continuous: true,
	        nextId: "news_next",
	        prevId: "news_prev"
	    });
    }
    
    if($('.accordion').size() > 0) { $( ".accordion" ).accordion(); }
    
    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/hooktop/autocomplete/" + document.getElementById("search_value").value + "",
                dataType: "json",
                data: request,
                success: function (data) {
                    cache.term = request.term;
                    cache.content = data;
                    response(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
		    alert(errorThrown);
		}
            });
        },
        minLength: 2
    });
    
    
});

    
function mycarousel_initCallback(carousel) {
	carousel.clip.hover(function() { carousel.stopAuto(); }, function() { carousel.startAuto(); });
};
    


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"); }
        }
    });
}

