/**
	TERMS OF USE - jQuery Easing
 
	Open source under the BSD License. 

	Copyright å© 2008 George McGinley Smith
	All rights reserved.
	
		
	TERMS OF USE - Easing Equations
	
	Open source under the BSD License. 
	
	Copyright å© 2001 Robert Penner
	All rights reserved.
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing,
{
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	}
});


/*!
 * Vodafone Animation Utility Functions
 * (c) Vodafone / Sapient Nitro 
 * Author:  James Westgate 2011 (www.queryj.com)
 */

//Add indexOf to Array object if missing
if (!Array.indexOf) {
	Array.prototype.indexOf = function(obj, start) {
    	for (var i = (start || 0), len = this.length; i < len; i++) {
      		if (this[i] == obj) return i;
    	}
    	return -1;
  	}
}

//Reverse each function
jQuery.fn.reverse = function(fn) {
   var i = this.length;
   
   while(i) {
   		i--;
		fn.call(this[i], i, this[i])
   }
};

/*
 * Vodafone pngFix plugin
 * Author: James Westgate (2010) (http://queryj.com)
 * Based on superpng plug-in (http://www.opencomponents.com/superpng/)
 * Version 1.0
 */
(function($){

	var opts;
	var vmlInit = true;
	var regex;

	$.fn.superpng = function(settings) {

		if (!$.browser.msie || parseInt($.browser.version, 10) > 6) return;

		if (opts == null) opts = $.extend({}, {
			cache: true,
			path: 'x.gif',
			mode: 'auto'  //filter, vml
		}, settings);

		//Caching of background images not enabled by default in IE6
		if (opts.cache) document.execCommand('BackgroundImageCache', false, true);

		return this.each(function(){

			var self = $(this);
			var bg = self.css('background-image');
			var initial = (this.vml === undefined);	

			//Background pngs
			if (!initial || (opts.mode !== 'filter' && bg.match(/\.png/i) !== null)) {

				var src = bg.substring(5, bg.length-2);
				var repeat = self.css('backgroundRepeat');
				var bgposx = self.css('backgroundPositionX');
				var bgposy = self.css('backgroundPositionY');

				self.css('background-image', ['url(', opts.path, ')'].join(''));

				//Determine if we use the alpha image method, or the vml method
				if (initial && opts.mode !== 'vml' && bgposx === undefined && bgposy === undefined) {
					self.css('filter', ["progid:DXImageTransform.Microsoft.AlphaImageLoader(src='", src, "', sizingMethod='crop')"].join(''));					
				}
				else {

					//Add vml namespace and built-in behaviour
					if (vmlInit) {

						vmlInit = false;

						//vml behaviour and additional stylesheet code			
						$('head').append('<style media="screen">vml\\:* {behavior:url(#default#vml);} .superpng-vml {position: absolute; top:0; left:0; z-index:0; margin: 0}</style>');

						regex = 'px'; // /[^\d]/g;
					}

					//Calculate position as percentage of width
					var width = self.width();
					var height = self.height();					
					var position = [parseFloat(bgposx.replace(regex, '') / width), ',', parseFloat(bgposy.replace(regex, '') / height)].join('');
					var style = ['width:', width, 'px; height:', height, 'px'].join('');

					//Insert a relative container containing vml
					if (initial) {

						//Create vml element and place inside container
						var builder = ['<vml:rect stroked="f" class="superpng-vml" style="', style, '"><vml:fill type="frame" aspect="atmost" src="',
							src, '" position="', position, '"/></vml:rect>'];

						//Add to DOM
						self.wrapInner(builder.join(''));
						this.vml = $('.superpng-vml', self); //set a reference to the vml called 'vml'

						self.css('background-image', 'url(' + opts.path + ')');
					}

					//Update existing vml
					else {
						var fill = this.vml.children(':first');
						this.vml.attr('style', style);

						fill.attr('position', position);
					}
				}
			}

			//image elements
			else if (self.is('img[src$=png]')){

				var styles = {
					'width': self.width() + 'px',
					'height': self.height() + 'px',
					'filter': ["progid:DXImageTransform.Microsoft.AlphaImageLoader(src='", self.attr('src'), "', sizingMethod='scale')"].join('')
				};

				self.css(styles).attr('src', opts.path);
			}

		});
	};

})(jQuery);

/*
	Vodafone Datatoy Function
	Copyright (c) 2011, Vodafone, SapinetNitro
	Author: James Westgate (http://queryj.com)
*/
(function($){
	
	vf.namespace('vf.datatoy', function() {
		
		this.init = function(options){
		
			var defaults = {
				step: 83,
				min: 18,
				max: 198
			};
			
			var plans = [
			
				{name:'Very occasional user plan', tenure:'pay as you go', desc: ['250MB UK data a day'], cost: ['&pound;3 / month + &pound;2 per day used', '&pound;19'], href: '?skuId=sku140039,sku4040032', tip: '#tooltip2', tab: 2}, //1
				{name:'Occasional user plan', tenure:'30 day plan', desc: ['500MB UK data a month', '1GB BT Openzone Wi-Fi'], cost: ['&pound;7.50 / month', 'Free'], href: '?skuId=sku140039,sku4040033', tip: '#tooltip3', tab: 0}, //2
				{name:'Regular user plan', tenure:'30 day plan', desc: ['2GB UK data a month', '1GB BT Openzone Wi-Fi'], cost: ['&pound;15 / month', '&pound;9'], href: '?skuId=sku140039,sku4040034', tip: '#tooltip1', tab: 0}, //3
				{name:'Very occasional user plan', tenure:'30 day plan', desc: ['500MB UK data a month', '1GB BT Openzone Wi-Fi'], cost: ['&pound;7.50 / month', 'Free'], href: '?skuId=sku140039,sku4040033', tip: '#tooltip3', tab: 0}, //4
				{name:'Occasional user plan', tenure:'30 day plan', desc: ['2GB UK data a month', '1GB BT Openzone Wi-Fi'], cost: ['&pound;15 / month', '&pound;9'], href: '?skuId=sku140039,sku4040034', tip: '#tooltip4', tab: 0}, //5
				{name:'Regular user plan', tenure:'18 months', desc: ['2GB UK data a month', '2GB BT Openzone Wi-Fi', '1GB off-peak UK data'], cost: ['&pound;15 / month', 'Free'], href: '?skuId=sku140039,sku4040039', tip: '#tooltip1', tab: 1}, //6
				{name:'Very occasional user plan', tenure:'pay as you go', desc: ['2GB UK data 30 days'], cost: ['&pound;15 TopUp', '&pound;25'], href: '?skuId=sku690026,sku840022', tip: '#tooltip1', tab: 2}, //7
				{name:'Occasional user plan', tenure:'18 months', desc: ['2GB UK data a month', '2GB BT Openzone Wi-Fi', '1GB off-peak UK data'], cost: ['&pound;15 / month', 'Free'], href: '?skuId=sku140039,sku4040039', tip: '#tooltip5', tab: 1}, //8
				{name:'Regular user plan', tenure:'18 months', desc: ['2GB UK data a month', '4GB BT Openzone Wi-Fi', '2GB off-peak UK data'], cost: ['&pound;20 / month', 'Free'], href: '?skuId=sku140038,sku4040040', tip: '#tooltip6', tab: 1} //9
			]
	
			var settings = $.extend({}, defaults, options); //Combine defaults and options
			
			$(document).ready(function(){
			
				//Show data toy
				$('#mod-dataTool').removeClass('hidden');
				
				//Display intial plan
				displayUsage(0, 0);
				
				//Hide accessibility table / show toy
				$('#data-plan-table').addClass('hidden');
				$('#mod-dataTool').removeClass('hidden');
				
				$('a.indicator').data('index', 0);
				
				//Prevent click on the slider
				$('a.indicator').click(function(e){
					e.preventDefault();
				});
				
				
				// -- Slider down
				var sliderDown = function(element) {
					$(element).closest('.dataTool-sliderControl').addClass('vf-slider-active');
					$(element).css('opacity', '0.8');
					
					$('#initState').addClass('hidden');
					$('#state1').removeClass('hidden');
				};
				
				//Indicator mouse down
				$('a.indicator').mousedown(function(e){
					sliderDown(this);
					e.preventDefault();
				});
				
				//Ipad / iPhone
				$('a.indicator').bind('touchstart', function(e) {
					sliderDown(this);
					e.preventDefault();
				});
				
				
				//-- Slider up
				var sliderUp = function() {
					
					if ($('.vf-slider-active').length) {
					
						$('.dataTool-sliderControl').removeClass('vf-slider-active');
						$('a.indicator').css('opacity', 1);
						
						var freq = $('#dataTool-q1 a.indicator').data('index');
						var amt = $('#dataTool-q2 a.indicator').data('index');
						
						$('#dataTool-q1 a.indicator').animate({'left': freq * settings.step}, 100, 'swing');
						$('#dataTool-q2 a.indicator').animate({'left': amt * settings.step}, 100, 'swing');
						
						$('#dataTool-q1 div.stripe').animate({'background-position': (freq * settings.step) - 225 + 'px 0'}, 100, 'swing');
						$('#dataTool-q2 div.stripe').animate({'background-position': (amt * settings.step) - 225 + 'px 0'}, 100, 'swing');
						
						var plan = displayUsage(freq, amt);
						
						//Set tab in data plans
						$('#data-plans div.tabbedContent li').eq(plan.tab).find('a').click();
					}
				};
				
				//Indicator mouse up	
				$(document).mouseup(function(e) {
					
					sliderUp();
					e.preventDefault();
				});
				
								
				//Ipad / iPhone
				$(document).bind('touchend', function(e) {
					sliderUp();
					e.preventDefault();
				});
				
				
				//-- Slider move
				var sliderMove = function(e) {
					
					var slider = $('.vf-slider-active');
					if (!slider.length) return;
					
					var x = e.pageX - slider.offset().left;
					var index = Math.round((x - 24) / settings.step); //Get index based on position (0 - 2)
					
					if (index >= 0 && index < 3) {
					
						if (x < settings.min) x = settings.min;
						if (x > settings.max) x = settings.max;
						
						var pos = (Math.round(x / 8) * 8) - 24;
						
						$('a.indicator', slider).css('left', pos);
						$('a.indicator', slider).data('index', index);
						$('div.stripe', slider).css('background-position', pos - 225 + 'px 0');
						
						//Do actions based on index
						$('blockquote.quote', slider).hide().eq(index).show();
						
						var freq = $('#dataTool-q1 a.indicator').data('index');
						var amt = $('#dataTool-q2 a.indicator').data('index');
						
						displayUsage(freq, amt);
					}
				};
				
				//Determine if there is a mousemove over any elements in the mod
				$('#mod-dataTool').mousemove(function(e) {

					sliderMove(e);
					e.preventDefault();
				});
				
				//Ipad / iPhone
				$('#mod-dataTool').bind('touchmove', function(e) {
					var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
					sliderMove(touch);
					e.preventDefault();
				});
			});
			
			var lastIndex = -1;
			
			function displayUsage(freq, amt){
	
				var index = (amt * 3 + freq);
				var plan = plans[index];
				var state = $('#state1');
				
				if (index === lastIndex) return plan;
				lastIndex = index;
				
				$('dt:first', state).text(plan.name);
				$('dd:first', state).text(plan.tenure);
				$('span.med-number', state).html(plan.desc.join('<br/>'));
				$('p.price', state).html(plan.cost[0] + '<br/>Dongle price - ' + plan.cost[1]);
				
				//Set tooltip
				$('#datatoy-tip div.tooltipContent ul').addClass('hidden');
				$(plan.tip).removeClass('hidden');
				
				//Set buy button
				$('#datatoy-buynow').attr('href', '//shop.vodafone.co.uk/shop/basket/addToCart.jsp' + plan.href);
				
				return plan;
			}
		}
	})
	
})(jQuery);	
			
/*
	Vodafone Mobile BroadBand Smod Hero Script
	Copyright (c) 2010, Vodafone, SapinetNitro
	Author: James Westgate (http://queryj.com)
*/
(function($){
			
	vf.namespace('vf.mobilebroadband.hero', function() {
		
		this.init = function(){
		
			var currentIndex = 0;
			var undefined;
			var ie6 = false;
			
			//Create a plugin to handle the card animations
			$.fn.animateCard = function(values){
			
				return this.each(function(){
				
					$(this).mouseenter(function(e){
					
						reset();
						$('span.smod-card', $(this).closest('div')).animate(values, {
							duration: 100
						});
					})
					
					$(this).mouseleave(function(e){
					
						reset();
					})
				});
			};
			
			//Helper plugin to handle resetting a card animation
			$.fn.resetCard = function(values){
			
				return this.each(function(){
				
					$(this).find('span.smod-card').animate(values, {
						duration: 32
					});
					
				});
			};
			
			//-- Init
			$(document).ready(function(){
			
				ie6 = ($.browser.msie && parseInt($.browser.version == 6));
				
				if (ie6) {
				
					//var shim = 'http://www.vodafone.co.uk/made-me-smile/img/x.gif';
					var shim = '/consumption/groups/system/documents/webasset/img_png_x.gif';
					
					if (window.location.hostname == 'cms-contributor.vodafone.co.uk') 
						shim = '/contributor/groups/system/documents/webasset/img_png_x.gif';
					
					$('.pngFix').superpng({path: shim});
				}
				
				//-- Events
				bindMouseEvents();
				
				//-- Click
				$('div.smod-laptop a, div.smod-phone a, div.smod-tablet a', '#product-panel').click(function(e){
				
					var indexes = ['smod-laptop', 'smod-phone', 'smod-tablet'];
					var parent = $(this).parent();
					
					for (var i = 0; i < indexes.length; i++) {
						if (parent.hasClass(indexes[i])) {
							currentIndex = i;
							doSwipe(i);
							e.preventDefault();
							break;
						}
					}
				})
				
				if (ie6) {
					$('#smod-phone-fix', '#product-panel').click(function(e){
						currentIndex = 1;
						doSwipe(1);
						e.preventDefault();
					})
				}
				
				
				//-- Close
				$('a.back').click(function(e){
					unSwipe(currentIndex);
					e.preventDefault();
				})
			});
			
			function bindMouseEvents(){
			
				//-- For IE6, create an additional absolute element over the phone card for mouse events
				if (ie6) {
				
					//Add an extra element to track the mouse input
					$('#product-panel').prepend('<div id="smod-phone-fix" style="position: absolute; z-index: 999; cursor: hand; width:115px; height: 57px; left: 330px; top: 50px"/>');
					
					//Use extra element to track mouse
					$('#smod-phone-fix', '#product-panel').mouseenter(function(){
						$('div.smod-phone span.smod-card', '#product-panel ').animate({
							left: '-15px'
						}, {
							duration: 100
						});
					});
					
					//Remove tracking
					$('#smod-phone-fix', '#product-panel').mouseleave(function(e){
						reset();
					});
				}
				
				
				//-- Mouseenter		
				$('div.smod-laptop a, div.smod-phone a', '#product-panel').animateCard({left: '-15px'});
				$('div.smod-tablet a', '#product-panel').animateCard({right: '-30px'});
				$('div.smod-phone a', '#computer-panel').animateCard({top: '-16px'});
				$('div.smod-dongle a', '#computer-panel').animateCard({left: '-10px'});
				$('div.smod-wireless a', '#computer-panel').animateCard({right: '-10px'});
				$('div.smod-laptop a', '#computer-panel').animateCard({top: '-25px'});
				$('div.smod-phone1 a', '#mobile-panel').animateCard({left: '-13px'});
				$('div.smod-phone2 a', '#mobile-panel').animateCard({left: '-5px'});
				$('div.smod-phone3 a', '#mobile-panel').animateCard({right: '-10px'});
				$('div.smod-microsim a', '#tablet-panel').animateCard({top: '5px'});
				$('div.smod-wireless a', '#tablet-panel').animateCard({top: '-200px'});
				$('div.smod-tablet a', '#tablet-panel').animateCard({top: '-25px'});
			}
			
			//Reset all animations
			function reset(){
			
				$('span.smod-card').clearQueue();
				
				$('div.smod-laptop a, div.smod-phone a', '#product-panel').resetCard({left: '-20px'});
				$('div.smod-tablet a', '#product-panel').resetCard({right: '-35px'});
				$('div.smod-phone a', '#computer-panel').resetCard({top: '-20px'});
				$('div.smod-dongle a', '#computer-panel').resetCard({left: '-15px'});
				$('div.smod-wireless a', '#computer-panel').resetCard({right: '-15px'});
				$('div.smod-laptop a', '#computer-panel').resetCard({top: '-30px'});
				$('div.smod-phone1 a', '#mobile-panel').resetCard({left: '-18px'});
				$('div.smod-phone2 a', '#mobile-panel').resetCard({left: '-10px'});
				$('div.smod-phone3 a', '#mobile-panel').resetCard({right: '-15px'});
				$('div.smod-microsim a', '#tablet-panel').resetCard({top: '0'});
				$('div.smod-wireless a', '#tablet-panel').resetCard({top: '-195px'});
				$('div.smod-tablet a', '#tablet-panel').resetCard({top: '-30px'});
			}
			
			function unbindMouseEvents(){
			
				$('div.smod-laptop a, div.smod-phone a, div.smod-tablet a', '#product-panel').unbind('mouseenter').unbind('mouseleave');
				
				if (ie6) $('#smod-phone-fix', '#product-panel').unbind('mouseenter').unbind('mouseleave');
			}
			
			//Move the block across the screen
			function doSwipe(index){
			
				unbindMouseEvents();
				
				//Animate block
				$('#smod-block').delay(100).animate({right: '596px'}, {duration: 500}); 
				
				//Move block to the left
				$('#product-panel h3.product-heading').fadeOut(200); //Remove heading
				$('#smod-product-select').fadeOut(300); //Fade out block content
				
				//Animate Fade outs
				var count = 1;
				
				$('#product-panel > div').reverse(function(i, e){
					$(this).delay(count * 100).fadeOut(100);
					count++;
				});
				
				//Swap borders, fade in content
				$('#smod-mobilebroadband').delay(300).queue(function(){
				
					//Swap borders on the block
					$('div.block-left').removeClass('block-left').addClass('block-left-alt');
					$('div.block-right').removeClass('block-right').addClass('block-right-alt');
					$('div.block-content-product').eq(index).fadeIn(300); //Fade in block content
					
					//Fade in product content
					var panel = $('div.product-content').eq(index);
					
					panel.find('div').hide();
					panel.show();
					
					count = 1;
					$('div', panel).each(function(){
						$(this).delay(count * 100).fadeIn(100);
						count++;
					});
					
					bindMouseEvents();
					
					$(this).dequeue();
				});
				
				$('a.back').delay(600).queue(function(){
					$(this).removeClass('hidden');
					$(this).dequeue();
				});
			}
			
			//Move the block back across the screen
			function unSwipe(index){
			
				unbindMouseEvents();
				
				//Animate block
				$('#smod-block').delay(100).animate({right: '0'}, {duration: 500}); 
				
				//Move block to the left
				$('#product-panel h3.product-heading').delay(400).fadeIn(200); //Remove heading
				$('div.block-content-product').eq(index).fadeOut(300); //Fade in block content
				//Animate Fade outs
				
				var panel = $('div.product-content').eq(index);
				var count = 1;
				
				$('div', panel).each(function(i, e){
					$(this).delay(count * 100).fadeOut(100);
					count++;
				});
				
				//Swap borders, fade in content
				$('#smod-mobilebroadband').delay(300).queue(function(){
				
					//$('#product-panel').hide();
					
					//Swap borders on the block
					$('div.block-left-alt').removeClass('block-left-alt').addClass('block-left');
					$('div.block-right-alt').removeClass('block-right-alt').addClass('block-right');
					$('#smod-product-select').fadeIn(300); //Fade in block content
					
					//Fade in product content
					count = 1;
					$('#product-panel > div').reverse(function(){
						$(this).delay(count * 100).fadeIn(100);
						count++;
					});
					
					bindMouseEvents();
					
					$(this).dequeue();
				});
				
				$('a.back').addClass('hidden');
			}
		}
	})
	
})(jQuery);
				


