$(function(){
	// Focusmagic Plugin
	jQuery('form.focusmagic').focusMagic();
	
	// Rollover widget
	jQuery('.rollerWidget div').css({'height':jQuery('.rollerWidget > ul').outerHeight(true)}).first().show().siblings('div').hide();
	jQuery('.rollerWidget > ul a').mouseover(function() {
		jQuery(this).parents('.rollerWidget').find('div.'+ jQuery(this).attr('class')).show().siblings('div').hide();
		jQuery('.rollerWidget > ul a').removeClass('active');
		jQuery(this).addClass('active');
	});
	
	// Tooltip
	jQuery('a.investors_tooltip_link').mouseover(function() {
		jQuery(this).next('.tooltip_content').fadeIn('fast');
	}).mouseout(function(){
		jQuery(this).next('.tooltip_content').fadeOut('fast');
	}).click(function(){
		return false;
	});
	
	// Add last to secondary nav
	jQuery('#secondary-menu li:last').addClass('last');
	
	// Home page scroller
	jQuery('#brandAds').cycle({
		fx: 'scrollHorz',
		speed: 'slow',
		timeout: 5000,
		pager: '#nav',
		pauseOnPagerHover: true,

	    // callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<li><a href="#"></a></li>'; 
	    }
	});
	// Pause on hover
	jQuery('#brandAds').mouseenter(function(){
		jQuery(this).cycle('pause');
	}).mouseleave(function(){
		jQuery(this).cycle('resume');
	});
	
	// History page scroller
	jQuery('#historySlider').cycle({
		fx: 'scrollHorz',
		speed: 'slow',
		timeout: 0,
		pager: '#nav',
		pauseOnPagerHover: true,
	    next:   '#next', 
	    prev:   '#prev'
	});
	// Pause on hover
	jQuery('#historySlider').mouseenter(function(){
		jQuery(this).cycle('pause');
	}).mouseleave(function(){
		jQuery(this).cycle('resume');
	});
	
	// History page accordion
	jQuery( "#historyAccordion" ).accordion({
		autoHeight: false,
		navigation: true
	});
	
	
	// TICKER
	jQuery('.ticker .content .item').first().show();
	if(jQuery('.ticker .content .item').length > 1){
		var inter = setInterval(changeTicker, 5000);
	}
	function changeTicker() {
		jQuery('.ticker .item:visible').fadeOut('slow', function(){
			if(jQuery(this).next('.item').length > 0){
				jQuery(this).next('.item').fadeIn();
			}else{
				jQuery('.ticker .item').first().fadeIn();
			}
		});
	}
	
	// Careers page Youtube autoplay
	if(getUrlVars()['autoplay'] == 'true'){
		var $embed = jQuery('#content object embed');
		$embed.attr('src',$embed.attr('src') + '&autoplay=1');
	}
	function getUrlVars() { 
		var map = {}; 
		var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, function(m,key,value) { 
			map[key] = (value === undefined) ? true : value.substring(1); 
		}); 
		return map;
	}
	
	
	// Build Footer Brand Links
	// var url = "http://wwwdev.cqlcorp.net/brands-json/?jsoncallback=?";
	// var url = "http://www.wolverineworldwide.com/brands-json/?jsoncallback=?";
	var url = "https://www.wolverineworldwide.com/brands-json/?jsoncallback=?";
	jQuery.getJSON(url, function(data) {
		$.each(data.brands, function(i,brand){
			jQuery('.brandShortcuts ul').append('<li><a href="' + brand.link + '">' + brand.title + '</a></li>')
	    });
		
		var copy = new RegExp('\u00a9','g');
	    var reg = new RegExp('\u00ae','g');
		var items = ['.sub_nav', '.brandShortcuts ul'];
		
		for (var i in items){
			if($(items[i]).length > 0){
			    var newBody = $(items[i]).html().replace(copy, '<span class="copy">&copy;</span>').replace(reg, '<span class="reg">&reg;</span>');
				$(items[i]).html(newBody);
			}
		}
	});
});


