
window.addEvent("domready", function () {

    //DROPDOWN MENU ----------------------------------------------
    //------------------------------------------------------------


    if ($('primary-links')) {

        // var menuWrapper = $("primary-links");
        // var menuItems = menuWrapper.getElements(".primary-link");
        // 
        // menuItems.each(function (item, index) {
        //     var currentDropdown = item.getElement("ul");
        //     var menuSlide = new Fx.Morph(currentDropdown);
        // 
        //     item.addEvent('mouseenter', function (e) {
        //         e.stop();
        // 
        //         currentDropdown.fade("hide");
        //         currentDropdown.fade("in");
        // 
        //     });
        // 
        // });
    }



    //SIGNIN & CART DROPDOWNS ------------------------------------
    //------------------------------------------------------------

    if ($('global-hud')) {

        var cartButton = $("shoppingcart-button");
        var signinButton = $("signin-button");
        var profileButton = $("profile-button");

        var cartArea = $("shoppingcart-area");
        var signinArea = $("signin-area");
        var profileArea = $("profile-area");
        //  console.log(profileButton);

        var cartActive;
        var signinActive;
        var profileActive;

        /*	cartButton.addEvent('click', function(e){
        e.stop();
			
        if(cartActive == 1){
        cartActive = null;
        signinActive = null;
        cartArea.fade("out");
        } else {
        cartActive = 1;
        signinActive = null;
        cartArea.fade("hide");
        signinArea.fade("hide");
        cartArea.setStyle("display", "block");
        cartArea.fade("in");
        }
			
        });*/
        //        if (signinButton != null) {
        //            signinButton.addEvent('click', function (e) {
        //                e.stop();

        //                if (signinActive == 1) {
        //                    signinActive = null;
        //                    cartActive = null;
        //                    profileActive = null;
        //                    signinArea.fade("out");
        //                } else {
        //                    signinActive = 1;
        //                    cartActive = null;
        //                    profileActive = null;
        //                    cartArea.fade("hide");
        //                    signinArea.fade("hide");
        //                    signinArea.setStyle("display", "block");
        //                    signinArea.fade("in");
        //                }

        //            });
        //        }
        if (profileButton != null) {
            profileButton.addEvent('click', function (e) {
                e.stop();

                if (profileActive == 1) {
                    signinActive = null;
                    cartActive = null;
                    profileActive = null;
                    profileArea.fade("out");
                } else {
                    profileActive = 1;
                    signinActive = null;
                    cartActive = null;
                    profileArea.fade("hide");
                    profileArea.setStyle("display", "block");
                    profileArea.fade("in");
                }

            });
        }
    }



    //HOME FOCAL SLIDER ------------------------------------
    //------------------------------------------------------------

    //    if ($('focal_container')) {

    //        var slideWrapper = $("focal_container");
    //        var slideInner = $("all-slides");
    //        var slide = slideInner.getElements(".slide");
    //        var slideMorph = new Fx.Morph(slideInner, { duration: 750, transition: Fx.Transitions.Sine.easeInOut });
    //        //var activeSlide;
    //        var myCount;
    //        // console.log(slide);
    //        var dotsHolder = new Element('div', { id: "slider_dots" }).inject(slideWrapper, 'top');

    //        slide.each(function (item, index) {
    //            var myCount = index;
    //            var marginPush;
    //            var dot = new Element('a', { "class": "dot" }).inject(dotsHolder);
    //            marginPush = (index * 680);

    //            item.setStyle("left", marginPush);

    //            if (index == 0) { dot.addClass("active"); }

    //            if (myCount == 0) { dotsHolder.fade("hide"); } else { dotsHolder.fade("in"); }

    //            dot.addEvent('click', function (e) {
    //                e.stop();

    //                dotsHolder.getElements(".dot").each(function (dots, index) {
    //                    if (dots.hasClass("active")) {
    //                        dots.removeClass("active");
    //                    }
    //                });

    //                slideMorph.start({
    //                    "margin-left": 0 - marginPush
    //                });
    //                dot.addClass("active");

    //            });

    //             	function moveSlide(){
    //             
    //             		slideMorph.start({
    //             			"left": 0-marginPush
    //             		});
    //             		dot.addClass("active");
    //             	}
    //             setTimeout(moveSlide, 2000);

    //        });

    //    }

    if ($("all-slides")) {

        var herobox = new PanelCarousel("all-slides", {
            stylesheet: "/wolverine/styles/screen.css",
            panelClass: ".slide",
            animationStyle: "slider",
            sliderDirection: "right-left",
            sliderDuration: 750,
            slideDuration: 7000,
            showNextPreviousButtons: false
        });

    }

    //HOME NEWS TICKER--------------------------------

    if ($('all-headlines')) {

        var headlineWrapper = $("all-headlines");
        var headlines = headlineWrapper.getElements(".headline");
        var i = 0;
        var newsduration = 8000;

        headlines.setStyle("position", "absolute");
        headlines.fade("hide");

        headlines.each(function (item, index) {
            item.addClass(index);
        });

        function showHeadline() {
            headlines.each(function (item, index) {
                item.fade("out");
                if (item.hasClass(i)) {
                    item.fade("in");
                }
            });

            if (i >= headlines.length - 1) {
                i = 0;
            } else {
                i++;
            }
            setTimeout(showHeadline, newsduration);
        }

        showHeadline();


    }



    //PRODUCT DETAILS ------------------------------------
    //------------------------------------------------------------

    if ($('all-details')) {

        var detailsWrapper = $("all-details");
        var productDetails = detailsWrapper.getElements(".detail");
        var topSize = $("product-left").getSize();
        var topHeight = topSize.y;
        var activeItem;
        var detailList = new Element('ul', { id: 'detail-list' }).inject(detailsWrapper, 'top');
        var wrapperMorph = new Fx.Morph(detailsWrapper);


        productDetails.each(function (item, index) {
            item.setStyle("margin-top", "60px");
            var contentSize = item.getSize();
            var contentHeight = contentSize.y;
            var fullHeight = contentHeight + topHeight;

            item.setStyle("position", "absolute");

            var detailHeader = item.getElement("h4");
            var detailheaderText = item.getElement("h4").get("text");

            detailHeader.setStyle("display", "none");

            var detaillistItem = new Element('li').inject(detailList);
            var detaillistItemLink = new Element('a', { text: detailheaderText }).inject(detaillistItem);

            if (index != 0) {
                item.fade("hide");

            } else {
                detaillistItem.addClass("active");
                item.addClass("active");
                detailsWrapper.setStyle("height", contentHeight + 60);
            }

            detaillistItemLink.addEvent('click', function (e) {
                e.stop();

                //detaillistItem.removeClass("active");
                item.getParent().getElement("ul").getElement(".active").removeClass("active");

                productDetails.each(function (item, index) {
                    if (item.hasClass("active")) {
                        item.fade("out");
                        item.removeClass("active");
                    }
                });

                detaillistItem.addClass("active");
                item.addClass("active");
                if (item.hasClass("active")) {
                    item.fade("in");
                }
                wrapperMorph.start({
                    "height": contentHeight + 60
                });
            });

        });



        // var arrowHolder = $("add-to-cart-button");
        // 
        // var cartArrow  = new Element('img',{src: "../images/arrow.png"}).inject(arrowHolder);
        // cartArrow.fade("hide");
        // 
        // cartArrow.morph = new Fx.Morph(cartArrow);
        // cartArrow.setStyle("position", "absolute");
        // 
        // arrowHolder.addEvent('click', function(e){
        // 	e.stop();
        // 	
        // 	cartArrow.setStyle("margin-top", "0");
        // 	
        // 	cartArrow.fade("in");
        // 	cartArrow.morph.start({
        // 		"margin-top": -200
        // 	});
        // 	
        // 	var fadeOut = function(){ exec(); };
        //      setTimeout(fadeOut, 500);
        //      function exec(){
        //           cartArrow.fade("out");
        //      }
        // 	
        // });


    }


    //FORM INPUT CLEARS -----------------------------------
    //------------------------------------------------------------

    //    if ($$("input")) {

    //        $$("input").each(function (item, index) {
    //            if (item.get("type") != "button" && item.get("type") != "submit") 
    //            {
    //                var defaultValue = item.get("value");

    //                item.addEvent('focus', function (e) {
    //                    e.stop();
    //                    if (item.get("value") == defaultValue) {
    //                        this.set("value", "");
    //                    }
    //                });

    //                item.addEvent('blur', function (e) {
    //                    e.stop();
    //                    if (item.get("value") == "") {
    //                        this.set("value", defaultValue);
    //                    }
    //                });
    //            }

    //        });

    //    }

    // Textfield Placeholders
    var inputs = $$("input[type=text], input[type=password], textarea");
    if (inputs.length > 0) {

        inputs.each(function (item, index) {

            var destroyLabel = false;
            if ((item.get('id') == "search_input") || (item.get('id') == "narrowsearch_input")) {

                destroyLabel = true;

            }

            item = new TextfieldPlaceholder(item, { destroyLabelIfPresent: destroyLabel });


        });

    }

    // Autocompleters - City

    //console.log(inputs);
    if ($("txtCity") && $("txtProvince") && $("txtPostalCode") && $("txtCountry")) {
        var inputCity = $("txtCity");
        var inputProvince = $("txtProvince");
        var inputPostalCode = $("txtPostalCode");
        var inputCountry = $("txtCountry");




        //City Validation
        var CityDataSource = function (ui, term) {
            if (term != '') {
                var ajaxComplete = function (data) {
                    ui.autocompleter('suggest', data);
                }; // eof ajaxComplete()
                var ajaxError = function () {
                    //alert('Connection error');
                }; // eof ajaxError()
                var request = new Request.JSON({
                    url: '/feeds/addressvalidation.aspx',
                    method: 'GET',
                    //data: { 'type': 'city', 'term': term },
                    data: { 'type': 'city', 'city': inputCity.value, 'province': inputProvince.value, 'postalcode': inputPostalCode.value, 'country': inputCountry.value },
                    onSuccess: ajaxComplete,
                    onFailure: ajaxError
                }).send();
            }
            return true;
        };

        var CityOptions = {
            'minLength': 1,
            'source': CityDataSource,
            'allowNewValues': true,
            'onCreate': function (ui) {
                var menu = ui.autocompleter('menu');
                var coords = ui.getCoordinates();
                menu.setPosition({ x: coords.left, y: coords.top + coords.height + 1 });
            }
        };
        inputCity.autocompleter(CityOptions);
        inputCity.autocompleter('option', 'onChange', function (ui) {
            $('txtCity').set('value', (ui.autocompleter('value') == '') ? '' : ui.autocompleter('value'));
        } // eof onChange()
        );



        //Province Validation
        var ProvDataSource = function (ui, term) {
            if (term != '') {
                var ajaxComplete = function (data) {
                    ui.autocompleter('suggest', data);
                }; // eof ajaxComplete()
                var ajaxError = function () {
                    //alert('Connection error');
                }; // eof ajaxError()
                var request = new Request.JSON({
                    url: '/feeds/addressvalidation.aspx',
                    method: 'GET',
                    //data: { 'type': 'city', 'term': term },
                    data: { 'type': 'province', 'city': inputCity.value, 'province': inputProvince.value, 'postalcode': inputPostalCode.value, 'country': inputCountry.value },
                    onSuccess: ajaxComplete,
                    onFailure: ajaxError
                }).send();
            }
            return true;
        };

        var ProvOptions = {
            'minLength': 1,
            'allowNewValues': true,
            'source': ProvDataSource,
            'onCreate': function (ui) {
                var menu = ui.autocompleter('menu');
                var coords = ui.getCoordinates();
                menu.setPosition({ x: coords.left, y: coords.top + coords.height + 1 });
            }
        };
        inputProvince.autocompleter(ProvOptions);
        inputProvince.autocompleter('option', 'onChange', function (ui) {
            $('txtProvince').set('value', (ui.autocompleter('value') == '') ? '' : ui.autocompleter('value'));
        } // eof onChange()
        );

        //Postal Validation
        var PostalDataSource = function (ui, term) {
            if (term != '') {
                var ajaxComplete = function (data) {
                    ui.autocompleter('suggest', data);
                }; // eof ajaxComplete()
                var ajaxError = function () {
                    //alert('Connection error');
                }; // eof ajaxError()
                var request = new Request.JSON({
                    url: '/feeds/addressvalidation.aspx',
                    method: 'GET',
                    //data: { 'type': 'city', 'term': term },
                    data: { 'type': 'postal', 'city': inputCity.value, 'province': inputProvince.value, 'postalcode': inputPostalCode.value, 'country': inputCountry.value },
                    onSuccess: ajaxComplete,
                    onFailure: ajaxError
                }).send();
            }
            return true;
        };
        var PostalOptions = {
            'minLength': 1,
            'allowNewValues': true,
            'source': PostalDataSource,
            'onCreate': function (ui) {
                var menu = ui.autocompleter('menu');
                var coords = ui.getCoordinates();
                menu.setPosition({ x: coords.left, y: coords.top + coords.height + 1 });
            }
        };
        inputPostalCode.autocompleter(PostalOptions);
        inputPostalCode.autocompleter('option', 'onChange', function (ui) {
            $('txtPostalCode').set('value', (ui.autocompleter('value') == '') ? '' : ui.autocompleter('value'));
        } // eof onChange()
        );






        //Country Validation
        var CountryDataSource = function (ui, term) {
            if (term != '') {
                var ajaxComplete = function (data) {
                    ui.autocompleter('suggest', data);
                }; // eof ajaxComplete()
                var ajaxError = function () {
                    // alert('Connection error');
                }; // eof ajaxError()
                var request = new Request.JSON({
                    url: '/feeds/addressvalidation.aspx',
                    method: 'GET',
                    //data: { 'type': 'city', 'term': term },
                    data: { 'type': 'country', 'city': inputCity.value, 'province': inputProvince.value, 'postalcode': inputPostalCode.value, 'country': inputCountry.value },
                    onSuccess: ajaxComplete,
                    onFailure: ajaxError
                }).send();
            }
            return true;
        };

        var CountryOptions = {
            'minLength': 1,
            'allowNewValues': true,
            'source': CountryDataSource,
            'onCreate': function (ui) {
                var menu = ui.autocompleter('menu');
                var coords = ui.getCoordinates();
                menu.setPosition({ x: coords.left, y: coords.top + coords.height + 1 });
            }
        };
        inputCountry.autocompleter(CountryOptions);
        inputCountry.autocompleter('option', 'onChange', function (ui) {
            $('txtCountry').set('value', (ui.autocompleter('value') == '') ? '' : ui.autocompleter('value'));
        } // eof onChange()
        );

    }


    if ($$(".question")) {

        var question = $$(".question");
        var securitycode = $("security-code-sample");

        question.addEvent('click', function (e) {
            e.stop();
            securitycode.fade("hide");
            securitycode.setStyle("display", "block");
            securitycode.fade("in");
        });

    }




    //FIREARM LICENCE POPUP -----------------------------------
    //------------------------------------------------------------

    //    if (document.getElement(".firearms_licence")) {

    //        var shroud = new Element('div', { "class": "shroud" }).inject(document.body, "top");

    //        shroud.setStyle("display", "block");
    //        shroud.fade("hide");
    //        shroud.fade(.75);

    //        var alertWrapper = new Element('div', { "class": "alert-wrapper shadow" }).inject(document.body, "top");
    //        var alertHeader = new Element('h3', { text: "Just a few things to be aware of..." }).inject(alertWrapper);
    //        var alertDescriptor = new Element('p', { html: "You have some items in your cart that are either firearms, or ammunition. Please read our shipping policy to lorem ipsum dolar sit amet. <br />Basically this just means that firearms and ammunition need to be shipped separetely" }).inject(alertWrapper);
    //        var continueButton = new Element('a', { "class": "small-button right", text: "Continue Checkout" }).inject(alertWrapper);

    //        alertWrapper.fade("hide");
    //        alertWrapper.fade("in");

    //        alertWrapper.morph = new Fx.Morph(alertWrapper);
    //        alertWrapper.morph.start({
    //            "margin-top": 150
    //        });


    //        //Continue Button---------------

    //        continueButton.addEvent("click", function (e) {
    //            e.stop();
    //            shroud.fade("out");

    //            alertWrapper.fade("out");
    //            alertWrapper.morph.start({
    //                "margin-top": 0
    //            });

    //        });

    //    }


    if ($("cvc")) {

        var cvc_button = $("cvc-button");
        var example = $("cvc");
        // console.log(cvc_button);

        cvc_button.addEvent('mouseover', function (e) {
            e.stop();
            example.fade("hide");
            example.setStyle("display", "block");
            example.fade("in");
        });

        cvc_button.addEvent('mouseout', function (e) {
            e.stop();
            example.fade("out");
        });
    }

    //SIDEBAR SEARCH ANIMATIONS -----------------------------------
    //------------------------------------------------------------

    //if (Browser.ie != true) { // not sure why we weren't letting this work for IE.

    //console.log(document.getElement('.search-button'));
    if (document.getElement(".search-button")) {

        var searchButton = document.getElement(".search-button");
        var searchInput = document.getElement(".input-search input");
        var defaultValue = searchInput.get("value");
        var quickResults = document.getElement("#quick-results");
        //var viewAll = document.getElement("#focal-search .view-all");
        searchButton.morph = new Fx.Morph(searchButton);

        if (document.getElement("#quick-results")) {
            quickResults.fade("hide");
            //viewAll.fade("hide");
        }
        searchButton.fade("hide");
        searchButton.setStyle("right", -80);
        var focalSearch = $("focal-search");

        searchInput.addEvents({

            "focus": function (e) {
                searchButton.fade("in");
                searchButton.morph.start({
                    "right": 0
                });
            },

            "blur": function (e) {

                var self = this;

                if (searchInput.get("value") == defaultValue) {
                    searchButton.fade("out");
                    searchButton.morph.start({
                        "right": -80
                    });

                    if (quickResults && self.get('value') == '') {
                        quickResults.fade("out");
                        //viewAll.fade("out");
                        (function () { quickResults.set('html', ''); }).delay(500);
                    }
                }
            },

            "keyup": function (evt) {

                var self = this;

                // we're about to send a new request, cancel the old one, if it exists.
                if (self.request && self.request.isRunning()) self.request.cancel();

                var fieldname = self.get('name');

                self.request = new Request({
                    url: '/feeds/search.aspx',
                    data: { ajaxsearch: self.get('value') },
                    method: 'post',
                    onRequest: function () { },

                    onSuccess: function (responseText, responseXML) {

                        if (responseText) {

                            if (quickResults) {
                                quickResults.set('html', responseText);
                                quickResults.fade("in");
                                //viewAll.fade("in");
                            }

                        } else {

                            if (quickResults) {
                                quickResults.fade("out");
                                //viewAll.fade("out");

                            }

                        }

                    },

                    onComplete: function () { },

                    onFailure: function () { }

                }).send();

            }

        });

    }



    //}


    /* Product Image Lightbox
    ----------------------------------------------------------- */

    if ($('large-product-image')) {

        var lightbox = new Lightbox({
            targetSelector: '#large-product-image',
            targetEnlargeButton: '.view-larger',
            enableEnlargeButton: true,
            stylesheet: '/wolverine/includes/lightbox.css'
        });

        // lightbox.targets.each(function (item, index) {
        // 
        //             item.addEvent("click", function (evt) {
        //                 evt.stop();
        //                 lightbox.loadContent(item.get("href"));
        //             });
        // 
        //         });


    }






});
