/**
 * Master Scripts
 * Author: Motionlab Marketing Ltd.
 * www.motionlab.co.uk
 */

(function($) {
	
	/** ---------------------------------------
	 * Modernizr
	 */
	
	Modernizr.addTest('textstroke', function() {
		var h1 = document.createElement('h1');
		if ( !( 'webkitTextStroke' in h1.style ) || !('textStroke' in h1.style) ) {
			return false;
		}
		else {
			return true;
		}
	});

	
	/** ---------------------------------------
	 * 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(!Modernizr.input.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('');
				}
			})
		});
	}
	
	/** ---------------------------------------
	 * Case Studies, DOM Injection.
	 */
	 /*
	 var case_studies = $('.case_studies_pres');
	 if(case_studies.length && !($.browser.msie && $.browser.version == '6.0')) {

	 	case_studies.find('h2 a').bind('click', function(e) {
	 		
	 		e.preventDefault();
	 		
	 		var __url = $(this).attr('href');
	 		
	 		var current = false;
	 		if($(this).parent('h2').hasClass('current')) {
	 			current = true;
	 		}
	 		
	 		if(__url) {
	 			
	 			$(case_studies).find('.accordion_content').slideUp(function() {
	 				$(this).remove();	
	 			});
	 			
	 			
	 			case_studies.find('h2').removeClass('current');
	 			
	 			
	 			if(current) {
	 				return;
	 			}
	 			
	 			var __html = '<div class="accordion_content" style="display: none"></div>';
	 			$(this).parent('h2').after(__html).addClass('current');
	
	 			var __accordion_content = $(this).parent('h2').next('.accordion_content');

	 			var href_no_hash = $(this).attr('href').substr(0,$(this).attr('href').indexOf('#'))
	 		
	 			__accordion_content.load(href_no_hash + ' #' + $(this).parent('h2').attr('id') + '_inner', function(data) {
	 				__accordion_content.find('ul').unwrap('<div class="accordion_content"></div>');
	 				__accordion_content.slideDown();
	 			});
	 			
	 		}
	 		
	 	});
	 
	 }
	 */
	 /** ---------------------------------------
	 * Effect bubbling fot hit areas
	 */
	 
	$('.bubble-to').live('click', function(e) {
 		var _anchor = $(this).find('.bubble-from').eq(0);
 		var _location = _anchor.attr('href') || '#';
 		window.location = _location;
	});
	
	$('.bubble-from').click(function() {
		return false;
	});

	
	/** ---------------------------------------
	 * Carousel-style slideshows
	 */
	
	$('.slide_container').livequery(function() {
		
		var slide_container = $(this);
		var step_nav = $(this).prev('.step_nav');
		var newDate = new Date();
		var id = newDate.getTime() + '_' + Math.floor(Math.random()*11);
		step_nav.attr('id', id);
		slide_container.scrollable({
			easing: 'custom',
			speed: 600
		}).navigator({
			navi: '#'+id,
			activeClass: 'current'
		});
		
		step_nav.find('a:first').trigger('click');
		
		var api = slide_container.data('scrollable');
		
		$(this).find('.primary_cta').click(function(e) {
			api.next();
			e.preventDefault();
		});
				
		var _height = $(this).find('.slide img').height();
		$(this).height(_height);
		$(this).find('.inner, .slide').height(_height);
		
	});
	
	$('.step_nav li.current').livequery(function() {
		$('.step_nav li.next').removeClass('next');
		$(this).next('li').addClass('next');
	});
	
	/** ---------------------------------------
	 * Installers --> toggle
	 */
	 
	$('[data-toggle]').each(function() {
		var link = $(this);
		if(link.data('toggle') === true) {
			var el = $(link.attr('href'));
			el.hide();
			link.click(function(e) {
				e.preventDefault();
				el.slideToggle(200);
			});
		}
	});
	
	/** ---------------------------------------
	 * Installers --> homepage spotlight
	 */
	
	var scrollableOptions = {
		touch: false,
		easing: 'easeOutExpo',
		duration: 600,
		items: '.horz-mask-inner'
	};
	
	if(Modernizr.touch) {
		scrollableOptions.touch = true;
	}
	
	$('.spotlight > div').scrollable(scrollableOptions);
	
	var spotlight = $('.spotlight > div').data('scrollable'),
		currentIndex = 0;
	
	$('.spotlight .pagination ol li a').click(function() {
		if($(this).hasClass('current')) return false;
		$('.spotlight .pagination ol li a').removeClass('current');
		$(this).addClass('current');
		var newIndex = $(this).parent().index();
		var currentItem = $('.spotlight ul li').eq(currentIndex);
		var newItem = $('.spotlight ul li').eq(newIndex);
		newItem.find('.right').css({
			left: -10,
			opacity: 0
		});
		currentItem.find('.right').animate({
			left: -100,
			opacity: 0
		}, {
			easing: 'easeOutExpo',
			duration: 200,
			complete: function() {
				spotlight.seekTo(newIndex, 600);
				setTimeout(function() {
					newItem.find('.right').animate({
						left: 0,
						opacity: 1
					}, {
						duration: 200
					});
				}, 600);
			}
		});
		currentIndex = newIndex;
		return false;
	});
	
	
	$('.openClose').bind('click', function() { 
		$number = $(this).attr('id').replace('case_study_shortcut_','');
	//	$('.accordion_content:not(#case_study_'+$number+'):is(.open)')
		$('.step_nav li.current').next('li').addClass('next');
		if($('#case_study_'+$number).is('.closed'))
		{
			$('.accordion_content:not(#case_study_'+$number+'):not(.closed)').slideUp(125);
			$('.accordion_content:not(#case_study_'+$number+'):not(.closed)').addClass('closed');
			$('h2.current').removeClass('current')
			$('#case_study_'+$number).slideDown(125);
			$('#case_study_'+$number).removeClass('closed');
			$('#case_study_shortcut_'+$number).addClass('current');
			
		}
		else
		{
			$('#case_study_'+$number).slideUp(125);
			$('#case_study_'+$number).addClass('closed');
			$('#case_study_shortcut_'+$number).removeClass('current');
		}
		return false; })
	
	
	
	/** ---------------------------------------
	 * Home hero double scroller using sync'd cycle
	 */
	 
	 if ( $.browser.msie ) {
		 
		 /* if browser is ie don't fade text in as produces flash of pixelated text first - Thom Rimmer - 28/april/11 */
	 
	 $('.spotlight .background .banners').cycle({ 
    	fx: 'scrollLeft',
		timeout: 9000,
		speed : 600,
		easing: 'easeOutExpo',
		before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
          $('.captions ul').cycle('next');
    }
		
	});

	
	$('.captions ul').cycle({ 
    	fx: 'none',
		speed : 1500,
		easing: 'easeOutExpo',
		timeout: 0
	});
	
	} else {
		
		/* not ie */
		
	$('.spotlight .background .banners').cycle({ 
    	fx: 'scrollLeft',
		timeout: 9000,
		speed : 600,
		easing: 'easeOutExpo',
		before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
          $('.captions ul').cycle('next');
    }
		
	});

	
	$('.captions ul').cycle({ 
    	fx: 'fade',
		speed : 1500,
		easing: 'easeOutExpo',
		timeout: 0
	});
	
	};
	
	
	/** ---------------------------------------
	 * pam n fern video overlay
	 */
	
	$(".video_overlay_trigger[rel]").each(function() {
		var overlayApiVideo = $(this).overlay({ api: true });
		var self = overlayApiVideo.getOverlay();
		self.data('iframe-src', self.find('> iframe').attr('src'));
		self.find('> iframe').attr('src', '');
		overlayApiVideo.onLoad(function() {
			self.find('> iframe').attr('src', self.data('iframe-src'));			
		}).onClose(function() {
			self.find('> iframe').attr('src', '');								
		});					   
	});
	
	
	/** ---------------------------------------
	 * Tweets
	 */
	 
	 $(".planithermtweet").tweet({
           	username: "planitherm",
            count: 1,
            loading_text: "loading Planitherm's tweets..."
        });
	 
	 
	 
	 
	 $(".pamnferntweet").tweet({
           	username: "pamnfern",
            count: 1,
            loading_text: "loading Pam n Fern's tweets..."
        });
	
	 $(".pamtweet").tweet({
           	username: "pamnfern_pam",
            count: 1,
            loading_text: "loading Pam's tweets..."
        });
	 
	 $(".ferntweet").tweet({
           	username: "pamnfern_fern",
            count: 1,
            loading_text: "loading Fern's tweets..."
        });
	 
	 $(".samtweet").tweet({
           	username: "pamnfern_sam",
            count: 1,
            loading_text: "loading Sam's tweets..."
      });
	 
	 $(".verntweet").tweet({
           	username: "pamnfern_vern",
            count: 1,
            loading_text: "loading Vern's tweets..."
        });
	 

	/** ---------------------------------------
	 * Pam n Fern Video Cycle
	 */
	 
	 
	/*$('.videos ul').cycle({ 
    	fx: 'scrollVert',
		timeout: 0,
		speed : 600,
		easing: 'easeOutExpo',
		pager: '.video_nav'
		
		
	});
	
	$('.video_nav a').addClass('cta');
	$('.video_nav a').prepend('Advert ');*/
	
	
	$('.videos ul').cycle({ 
    	fx: 'scrollVert',
		timeout: 0,
		speed : 600,
		easing: 'easeOutExpo',
		pager: '.video_nav ul',
		pagerAnchorBuilder: function(idx, slide) { 
	        return '.video_nav ul li:eq(' + idx + ') a'; 
    	}
		
	});
	
	//$('.video_nav a').addClass('cta');
	//$('.video_nav a').prepend('Advert ');
	
	 
		 /** ---------------------------------------
	 * Questions SlideToggle FAQ's
	 */
	 
	 $('a.ques').click(function() {
	 	
	 	$(this).toggleClass('open');
	 	$(this).next('.answer').slideToggle(200);
	 	
	 });
	


})(this.jQuery);


/*$(window).load(function() {
	
	/** ---------------------------------------
	 * Tweets
	 */ /*username: ["pamnfern", "pamnfern_pam", "pamnfern_fern", "pamnfern_sam", "pamnfern_vern"],*/
	 
	/*var tweets = $('.tweets');
	if(tweets.length) {
		tweets.formatTweets({
			url: 'http://pipes.yahoo.com/pipes/pipe.run?_id=5d5ef9515943e871cc1c57d4aa894791&_render=json&uid1=pamnfern&uid2=pamnfern_pam&uid3=pamnfern_fern&uid4=pamnfern_sam&uid5=pamnfern_vern'
		});				
	}
});*/
