

/* Rollover arrows in promos*/
function ft_hoverArrow(elem, mode, basePath) {
    var pathName = ''; 
    pathName = (("https:" == document.location.protocol) ? "https://www.tacklefanatics.co.uk/" : "http://www.tacklefanatics.co.uk/");
    //pathName = document.location.protocol + '//' + document.location.hostname + '/' + document.location.pathname;
    //alert(pathName);
    if (mode == 'over') {
        var url1 = 'url(\'' + pathName + 'fishing-images/list-arrow-selected.png\') no-repeat 0 50%';
        Ojay(elem).setStyle({ background: url1 });
    } else {
        var url2 = 'url(\''+ pathName +'fishing-images/list-arrow.png\') no-repeat 0 50%';
        Ojay(elem).setStyle({ background: url2 });    
    }
}
function promobox_hoverArrow(elem, mode) {
    if (mode == 'over') {
        Ojay(elem).setStyle({ background: 'url(\'fishing-images/list-arrow-selected.png\') no-repeat 0 50%' });
    } else {
        Ojay(elem).setStyle({ background: 'url(\'fishing-images/list-arrow.png\') no-repeat 0 50%' });
    }
}
  
/**
 * @param {HTMLElement} input
 */
labelInField = function(input) {
  input = Ojay(input);
  
  if (input.length < 1) return;
  
  var label = Ojay.Forms.getLabel(input);
  
  label.hide();
  
  var focus = function() {
    if (input.node.value == label.node.innerHTML) input.node.value = '';
    input.node.focus();
  };
  
  var blur = function() {
    if (input.node.value == '') input.node.value = label.node.innerHTML;
  };
  
  label.on('click', focus);
  input.on('focus', focus);
  input.on('blur', blur);
  
  blur();
};

/**
 * Setup code to be run once the DOM is ready to accept input.
 */
Ojay.onDOMReady(function() {

    /*
    Set a cookie to define the default ProductView (Gallery or List)        
    */
    try {
        //        var ckName = "_CookieProductView_";
        //        if (YAHOO.util.Cookie.get(ckName) == null)
        //        { YAHOO.util.Cookie.set(ckName, "gallery"); }
    }
    catch (err) {
        //Handle errors here
    }


    /*
    Rollover image rollovers
    */
    Ojay('.fishing-theme #ft .section LI').forEach(function(item) {
        item.set({ onmouseover: 'javascript:ft_hoverArrow(this,"over")' });
        item.set({ onmouseout: 'javascript:ft_hoverArrow(this, "out")' });
    }
      );
    Ojay('.fishing-theme .promo-boxes .promo-box-content LI').forEach(function(item) {
        item.set({ onmouseover: 'javascript:promobox_hoverArrow(this,"over")' });
        item.set({ onmouseout: 'javascript:promobox_hoverArrow(this, "out")' });
    }
      );


    /**
    * Hide the text for the following fields when they are selected.
    */
    //  ['#search_form input.text', '.bid-form input.text'].forEach(labelInField); /* DONOT in bidding !!! */
    ['#search_form input.text'].forEach(labelInField);

    /**
    * Forms styling.
    */
    Ojay('input[type=checkbox]').forEach(function(input) { new Ojay.Forms.Checkbox(input) });

    Ojay('select').forEach(function(input) {

        var applyOjayStylingFlag = true;                                            // apply Ojay styling to SELECT?
        var submitFormOnChange = false;                                             // perform postback on change event?
        var browserName = navigator.appName;
        var browserVer = parseInt(navigator.appVersion);

        if ((browserName == 'Microsoft Internet Explorer') && (browserVer == 4)) {// do not tyle SELECT because of IE rendering bug
            applyOjayStylingFlag = false;
        };
        if (input.hasClass('no-css')) {
            applyOjayStylingFlag = false;
        };                                     // don't do styling in this control          
        if (input.hasClass('product-list-sort')) {                                // we want Ojay styling in this control
            applyOjayStylingFlag = true;
            submitFormOnChange = true;
        }
        if (applyOjayStylingFlag) {
            if (input.hasClass('oJay-autopostback')) {                                // we want Ojay styling in this control and to postback
                submitFormOnChange = true;
            }
            var select = new Ojay.Forms.Select(input);
            if (submitFormOnChange) {
                select.on('change', function(select) {
                    input.ancestors('form').node.submit();
                });
            }
        }
    });

    var radios = Ojay('input[type=radio]').reduce(function(map, radio) {
        var name = radio.node.name;
        map[name] = map[name] || [];
        map[name].push(radio.node);
        return map;
    }, {});

    for (var name in radios) {
        new Ojay.Forms.RadioButtons(radios[name]);
    }

    /**
    * Tabbed carousels.
    */
    Ojay('.tabbed-carousel').forEach(function(t) {
        var n = [3, 4, 5].filter(function(n) {
            return t.hasClass('carousel-' + n);
        })[0];

        var tc = new TabbedCarousel(t, (typeof n == 'number') ? { columns: n} : {});
    });

    /**
    * Main home page promo area.
    */


    (function() {
        var wrapper = Ojay('#home_promo');
        if (wrapper.length < 1) return;

        wrapper.setStyle({ display: 'block' });

        var promo = new TabbedPromotion(wrapper,
      { position: 'bottom', animateSpeed: 0.5 });

        var panels = [promo.wrapper.children('.tabbed'),
                  wrapper.siblings().at(0)];
        var bottom = panels.reduce(function(min, panel) {
            return Math.min(min, panel.getBottom());
        }, Infinity);
        panels.forEach(function(panel) {
            var region = panel.getRegion();
            if (region.bottom == bottom) return;
            panel.setStyle({ height: (region.getHeight() + bottom - region.bottom) - 1 + 'px' });
        });
    })();


    /**
    * Generic tabbed content areas.
    */
    Ojay('.tabbed-generic, .tabbed-promos').forEach(function(t) {
        var tabs = new Tabbed(t.descendants('.tab'));
    });


    /**
    * Tabbed headers.
    */
    Ojay('.tabbed-header').forEach(function(t) {
        var theader = new Tabbed(t.descendants('.tab'));
        var theight = theader.tabs[0]._wrapper.getHeight();
        theader.tabs.forEach(function(tab, i) {
            tab._wrapper.setStyle({ height: theight + 'px', overflow: 'hidden' });
            if (i > 0)
                tab._wrapper.descendants('.tab-content').setStyle({ overflow: 'auto' });
        });
    });

    /**
    * Small image gallery for the item page. Switches between large images based
    * on which thumbnail link is clicked.
    */
    (function() {
        var galleries = Ojay('.item-photos');
        if (galleries.length < 1) return;

        galleries.forEach(function(gallery) {
            var main = gallery.descendants('.main-image img').at(0);
            var links = gallery.descendants('.thumbnails a.image-link');

            links.filter(function(link) {
                return link.node.href == main.node.src;
            }).at(0).addClass('current');

            links.on('click', function(link, ev) {
                ev.stopDefault();
                if (link.hasClass('current')) return;
                links.removeClass('current');
                link.addClass('current');
                main.animate({ opacity: { to: 0} }, 0.2, {
                    after: function() {
                        main.set({ src: link.node.href });
                    }
                }).animate({ opacity: { to: 1} }, 0.2);
            });
        });
    })();
});

