﻿
function onPageRefresh() {
    styleButtons();
    setTopLinks();
    myFx = new Fx.Scroll(window, {
        wait: false,
        transition: Fx.Transitions.Quad.easeInOut
    });
}

function dynamicScroll() {
    if (typeof (enableScroll) == "undefined") enableScroll = true;
    if (enableScroll != false) {
        tgt = (typeof (scrollTarget) == "undefined" || scrollTarget == '') ? 'fwHeader' : scrollTarget;
        if ($(tgt))  myFx.toElement(tgt); 
    }
}

function pageLoadedHandler(sender, args) {
    onPageRefresh();
    dynamicScroll();
}


window.addEvent("domready", function() {
    onPageRefresh();  //: Initial Load
    if (typeof (Sys) != "undefined" && typeof (Sys.WebForms) != "undefined" && (Sys.WebForms.PageRequestManager) != "undefined") Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler)
    new SmoothScroll({ duration: 1500 });
});

/* wsShopper */
if (typeof (wsShopper) == "undefined") wsShopper = {};
wsShopper.addToWishlist=function() {
    if (wsShopper.shopperID!=null && wsShopper.shopperID!="") 
        addtocart('/addtowishlist.aspx','default');
    else 
        wsModal(root+"/popups/modal/loginRequired.aspx","Login Required.","300");
}
wsShopper.emailAFriend = function (productID, productDisplayID, branch) {
    if (typeof (branch) == "undefined") branch = "";
    popup(root + '/popups/emailAFriend.aspx?productID=' + productID + '&productDisplayID=' + productDisplayID+'&BRANCH='+branch, 'emailAFriend', 600, 500, false, 1, false, false);
}
wsShopper.logout = function () {
    top.location.href = root + "/logout.aspx";
}

/* wsEmail */
if (typeof (wsEmail) == "undefined") wsEmail = {};
wsEmail.clientID = "";
wsEmail.text = "name@domain.com";
wsEmail.init = function (clientID) {
    wsEmail.clientID = clientID;
    $(wsEmail.clientID).value = wsEmail.text;
    $(wsEmail.clientID).onblur = function () { if (this.value == '') { this.value = wsEmail.text; return true; } }
    $(wsEmail.clientID).onfocus = function () { if (this.value == wsEmail.text) { this.value = ''; return true; } }
    $(wsEmail.clientID).addEvent('keydown', function (event) { if (event.key == 'enter') { wsEmail.submit(); return false; } });
}
wsEmail.submit = function () {
    if ($(wsEmail.clientID).value != wsEmail.text) {
        location.replace(root + '/emailUpdates.aspx?e=' + escape($(wsEmail.clientID).value));
    } else {
        location.replace(root + '/emailUpdates.aspx?e=');
    }
}


/* wsSearch */
if (typeof (wsSearch) == "undefined") wsSearch = {};
wsSearch.clientID = "";
wsSearch.text = "Enter keyword or item #";
wsSearch.init = function (clientID) {
    wsSearch.clientID = clientID;
    $(wsSearch.clientID).value = wsSearch.text;
    $(wsSearch.clientID).onblur = function () { if (this.value == '') { this.value = wsSearch.text; return true; } }
    $(wsSearch.clientID).onfocus = function () { if (this.value == wsSearch.text) { this.value = ''; return true; } }
    $(wsSearch.clientID).addEvent('keydown', function (event) { if (event.key=='enter') { wsSearch.submit(); return false; } });
}
wsSearch.submit = function () {
    if ($(wsSearch.clientID).value != wsSearch.text)
        if ($(wsSearch.clientID).value != "") {
            location.replace(root + '/search.aspx?searchterm=' + escape($(wsSearch.clientID).value));
        } else {
            wsModalAlert("Search Help", "Please enter in some keywords or an Item # to search the site with.", 225);
        }
}

if (typeof (wsCart) == "undefined") wsCart = {};
wsCart.count = 0;
wsCart.clientID = "";
wsCart.refresh = function () { }
wsCart.updateCart = function (numItems) {
    if (numItems == null || isNaN(numItems)) {
        wsCart.serverRefreshCart();
    } else {
        wsCart.count = numItems;
        $(wsCart.clientID).innerHTML = wsCart.count;
    }
}

/* buttons */
function styleButtons() {
    btns = $$('.button');
    btns.each(function (item) {
        item.addEvents({
            mouseenter: function () {
                this.className = 'button_over'
            },
            mouseleave: function () {
                this.className = 'button'
            }
        });
    });

    btns2 = $$('.button2');
    btns2.each(function (item) {
        item.addEvents({
            mouseenter: function () {
                this.className = 'button2_over'
            },
            mouseleave: function () {
                this.className = 'button2'
            }
        });
    });

    btnsb = $$('.buttonBag');
    btnsb.each(function (item) {
        item.addEvents({
            mouseenter: function () {
                this.className = 'buttonBag_over'
            },
            mouseleave: function () {
                this.className = 'buttonBag'
            }
        });
    });
}

function setTopLinks() {
    tlinks = $$('.goToTop');
    tlinks.each(function (item) {
        //item.innerHTML = "<img src='" + root + "/assets/backtotop.gif' border='0' align='absmiddle' alt='Back to Top' />"
        item.innerHTML = "<div class='buttonTop'>Back to Top<\/div>";
        item.addEvent("click", function () {
            var myFx = new Fx.Scroll(document.body, {
                wait: false
            }).toTop();
        });
    });    
}

/* modal */
function wsModal(mURL, mCaption, mWidth, onCloseFunc) {
    if (typeof (mWidth) == "undefined") mWidth = 300;
    mWidth = parseInt(mWidth);
    if (typeof (onCloseFunc) == "undefined") onCloseFunc = $empty;
    sw = null;
    sw = new StickyWin.Modal.Ajax({
        url: mURL,
        wrapWithUi: true,
        caption: mCaption,
        fadeDuration: 300,
        draggable: true,
        onClose: function () { onCloseFunc(); },
        uiOptions: {
            width: mWidth,
            cornerHandle: true
        },
        modalOptions: {
            modalStyle: {
                'background-color': '#FFF',
                'opacity': .6
            }
        }

    }).update();
}

function wsModalAlert(mHeader, mMessage, mWidth) {
    if (typeof (mHeader) == "undefined" || mHeader == null) mHeader = "WinterSilks";
    if (typeof (mWidth) == "undefined") mWidth = 300;
    swa = null;
    swa = new StickyWin.Alert(mHeader, mMessage, {
        fadeDuration: 300,
        draggable: false,
        uiOptions: {
            width: mWidth,
            cornerHandle: false
        },        
        modalOptions: {
            modalStyle: {
                'background-color': '#FFF',
                'opacity': .6
            }
        }
    });
}

