/* --------------------------
 * ON LOAD
 * -------------------------- */

$(document).ready(function(){
	if(typeof(IE6UPDATE_OPTIONS) == "undefined") {
		cushman_ready();
	}
});

function cushman_ready() {
	// Load custom fonts
    cushman_font();
    
    // Dropdown navigation
    cushman_navigation.init();
    
    // Third tier navigation styles
    cushman_navigation_third_tier();
    
    // Default form values
    cushman_forms_values();
    
    // Link icons and open external links in new window
    cushman_link_icons(); 
    
    // Carousels
    cushman_carousel_rotate();
    cushman_carousel_slide();
    
    // Tabs and Accordion
    cushman_tabs();
    cushman_accordion();
    
    // Interior link scrolling
    cushman_scroll();
}


/* --------------------------
 * Font Deck and Typekit
 * -------------------------- */

function cushman_font() {
    WebFontConfig = {
        typekit: {
          id: 'hgn6wci'
        },
        custom: { families: ['Cargo Bold'],
        // urls: [ 'http://f.fontdeck.com/s/css/l4MsFun30OJhB9DTDGcb8JQSAOQ/' + window.location.host + '/5754.css' ] }
		urls: [ 'http://f.fontdeck.com/s/css/l4MsFun30OJhB9DTDGcb8JQSAOQ/' + window.location.host + '/6658.css' ] }
    };
    
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
    
    setTimeout("cushman_font_timeout()", 5000);
}
function cushman_font_timeout() {
    $('html:not(.wf-active)').addClass('wf-inactive');
}


/* --------------------------
 * NAVIGATION
 * -------------------------- */

var cushman_navigation_defaultLink = $('.nav-main > li.active').attr('id');
var cushman_navigation = {
    
    /* Show related dropdown on hover */
    init: function() {
        // Set up shared selectors
        var navLinks = $('.nav-main > li', '#main');
       
        // Step throuh each main link
        $(navLinks).each(function(i) {
            
            // Assign dynamic ids
            $(this).attr('id', 'nav-primary-' + i);
            $(this).children('ul.dropdown-wrapper').attr('id', 'nav-dropdown-' + i);
            
            // Detect name based on element id
            var name = cushman_navigation.name(this);
            
            // Show hide individual dropdowns
            $('a', this).hover(function() {
                // Show active div
                cushman_navigation.show(name);
                
                // Clear nav timeout
                clearTimeout(dropdownHideTimeout);
                
            }, function() {
                // Hide active div 
                dropdownHideTimeout = setTimeout("cushman_navigation.hideTimeout()", 400);
            });
            
            // Set active state on page load
            if($(this).hasClass('active-trail')){
                cushman_navigation.show(name);
            }
        });
        
        // Hover action on actual dropdown; necessary since dropdown markup
        // is actually located at the bottom of the document instead
        $('ul.dropdown-wrapper').hover(function() {
            var name = cushman_navigation.name(this);
            
            // Show dropdown and retain active state
            cushman_navigation.show(name);
            
            // Clear nav timeout
            clearTimeout(dropdownHideTimeout);
        }, function() {
            var name = cushman_navigation.name(this);
            
            if(!$('#nav-primary-'+ name).hasClass('active') || !$('#nav-primary-'+ name).hasClass('active-trail')) {
                // Hide active div 
                dropdownHideTimeout = setTimeout("cushman_navigation.hideTimeout()", 200);
            }
        });
    },
    
    /* Show selected dropdown */
    show: function(name) {
        // Remove previous active states
        cushman_navigation.hide();
       
        // Show dropdown and add link class
        $('#nav-primary-'+ name).addClass('active');
        
        // Position secondary nav
        var mainOffset = $('#main').offset();
        var offset = $('#nav-primary-'+ name).offset();
        
        if($('#nav-primary-'+ name).hasClass('left')) {
            $('#nav-dropdown-'+ name).css('left', offset.left-mainOffset.left+13) ;
        }
        else {
            var offsetRight = $('#nav-dropdown-'+ name).width() - $('#nav-primary-'+ name).width();
            if (offsetRight < 0) {
                offsetRight = 0 - offsetRight;
            }
            var offsetLeft = offset.left - offsetRight;
            $('#nav-dropdown-' + name).css('left', offsetLeft-mainOffset.left+13);
        }
        
        $('#nav-dropdown-'+ name).show();
    },
    
    /* Hide active dropdown */
    hide: function() {
        
        // Remove active class and hide active dropdowns
        $('.nav-main > li.active', '#main').removeClass('active');
        
        // Don't hide active page's nav
        $('.dropdown-wrapper:visible').each(function() {
            if(!$(this).hasClass('active-nav')){
                $(this).hide();
            }
        });
        
    },
    
    /* Reset default active state */
    reset: function() {
        $('#'+ cushman_navigation_defaultLink).addClass('active');
    },
    
    /* Hide timeout */
    hideTimeout: function() {
        cushman_navigation.hide();
        cushman_navigation.reset();
        
        // Force the active trail second-tier to re-appear
        $('.nav-main > li.active-trail ul.dropdown-wrapper', '#main').show();
    },
    
    /* Get share name value */
    name: function(el) {
        var new_name = $(el).attr('id').replace('nav-dropdown-', '').replace('nav-primary-','');
        
        return new_name;
    }
}



/* --------------------------------
 * NAVIGATION:  THIRD TIER STYLES
 * -------------------------------- */
 
function cushman_navigation_third_tier() {
    $('ul.third-tier', 'ul#nav-primary li').each(function() {
        // Cache selectors
        var self = this;
        var $parent = $(this).parents('li.second-tier');
    
        // Mark second-tier parent as active
        $parent.addClass('hasThirdTier');
        
        // Hover action on second-tier parent
        $parent.hover(function() {
            $(this).addClass('active');
            $(self).show();
            
            cushman_navigation_third_tier_hover(self);
        }, function() {
            $(this).removeClass('active');
            $(self).hide();
        });
    });
}
function cushman_navigation_third_tier_hover(self) {
    var $self = $(self);
    var $parent = $self.parents('li.second-tier');
    var maxWidth = 0;
    var dimensions = [];
    
    $('li > a', $self).each(function() {
        // Find maximum width
        var thisWidth = $(this).width();
        
        if(thisWidth > maxWidth) {
            maxWidth = thisWidth;
        }
        
        // Adjust left offset
        dimensions['parent'] = $parent.position();
        dimensions['width'] = $parent.outerWidth();
    });
    
    // Assign calculated width to second-tier parent
    //var parentWidth = $parent.outerWidth() - 40;
    maxWidth = (maxWidth % 2) ? maxWidth + 1 : maxWidth;
    $self.width(maxWidth);
    $self.children('li').width(maxWidth);
    
    // Center third-tier related to second-tier
    var offset = dimensions['parent'].left + ( dimensions['width'] / 2); 
    var center = '-' + ($self.outerWidth() / 2) + 'px';
    $self.css({
        'left': offset,
        'marginLeft': center
    });
}


/* --------------------------
 * FORM DEFAULT VALUES
 * -------------------------- */

function cushman_forms_values() {
    $('input[rel], textarea[rel]', 'form').each(function() {
        // Default value
        var defaultValue = $(this).attr('rel');
        $(this).val($(this).attr('rel'));
        
        // Focus
        $(this).focus(function() {
            if($(this).val() == defaultValue) {
                $(this).val('');
            }
        });
        
        // Blur
        $(this).blur(function() {
            if($(this).val() == '') {
                $(this).val(defaultValue);
            }
        });
    })
}


/* --------------------------
 * LINK ICONS
 * -------------------------- */
 
function cushman_link_icons(){
	// Add link icons
	$('#main a').linkIcons({
		'direction': 'right',
		'extensions': {
			'pdf' : '/dc_CUS_EXT/_ui/skin/img/icons/icon-download.gif',
            'doc' : '/dc_CUS_EXT/_ui/skin/img/icons/icon-download.gif',
            'xls' : '/dc_CUS_EXT/_ui/skin/img/icons/icon-download.gif',
            'ppt' : '/dc_CUS_EXT/_ui/skin/img/icons/icon-download.gif',
			'www' : '/dc_CUS_EXT/_ui/skin/img/icons/icon-external.gif'
		}
	});
	// Open external links in a new window
	$("a[href^='http']").attr('target','_blank');
} 


/* --------------------------
 * CAROUSEL
 * -------------------------- */

function cushman_carousel_slide() {
    
    var slider3 = $('.carousel-slide', '#main.customization').bxSlider({
        startingSlide: 0,
        displaySlideQty: 3,
        moveSlideQty: 3 
    });
    var slider2 = $('.carousel-slide', '#main.product').bxSlider({
        startingSlide: 0,
        displaySlideQty: 2,
        moveSlideQty: 2 
    });
}

function cushman_carousel_rotate(){
    
    var slider = $('.carousel-rotate').bxSlider({
        auto: true,
        speed: 1500,
        pause: 15000,
        mode: 'fade',
            controls: false,
            onAfterSlide: function(currentSlide, totalSlides, slideElement){
                $('.carousel-container .bx-window','#main.home').css('overflow','visible');
                $('ul.carousel-rotate li','#main.home').removeClass('active');
                $('.thumbs a').removeClass('pager-active');
                slideElement.addClass('active');
                var currentSlide = currentSlide+1;
                $('a#thumb'+currentSlide).addClass('pager-active');
            }
     });
     $('.thumbs a').click(function(){
        var thumbIndex = $('.thumbs a').index(this);
        slider.goToSlide(thumbIndex);
        $('.thumbs a').removeClass('pager-active');
        $(this).addClass('pager-active');
        return false;
    });
    $('.thumbs a:first').addClass('pager-active');
}


/* --------------------------
 * TABS
 * -------------------------- */
 
function cushman_tabs(){
    
    // Set up tabs
    $('.specifications .tabs-wrapper').prepend('<ul class="options"></ul>')
    $('.tabs-wrapper .tab').each(function() {
        $('h3', this).remove();
        var name = $(this).attr('rel');
        $('ul.options').append('<li><a rel="'+name+'"href="#">'+ name + '</a></li>');
     });
    $('ul.options li:first-child').addClass('active');
    
    // Tab functionality
    $('#.tabs-wrapper .tab+.tab').hide();
    $('ul.options li a').click(function() {
        $('.tab').hide();
        $('ul.options li').removeClass('active');
        var category = $(this).attr('rel');
        $('.tab.'+category).show();
        $(this).parent('li').addClass('active');
    });
}


/* --------------------------
 * ACCORDION
 * -------------------------- */
 
function cushman_accordion() {
    
    // Show first pane and hide others by default
    $('.categories .heading').each(function() {
        if(!$(this).hasClass('first')){
            $(this).addClass('closed');
           $('div.content', this).hide(); 
        }
    }); 
    
    // Show or hide each pane
    $('.categories .heading .head').click(function() {
        if($(this).parent('.heading').hasClass('closed')){
            $(this).parent('.heading').removeClass('closed').addClass('open');
        }
        else {
            $(this).parent('.heading').removeClass('open').addClass('closed');
        }
        if(!$(this).parent().hasClass('first')){
            $(this).next().slideToggle('slow', function() {
            
            });
            return false;
        }
    });    
}


/* --------------------------
 * SMOOTH SCROLL
 * -------------------------- */
 
function cushman_scroll() {
    $('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
             if ($target.length) {
                 var targetOffset = $target.offset().top;
                 $('html,body').animate({scrollTop: targetOffset}, 1000);
                 return false;
            }
        }
    });
}

/* --------------------------
 * GOOGLE ANALYTICS
 * -------------------------- */
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-534014-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


