/**
 * Master Scripts
 * Author: Thom Rimmer, Motionlab Marketing Ltd.
 * www.motionlab.co.uk
 */

//no idea why, but this only works if we put it up here.  Feel free to correct me if I'm wrong..
$(function(){
	/** ---------------------------------------
	 * Listings - swap roomset and swatch
	 */
	
	$('.specialTick').bind('click',function() { window.location.href = $(this).parent().attr('href') })
	 
	 $('.fab-details span').click(function(e){
					
		largeImage = $(this).closest('li').find('.focus-img');
			
						
		if($(this).attr('data-large') == largeImage.attr('src'))
		{
		} 
		else {
			
			// removes the on classes to make the switch appearance
			$(this).siblings().removeClass('on');
			$(this).addClass('on');
			
			var newLargeSrc = $(this).attr('data-large');
			largeImage.fadeOut(200, function() {
				var img = new Image();
				img.onload = function() {
					largeImage.attr('src', newLargeSrc);
					largeImage.fadeIn(200);	
				};
				img.src = newLargeSrc;
				
						
			});
			
		}
	});
});

(function($) {

	
		
	/** ---------------------------------------
	 * IE6 PNG fixes
	 */
	
	if(typeof DD_belatedPNG !== 'undefined') {
		DD_belatedPNG.fix('img, .png_bg');
	}
	
	/** ---------------------------------------
	 * Bubbling
	 */
	  
	 $('[data-bubble="to"]').bind('click', function() {
 		var _anchor = $(this).find('[data-bubble="from"]').eq(0);
 		var _location = _anchor.attr('href') || '#';
 		window.location = _location;
 		return false;
 	});
 	
 	$('[data-bubble="from"]').click(function() {
 		/* $(this).closest('[data-bubble="to"]').trigger('click'); */
 		return false;
 	});
	
	/** ---------------------------------------
	 * 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;
	};
	
	

	
	/** ---------------------------------------
	 * 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);
	});

	
	/** ---------------------------------------
	 * Brands Carousel
	 */
	
	if ( $('.brands-carousel .carousel').length > 0 ) {
		
    	$(".brands-carousel .carousel").carouFredSel({
			direction: "left",
			circular: true,
			scroll : {
				items: 1,
				duration: 200
			},
			prev : {
				button: ".prev"
			},
			next : {
				button: ".next"
			},
			auto : {
				play: false
			}
		});
	
	}
	
	if ( $('.listings-list ul li .outer-cont').length > 0 ) {
	
		$(".listings-list ul li .outer-cont").hover(
			function () {
			$(this).css({'z-index' : '200'});
		},
			function () {
			//$('.listings-list ul li .outer-cont').css({'z-index' : '1'});
			$(this).delay(500).css({'z-index' : '1'});
		});
	}
	
	
	
	/** ---------------------------------------
	 * Listings - form styling
	 */
	 
	 /* custom select lists */
	if ( $('.pretty').length > 0 ) {
		$('.pretty').dropkick();
	}
	
	/* prod type */
	if ( $('.prod-type ul').length > 0 ) {
		$(".prod-type ul").carouFredSel({
			direction: "up",
			circular: true,
			scroll : {
				items: 1,
				duration: 200
			},
			prev : {
				button: ".prod-prev"
			},
			next : {
				button: ".prod-next"
			},
			auto : {
				play: false
			}
		});
	}
	 
	 
	 
	
	/* custom select lists */
	if ( $('.pretty').length > 0 ) {
		$('.pretty').dropkick();
	}
	
	/* fab type */
	if ( $('.fab-type ul').length > 0 ) {
		$(".fab-type ul").carouFredSel({
			direction: "up",
			circular: true,
			scroll : {
				items: 1,
				duration: 200
			},
			prev : {
				button: ".fab-prev"
			},
			next : {
				button: ".fab-next"
			},
			auto : {
				play: false
			}
		});
	}
	
	
	/* collections */
	if ( $('.collections ul').length > 0 ) {
		$(".collections ul").carouFredSel({
			direction: "up",
			circular: true,
			scroll : {
				items: 1,
				duration: 200
			},
			prev : {
				button: ".collections-prev"
			},
			next : {
				button: ".collections-next"
			},
			auto : {
				play: false
			}
		});
	}
	
	
	/* coffers */
	if ( $('.offer ul').length > 0 ) {
		$(".offer ul").carouFredSel({
			direction: "up",
			circular: true,
			scroll : {
				items: 1,
				duration: 200
			},
			prev : {
				button: ".offer-prev"
			},
			next : {
				button: ".offer-next"
			},
			auto : {
				play: false
			}
		});
	}
	
	
	
	/** ---------------------------------------
	 * New Home Tabs
	 */
	
	/*$(function() {
	 $(".tabs ul").tabs(".panes ul li", {
			effect: 'fade',
			fadeInSpeed: 800,
			fadeOutSpeed: 800,
			rotate: true,
			clickable: false
	
	 }).slideshow({
		 	interval: 4000,
        	autoplay: true,
			clickable: false
		 });
	 });*/
	 
	 if ( $('.panes ul').length > 0 ) 
	 {
	 	if($('.panes ul li').length > 1)
		{
			$(function() {
				$('.panes ul').cycle({ 
					fx:      'fade', 
					speed:    800, 
					timeout:  6000 
				});
			});
		}
		else
		{
			$('.panes ul li:eq(0)').show();	
		}
	}
	
	/** ---------------------------------------
	 * Hero cycling
	 */
	/*pagerAnchorBuilder: function(i) {
			return '.co-navigation li a:eq('+i+')';
		}*/
	
	$(window).load(function () {
  
	//make sure images are loaded first
	
	/*if ( $('.hero-container.landscape').length > 0 ) {
	
	$(function() {
	$('.hero-container.landscape').before('<ul class="hero-tabs">').cycle({ 
		fx:     'fade', 
		speed:  200, 
		timeout: 6000, 
		pager:  '.hero-tabs', 
     
		// callback fn to create tabs on the side
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#"><img src="' + slide.src + '" width="136" height="136" /><span class="mask"></span></a></li>'; 
		} 
	});
	});
	
	}*/
	
	if ( $('.hero-container.landscape').length > 0 ) {
	
	
	$('.hero-container.landscape').cycle({ 
		fx:     'fade', 
		speed:  200, 
		timeout: 6000, 
		pager:  '.hero-tabs', 
     	pagerAnchorBuilder: function(i) {
			return '.hero-tabs li a:eq('+i+')';
		}
	});
	
	
	}
	
	/*portrait*/
	/*if ( $('.hero-container.portrait').length > 0 ) {
	$(function() {
	$('.hero-container.portrait').before('<ul class="hero-tabs">').cycle({ 
		fx:     'fade', 
		speed:  200, 
		timeout: 6000, 
		pager:  '.hero-tabs', 
     
		// callback fn to create tabs on the side
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#"><img src="' + slide.src + '" width="136" height="237" /><span class="mask"></span></a></li>'; 
		} 	});
	});
	
	}*/
	
	if ( $('.hero-container.portrait').length > 0 ) {
	
	$('.hero-container.portrait').cycle({ 
		fx:     'fade', 
		speed:  200, 
		timeout: 6000, 
		pager:  '.hero-tabs', 
     	pagerAnchorBuilder: function(i) {
			return '.hero-tabs li a:eq('+i+')';
		} 	
	
	});
	
	}
	
	
	});
	
	
	
	

$('.vid').fancybox({
		'titleShow'     : false,
		'transitionIn'  : 'elastic',
		'transitionOut' : 'elastic'
		

	});	
	
	/* fancybox video */
	$('.video').click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
						 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	})


	/** ---------------------------------------
	 * co-ordination story cycling
	 */

	if ( $('.co-ordination-cycle').length > 0 ) {
	
	$('.co-ordination-cycle .images ul').cycle({ 
		fx:     'scrollHorz', 
		speed:  500, 
		timeout: 0,
		prev: '.co-prev',
		next: '.co-next',
		pager: '.co-navigation',
		pagerAnchorBuilder: function(i) {
			return '<li><a href="#">'+(i+1)+'</a></li>';
		}
		
	});
	
	$('.co-ordination-cycle .left-col ul').cycle({ 
		fx:     'fade', 
		speed:  500, 
		timeout: 0,
		prev: '.co-prev',
		next: '.co-next',
		pager: '.co-navigation',
		pagerAnchorBuilder: function(i) {
			return '.co-navigation li a:eq('+i+')';
		}
	});
	
	}
	
	
	/** ---------------------------------------
	 * co-ordination story cycling
	 */
	 if ( $('.carousel .car-cont ul').length > 0 ) {
	$(".carousel .car-cont ul").carouFredSel({
  		items: 4,
  		scroll: {
  			items: 1
  		},
  		prev: {
  			button: '.fc-prev'
  		},
  		next: {
  			button: '.fc-next'
  		}
  		
  	});
	}
	
})(this.jQuery);


