var TTC = {};

(function ($) {

	TTC.behaviours = function(){
		
		if ($.browser.msie && $.browser.version.substr(0,1) < 8) {
			$('#zone-branding').css({
				position: 	'relative',
				zIndex:		99
			});
			$('#section-content').css({
				position: 	'relative'
			});
			
			$('.social-media-feed > li').addClass('no-bubble').append('<b></b>');
		}
		if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
			/*
			$('#main-menu, #secondary-menu').find('li')
			.hover(
				function(){ $(this).addClass('hover'); },
				function(){ $(this).removeClass('hover'); }
			);
			*/
		}
		
		$('.menu li').hover(function(){
			$(this).addClass('hover');
			$('#zone-branding').css({
				zIndex:		99
			});
		}, function(){
			$(this).removeClass('hover');
		});
		
		var BrandRegion = $('#region-user-first'),
			BrandRegionInner = $('#region-user-first > .region-user-first-inner'),
			BrandExpand = $('a#our-brands-expander'),
			BrandAnim = function(show, autohide) {
				var duration = autohide ? 0 : 200,
				params = { duration: duration };
				if (show) {
					BrandRegionInner.animate({ height: '470px' }, params); // 487
				}
				else {
					BrandRegionInner.animate({ height: '0px' }, params);
				}
			},
			BrandDisplay = function(autohide) {
				autohide = !!autohide;
				if (autohide == true || BrandRegion.hasClass('toggled')) {
					BrandRegion.removeClass('toggled');
					BrandExpand.removeClass('toggled');
					BrandAnim(false, autohide);
				}
				else {
					BrandRegion.addClass('toggled');
					BrandExpand.addClass('toggled');
					BrandAnim(true, autohide);
				}
			};
		BrandDisplay(true);
		BrandExpand.click( function(e){
			e.preventDefault();
			BrandDisplay();
		});
		
	};
	
	TTC.brandFilter = function() {
		
		var styleLinks = $('.view-locations.view-display-id-grid_filter .view-content a'), 
		locationLinks = $('.view-holiday-styles.view-display-id-grid_filter .view-content a'),
		brandFilterUtil = {
			hasClass: function(text, classes) {
				return classes.indexOf(text) >= 0;
			},
			checkSlug: function(elem, classes) {
				var slug = brandFilterUtil.convertToSlug($(elem).text());
				if(brandFilterUtil.hasClass(slug, classes)) {
					return true;
				}
				return false;
			},
			convertToSlug: function (Text) {
				return Text.replace(/[^\w ]+/g,'').replace(/ +/g,'-');
			}
		},
		testLinks = function(links, classes) {
			var result = false;
			if (links.filter('.checked').length > 0) {
				result = false;
				links.filter('.checked').each(function() {
					result = (!!result) ? result : brandFilterUtil.checkSlug(this, classes);
				});
			}
			else {
				result = false;
			}
			return result;
		},
		testVisible = function (theimage) {
			var classes = theimage.attr('class');
			classes = classes.replace(/-{2,}/g, ' ');
			
			//hidden by default... shown when at least one class matches
			var visible     = false, 
			anySet			= (styleLinks.filter('.checked').length > 0 || locationLinks.filter('.checked').length > 0),
			bothSet			= (styleLinks.filter('.checked').length > 0 && locationLinks.filter('.checked').length > 0),
			matchesLocation = (locationLinks.filter('.checked').length > 0),  // 
			matchesStyle    = (styleLinks.filter('.checked').length > 0);  // 
			
			if (locationLinks.filter('.checked').length > 0) {
				matchesLocation = testLinks(locationLinks, classes);
			}
			
			if (styleLinks.filter('.checked').length > 0) {
				matchesStyle = testLinks(styleLinks, classes);
			}
			
			visible = matchesLocation || matchesStyle;
			if (bothSet) {
				visible = matchesLocation && matchesStyle;
			}
			
			return anySet && visible;
		},
		loopGridFilter = function() {
			$('.view-ttc-brands.view-display-id-grid .views-field-field-image .field-content').each(function() {
				var visible = testVisible($(this)),
				fade = visible ? 1 : 0;
				if (visible) {
					$(this).addClass('toggled');
				}
				else {
					$(this).removeClass('toggled');
				}
				$(this).fadeTo('normal',fade);
			});
		},
		triggerGridFilter = function(e) {
			e.preventDefault();
			$(this).toggleClass('checked');			
			loopGridFilter();
		};
		
		$('.view-ttc-brands.view-display-id-grid .views-field-field-image .field-content a').hover(function(){
			if (!$(this).parent().hasClass('toggled')) {
				$(this).parent().fadeTo(0, 1); // 'normal'
			}
		}, function(){
			if (!$(this).parent().hasClass('toggled')) {
				$(this).parent().fadeTo(0, 0); // 'normal'
			}
		});
		
		styleLinks.click(triggerGridFilter);
		locationLinks.click(triggerGridFilter);
		loopGridFilter();
	};
	
	TTC.placeholders = function(){
	
		// http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		$('[placeholder]').parents('form').submit(function() {
		  $(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
			  input.val('');
			}
		  })
		});
	
	};
	
	TTC.mediaSlideshow = function() {
		var slideshowIDs = {};
		
		var clearPause = function(cycle) {
			$(cycle).removeClass('force-pause').unbind('mouseOut', checkMouseOut);
			Drupal.viewsSlideshow.action({
				action: 'play',
				slideshowID: slideshowIDs[cycle.id]
			});
		},
		checkMouseOut = function(cycle) {
			var c = $(cycle);
			c.mouseout(function(){
				if (c.hasClass('force-pause')) {
					var slideshowID = slideshowIDs[this.id];
					Drupal.viewsSlideshow.action({
						action: 'pause',
						slideshowID: slideshowID
					});
				}
			});
			c.find('.views-slideshow-pager-field-item').click(function(){
				clearPause(cycle);
			}).mouseover(function(){
				clearPause(cycle);
			});
		};
		
		$('.view-media-cycle .views_slideshow_slide').each(function(){
			var cycle = $(this).parents('.views_slideshow_cycle_main').get(0),
			cycleID = cycle.id,
			slideshowID = null,
			media = $(this).find('object, embed, video, audio, iframe'),
			contains = media.length > 0;
			
			if (slideshowIDs[cycleID]) {
				slideshowID = slideshowIDs[cycleID];
			}
			else {
				slideshowID = Drupal.settings.viewsSlideshowCycle['#'+cycleID].slideshowId;
				slideshowIDs[cycleID] = slideshowID;
			}
			
			if (contains) {
				var checkOutCycle = function(){
					var cycle = $(this).parents('.views_slideshow_cycle_main').get(0),
					c = $(cycle);
					if (!c.hasClass('force-pause')) {
						c.addClass('force-pause');
						checkMouseOut(cycle);
					}
				};
				media.mouseover(checkOutCycle);
				$(this).mousedown(checkOutCycle);
			}
		});
	};
	
	TTC.mediaRoom = function() {
		var view = $('.view-media.view-display-id-media_room'),
		re = new RegExp(/(https?:\/\/[^\/]+)/),
		currentDomain = re.exec(window.location.href)[1];
		if (view.length > 0) {
			var populateLinks = function() {
				view.find('a').each(function(){
					if (this.href.indexOf('/') === 0) return;
					
					if (this.href.indexOf(currentDomain) != 0) {
						$(this).attr('target', '_blank');
					}
					else if (!!$(this).attr('target')) {
						$(this).attr('target', null);
					}
				});
			};
			populateLinks();
		}
	};
	
	TTC.frontPage = function() {
		if ($('body').hasClass('front')) {
			var hpCycle = '.view-display-id-homepage_media_cycle',
			pagerItems = $('.views-slideshow-pager-field-item', hpCycle),
			margin = 5,
			fullWidth = $('.views-slideshow-controls-bottom', hpCycle).width(),
			combinedWidth = 0,
			newCombinedWidth = 0;
			
			pagerItems.each(function(){
				combinedWidth += $(this).width() + margin*2;
			});
			
			var remainder = fullWidth - combinedWidth,
			addRem = Math.floor(remainder / pagerItems.length);
			
			pagerItems.each(function(i){
				var width = $(this).width(),
				newWidth = width + Math.floor(addRem);
				newCombinedWidth += (newWidth + margin*2);
				
				if (i == pagerItems.length - 1 && newCombinedWidth < fullWidth) {
					newWidth += (fullWidth - newCombinedWidth);
				}
				
				$(this).css({
					width: newWidth + 'px'
				});
			});
		}
	};
	
	$(document).ready(function(){
		TTC.behaviours();
		TTC.placeholders();
		TTC.brandFilter();
		TTC.mediaSlideshow();
		TTC.mediaRoom();
		TTC.frontPage();
	});

}(jQuery));;

