jQuery.noConflict();

  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();

window.onload = function(){
    jQuery('.homeslide').cycle({ 
        fx:    'fade', 
        speed:  1500,
        timeout: 12000
    });
    
    /* Home carousel */
    var instance = 0;
    jQuery('.carousel-position').each(function(){
        jQuery(this).addClass('carousel-pager-'+instance);
        instance++;
    });
    
    instance = 0;
    jQuery('.carousel-products-list').each(function(){
        jQuery(this).cycle({
            fx: 'scrollHorz',
            speed: 1300,
            timeout: 6000,
            fit: 1,
            width: 642,
            pager: '.carousel-pager-'+instance,
            pagerAnchorBuilder: function(idx, slide){return '<span class="carousel-book-icon"></span>';}
        });
        
        instance++;
    });
    
    jQuery('.carousel-control-left').click(function(){
        jQuery(this).parents('.home-carousel').find('.carousel-products-list').cycle('prev');
    });
    
    jQuery('.carousel-control-right').click(function(){
        jQuery(this).parents('.home-carousel').find('.carousel-products-list').cycle('next');
    });
    
    jQuery('.carousel-control-play').click(function(){
        jQuery(this).parents('.home-carousel').find('.carousel-products-list').cycle('toggle',true);
        
        if(jQuery(this).hasClass('paused')){
            jQuery(this).removeClass('paused');
        }else{
            jQuery(this).addClass('paused');
        }
    });
    
}

jQuery(document).ready(function(){
    /************************************* Product Page enhancements *********************************/
    
    jQuery('.product-collateral-tabs li>div>div').css('display','none');
    jQuery('.product-collateral-tabs li:first-child').addClass('active');
    jQuery('<div class="product-collatoral-output"></div>').insertAfter('.product-collateral-tabs').append(jQuery('.product-collateral-tabs .active div div').html());
    jQuery('.product-collateral-tabs li').click(function(){
        jQuery('.product-collateral-tabs li').removeClass('active');
        jQuery(this).addClass('active');
        jQuery('.product-collatoral-output')
            .empty()
            .append(jQuery('.product-collateral-tabs .active div div').html()); 
    });    
    
    /* get ajax response for newsletter sign-up*/
    jQuery('#constant-contact-submit').click(function(){
        var email = jQuery('#constant-contact-email').val();
        jQuery.get('http://'+document.location.hostname+'/constantcontact/submission/submit/email/'+email, function(data){
            jQuery('#constant-contact-response p.message').empty().append(data);
            jQuery('#constant-contact-response').show();
        });
        return false;
    });
    
    jQuery('#constant-contact-dismiss').click(function(){
        jQuery('#constant-contact-response').fadeOut(100);
        return false;
    });
    
    /* Hide cart and checkout link items. Done here because css has no way to identify parent items.*/
    jQuery('li .top-link-cart, li .top-link-checkout').parent().css('display', 'none');    
    
    /**************** Reveal fly-away menus on hover *************************************************/
    jQuery(".block-layered-nav li.parent").mouseenter(function(){
        
        //pin child menu to top of nav window
        child_menu = jQuery(this).children('div');
        child_height = child_menu.outerHeight();
        child_top_offset = jQuery('div.block-layered-nav').offset().top-jQuery(this).offset().top;
        
        if(jQuery('div.block-layered-nav').offset().top < jQuery(window).scrollTop())
        {
            //adjust child menu top if window is scrolled down past the top of the main frame.
            child_top_offset = jQuery(window).scrollTop() - jQuery(this).offset().top;
        }
        
        
        if(jQuery('div.block-layered-nav').offset().top + child_height - jQuery(window).scrollTop() > jQuery(window).height())
        {
            //Make false multi-column layout if the height of the menu exceeds the bottom of the viewport.
            
            items = child_menu.children('ul').children('li');
            number_of_elements = items.length;
            
            desired_height = jQuery(window).height()-jQuery('div.block-layered-nav').offset().top + jQuery(window).scrollTop();
            
            if(desired_height > jQuery(window).height())
            {
                //if user is scrolled below the top of the main content window, just set column height to the screen height.
                desired_height = jQuery(window).height();
            }
            
            number_of_columns = Math.ceil(child_height/desired_height);
            column_break = Math.ceil(number_of_elements/number_of_columns);
            
            output="";
            
            for(column = 0 ; column < number_of_columns ; column++)
            {
                output += "<ul>";
                for(i=column_break*column ; i < column_break*(column+1) && i < number_of_elements; i++)
                    {
                        output += jQuery(items.get(i)).outerHTML();
                    }
                output += "</ul>";
                
            }
            
            child_menu.html(output);
            child_height = child_menu.outerHeight();
        }
        
        //if Child window is too short move next to parent link
        if(child_height+child_top_offset-40 < 0){
            child_top_offset = 40-child_height;
        }
        
        child_menu.css('top',child_top_offset+'px');
        
        child_menu.show();
    });
    
    /* Hide fly-away menus once the cursor leaves */
    jQuery(".block-layered-nav li.parent").mouseleave(function(){
        sub_menu = jQuery(this).children('div');
        sub_menu.hide();
        
        //If false multi-column layout remove modifications to html.
        if(sub_menu.children('ul').length > 1){
            items = sub_menu.children('ul');
            output = "<ul>";
            items.each(function(){
               output+=this.innerHTML; 
            });
            output += "</ul>";
            sub_menu.html(output);
        }
    });

});

//Allow getting html of an element, including the element itself
jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}




