/**
 * Master Scripts
 * Author: Motionlab Marketing Ltd.
 * www.motionlab.co.uk
 */

(function($) {
	
	/** ---------------------------------------
	 * Extra head.js feature detection
	 */
	
	head.feature('placeholder', function() {
		var t = document.createElement('textarea'); // #=> <textarea></textarea>
		return (t.placeholder !== undefined); // #=> true || false
	});
	
	/** ---------------------------------------
	 * IE6 PNG fixes
	 */
	
	if(typeof DD_belatedPNG !== 'undefined') {
		DD_belatedPNG.fix('img, .png_bg');
	}
	
	/** ---------------------------------------
	 * Custom universal easing
	 */
	
	$.easing.custom = function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	};
	
	$.easing.easeInExpo = function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	};
	
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	
	/** ---------------------------------------
	 * IE6 upgrade message
	 */
	
	if(($.browser.msie && $.browser.version == '6.0')) {
		$('body').prepend("<div style='border: 1px solid #F7941D; background: #FEEFDA; text-align: center; clear: both; height: 75px; position: relative;'><div style='position: absolute; right: 3px; top: 3px; font-family: courier new; font-weight: bold;'><a href='#' onclick='javascript:this.parentNode.parentNode.style.display=\"none\"; return false;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-cornerx.jpg' style='border: none;' alt='Close this notice'/></a></div>    <div style='width: 640px; margin: 0 auto; text-align: left; padding: 0; overflow: hidden; color: black;'>      <div style='width: 75px; float: left;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-warning.jpg' alt='Warning!'/></div>      <div style='width: 275px; float: left; font-family: Arial, sans-serif;'>        <div style='font-size: 14px; font-weight: bold; margin-top: 12px;'>You are using an outdated browser</div>        <div style='font-size: 12px; margin-top: 6px; line-height: 12px;'>For a better experience using this site, please upgrade to a modern web browser.</div>      </div>      <div style='width: 75px; float: left;'><a href='http://www.firefox.com' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-firefox.jpg' style='border: none;' alt='Get Firefox 3.5'/></a></div>      <div style='width: 75px; float: left;'><a href='http://www.browserforthebetter.com/download.html' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-ie8.jpg' style='border: none;' alt='Get Internet Explorer 8'/></a></div>      <div style='width: 73px; float: left;'><a href='http://www.apple.com/safari/download/' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-safari.jpg' style='border: none;' alt='Get Safari 4'/></a></div><div style='float: left;'><a href='http://www.google.com/chrome' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-chrome.jpg' style='border: none;' alt='Get Google Chrome'/></a></div></div></div>");
	}
	
	/** ---------------------------------------
	 * Reverse stacking
	 */

	$('.reverse-stack').each(function() {
		var childLength = $(this).children().length;
		$(this).children().each(function() {	
			$(this).css('zIndex', childLength);
			childLength--;
		});
	});
	
	/** ---------------------------------------
	 * Full height elements
	 */
	
	$('.full-height').each(function() {
		$(this).height($(this).parent().height());
	});
	
	/** ---------------------------------------
	 * Vertically centered elements
	 */
	
	$('.vertical-center').each(function() {
		var mid = $(this).children('.mid').eq(0);
		var val = ((mid.parent().height() - mid.height()) / 2);
		mid.css('top', val + 'px');
	});
	
	/** ---------------------------------------
	 * Horizontal masking (for carousels, tickers, etc.)
	 */
	
	$('.horz-mask').each(function() {
		var self = $(this);
		var height = self.height();
		var width = self.width();
		self.find('.horz-mask-inner > *').css('width', width);
	});
	
	/** ---------------------------------------
	 * Placeholder fallback
	 */
	
	if(!head.placeholder) {
		$('[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().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			});
		});
	}
	
	/** ---------------------------------------
	 * Overlays 
	 */
	 
	/** Overlay triggers **/
	
	$('.overlay-trigger').click(function() {	
		$('#' + $(this).attr('data-for')).addClass('open').animate({
			top: 0
		}, {
			duration: 500,
			easing: 'easeOutExpo'
		});
		return false;
	});
	
	$('.overlay .cancel').click(function() {
		overlaySelf = $(this).closest('.overlay');
		close_overlay(null);
	});
	
	var overlaySelf;
	
	function close_overlay(e) {
		overlaySelf.animate({
			top: -500
		}, {
			duration: 500,
			complete: function() {
				$(this).removeClass('open');
			}
		});
	}
	
	function close_overlay_listener(e) {
		overlaySelf = e.data.overlay;
		close_overlay();
	}
	 
	/** Create and fade in a background if an overlay is to be used **/
	
	$('.overlay.open').livequery(function() {
		$('body').prepend('<div class="overlay-bg"></div>');
		var bg = $('.overlay-bg');
		bg.bind('click', {overlay: $(this)}, close_overlay);
		bg.fadeIn(500, function() {
			bg.addClass('open');
		});
		
	}, function() {
		var bg = $('.overlay-bg');
		bg.unbind('click', close_overlay);
		bg.fadeOut(500, function() {
			bg.removeClass('open');
			bg.remove();
		});
	});
	
	
	
	/** ---------------------------------------
	 * MSL - Vertical drop-down nav
	 */
	
	var vertNav = $('.vert');
	var disableMenu = false;
	if(vertNav.length) {
		vertNav.each(function() {
			
			$(this).find('> ul > li:not(.current) a').click(function(e) {
				if(disableMenu) {
					return;
				}
				
				
				
				var self = $(this);

				
				if(self.next('ul').length) {
				e.preventDefault();
					self.next('ul').slideDown(600, 'easeOutExpo');
					if(typeof self.attr('href') !== 'undefined') {
						setTimeout(function() {
							window.location = self.attr('href');
						}, 800);
					}
				}
			});
	
			
		});
	}
	
	/** ---------------------------------------
	 * MSL - Product image zoom
	 */
	 
	var productImages = $('.product-images a');
	if(productImages.length) {
		productImages.joeZoom();
	}
	
	/** ---------------------------------------
	 * MSL - News Ticker
	 */
	
	var newsTicker = $('.news-ticker');
	 
	if(newsTicker.length) {
		
		newsTicker.each(function() {
			
			var self = $(this),
				currentLi = self.find('.current');
				
			if(currentLi.not('.visible')) {
				currentLi.fadeIn();
			}
			
			(function tickNews() {
				setTimeout(function() {
					currentLi = self.find('.current');
					currentLi.removeClass('current');
					currentLi.fadeOut(function() {
						var nextLi = currentLi.next();
						if(!nextLi.length) {
							nextLi = self.find('li:first');
						}
						nextLi.fadeIn(function() {
							nextLi.addClass('current');
							tickNews();
						});
					});		
				}, 4000);
			})();
			
		});
	}
	
	/** ---------------------------------------
	 * MSL - Spotlights
	 */
	 
	var spotlights = $('.spotlight');
	if(spotlights.length) {
		spotlights.joeSpotlight();
	}	
	
	/** ---------------------------------------
	 * Tooltip
	 */
	
	$('.tooltip').each(function() {
		var self = $(this);
		self.data('title', self.attr('data-title'));
		self.prepend('<span class="detail">' + self.data('title') + '</span>');
		self.hover(function() {	
			self.find('.detail').stop().fadeIn(200);
		}, function() {
			self.find('.detail').stop().fadeOut(200);
		});
	});
	
	/** ---------------------------------------
	 * Google map
	 */
	 
	var map = $('.googleMap');
	if(map.length) {
		map.hover(function() {
			map.find('.mask').stop().fadeOut(500, 'easeOutExpo');
		}, function() {
			map.find('.mask').stop().fadeIn(500, 'easeOutExpo');
		});
	}
	
	/** ---------------------------------------
	 * Fixed layouts
	 */

	var fixeds = $('.fix');
	var scrollTimeout;
	var ready = false;
	if(fixeds.length) {
		
		fixeds.each(function(i) {
			
			$(this).wrapInner('<div class="fix-inner"></div>');
			
			$(this).css({
				width: $(this).width(),
				height: $(this).height()
			});
			
			$(this).find('.fix-inner').css({
				width: $(this).width(),
				height: $(this).height()
			})
			.css('position', 'absolute')
			.data('offset', $(this).offset().top);

		});
		
		var scrollIn = function() {
			var _window = $(window);
			fixeds.find('.fix-inner').each(function() {
				
				var __top = parseInt($(this).data('offset') + _window.scrollTop());
				
				if($(this).closest('.bounds').length) {
					var __bottomOffset = __top + $(this).height();
					var __boundsBottom = $(this).closest('.bounds').offset().top + $(this).closest('.bounds').height();
					if(__bottomOffset > __boundsBottom) {
						__top = parseInt(__boundsBottom - $(this).height());
						$('#header .logo img').fadeOut();
						disableMenu = true;
					}
					else {
						$('#header .logo img').fadeIn();
						disableMenu = false;
					}
				}
				else {
					$('#header .logo img').fadeIn();
					disableMenu = false;
				}
				
				$(this).stop().animate({
					top: __top
				}, {
					duration: 500,
					easing: 'easeOutExpo'
				});
			});
		};
		
		$(window).scroll(function() {
			if(ready) {
				return false;
			}
			ready = true;
			scrollTimeout = setTimeout(function() {
				ready = false;
				scrollIn();
			}, 200);
		});
		
		if($(window).scrollTop() !== 0) {
			scrollIn();
		}

	}
	
	/** ---------------------------------------
	 * Subtle header border effect.
	 */
	
	var headerBorder = $('#header .border span');
	var checkScroll = function() {
		if($(window).scrollTop() >= 100) {
			headerBorder.addClass('shadow');
		}
		else {
			headerBorder.removeClass('shadow');
		}
	};
	$(window).scroll(checkScroll);
	checkScroll();
	
	/** ---------------------------------------
	 * News section
	 */
	 
	var newsLists = $('.articles');
	if(newsLists.length) {
		newsLists.joeNews();
	} 
	
	/** ---------------------------------------
	 * Collapsible block items
	 */
	 
	var blockItems = $('.block-item');
	if(blockItems.length) {
		blockItems.each(function() {
			var item = $(this),
				detail = item.find('.detail');
			if(detail.length) {
				detail.data('fullHeight', detail.outerHeight());
				detail.height(0);
				$(this).find('.title').click(function() {
					if(!detail.height()) {
						item.addClass('open');
						detail.animate({
							height: detail.data('fullHeight')
						}, {
							duration: 500,
							easing: 'easeOutExpo'
						});	
					}
					else {
						item.removeClass('open');
						detail.animate({
							height: 0
						}, {
							duration: 500,
							easing: 'easeOutExpo'
						});
					}
				});
			}
		});
	}
	
	/** ---------------------------------------
	 * Product faves
	 */
	 
	$('#header .faves').hoverIntent({
		timeout: 1000,
		over: function() {
			$(this).find('ul:first').slideToggle(500, 'custom');
		},
		out: function() {
			$(this).find('ul:first').slideToggle(500, 'custom');
		}
	}).click(function() {
		//return false;
	});
	

	
	/** ---------------------------------------
	 * Product listings
	 */
	
	var productListings = $('.product-listings, .category-listings'),
		loaded,
		cancel;
		
	if(productListings.length && !$('body').hasClass('home-body')) {
		
		var fadeIn = function() {
			productListings.find('li img').each(function(i) {
				var self = $(this);
				setTimeout(function() {
					
					self.closest('a').css({
						width: self.closest('a').width(),
						height: self.closest('a').height()
					});
					
					self.stop().animate({opacity: 1}, {duration: 500});
					
					setTimeout(function() {
					
						self.closest('a').hover(function() {
							$(this).find('img').stop().animate({
								top: '7px'
							}, {
								duration: 200
							});
							$(this).stop().animate({
								top: '-4px'
							}, {
								duration: 200
							});
						}, function() {
							$(this).find('img').stop().animate({
								top: '0'
							}, {
								duration: 200
							});
							$(this).stop().animate({
								top: '0'
							}, {
								duration: 200
							});
						});
					
					}, 500);
					
				}, (100 * i));
				self.hover(function() {
					self.stop().animate({opacity: 1}, {duration: 500});
				});
			});	
		};
		
		
		
		$(window).load(function() {
			loaded = true;
			if(!cancel) {
				fadeIn();
			}
		});
		
		setTimeout(function() {
			if(!loaded) {
				fadeIn();
				cancel = true;
			}
		}, 1000);
		
	}
	else {
	
		$('.listings:not(.adjacent-listings)').each(function() {
		
			$(this).find('li a').hover(function() {
				$(this).find('img').stop().animate({
					top: '7px'
				}, {
					duration: 200
				});
				$(this).stop().animate({
					top: '-4px'
				}, {
					duration: 200
				});
			}, function() {
				$(this).find('img').stop().animate({
					top: '0'
				}, {
					duration: 200
				});
				$(this).stop().animate({
					top: '0'
				}, {
					duration: 200
				});
			});
			
		});
	
	}
	
	/** ---------------------------------------
	 * Social links
	 */
	
	if(!head.transitions) {
			$('.social-links a').each(function() {
				$(this).css({backgroundPosition: "0px 0px"});
			}).hover(function() {
				$(this).stop().animate({
					backgroundPosition: "0px -14px"
				}, {
					duration: 1000,
					easing: 'linear',
					queue: false
				});
			}, function() {
				$(this).stop().animate({
					backgroundPosition: "0px 0px"
				}, {
					duration: 1000,
					easing: 'linear',
					queue: false
				});
			}
		);
	}
	
	/** ---------------------------------------
	 * MAY ALSO LIKE
	 */
	
	var ul = $("#may_like_scroll");
	if(ul.length) {
		var ul_next = ul.prevAll('h2').find('.next');
		if(ul_next.length) {
			ul.scrollable({
				circular: true,
				items : '.horz-mask-inner'
			});
			var ul_api = ul.data('scrollable');
			ul_next.click(function(e) {
				e.preventDefault();
			});
		}
	}
	
	
})(this.jQuery);

