﻿function wDoCatalogSeach(term) {
    $.ajax({
        async: false,
        type: "POST",
        url: "/WS/wsWidget.asmx/SearchProductGetUrl",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: JSON2.stringify({ 'term': term }),
        dataType: "json",
        success: function (data) {
            window.open(data.d, "_blank");
        }
    })
}

/* INIT */
$(document).ready(function () {

    $('input#catalogueSrc,input#search,input#newsletter').click(function () {
        if ($(this).val() == $(this).attr("rel")) {
            $(this).val("")
            return false;
        }
    }).focusout(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("rel"))
        }
    });


    $("#fSearch a.inputSubmit").click(function () {
        var _$this = $("input#search")
        if (_$this.val() != _$this.attr("rel") && _$this.val().length > 0) {
            $("#fSearch").submit()
        }
    });

    $('input#catalogueSrc').keypress(function (e) {
        if (e.which == 13) {
            $(this).blur();
            var val = $(this).val()
            if (val.length > 0) { wDoCatalogSeach(val) }
            return false;
        }
    });

    $("input#catalogueSrc").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ws/wsWidget.asmx/SearchProduct",
                data: JSON2.stringify({ 'term': request.term }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            id: item.id,
                            label: item.label,
                            value: item.value
                        }
                    }))
                }
            }
            );
        },
        minLength: 2,
        select: function (event, ui) {
            wDoCatalogSeach(ui.item.value)
        }
    }
    );

    $("input#search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ws/wsWidget.asmx/SearchSiteAuto",
                data: JSON2.stringify({ 'term': request.term }),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            id: item.id,
                            label: item.label,
                            value: item.value
                        }
                    }))
                }
            }
            );
        },
        minLength: 2,
        select: function (event, ui) {
            location.href = ui.item.value
        }
    }
    );

    $('#formNewsletter a').click(function () {
        var _$this = $("#formNewsletter input#newsletter")
        var val = _$this.val();
        if (val == _$this.attr("rel")) { val = "" }
        var href = $(this).attr("href") + val
        $.fancybox({
            'href': href,
            'type': 'iframe',
            'overlayColor': '#EDEDED',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            //'scrolling'	: 'no',
            'height': 540,
            'titleShow': false
        });
        return false;
    });	

	$('a.fLikeN').fancybox({
            'type': 'iframe',
            'overlayColor': '#EDEDED',
            'transitionIn': 'fade',
            'transitionOut': 'fade',
            'height': 550,
            'titleShow': false
    });

})


