﻿var disProductsPage = new Class({
    initialize: function () {
        this.openDetailsTxt = '';
        this.closeDetailsTxt = '';
        this.addToBasket = [];
        this.divAddToBasketItems = [];
    },
    initPView: function () {
        this.items = $$('ul#products li.productElements');
        this.items.each(function (item) {
            item.getElements('button').each(function (button) {
                var buttonId = button.get('id') != null ? button.get('id').toString() : "";

                if (buttonId != "") {
                    if (buttonId.test('btnSendToFriendJSON')) {
                        button.addEvent('click', function (e) {
                            e.stop();

                            var email = "";
                            if (button.getParent('div').getChildren('input')[0].get('value') != "")
                                email = button.getParent('div').getChildren('input')[0].get('value').toString();

                            var productNumber = '';
                            var spanPN = button.getParent('li').getElements('h4 span.productNumber')[0];
                            if (spanPN != null)
                                productNumber = spanPN.get('text');

                            var image = null;
                            var productImage = button.getParent('li').getElement('table.productInfo td.productInfo3 a img');
                            if (productImage != null) {
                                image = productImage.get('src');
                            }

                            var html = button.getParent('div').getParent('div').getParent('li').clone(true, true);
                            html.getElements('div.moreProductInfo div.moreProductInfoHidden')[0].setStyles({ 'display': 'block' });

                            var spanMsg = button.getNext('span');
                            spanMsg.set('text', '');
                            html.set('html', '<div id="products">' + html.get('html') + '</div>');

                            if (email != "" && productNumber != "") {
                                var origBtnTxt = button.get('text');
                                var intervalId = null;

                                var jsonRequest = new Request.JSON({
                                    url: '/WebService.asmx/SE',
                                    method: 'post',
                                    evalResponse: true,
                                    urlEncoded: false,
                                    data: { 'default': 'default' },
                                    headers: { 'Content-Type': 'application/json; charset=utf-8' },
                                    onRequest: function () {
                                        button.setStyles({ 'width': button.getSize().x });
                                        button.set('text', '.');
                                        var animate = function () {
                                            if (this.get('text') == '.....')
                                                this.set('text', '.');
                                            else
                                                this.set('text', this.get('text') + '.');
                                        };
                                        intervalId = animate.periodical(500, button);
                                    },
                                    onComplete: function (msg) {
                                        $clear(intervalId);
                                        button.set('text', origBtnTxt);

                                        if (msg != null) {
                                            if (msg.d != null) {
                                                if (typeof (msg.d) != 'undefined') {
                                                    spanMsg.set('text', msg.d.toString());
                                                } else { spanMsg.set('text', errMsg); }
                                            } else { spanMsg.set('text', errMsg); }
                                        } else { spanMsg.set('text', errMsg); }
                                    }
                                }).send(JSON.encode({ 'b': html.get('html'),
                                    'pn': productNumber,
                                    'i': image,
                                    'e': email,
                                    'sl': selectedLanguage,
                                    'pty': productType,
                                    'uc': productType
                                }));
                            }
                            else { spanMsg.set('text', errMsg); }
                        } .bind(this));
                    }
                }
            }, this);

            item.getElements('input').each(function (input) {
                var inputId = input.get('id') != null ? input.get('id').toString() : "";

                if (inputId != "") {
                    if (inputId.test('txtAddToBasket')) {
                        input.addEvent('focus', function () {
                            doSearch = false;
                        });

                        input.addEvent('blur', function () {
                            doSearch = true;
                        });

                        input.addEvent('keydown', function (e) {
                            if (e.key == 'enter') {

                                var test3 = this;
                                var doStop = true;
                                var breakEach = false;

                                this.addToBasket.each(function (item) {
                                    if (!breakEach) {
                                        if (item[0] == e.target.id) {
                                            eval(item[1]);
                                            breakEach = true;
                                        }
                                    }
                                });

                                var curCtlNo = this.getCtlNo(input.get('id'));

                                Page_Validators.each(function (item) {
                                    curVdtNo = this.getCtlNo(item.id);

                                    if (curCtlNo == curVdtNo && item.isvalid)
                                        doStop = false;

                                    if (!item.isvalid) {
                                        this.addErrorStyle($(item.id));
                                    }
                                    else {
                                        this.removeErrorStyle($(item.id));
                                    }
                                }, this);

                                if (doStop)
                                    e.stop();
                            }
                        } .bind(this));
                    }

                    if (inputId.test('txtSendToFriend')) {
                        input.addEvent('focus', function () {
                            doSearch = false;
                        });

                        input.addEvent('blur', function () {
                            doSearch = true;
                        });

                        input.addEvent('keydown', function (e) {
                            if (e.key == 'enter') {
                                e.stop();
                                input.getNext('button').fireEvent('click', e);
                            }
                        });
                    }
                }
            }, this);

            item.getElements('div.divAddToBasket').each(function (div) {
                this.divAddToBasketItems.push(div.get('id'));
                div.addEvent('click', function (e) {
                    var doStop = true;
                    var index = this.divAddToBasketItems.indexOf(div.get('id'));
                    var deliveryDays = this.getDeliveryDays(div);

                    if (deliveryDays > 0) {
                        if (!confirm(this.expectedDeliveryTime.replace('[TIME]', deliveryDays)))
                            e.stop();
                        else
                            eval(this.addToBasket[index][1]);
                    }
                    else
                        eval(this.addToBasket[index][1]);

                    var curCtlNo = this.getCtlNo(div.get('id'));

                    Page_Validators.each(function (item) {
                        curVdtNo = this.getCtlNo(item.id);

                        if (curCtlNo == curVdtNo && item.isvalid)
                            doStop = false;

                        if (!item.isvalid) {
                            this.addErrorStyle($(item.id));
                        }
                        else {
                            this.removeErrorStyle($(item.id));
                        }
                    }, this);

                    if (doStop)
                        e.stop();
                } .bind(this));
            }, this);

            item.getElements('a').each(function (a) {
                var aId = a.get('id') != null ? a.get('id').toString() : "";
                var aClass = a.get('class') != null ? a.get('class').toString() : "";

                if (aClass != "") {
                    if (aClass.test('toggleDetails')) {
                        a.addEvent('click', function (e) {
                            e.stop();
                            var ctlDetails = a.getParent('div.moreProductButtonPanel').getNext('div.moreProductInfo').getChildren('div.moreProductInfoHidden')[0];
                            var aText = a.get('text').toString();

                            if (this.openDetailsTxt == aText)
                                aText = this.closeDetailsTxt;
                            else if (this.closeDetailsTxt == aText)
                                aText = this.openDetailsTxt;

                            new Fx.Reveal(ctlDetails).toggle().chain(function () {
                                a.set('text', aText);
                            });
                        } .bind(this));
                    }

                    if (aClass.test('moreImages')) {
                        a.addEvent('click', function (e) {
                            e.stop();
                            a.getParent('td').getChildren('a').each(function (elm) {
                                if (elm.hasChild) {
                                    elm.getChildren('img').each(function (elmImg) {
                                        if (elm.getProperty('rel').toString().length > 0) {
                                            elm.fireEvent('click');
                                        }
                                    });
                                }
                            });
                        });
                    }

                    if (aClass.test('sendtofriendtoggle')) {
                        a.addEvent('click', function (e) {
                            e.stop();

                            var breakEach = false;
                            this.getParent('div').getNext('div').getChildren('div').each(function (ctlDiv) {
                                if (!breakEach && ctlDiv.get('id') != null) {
                                    if (ctlDiv.get('id').toString().test('divSendToFriend', 'i')) {
                                        ctlDiv.getChildren('span.sendToFriendMsgs').set('text', '');
                                        new Fx.Reveal(ctlDiv).toggle();
                                        breakEach = true;
                                    }
                                }
                            });
                        });
                    }

                    if (aClass.test('sendtofriendjson')) {
                        a.addEvent('click', function (e) {
                            e.stop();

                            var email = "";
                            if (a.getParent('div').getChildren('input')[0].get('value') != "")
                                email = a.getParent('div').getChildren('input')[0].get('value').toString();

                            var productNumber = '';
                            var spanPN = a.getParent('li').getElements('h4 span.productNumber')[0];
                            if (spanPN != null)
                                productNumber = spanPN.get('text');

                            var image = null;
                            var productImage = a.getParent('li').getElement('table.productInfo td.productInfo3 a img');
                            if (productImage != null) {
                                image = productImage.get('src');
                            }

                            var html = a.getParent('div').getParent('div').getParent('li').clone(true, true);
                            html.getElements('div.moreProductInfo div.moreProductInfoHidden')[0].setStyles({ 'display': 'block' });

                            var spanMsg = a.getNext('span');
                            spanMsg.set('text', '');
                            html.set('html', '<div id="products">' + html.get('html') + '</div>');

                            if (email != "" && productNumber != "") {
                                var origBtnTxt = a.get('text');
                                var intervalId = null;

                                var jsonRequest = new Request.JSON({
                                    url: '/WebService.asmx/SE',
                                    method: 'post',
                                    evalResponse: true,
                                    urlEncoded: false,
                                    data: { 'default': 'default' },
                                    headers: { 'Content-Type': 'application/json; charset=utf-8' },
                                    onRequest: function () {
                                        a.setStyles({ 'width': a.getSize().x });
                                        a.set('text', '.');
                                        var animate = function () {
                                            if (this.get('text') == '.....')
                                                this.set('text', '.');
                                            else
                                                this.set('text', this.get('text') + '.');
                                        };
                                        intervalId = animate.periodical(500, a);
                                    },
                                    onComplete: function (msg) {
                                        $clear(intervalId);
                                        a.set('text', origBtnTxt);

                                        if (msg != null) {
                                            if (msg.d != null) {
                                                if (typeof (msg.d) != 'undefined') {
                                                    spanMsg.set('text', msg.d.toString());
                                                } else { spanMsg.set('text', errMsg); }
                                            } else { spanMsg.set('text', errMsg); }
                                        } else { spanMsg.set('text', errMsg); }
                                    }
                                }).send(JSON.encode({ 'b': html.get('html'),
                                    'pn': productNumber,
                                    'i': image,
                                    'e': email,
                                    'sl': selectedLanguage,
                                    'pty': productType,
                                    'uc': productType
                                }));
                            }
                            else { spanMsg.set('text', errMsg); }
                        } .bind(this));
                    }
                }

                if (aId != "") {
                    if (aId.test('lnkBtnSendToFriendToggle')) {
                        a.addEvent('click', function (e) {
                            e.stop();

                            var breakEach = false;
                            this.getParent('div').getNext('div').getChildren('div').each(function (ctlDiv) {
                                if (!breakEach && ctlDiv.get('id') != null) {
                                    if (ctlDiv.get('id').toString().test('divSendToFriend', 'i')) {
                                        ctlDiv.getChildren('span.sendToFriendMsgs').set('text', '');
                                        new Fx.Reveal(ctlDiv).toggle();
                                        breakEach = true;
                                    }
                                }
                            });
                        });
                    }
                }
            }, this);
        }, this);
    },
    getDeliveryDays: function (divAddToBasket) {
        var deliveryDays = divAddToBasket.getParent('div').getParent('td.productInfo2').getElement('span.deliveryDays');

        if ($chk(deliveryDays))
            return deliveryDays.get('text').toInt();
        else
            return -1;
    },
    getCtlNo: function (id) {
        var splitCtlId = id.toString().split("_");

        if (splitCtlId.length > 1)
            return splitCtlId[splitCtlId.length - 2];
        else
            return "";
    },
    addErrorStyle: function (item) {
        if (item.hasErrorStyle == undefined && !item.hasErrorStyle) {
            item.getParent().set('class', 'errorStyle');
            item.hasErrorStyle = true;
        }
    },

    removeErrorStyle: function (item) {
        if (item.hasErrorStyle != undefined && item.hasErrorStyle) {
            item.getParent().set('class', 'noErrorStyle');
            item.hasErrorStyle = false;
        }
    },
    initSearch: function () {
        var txtPn = $('ctl00_ctl00_cphContentMiddle_cphContentMiddleRight_txtSearchByProductNumber');
        var txtSt = $('ctl00_ctl00_cphContentMiddle_cphContentMiddleRight_txtSearchText');
        var cmdPn = $('ctl00_ctl00_cphContentMiddle_cphContentMiddleRight_cmdSearchByProductNumber');
        var cmdSt = $('ctl00_ctl00_cphContentMiddle_cphContentMiddleRight_cmdSearchByText');

        if (txtPn != null && txtSt != null) {
            if (txtPn.value.length > 0 && txtSt.value.length > 0 || (
            txtPn.value.length == txtSt.value.length))
                txtPn.focus();
            else if (txtPn.value.length == 0 && txtSt.value.length > 0)
                txtSt.focus();
            else
                txtPn.focus();

            txtPn.addEvent('focus', function () {
                doSearch = false;
            });

            txtPn.addEvent('blur', function () {
                doSearch = true;
            });

            txtPn.addEvent("keydown", function (e) {
                if (e.key == 'enter') {
                    e.stop();
                    document.doSearch = false;

                    if (txtPn.value.length > 2) {
                        document.location = getSearchUrl(txtPn.value, urlstrProductType, urlstrSearchByPn, 'dpsbsbpn');
                    }
                    else {
                        alert(searchErrMsg);
                    }
                }
            });

            txtSt.addEvent('focus', function () {
                doSearch = false;
            });

            txtSt.addEvent('blur', function () {
                doSearch = true;
            });

            txtSt.addEvent("keydown", function (e) {
                if (e.key == 'enter') {
                    e.stop();
                    document.doSearch = false;

                    if (txtSt.value.length > 2) {
                        document.location = getSearchUrl(txtSt.value, urlstrProductType, urlstrSearchByTxt, 'dpsbsbt');
                    }
                    else {
                        alert(searchErrMsg);
                    }
                }
            });
        }

        if (cmdPn != null) {
            cmdPn.addEvent('click', function (e) {
                e.stop();
                if (txtPn.value.length > 2)
                    document.location = getSearchUrl(txtPn.value, urlstrProductType, urlstrSearchByPn, 'dpsbsbpn');
                else
                    alert(searchErrMsg);
            });
        }

        if (cmdSt != null) {
            cmdSt.addEvent('click', function (e) {
                e.stop();
                if (txtSt.value.length > 2) {
                    document.location = getSearchUrl(txtSt.value, urlstrProductType, urlstrSearchByTxt, 'dpsbsbt');
                }
                else
                    alert(searchErrMsg);
            });
        }
    }
});

var dpp = null;