﻿
var boxAreaArray;

window.addEvent('domready', function () {

    SelectedMenu();
    MainNav();
    SubmenuItems();
    ProductThumbs();
    signup();

    boxAreaArray = new Array($$('.ProductCategoryImageContainer').length);
    for (var i = 0; i < boxAreaArray.length; ++i) {
        boxAreaArray[i] = false;
    }

    menuArray = new Array($$('.mega-menu-bg').length);
    for (var i = 0; i < menuArray.length; ++i) {
        menuArray[i] = false;
    }


    if ($('Product-Images')) {
        ProductGallery();
    }

    if ($('CatProducts')) {

        $('CatProducts').getElements('.per-buy-box:nth-child(4n+4)').getFirst('.per-buy-box-hidden').setStyle('left', '-240px');

        if (Browser.Engine.trident) {
            $$('.per-buy-box', '.per-buy-box-hidden').setStyle('height', '303px');
        }
    }

    if ($('BillingNumberAndStreet')) {
        addDeliveryAddressValidation();
        checkForDeliveryAddressValidation();
        CheckoutUseSameAddressFunction();
    }

    if ($('ContactForm')) {

        var val3 = new Form.Validator($("ContactForm"), {
            stopOnFailure: true,
            onElementPass: function (e, arr) {
                if (e.hasClass("overtextContact") == true) {

                    e.morph({ "background-color": "#ffffff" });

                }
            },
            onElementFail: function (e, arr) {
                if (e.hasClass("overtextContact") == true) {

                    e.morph({ "background-color": "#b13113" });

                }
            },
            onFormValidate: function (passed, form, event) {

                return passed;
            }
        });
    }


});

function SubmenuItems() {
    var imageFadeContainers = $$('.menu-items-wrapper');
    imageFadeContainers.each(function (element, index, array) {
        var thumbs = element.getElements('.menu-link-container a');
        //alert(thumbs);
        thumbs.each(function (e, i, a) {
            e.addEvent("mouseenter", function () {
                var imgContainer = element.getFirst('.menu-product-info-container');

                imgContainer.getElements('.menu-product-info-Items').each(function (e2, i2, a2) {
                    //alert(imgContainer);
                    if (i == i2) {
                        e2.morph({ 'opacity': '1' });
                    }
                    else {
                        e2.morph({ 'opacity': '0' });
                    }
                });
            });
        });
    });
}

function ProductThumbs() {

    var ProductThumbs = $$('.ProductCategoryImageContainer');
    ProductThumbs.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {

            DelayMouseOver.delay(250);
            boxAreaArray[i] = true;
        },
            'mouseleave': function () {

                boxAreaArray[i] = false;
            }
        });
        var DelayMouseOver = function () {

            if (boxAreaArray[i] == true) {
                el.getParent().setStyle('z-index', '15');
                el.getParent().getFirst('.per-buy-box-hidden').setStyle("display", "block");
                el.getParent().getFirst('.per-buy-box-hit').setStyle("display", "block");
            }
        }
    });

    var ProductHiddenThumbs = $$('.per-buy-box-hit');
    ProductHiddenThumbs.each(function (el, i, a) {
        el.addEvents({ 'mouseleave': function () {
            boxDissapear(el);
        }
        });
    });

}


function boxDissapear(box) {
    box.setStyle("display", "none");
    box.getParent().getFirst('.per-buy-box-hidden').setStyle("display", "none");
    box.getParent().setStyle('z-index', '1');

}



function signup() {

    if ($$('.view-basket-rollout li')) {
        $$('.view-basket-rollout li').getLast().setStyle('border-bottom', 'none');
    }

    $('askanExpert').addEvent('click', function () {
        $('callback-wrapper').morph({ 'visibility': 'visible' });
    });

    if (Browser.Engine.trident) {
        $$('.askTextArea').setStyle('height', '100px');
        $$('.askTextbox').setStyle('padding', '4px 4px 7px 4px');

    }


    var FormWrapper = $$('#callback-wrapper');
    FormWrapper.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {
            el.set('morph', { duration: '1000', transition: 'expo:out' });
            WrapperArea = false;
        },
            'mouseleave': function () {
                WrapperArea = true;
                DelayWrapperMouseOut.delay(1500);
            }
        });
        var DelayWrapperMouseOut = function () {
            if (WrapperArea == true) {
                el.setStyle("visibility", "hidden");
            }
        }
    });





    $$('.modelinfo').each(function (e, i, a) {
        new OverText(e);
    });

    $$('.model-item-row').each(function (e, i, a) {

        e.getFirst('.overTxtLabel').morph({ 'margin': '3px 0px 0px 10px', 'font-family': 'PTSansNarrowRegular', 'font-size': '9px' });
    });




    $$('.overtext').each(function (e, i, a) {
        new OverText(e);
    });
    $$('.callbackrow').each(function (e, i, a) {
        if (e.getFirst('label')) {
            e.getFirst('label').morph({ 'font-family': 'PTSansNarrowRegular', 'font-size': '14px', 'margin-top': '4px' });
        }
    });

    $$(".overtext").each(function (e, i, a) {

        e.addEvent("focus", function () {
            e.getParent().getFirst(".formlabel").morph({ 'display': 'block' });
        });
        e.addEvent("blur", function () {
            e.getParent().getFirst(".formlabel").morph({ 'display': 'none' });
        });
    });

    var val2 = new Form.Validator($("AskExpert"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {

                e.morph({ "background-color": "#ffffff", "color": "#4D4D4D" });

            }
        },
        onElementFail: function (e, arr) {
            if (e.hasClass("overtext") == true) {

                e.morph({ "background-color": "#b13113", "color": "#ffffff" });

            }
        },
        onFormValidate: function (passed, form, event) {

            return passed;
        }
    });
}


function ProductGallery() {
    var container = $('Product-Images');
    var thumbs = $$(".ProductThumb");

    //alert(thumbs);
    thumbs.each(function (e, i, a) {
        e.addEvent("click", function () {
            container.getChildren().each(function (e2, i2, a2) {

                thumbs.each(function (el3, i3, a3) {
                    el3.setStyle('border-color', '#cccccc');
                });

                if (i == i2) {
                    e2.morph({ 'opacity': '1' });
                    e.setStyle('border-color', '#333333');
                }
                else {
                    e2.morph({ 'opacity': '0' });
                    e.setStyle('border-color', '#333333');
                }
            });
        });
    });

    if (thumbs.length > 0) {
        thumbs[0].fireEvent("click");
    }
}

function SelectedMenu() {
    /*Menu Selected*/
    var MenuItems = $$(".topLinks");

    var urlArray = document.location.toString().split("/");

    //alert(urlArray);

    MenuItems.each(function (e, i, a) {
        //alert(e.getFirst().getProperty("href"));
        var urlArrayLink = e.getFirst().getProperty("href").split("/");

        if (urlArray.length > 2) {
            // alert(urlArrayLink[3].toLowerCase());
            //alert(urlArray[5].toLowerCase());
            if (urlArrayLink[1].toLowerCase() == urlArray[3].toLowerCase()) {
                // Highlight e (the li) as link)
                //  var linkitem = e.getFirst();
                e.addClass('TopMenuSelected');
            }
        }
    });


    var url = document.location.toString().split("/");
    var SelectedTopMenuLink = 0;
    if (location.pathname.toLowerCase().indexOf("/led-tv-wall-mounts") == 0) {
        $$('body').setStyle('background', 'url(/resources/images/bg/LED-Wall-Mounts-BG-Crop-Light.jpg) no-repeat fixed center 0px');
    }
    if (location.pathname.toLowerCase().indexOf("/projector-mounts") == 0) {
        $$('body').setStyle('background', 'url(/resources/images/bg/Projector-Mounts-BG-Crop-Light.jpg) no-repeat fixed center 0px');
    }
    if (location.pathname.toLowerCase().indexOf("/tv-stands") == 0) {
        $$('body').setStyle('background', 'url(/resources/images/bg/TV-Stands-BG-Crop-Light.jpg) no-repeat fixed center 0px');
    }
    if (location.pathname.toLowerCase().indexOf("/speaker-mounts") == 0) {
        $$('body').setStyle('background', 'url(/resources/images/bg/Speaker-Mounts-BG-Crop-Light.jpg) no-repeat fixed center 0px');
    }

    //            $$(".topLinks").each(function (e, i, a) {
    //                if (i == SelectedTopMenuLink) {
    //                    e.addClass("current");
}


//function MainNav() {

//    var MenuItems = $$('.sMenu ul li');
//    MenuItems.each(function (el, i, a) {

//        $$('.menu-click-for-detials a').setStyle('color', '#333333');

//        el.addEvents({ 'mouseenter': function () {

//            if (el.getFirst('.mega-menu-bg')) {

//                el.getFirst('.mega-menu-bg').set('morph', { duration: '100', transition: 'expo:out' });

//                MainMenuItemIn = true;
//                DelayMouseIn.delay(1000);
//            }
//            //Hide other elements 
//            MenuItems.each(function (el2, i2, a2) {

//                if (i2 != i) {
//                    // alert('sdf');
//                    if (el2.getFirst('.mega-menu-bg')) {
//                        el2.getFirst('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
//                    }
//                }

//            });
//        },
//            'mouseleave': function () {
//                if (el.getFirst('.mega-menu-bg')) {

//                    MainMenuItemIn = false;
//                    el.getFirst('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
//                }
//                MenuItems.each(function (el2, i2, a2) {
//                    if (i2 != i) {
//                        // alert('sdf');
//                        //Hide other elements 
//                        if (el2.getFirst('.mega-menu-bg')) {
//                            el2.getFirst('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
//                        }
//                    }
//                });
//            }
//        });

//        var DelayMouseIn = function () {
//            if (MainMenuItemIn == true) {
//                el.getFirst('.mega-menu-bg').morph({ "display": "block", "z-index": "26" });
//            }
//        }
//    });
//}



function MainNav() {
    $$('.menu-click-for-detials a').setStyle('color', '#333333');

    var MenuItems = $$('.sMenu ul li');

    MenuItems.each(function (el, i, a) {

        el.addEvents({ 'mouseenter': function () {

            if (el.getFirst('.mega-menu-bg')) {

                $$('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
                DelayMouseEnter.delay(500);
                menuArray[i] = true;
                
            }

        },
            'mouseleave': function () {
                if (el.getFirst('.mega-menu-bg')) {
                    menuArray[i] = false;
                    el.getFirst('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
                }
            }
        });

        var DelayMouseEnter = function () {
            if (menuArray[i] == true) {
                $$('.mega-menu-bg').morph({ "display": "none", "z-index": "0" });
                el.getFirst('.mega-menu-bg').morph({ "display": "block", "z-index": "26" });
            }
        }


    });





}


var myScript = new Asset.javascript('/Resources/js/menuClass.js', {
    id: 'myScript',
    onload: function () {
        //alert('myScript.js is loaded!');        
    }
});


function AddOneToBasket(OrderProductID, CurrentQuantity) {

    var newQuantity = CurrentQuantity.toInt() + 1;

    var myRequest = new Request({
        url: '/basket/UpdateProductQuantityJs?OrderProductID=' + OrderProductID + "&Quantity=" + newQuantity,
        method: 'get',
        onSuccess: function (responseText, responseXML) {
            if ($("BasketRow" + OrderProductID)) {
                $("BasketRow" + OrderProductID).getParent().set("html", responseText);
            }
            UpdateTotals();
            UpdateTopBasket();
        }
    });

    myRequest.send();
}

function RemoveOneFromBasket(OrderProductID, CurrentQuantity) {

    var newQuantity = CurrentQuantity.toInt() - 1;
    var myRequest = new Request({
        url: '/basket/UpdateProductQuantityJs?OrderProductID=' + OrderProductID + "&Quantity=" + newQuantity,
        method: 'get',
        onSuccess: function (responseText, responseXML) {
            if ($("BasketRow" + OrderProductID)) {
                $("BasketRow" + OrderProductID).getParent().set("html", responseText);
            }

            UpdateTotals();
            UpdateTopBasket();
        }
    });

    myRequest.send();
}

function UpdateTotals() {
    var targetToUpdate = $("BasketTotalContainer")

    var myRequest = new Request({
        url: '/basket/BasketTotal',
        method: 'get',
        onSuccess: function (responseText, responseXML) {
            if ($("BasketTotalContainer")) {
                $("BasketTotalContainer").set("html", responseText);
            }
        }
    });

    myRequest.send();
}

function UpdateTopBasket() {
    var myRequest = new Request.HTML({

        url: '/basket/TopBasket',
        method: 'get',
        onSuccess: function (Text, XML, HTML, fff) {
            // alert(HTML);
            if ($("TopBasket")) {
                $("TopBasket").set("html", HTML);
            }

        }
    });

    myRequest.send();
}




function CheckoutUseSameAddressFunction() {
    $('CheckForUsemybillingAddress').addEvent('click', function () {
        //alert('checked');
        var BillingNumberAndStreet = $('BillingNumberAndStreet').getProperty("value");
        var BillingAddress2 = $('BillingAddress2').getProperty("value");
        var BillingCity = $('BillingCity').getProperty("value");
        var BillingPostcode = $('BillingPostcode').getProperty("value");
        var BillingCounty = $('BillingCounty').getProperty("value");

        $('DeliveryNumberAndStreet').setProperty("value", BillingNumberAndStreet);
        $('DeliveryAddress2').setProperty("value", BillingAddress2);
        $('DeliveryCity').setProperty("value", BillingCity);
        $('DeliveryPostcode').setProperty("value", BillingPostcode);
        $('DeliveryCounty').setProperty("value", BillingCounty);
    });

    var val4 = new Form.Validator($("CheckoutForm"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtextcheckout") == true) {

                e.getParent().getParent().getFirst().morph({ "background-color": "#ffffff", "color": "#4D4D4D" });

            }
        },
        onElementFail: function (e, arr) {
            if (e.hasClass("overtextcheckout") == true) {

                e.getParent().getParent().getFirst().morph({ "background-color": "#b13113", "color": "#ffffff" });

            }
        },
        onFormValidate: function (passed, form, event) {

            return passed;
        }
    });
}

function addDeliveryAddressValidation() {

    $$('.delivery-address-part').each(function (e, i, a) {

        e.addEvent('change', function (event) {

            checkForDeliveryAddressValidation();

        });

    });

}

function checkForDeliveryAddressValidation() {
    if (allDeliveryPartsEmpty()) {
        removeRequiredDeliveryClass();
    }
    else {
        addRequiredDeliveryClass();
    }
}

function allDeliveryPartsEmpty() {

    var empty = true;
    $$('.delivery-address-part').each(function (e, i, a) {

        if (e.get('value').length > 0) {
            empty = false;
        }

    });

    return empty;
}

function addRequiredDeliveryClass() {
    $$('.delivery-validate').each(function (e, i, a) {
        e.addClass('overtextcheckout required');
    });
}

function removeRequiredDeliveryClass() {
    $$('.delivery-validate').each(function (e, i, a) {
        e.removeClass('overtextcheckout required');
        e.getParent().getParent().getFirst().morph({ "background-color": "#ffffff", "color": "#4D4D4D" });
    });
}

