var sw = sw || {};

(function (self, $) {

  self.init = function ()
  {

	$('li.current a').click(__openDropdown);
	$('#dropdown, body').mouseleave(__closeDropdown);
	$('li.cart a').hover(__closeDropdown);

	$('li.current a').click(function(e){ e.preventDefault(); });

	$('.column .preview').hover(function (e) {
	  $(this).prev().find('a').css('color', '#b2bb1c');
	}, function (e) {
	  $(this).prev().find('a').css('color', '#fff');
	});

	$('.column h3 a').hover(function (e) {
	  $(this).parent().next().addClass('hover');
	}, function (e) {
	  $(this).parent().next().removeClass('hover');
	});

	$('#wrapper-discover .column a').click(function(e) {
		setTimeout(__closeDropdown, 200);
	});

	$('.categories li.back a').click(__hideWidget);

	$('a.widget-product, a.widget-activity').click(__showWidget);
	$('.widget ul.widgetTabs li a').click(__changeWidgetType);
	$('.widget .categories a').click(__changeWidgetCategory);

	$('#header .extra').hide();

	$.get('/is_logged_in.php', function(data){

	   $('#header .extra').html(data).fadeIn(300);

		$('#header .login').click(function(e){

			if ($.browser.msie && $.browser.version < 9) {
				$('.login-modal-overlay').fadeTo(200,'0.8');
				$('.login-modal').fadeTo(500,'1', function(){
					this.style.removeAttribute('filter');
				});
				e.preventDefault();
			} else {
				$('.login-modal-overlay').fadeTo(500,'0.8');
				$('.login-modal').fadeTo(500,'1');
				e.preventDefault();
			}
		});

		$('.close-login-modal').click(function(){
			$('.login-modal, .login-modal-overlay').fadeOut();
			//e.preventDefault();
		});

	});


	if ($.browser.msie && $.browser.version < 8) {
	  $('ul.messages').css({'padding-top':'0'});
	};
	$('.messages').appendTo('#header .top').delay(1000).fadeIn(666, function(){
		if ($.browser.msie && $.browser.version < 9) {
		  this.style.removeAttribute('filter');
		};
		$(this).delay(7500).fadeOut(666);
	});
	
	$('#wrapper-discover a').click(function(e){ s.prop5 = 'top nav'; });
	$('.horList a').click(function(e){ s.prop5 = 'page nav'; });
	$('.leftNavList a').click(function(e){ s.prop5 = 'side nav'; });
	
	
    s.section = "discover";
    var om_titles = ['made for eachother', 'poc', 'mtn smarts', 'proof', 'tkr', 'chatter'];
        
    var om_section = [];
    om_section[0] = [];
    om_section[1] = ['the-epiphany', 'comfort-solved', 'your-body-in-wool'];
    om_section[2] = ['our-mountains', 'why-merino-wool', 'why-smartwool', 'layer-up'];
    om_section[3] = ['thermal-midlayer', 'next-to-skin', 'ski-socks', 'accessories'];
    om_section[4] = ['our-values', 'supply-chain', 'advocacy', 'our-communities'];
    om_section[5] = ['facebook', 'twitter', 'blogs', 'handwritten'];
    
    var domBody = getDomBody();
    var last_section = '';
    var last_vert = 0;
    var section_timeout;
    
    $(domBody).bind('scroll mousedown DOMMouseScroll mousewheel', function(){
        
        var _index = getCurHorizIndex();
        var _scrollTop = domBody.scrollTop();
        var _current = '';
        
        var l = om_section[_index].length;        
        for(k=0; k<l; k++){
            if(parseInt($('#'+om_section[_index][k]).css('top'), 10) < _scrollTop-430){
                _current = om_section[_index][k];
            } else { 
                break;
            }
        }
        
        // have they gone to a new vertical, or section?
        if(_current != last_section){
            
            last_section = _current;
            clearTimeout(section_timeout);
            section_timeout = setTimeout(function(){
                s.prop1 = 'discover: ' + om_titles[_index];
                s.prop2 = 'discover: ' + om_titles[_index] + ': ' + _current.replace(/\-/g, ' ');
                s.pageName = s.prop2;
                s.t(true, 'o', 'Discover link');
                __reset_scode();
            }, 3000);
            
        } else if(_index == 3 && last_vert != _index){
        
            last_vert = _index
            clearTimeout(section_timeout);
            section_timeout = setTimeout(function(){
                s.prop1 = 'discover: ' + om_titles[_index];
                s.prop2 = 'discover: ' + om_titles[_index] + ': ' + _current.replace(/\-/g, ' ');
                s.pageName = s.prop2;
                s.t(true, 'o', 'Discover link');
                __reset_scode();
            }, 3000);
            
        }
    
    });

  };
  
  // there will be others.
  var __reset_scode = function(){
    s.prop5 = '';
    s.events = '';
  };
  
  // a multitude of hot links around the discover site.
  $('.eachHotLink, .eachBWLink').live('click', function(){
                            
                            // hotLinks come in a few varieties...
                            if($(this).hasClass('eachTile')){
                                var hotlink_title = $(this).attr('data-patch').substring(4).toLowerCase();
                            } else if($(this).hasClass('bookCoverLink')) {
                                var hotlink_title = 'wool 101 book';
                            } else {
                                var hotlink_title = $(this).html().toLowerCase();
                            }
                            
                            s.pageName = s.prop2 + ': ' + hotlink_title;
                            s.t(true, 'o', 'Hot Link');
                            __reset_scode(); });
                            
  // steps for TKR/Supply cHain
  $('#stepsTab .tabLink').live('click', function(){
                            s.pageName = s.prop2 + ': ' + $(this).attr('data-target');
                            s.t(true, 'o', 'Step Tab Link');
                            __reset_scode(); });
                            
  // links to Proof gallery products
  $('#Proof .imgCon').live('click', function(){
                            var product_title = $('#'+$(this).attr('data-target')).find('h2').html().toLowerCase();
                            s.prop3 = s.prop2 + ': ' + product_title;
                            s.pageName = s.prop3;
                            s.t(true, 'o', 'Featured Product Link');
                            __reset_scode(); });

  // links to Proof galleries                    
  $('#proofText a, #proofMarkers a').live('click', function(){
                            var dest = $(this).attr('href').substring(7).replace(/\-/g, ' ');
                            s.prop2 = s.prop1 + ': ' + dest;
                            s.pageName = s.prop2;
                            s.t(true, 'o', 'Featured Product Link');
                            __reset_scode(); });
                            
  // prev/next for Comfort Solved.
  $('.bigArrows prev, .bigArrows.next').live('click', function(){
                                                        setTimeout(function(){
                                                                    s.prop3 = s.prop2 + ': ' + $('.currentBox').find('h2').html().toLowerCase();
                                                                    s.pageName = s.prop3;
                                                                    s.t(true, 'o', 'Prev/Next Link');
                                                                    }, 250);
                                                    });
                                                    
  // prev/next for Your Body in Wool.
  $('.bodyWoolList .textR a').live('click', function(){
                                                        setTimeout(function(){
                                                                    s.prop3 = s.prop2 + ': ' + $('.otagoList .current').html().toLowerCase();
                                                                    s.pageName = s.prop3;
                                                                    s.t(true, 'o', 'Prev/Next Link');
                                                                    }, 250);
                                                    
                                                    });
                                                    
  // why SmartWool prev/next
  $('.nextPlease a').live('click', function(){
                                                    setTimeout(function(){
                                                        s.prop3 = s.prop2 + ': ' + $('.currentBox').find('h2').html().toLowerCase();
                                                        s.pageName = s.prop3;
                                                        s.t(true, 'o', 'Prev/Next Link');
                                                    }, 250);
                                            });
                                                    
  // featured product prev/next.
  $('#level2Gallery .arrowBtn').live('click', function(){
                                                    setTimeout(function(){
                                                        s.prop3 = s.prop2 + ': ' + $('.activeProduct').find('h2').html().toLowerCase();
                                                        s.pageName = s.prop3;
                                                        s.t(true, 'o', 'Prev/Next Link');
                                                    }, 250);
                                        });
                                                    
  // supply chain prev/next.
  $('.stepArrows a').live('click', function(){
                                                    setTimeout(function(){
                                                        s.prop3 = s.prop2 + ': ' + $('.activeSlide').find('.stepTitleTop').html().toLowerCase();
                                                        s.pageName = s.prop3;
                                                        s.t(true, 'o', 'Prev/Next Link');
                                                    }, 250);
                                                    
                                            });
  
  var full_scrolled = [];                          
  
  self.push_fully_scrolled = function(){
    
    if($.inArray(s.pageName, full_scrolled) > -1)
        return;
        
    s.events = "event20";
    s.t(true, 'o', 'Full Scroll');
    __reset_scode();
    full_scrolled.push(s.pageName);
    
  }
                            
  // Ennnnnd Omniture fun.

  // Add CSS transforms if possible
  var __openDropdown = function (e)
  {
	$('#dropdown').stop().animate({top:'0px'}, 'fast', 'easeInOutQuad');
	$('li.current a').css({'height':'76px'});

	$('.droplet_wrapper.open').removeClass('open');
	$('#open_droplet').hide().removeAttr('id');

	$(this).parent().siblings().removeClass('hover');
	$(this).parent().addClass('hover');

	var navClass = $('li.current.hover').attr('data-target');
	if ($.browser.msie && $.browser.version < 9) {
		$('#'+navClass).show().siblings().hide();
	} else {
		$('#'+navClass).fadeIn('fast').siblings().fadeOut('fast');
	};
  };

  var __closeDropdown = function (e)
  {
	$('#dropdown').stop().animate({top:'-480px'}, 'fast', 'easeInOutQuad', function(){
		$('li.current a').css({'height':'35px'});
	});
	$('li.current.hover').removeClass('hover');

  };

  var __hideWidget = function (e)
  {
	  $(this).parents('.widget').hide();
  };

  var __showWidget = function (e)
  {
	  e.preventDefault();

	  var parentType;
	  var widgetType = ($(this).hasClass('widget-activity')) ? 'activity' : 'product';

	  $('.widget, .widget .options, .widget ul.widget-product, .widget ul.widget-activity').hide();
	  $('.widget .categories a.active, .widget ul.widgetTabs li.active').removeClass('active');

	  if ($(this).hasClass('widget-mens'))
	  {
		$('.widget, .widget div.widget-mens').show();
		$('.widget .categories .widget-mens a').addClass('active');
		parentType = 'mens';
	  }
	  else if ($(this).hasClass('widget-womens'))
	  {
		$('.widget, .widget div.widget-womens').show();
		$('.widget .categories .widget-womens a').addClass('active');
		parentType = 'womens';
	  }
	  else if ($(this).hasClass('widget-kids'))
	  {
		$('.widget, .widget div.widget-kids').show();
		$('.widget .categories .widget-kids a').addClass('active');
		parentType = 'kids';
	  }

	  $('.widget .widget-' + parentType + ' ul.types.widget-'+ widgetType).show();
	  $('.widget .widget-' + parentType + ' ul.widgetTabs li.'+ widgetType).addClass('active');
  };

  var __changeWidgetType = function (e)
  {
	  e.preventDefault();

	  // Change content
	  $('.widget ul.types').hide();
	  var widgetType = $(this).parent().attr('class').replace(/active/i, '');

	  $(this).parents('.options').find('.widget-'+ widgetType).show();

	  // Change tab on the side
	  var current = $(this).parent().addClass('active');
	  (current.prev().size() > 0) ? current.prev().removeClass('active') : current.next().removeClass('active');
  };

  var __changeWidgetCategory = function (e)
  {
	e.preventDefault();

	$('.widget .categories a.active, .widget .widgetTabs li.active').removeClass('active');
	$('.widget .options, .widget .types').hide();

	var widgetCategory = $(this).addClass('active').parent().attr('class').substr(7);

	$('.widget .options.widget-' + widgetCategory + ', .widget .types.widget-product').show();
	$('.widget .options.widget-' + widgetCategory + ' .widgetTabs li.product').addClass('active');
  };

})(sw.core = {}, jQuery);


$(sw.core.init);

