﻿function _InitClicks() {
    __hlRootSearch.click(function () {
        _RootSearch();
    });
}

function _InitEvents() {

    /* init search */
    __txtRootSearch.keyup(function (e) {
        if (e.keyCode == 13) {
            __hlRootSearch.click();
            return false;
        }
    });
}

function _InitImages() {
    // async load product image thumbnails
    // note this will pull all <img> elements with "pt" class on the page
    var turl = "/WebServices/Products.aspx/GetThumbnail";
    var furl = "/WebServices/Products.aspx/GetImage";
    var curl = "/WebServices/Products.aspx/GetCategory";
    var aurl = "/WebServices/Products.aspx/GetAccent";

    $('img.pc').each(function () {
        var irel = $(this).attr("rel");
        var iid = $(this).attr("id");
        var params = new Object();
        params.Sku = irel;

        $.ajax({
            type: "POST",
            url: curl,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(params),
            dataType: "json",

            success: function (data) {
                if (data != null) {
                    if (data.d != null) {
                        if (data.d.url != null) {
                            $("#" + iid).attr("src", data.d.url);
                        }
                    }
                }
            }
        });

    });

    $('img.pt').each(function () {
        var irel = $(this).attr("rel");
        var iid = $(this).attr("id");
        var params = new Object();
        params.Sku = irel;

        $.ajax({
            type: "POST",
            url: turl,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(params),
            dataType: "json",

            success: function (data) {
                if (data != null) {
                    if (data.d != null) {
                        if (data.d.url != null) {
                            $("#" + iid).attr("src", data.d.url);
                        }
                    }
                }
            }
        });

    });

    $('img.pf').each(function () {
        var irel = $(this).attr("rel");
        var iid = $(this).attr("id");
        var params = new Object();
        params.Sku = irel;

        $.ajax({
            type: "POST",
            url: furl,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(params),
            dataType: "json",

            success: function (data) {
                if (data != null) {
                    if (data.d != null) {
                        if (data.d.url != null) {
                            $("#" + iid).attr("src", data.d.url);
                        }
                    }
                }
            }
        });

    });

    $('img.pa').each(function () {
        var irel = $(this).attr("rel");
        var iid = $(this).attr("id");
        var params = new Object();
        params.ImageName = irel;

        $.ajax({
            type: "POST",
            url: aurl,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(params),
            dataType: "json",

            success: function (data) {
                if (data != null) {
                    if (data.d != null) {
                        if (data.d.url != null) {
                            $("#" + iid).attr("src", data.d.url);
                        }
                    }
                }
            }
        });

    });

}

function _RootSearch() {

    var qs = "";
    var k = __txtRootSearch.val();

    // clear other cookies
    _DeleteCookie(__ck_SearchSku);
    _DeleteCookie(__ck_SearchColor);
    _DeleteCookie(__ck_SearchPriceRange);
    _DeleteCookie(__ck_SearchBrand);
    _DeleteCookie(__ck_SearchDepartment);
    _DeleteCookie(__ck_SearchSearchDescriptions);

    // save keywords
    _CreateCookie(__ck_SearchKeywords, k);
    if (k) { qs = _SetUrlEncodedKey(__qs_SearchKeywords, k, qs); }

    // rebuild url
    var url = "/Search.aspx" + qs;

    // show animation
    $.fancybox.showActivity();

    // redirect
    window.location.href = url;
}

$(document).ready(function () {

    //if not IE4+ nor NS6+
    if (typeof navigator.__cookieEnabled == "undefined" && !__cookieEnabled) {
        document.cookie = "testcookie";
        __cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    if (!__cookieEnabled) {
        __showCDiv = "block";
    }
    if (__cObj) {
        __cObj.style.display = __showCDiv;
    }

    // set up system-wide animations for ajax calls
    _divFB.ajaxStart(function () {
        $.fancybox.showActivity();
    });
    _divFB.ajaxStop(function () {
        $.fancybox.hideActivity();
    });

    /* set up textbox niceys across entire app*/
    $("input:text").focus(function () { $(this).select(); });

    _InitClicks();
    _InitEvents();
    _InitImages();

});
