
(function($) {

$.snippets = {"search\/items\/item":"<li>\r\n   <h3><a href=\"{url}\">{title}<\/a><\/h3>\r\n   <div class=\"wysiwyg\"><p>{description}<\/p><\/div>\r\n   <span class=\"url\">{url}<\/span>\r\n<\/li>","kids-in-the-middle\/quiz2\/results\/comparison\/item":"<li{style}>\r\n   <label class=\"marker marker{marker}\" style=\"margin-top: {top}px;\">You<\/label>\r\n   <span class=\"top\" style=\"height: {height1}px;\"><strong style=\"line-height: {height1}px;\">{value1}%<\/strong><\/span>\r\n   <span class=\"middle\"><\/span>\r\n   <span class=\"bottom\" style=\"height: {height2}px;\"><strong style=\"line-height: {height2}px;\">{value2}%<\/strong><\/span>\r\n   <a class=\"title\" href=\"javascript:void(0)\" title=\"{question}\">Question {index}<\/a>\r\n<\/li>"};
$.snippet = function(template, data) { 
	var i;
   
   for (i in data) {
   	template = template.split('{' + i + '}').join(data[i]);
   }

   return template;
}


$.fn.fix = function(size) {
	size = parseInt(size, 10);
	
	var method = $.proxy(function() {
      var ratio = parseInt($('html').css('font-size'), 10);

      this.each(function() {
      	$(this).css('font-size', (size / ratio) + 'em');
      });
   }, this);

	$(window).resize(method);
   method();
};


$.fn.equal = function(property, reset) {
	if (this.length <= 1) return;

	var value = 0;
   var setting = ($.browser.msie && $.browser.version < 8 ? property : 'min-' + property);

	$(this).css(setting, '');
   
   if (reset === true) return;

   this.each(function() {
      value = Math.max(value, $(this)[property]());							
   });

	$(this).css(setting, value + 'px');
};


$.widget('ui.quizComments', {

	_init: function() {
      this.element.find('form').validate({
         submit: $.proxy(this.submit, this)
      });
   },


   submit: function(valid) {
   	if (!valid) return;
   
   	$.ajax({
         type: 'POST',
         url: 'http://www.workingfamilies.org.uk/kids-in-the-middle/quiz1/comment',
         data: this.element.find('form').serialize()
      });
      
      this.element.find('input, textarea').val('');
      alert('Thanks for your comment!');
   }

});


$.widget('ui.quizChoice', {

	_init: function() {
		var $element;

		$element = this.element.find('.track');
		$element.slider({
			parent: this,
			animate: true,
			orientation: 'horizontal',
			min: 0,
			max: 100,
			value: 3,
			start: $.proxy(this.drag, this),
			slide: $.proxy(this.slide, this),
			stop: $.proxy(this.snap, this)
		});

		$element.find('a').unbind('keydown').touch();

      this.element.find('.options a').bind('click', { target: this }, function(event) {
      	var target = event.data.target;
         target.snap(target.element.find('.options li a').index(this) + 1);
         return false;
      });

		this._setData('slider', $element);
		this.update();
		this._setData('enabled', true);

		if (this._getData('disable')) this.disable();
	},


	drag: function() {
		this._getData('slider').slider('value', this.position());
		this.interval = setInterval($.proxy(this.update, this), 30);
		this.element.find('.track a').addClass('down');
      return false;
	},
   
   
   slide: function() {
   	this.highlight(this.closest());
      this.update();
   },


	position: function() { return parseInt(this.element.find('.track a').css('left'), 10); },
   
   
   value: function(value) {
   	if (value === false) return;

   	if (!isNaN(value)) {
      	this.snap(value, false);
      	return;
      }

		if (isNaN(this._getData('index'))) return false;
   	return this._getData('index');
   },


   closest: function() {
   	var value = this.position();
   	var points = this._getData('points');
		var orders = [];

      for (var i = 0; i < points.length; i++) {
			orders.push(Math.abs(points[i] - value));
		}

      orders.sort(function(a, b) { return a - b; })[0];

      for (var i = 0; i < points.length; i++) {
         if (Math.abs(points[i] - value) == orders[0]) break;
      }

      return i + 1;
   },


	snap: function(index, animate) {
		if (!this._getData('enabled')) return;

		var value = this._getData('slider').slider('value');	

		if (isNaN(index)) index = this.closest();

		this._setData('index', index);
		this.highlight(index);

		value = this._getData('points')[index - 1];

		var $element = this.element.find('.track a');
		var speed = Math.max(300, Math.round(Math.abs(this.position() - value) * 15));

		this.element.find('.track a').removeClass('down');
		this.update();

      if (animate === false) {
      	$element.css('left', value + '%');
         this.snapComplete();
      }
      else {
			$element.stop(true);
			$element.animate({ left: value + '%' }, { duration: speed, easing: 'easeOutBounce', step: $.proxy(this.update, this), complete: $.proxy(this.snapComplete, this) });
		}

		if (this._getData('change') != null) this._getData('change')(index);
	},
   
   
   highlight: function(index) {
   	this.element.find('.options a').removeClass('active').eq(index - 1).addClass('active');
   },


	snapComplete: function() {
      clearInterval(this.interval);
      this.update();
	},


	update: function() {
		var value = Math.max(0, Math.min(1, this.position() / 100));

		this.element.find('.highlight').css('width', (35 + Math.round(value * 888)) + 'px');
	},
	
	
	enable: function() {
		this._setData('enabled', true);
		this._getData('slider').slider('enable');	
	},
	
	
	disable: function() {
		this._setData('enabled', false);
		this._getData('slider').slider('disable');	
	}

});


$.extend($.ui.quizChoice, {
	getter: 'value',
   setter: 'value',
	defaults: { }
});


$.widget('ui.validate', {

	submit: true,

	_init: function() {
		var $this = this;
      var $fields, $field;

      if (this.element.find('button[type=submit]').length > 0) {
         this.element.filter('form').unbind().submit(function() {
            return $this.validate();										  
         });
      }
      else {
      	this.submit = false;
      
         this.element.find('.submit').unbind().click(function() {
            $this.validate(this);
         });
      }

		$fields = this.fields();
		for (var $i = 0; $i < $fields.length; $i++) {
			$field = $fields.eq($i);
         $field.data('validator', this);
			if ($field.data('type') == null) {
         	$field.data('type', $field.attr('type'));
            $field.focus(function() {
               if ($(this).val() == $(this).attr('title')) {
                  $(this).val('');
                  $this.clone($(this), true);
               }
            });
            $field.blur(function() {
               if ($(this).val().length == 0) {
                  $(this).val($(this).attr('title'));
                  $this.clone($(this), false);
               }
            });
            $field.trigger('blur');
         }
		}
	},

	fields: function() {
		return this.element.find('input, textarea, select').filter(':visible');
	},

	clone: function($field, password) {
		var type = $field.data('type');
      var $clone;

		if (type == 'password') {
			$clone = $field.clone(true);
			$clone.attr('type', (password ? 'password' : 'text'));
			$clone.insertBefore($field[0]);
			$clone.data('type', type);
         $clone.data('validator', this);
			if (password) $clone.focus();
			$field.remove();
		}
	},

	validate: function(button) {
		var $fields = this.fields();
      var $field;
      var values = {};
      var value;
      var parts, part;
      var valid;
		var result = true;
      var errors = {};
      var error;

		for ($i = 0; $i < $fields.length; $i++) {
			$field = $fields.eq($i);
         if ($field.data('validator') == this || $field.data('validator') == null) {
            value = ($field.val() == $field.attr('title') ? '' : $field.val());
            parts = $field.attr('class').split(" ");
            part = null;
   
            for ($j = 0; $j < parts.length; $j++) {
               if (parts[$j].slice(0, 8) == 'validate') {
                  part = parts[$j].slice(9, -1).split(',');
                  break;	
               }
            }

            if (part != null) {
               switch (part[0]) {
                  case 'email': valid = $.email(value); break;
						case 'number': valid = (Number(value) > 0); break;
                  default: valid = (value.length >= 2);
               }

               if ($field.is('input[type=text]')) error = 'error';
               else if ($field.is('input[type=password]')) error = 'error';
               else if ($field.is('textarea')) error = 'error';
					else if ($field.is('select')) error = 'error';

               $field.toggleClass(error, !valid);
               errors[$field.attr('name')] = !valid;
               values[$field.attr('name')] = value;
   
               if (!valid) result = false;
            }
         }
		}

		if (this._getData('submit') != null)
      {
      	this._getData('submit').apply(this.element[0], [result, errors, values, button]);
      	return false;
      }

		if (result) this.cleanup();
      if (!this.submit) this.element[0].submit();
		return result;
	},
 
   cleanup: function() {
   	var $fields = this.fields();
      var $field;

      for (var i = 0; i < $fields.length; i++) {
         $field = $fields.eq(i);
         if ($field.val() == $field.attr('title')) $field.val('');
      }
   },

	destroy: function() {
		this.element.filter('form').unbind('submit');	
	},
   
   submit: function() {
   	this.cleanup();
      this.element[0].submit();
   }
	
});


$.email = function($value) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($value);						
}


$.extend({
	delegate: function(fn, thisObject) {
      var parameters = Array.prototype.slice.call(arguments).slice(2);
 
      return function() {
         return fn.apply(thisObject || this, parameters.concat(Array.prototype.slice.call(arguments)));
      };
	}

});


$.widget('ui.navigation', {

	_init: function() {
      if ($.browser.msie && $.browser.version < 7) {
			$(window).resize(function() {
				$('#overlay').css('height', $(window).height() + 'px');	  
			});
         
         $(window).scroll(function() {
				$('#overlay').css('top', $(window).scrollTop() + 'px');	  
			});
		}

      $('#overlay').bind('click mouseover', this, function(event) {
         event.data.menuHide();
      });
      
      this.element.find('ul:first li a.arrow').click(function(event) {
      	event.preventDefault();
      });
      this.element.find('ul:first li a.arrow strong').click(function(event) {
      	document.location = $(this).closest('a').attr('href');
      });

      this.update();
      
      $(window).bind('resize', $.proxy(this.resize, this));
   },
 
   
   update: function() {
   	var $buttons;
      
      $buttons = this.element.find('ul:first li a em');
      $buttons.unbind().bind('click', this, function(event) {
         event.data.itemOver($(this).closest('a'));
      });
      $buttons.hover(
      	function() {
         	$(this).addClass('hover');
         },
         function() {
         	$(this).removeClass('hover');
         }
      );
      
      /*
      $buttons.bind('mouseout', this, function(event) {
         event.data.itemOut(!$(this).hasClass('arrow'));
      });
      */
   },

   
   itemOut: function(all) {
   	var $items = this.element.find('ul:first li a');
      var $selected = (all === true ? $items : $items.filter('.hover'));

      $selected.not('.active').each(function() {
         $(this).removeClass((all ? 'selected' : '') + ' hover');
      });
      
      this.update();
   },


   itemOver: function($button) {
   	var $menu;
      var $items = this.element.find('ul:first li a');
      var index = $items.index($button) + 1;

		this.menuHide();

      $button.addClass('selected');
      
      if (!$button.hasClass('arrow')) return;
      
      $menu = this.element.find('.submenu');

      var data = JSON.parse($menu.find('textarea:first').val());

      $menu.removeClass();
      $menu.addClass('submenu color' + index);
      $menu.find('.content').html(data[index - 1]);
      $menu.css('width', (Math.min(5, $menu.find('.content ul > li').length) * 195 + 13) + 'px');
      $menu.css('left', Math.min(this.element.find('.menu').offset().left + 988 - $menu.width(), $button.offset().left) + 'px');
      $menu.show();

      $('#overlay').show();
      this.element.find('.submenu-overlay').show();
      this.resize();
   },
   
   
   resize: function() {
   	var $element;
      var visible;
   
   	this.element.find('.submenu .content ul').each(function() { 	
         var $parent;

         $parent = $(this).find('li .box');
         $parent.equal('height', true);
         $parent.find('.info').equal('height');
         $parent.find('.links-first').equal('height');
         $parent.equal('height');
      });
      
      $element = this.element.find('.submenu-overlay');
      visible = $element.is(':visible');
      $element.show();
      $element.css('height', ($('#footer .links').position().top - $element.position().top + 5) + 'px');
      $element[(visible ? 'show' : 'hide')]();
   },


   menuHide: function() {
      this.itemOut(true);
      this.element.find('.submenu').hide();
      this.element.find('ul:first li a').removeClass('selected');

      if ($('#overlay').data('show') !== true) {
      	$('#overlay').hide();
         this.element.find('.submenu-overlay').hide();
      }
   }

});


$.widget('ui.links', {

	_init: function() {
   	var $buttons = this.element.find('ul:first li a');

		if (!this.element.hasClass('links-top')) {
      	$buttons.eq(0).bind('click', $.delegate(this.share, this, true));
      	$buttons.eq(1).bind('click', $.delegate(this.email, this, true));
         this.element.find('.top').click(function() { window.scrollTo(0, 0); });
      }
      else
      {
      	$buttons.eq(1).attr('href', 'javascript:void(0)').attr('target', '_self').bind('click', $.delegate(this.print, this));
 		}
 
 		$(window).bind('resize scroll', $.delegate(this.scroll, this));
 		this.scroll();
      this.disabled = false
   },
   
   
   scroll: function() {
   	var $link = this.element.find('.top');
      
      if ($link.length == 0) return;
      
      $link.toggleClass('hidden', ($link.offset().top < $(window).height()));
   },
   
   
   print: function() {
   	window.print();
   },

   
   menu: function(show, callback) {
   	var $element;
      
      $element = $('#navigation .submenu-overlay');
      $element.unbind();
      $element[show ? 'show' : 'hide']();
      $element = $('#overlay');
      $element[show ? 'show' : 'hide']();
      $element.data('show', show);
      
      if (!show) this.element.find('.extra > *').hide();
      if (show) $element.click(callback);
   },
   
   
   email: function(show) {
   	var $element;
      
      this.menu(show, $.delegate(this.share, this, false));

      $element = this.element.find('.email');
      $element.find('input, textarea').val('');
      $element.css('margin-top', -($element.height() + 40) + 'px');
      $element[show ? 'show' : 'hide']();
      $element.find('.close').unbind().bind('click', $.delegate(this.email, this, false));
      $element.find('form').validate({
      	submit: $.delegate(this.emailSubmit, this)
      });
   },
   
   
   emailSubmit: function(valid) {
   	if (!valid) return;
      if (this.disabled) return;
      
      this.disabled = true;

		var $element = this.element.find('.email form');

		var data = {
         title: document.title,
         url: document.location,
         name1: $element.find('input[name=name1]').val(),
         email1: $element.find('input[name=email1]').val(),
         name2: $element.find('input[name=name2]').val(),
         email2: $element.find('input[name=email2]').val(),
         message: $element.find('textarea[name=message]').val()
      };

      $.ajax({
         type: 'POST',
         url: 'http://www.workingfamilies.org.uk/articles/email',
         data: $.param(data, true),
         success: $.proxy(this.emailSubmitComplete, this)
      });
   },
   
   
   emailSubmitComplete: function(data) {
   	alert('Thanks for sharing this article.');
   	this.disabled = false;
      this.email(false);
   },

   
   share: function(show) {
      var $element;

      this.menu(show, $.delegate(this.share, this, false));

      $element = this.element.find('.share');
      $element.css('margin-top', -($element.height() + 40) + 'px');
      $element[show ? 'show' : 'hide']();
      $element.find('a').unbind().bind('click', this, function(event) {
      	event.data.shareURL(parseInt($(this).attr('class').slice(6), 10));
     	});
   },
   
   
   shareURL: function(index) {
   	var url;
   
   	switch (index) {
      	case 1: url = ''; break;
         case 2: url = 'http://www.facebook.com/sharer.php?u={url}&t={title}'; break;
         case 3: url = 'http://twitter.com/home?status={url}'; break;
         case 4: url = 'http://www.myspace.com/Modules/PostTo/Pages/?u={url}&t={title}&c=%20'; break;
         case 5: url = 'http://digg.com/submit?phase=2&partner=[partner]&url={url}&title={title}'; break;
         case 6: url = ''; break;
         case 7: url = 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={url}&title={title}&top=1'; break;
         case 8: url = 'http://del.icio.us/post?v=4&partner=[partner]&noui&url={url}&title={title}'; break;
         case 9: url = 'http://www.stumbleupon.com/submit?url={url}&title={title}'; break;
         case 10: url = 'http://reddit.com/submit?url={url}&title={title}'; break;
         case 11: url = 'http://www.google.com/bookmarks/mark?op=add&bkmk={url}&title={title}'; break;
         case 12: url = 'http://www.linkedin.com/shareArticle?mini=true&url={url}&title={title}&ro=false&summary=&source='; break;
      }
      
      if (url.length == '') return;
      
      url = $.snippet(url, {
      	title: document.title,
         url: document.location,
         partner: ''
     	});
      
      window.open(url);
   }

});


$.widget('ui.article', {

	_init: function() {
		if (String(document.location).split('/').pop() == 'print') return;

		$(window).resize($.delegate(this.resize, this));

      this.element.find('.back').click(function() { history.go(-1); });

		this.element.find('.links').links();
		this.element.find('.pagination .previous, .pagination .next').bind('click', this, function(event) {
			event.data.change($(this).hasClass('previous') ? '-1' : '1');
		});
		this.element.find('.pagination input').bind('keyup', this, function(event) {
			event.data.change(parseInt($(this).val(), 10));																			 
		});

		this._setData('data', this.element.find('.wysiwyg').html());
      this.element.find('.wysiwyg').html('');

      var pages = [];
		var page = [];
		var limit = 3000;
		var html, text;
		var counter = 0;
		var length;
      var $items;
      var $item;

		var re = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
      var match;
      var scripts = [];

      while (match = re.exec(this._getData('data'))) {
      	match = match[0].split('<script').join('<div').split('</script>').join('</div>');
         scripts.push(($('<div />').append(match).find('> *:first').attr('src')));
      }
      
      this.scripts = scripts;
		
		var $body = $('<div />').append(this._getData('data'));
		var $styles = $body.find('style');
		var style = "";
		
		$styles.each(function() {
			style += $('<div />').append(this).html();
		});

		$items = $body.find('> *');

		for (var i = 0; i < $items.length; i++) {
			$item = $items.eq(i);
			html = $('<div />').append($item.clone()).html();
			length = html.replace(/<\/?[^>]+>/gi, '').replace(/&nbsp;/gi, '').length;
         if (html.indexOf('<img') != -1 || html.indexOf('<a') != -1) length += 1;
			page.push([style + html, length]);
			counter += length;

			if (counter > limit) {
				pages.push(page);
				page = [];
				counter = 0;
			}
		}
		
		if (page.length > 0) pages.push(page);

      this._setData('pages', pages);
      this.change(1);
   },
	
	
	resize: function() {
		var offset = $.cookie('font-size');

	 	if (!offset) offset = 62.5;
		offset /= 62.5;

		this.element.find('.wysiwyg *').each(function() {
			var size = $(this).data('font_size');

			if (size == null) {
				if (String($(this).attr('style')).indexOf('font-size') == -1) {
					size = false;
				}
				else {
					size = parseFloat($(this).css('font-size'));
				}

				$(this).data('font_size', size);
			}
			
			if (size == false) {
				$(this).css('font-size', (offset == 1 ? '' : 'inherit'));
			}
			else {
				$(this).css('font-size', (size * offset) + 'px');
			}
		});
	},


	change: function(offset) {
		var $parent = this.element.find('.wysiwyg');
		var pages = this._getData('pages');
      var page;
      var pageCount = Math.max(1, pages.length);
		var html;

		switch (offset) {
      	case '-1':
         	index = (index == 1 ? pageCount : index - 1);
            break;
         case '1': 
         	index = (index == pageCount ? 1 : index + 1);
         	break;
			default:
         	index = offset;
      }

		if (isNaN(index)) return;

		index = Math.max(1, Math.min(index, pageCount));

		html = '';
      
      for (var i = 0; i < pages.length; i++)
      {
      	page = pages[i];
         
         if (page != null) {
         	html += "<div" + (i == index - 1 ? "" : " class=\"hidden\"") + ">";
            
            for (var j = 0; j < page.length; j++) {
               if (page[j][1] >= 0) {
               	html += page[j][0];
               }
            }
            
            html += "</div>";
         }
      }

      for (var i = 0; i < this.scripts.length; i++) {
      	$.getScript(this.scripts[i], function() {
         	//
         });
      }

		//$parent.html(html);
		$parent[0].innerHTML = html;
		$parent.find('a').attr('target', '_self').unbind().bind('click', this, function(event) {
      	event.preventDefault();
         event.data.link($(this));
      });

		this.element.find('.pagination')[pageCount == 1 ? 'hide' : 'show']();
		this.element.find('.pagination .previous').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination .next').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination input').val(index);
		this.element.find('.pagination .page .total').html('of ' + pageCount);
		this.element.find('.pagination .buttons').toggleClass('hidden', (pageCount == 1));
      
      $(window).trigger('resize');
		window.scrollTo(0, 0);
	},


   link: function($button) {
   	var pages = this._getData('pages');
      var page;
      var index;
      var html;
   	var url = $button.attr('href');
      var path = 'http://www.workingfamilies.org.uk/';
      
   	if (url.slice(0, 1) != '#') {
      	if (url.slice(0, path.length) == path) {
         	document.location = url;
         }
         else {
         	window.open(url);
         }
         
         return;
      }

      for (var i = 0; i < pages.length; i++) {
      	page = pages[i];
         html = '';
         
         for (var j = 0; j < page.length; j++) {
         	html += page[j][0];
         }

         if ($('<div />').append(html).find('a[name=' + url.slice(1) + ']').length > 0) {
         	index = i + 1;
         }
      }
      
      if (index != null) {
      	this.change(index);
      	window.scrollTo(0, this.element.find('.wysiwyg').find('a[name=' + url.slice(1) + ']').position().top);
      }
   }

});


$.widget('ui.results', {

	_init: function() {
		var data = JSON.parse(this.element.find('textarea:first').val());

		this.element.find('.menu').show();
		this.element.find('.menu ul li a').bind('click', this, function(event) {
			event.data.type($(this).closest('ul').find('a').index(this) + 1);
		});
		this.element.find('.pagination input').bind('keyup', this, function(event) {
			event.data.change(parseInt($(this).val(), 10));																			 
		});
		this.element.find('.pagination .previous, .pagination .next').bind('click', this, function(event) {
			event.data.change($(this).hasClass('previous') ? '-1' : '1');
		});
		this._setData('data', data);
		this.change(1);
   },


	change: function(offset) {
		var min, max;
		var limit = 10;
		var $items = this.element.find('.items li');
		var pageCount = Math.ceil($items.length / limit);

		switch (offset) {
      	case '-1':
         	index = (index == 1 ? pageCount : index - 1);
            break;
         case '1': 
         	index = (index == pageCount ? 1 : index + 1);
         	break;
			default:
         	index = offset;
      }
		
		if (isNaN(index)) return;
		
		$items.hide();

		index = Math.max(1, Math.min(index, pageCount));
		min = (index - 1) * limit;
		max = min + limit;
		
		for (var i = min; i < max; i++) {
			$items.eq(i).show();
		}
		
		$items.filter(':visible:first').addClass('first');
		$items.filter(':visible:last').addClass('last');

		this.element.find('.pagination')[pageCount == 1 ? 'hide' : 'show']();
		this.element.find('.pagination .previous').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination .next').toggleClass('disabled', (pageCount == 1));
		this.element.find('.pagination input').val(index);
		this.element.find('.pagination .page .total').html('of ' + pageCount);
		this.element.find('.pagination .buttons').toggleClass('hidden', (pageCount == 1));
      
      $(window).trigger('resize');
	},


	type: function(index) {
		var $buttons = this.element.find('.menu ul li a');
		var $button = $buttons.eq(index - 1);

		$buttons.removeClass('active');
		$button.addClass('active');

		this.element.find('.items').html(this._getData('data')[index - 1]);
		this.change(1);
	}

});

})(jQuery);


sIFR.fonts = [];
sIFR.fonts[0] = { src: 'http://www.workingfamilies.org.uk/assets/swf/fonts/arial-rounded-bold.swf', ratios: [7, 1.32, 11, 1.31, 12, 1.24, 16, 1.27, 19, 1.23, 24, 1.22, 33, 1.2, 37, 1.19, 38, 1.2, 46, 1.19, 50, 1.18, 51, 1.19, 74, 1.18, 75, 1.17, 76, 1.18, 81, 1.17, 82, 1.18, 1.17] };
sIFR.activate(sIFR.fonts[0]);
sIFR.reset = function(element) {
	$(element).removeClass('sIFR-replaced');
   $(element).html($(element).find('.sIFR-alternate').html());
}


setInterval(function() {
	var size = parseInt($('html').css('font-size'), 10);
   var sizeOld = $('html').data('font-size');

	if (size != sizeOld && sizeOld != null) $(window).trigger('resize');
   if (size != sizeOld) $('html').data('font-size', size);
}, 50);


$().ready(function() {
	if (!($.browser.msie && $.browser.version < 7)) $('.banner .info .title span, .banner .info .title .wysiwyg, .banner .info ul li a').corner({ tl: { radius: 2 }, tr: { radius: 2 }, bl: { radius: 2 }, br: { radius: 2 } });

   $('#header .search form').validate();
   $('#navigation').navigation();

   $(window).resize(function() {
		var $parent;

		$parent = $('#footer .links ul:first > li');
		$parent.equal('height');
      
      $('#legal .column1, #legal .column2').equal('height');
	});
	$(window).trigger('resize');

   $('#header .small, #header .medium, #header .large').click(function() {
   	var offset;

      switch ($(this).attr('class')) {
      	case 'small': offset = 1; break;
         case 'medium': offset = 1.25; break;
         case 'large': offset = 1.5; break;
      }

		$.cookie('font-size', (62.5 * offset));
      $('html').css('font-size', $.cookie('font-size') + '%');
   });

   if ($.cookie('font-size') != null) $('html').css('font-size', $.cookie('font-size') + '%');
   
   $('.wysiwyg').each(function() { 
   	$(this).find('table').css('width', '').css('height', '');
      $(this).find('> *:first').addClass('first');
      $(this).find('> *:last').addClass('last');
   });
   
   $('.advert:first-child').addClass('first');
   $('.advert:last-child').addClass('last');
   
   $('.faq-banner form').validate();
});
