/*!
 * (v) hrefID jQuery extention
 * returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);


/* 
 * Tiny scrollbar
 * http://baijs.nl/tinyscrollbar/
 */
(function(a){function b(b,c){function w(a){if(!(g.ratio>=1)){o.now=Math.min(i[c.axis]-j[c.axis],Math.max(0,o.start+((k?a.pageX:a.pageY)-p.start)));n=o.now*h.ratio;g.obj.css(l,-n);j.obj.css(l,o.now)}return false}function v(b){a(document).unbind("mousemove",w);a(document).unbind("mouseup",v);j.obj.unbind("mouseup",v);document.ontouchmove=j.obj[0].ontouchend=document.ontouchend=null;return false}function u(b){if(!(g.ratio>=1)){var b=b||window.event;var d=b.wheelDelta?b.wheelDelta/120:-b.detail/3;n-=d*c.wheel;n=Math.min(g[c.axis]-f[c.axis],Math.max(0,n));j.obj.css(l,n/h.ratio);g.obj.css(l,-n);b=a.event.fix(b);b.preventDefault()}}function t(b){p.start=k?b.pageX:b.pageY;var c=parseInt(j.obj.css(l));o.start=c=="auto"?0:c;a(document).bind("mousemove",w);document.ontouchmove=function(b){a(document).unbind("mousemove");w(b.touches[0])};a(document).bind("mouseup",v);j.obj.bind("mouseup",v);j.obj[0].ontouchend=document.ontouchend=function(b){a(document).unbind("mouseup");j.obj.unbind("mouseup");v(b.touches[0])};return false}function s(){j.obj.bind("mousedown",t);j.obj[0].ontouchstart=function(a){a.preventDefault();j.obj.unbind("mousedown");t(a.touches[0]);return false};i.obj.bind("mouseup",w);if(c.scroll&&this.addEventListener){e[0].addEventListener("DOMMouseScroll",u,false);e[0].addEventListener("mousewheel",u,false)}else if(c.scroll){e[0].onmousewheel=u}}function r(){j.obj.css(l,n/h.ratio);g.obj.css(l,-n);p["start"]=j.obj.offset()[l];var a=m.toLowerCase();h.obj.css(a,i[c.axis]);i.obj.css(a,i[c.axis]);j.obj.css(a,j[c.axis])}function q(){d.update();s();return d}var d=this;var e=b;var f={obj:a(".viewport",b)};var g={obj:a(".overview",b)};var h={obj:a(".scrollbar",b)};var i={obj:a(".track",h.obj)};var j={obj:a(".thumb",h.obj)};var k=c.axis=="x",l=k?"left":"top",m=k?"Width":"Height";var n,o={start:0,now:0},p={};this.update=function(a){f[c.axis]=f.obj[0]["offset"+m];g[c.axis]=g.obj[0]["scroll"+m];g.ratio=f[c.axis]/g[c.axis];h.obj.toggleClass("disable",g.ratio>=1);i[c.axis]=c.size=="auto"?f[c.axis]:c.size;j[c.axis]=Math.min(i[c.axis],Math.max(0,c.sizethumb=="auto"?i[c.axis]*g.ratio:c.sizethumb));h.ratio=c.sizethumb=="auto"?g[c.axis]/i[c.axis]:(g[c.axis]-f[c.axis])/(i[c.axis]-j[c.axis]);n=a=="relative"&&g.ratio<=1?Math.min(g[c.axis]-f[c.axis],Math.max(0,n)):0;n=a=="bottom"&&g.ratio<=1?g[c.axis]-f[c.axis]:isNaN(parseInt(a))?n:parseInt(a);r()};return q()}a.tiny=a.tiny||{};a.tiny.scrollbar={options:{axis:"y",wheel:40,scroll:true,size:"auto",sizethumb:"auto"}};a.fn.tinyscrollbar=function(c){var c=a.extend({},a.tiny.scrollbar.options,c);this.each(function(){a(this).data("tsb",new b(a(this),c))});return this};a.fn.tinyscrollbar_update=function(b){return a(this).data("tsb").update(b)};})(jQuery);

/*!
 * (v) Compact labels plugin (v20110124)
 * Takes one option: labelOpacity [default: true] set to false to disable label opacity change on empty input focus
 */
(function($){$.fn.compactize=function(options){var defaults={labelOpacity:true};options=$.extend(defaults,options);return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){if(options.labelOpacity){if(input.val()===''){label.css('opacity','0.5');}}else{label.hide();}});input.keydown(function(){label.hide();});input.blur(function(){if(input.val()===''){label.show();if(options.labelOpacity){label.css('opacity',1);}}});window.setInterval(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 */
jQuery.easing['jswing']=jQuery.easing['swing'];jQuery.extend(jQuery.easing,{def:'easeOutQuad',swing:function(x,t,b,c,d){return jQuery.easing[jQuery.easing.def](x,t,b,c,d)},easeInQuad:function(x,t,b,c,d){return c*(t/=d)*t+b},easeOutQuad:function(x,t,b,c,d){return-c*(t/=d)*(t-2)+b},easeInOutQuad:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t+b;return-c/2*((--t)*(t-2)-1)+b},easeInCubic:function(x,t,b,c,d){return c*(t/=d)*t*t+b},easeOutCubic:function(x,t,b,c,d){return c*((t=t/d-1)*t*t+1)+b},easeInOutCubic:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t+b;return c/2*((t-=2)*t*t+2)+b},easeInQuart:function(x,t,b,c,d){return c*(t/=d)*t*t*t+b},easeOutQuart:function(x,t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b},easeInOutQuart:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t+b;return-c/2*((t-=2)*t*t*t-2)+b},easeInQuint:function(x,t,b,c,d){return c*(t/=d)*t*t*t*t+b},easeOutQuint:function(x,t,b,c,d){return c*((t=t/d-1)*t*t*t*t+1)+b},easeInOutQuint:function(x,t,b,c,d){if((t/=d/2)<1)return c/2*t*t*t*t*t+b;return c/2*((t-=2)*t*t*t*t+2)+b},easeInSine:function(x,t,b,c,d){return-c*Math.cos(t/d*(Math.PI/2))+c+b},easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b},easeInOutSine:function(x,t,b,c,d){return-c/2*(Math.cos(Math.PI*t/d)-1)+b},easeInExpo:function(x,t,b,c,d){return(t==0)?b:c*Math.pow(2,10*(t/d-1))+b},easeOutExpo:function(x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2,-10*t/d)+1)+b},easeInOutExpo: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},easeInCirc:function(x,t,b,c,d){return-c*(Math.sqrt(1-(t/=d)*t)-1)+b},easeOutCirc:function(x,t,b,c,d){return c*Math.sqrt(1-(t=t/d-1)*t)+b},easeInOutCirc:function(x,t,b,c,d){if((t/=d/2)<1)return-c/2*(Math.sqrt(1-t*t)-1)+b;return c/2*(Math.sqrt(1-(t-=2)*t)+1)+b},easeInElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return-(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b},easeOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d)==1)return b+c;if(!p)p=d*.3;if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);return a*Math.pow(2,-10*t)*Math.sin((t*d-s)*(2*Math.PI)/p)+c+b},easeInOutElastic:function(x,t,b,c,d){var s=1.70158;var p=0;var a=c;if(t==0)return b;if((t/=d/2)==2)return b+c;if(!p)p=d*(.3*1.5);if(a<Math.abs(c)){a=c;var s=p/4}else var s=p/(2*Math.PI)*Math.asin(c/a);if(t<1)return-.5*(a*Math.pow(2,10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p))+b;return a*Math.pow(2,-10*(t-=1))*Math.sin((t*d-s)*(2*Math.PI)/p)*.5+c+b},easeInBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*(t/=d)*t*((s+1)*t-s)+b},easeOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;return c*((t=t/d-1)*t*((s+1)*t+s)+1)+b},easeInOutBack:function(x,t,b,c,d,s){if(s==undefined)s=1.70158;if((t/=d/2)<1)return c/2*(t*t*(((s*=(1.525))+1)*t-s))+b;return c/2*((t-=2)*t*(((s*=(1.525))+1)*t+s)+2)+b},easeInBounce:function(x,t,b,c,d){return c-jQuery.easing.easeOutBounce(x,d-t,0,c,d)+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}},easeInOutBounce:function(x,t,b,c,d){if(t<d/2)return jQuery.easing.easeInBounce(x,t*2,0,c,d)*.5+b;return jQuery.easing.easeOutBounce(x,t*2-d,0,c,d)*.5+c*.5+b}});

 
(function($) {
	"use strict";
	var CC_base_url = window.CC_base_url || '/';
	
	$(document).bind('refresh.pie',function() {
		if($.browser.msie && parseInt($.browser.version,10) < 9){
			if(typeof window.PIE !== 'undefined'){							
				$('.form-a button').each(function() {
					window.PIE.detach(this);
					window.PIE.attach(this);
				});
			}
		}
	});



	//OVERLAY
	var CampusCooksOverlay = function() {
		this.overlay = $('<div />',{
			id : "overlay",
			css : {
				opacity : 0.85
			}
		});
		this.resize();

		$('body').append(this.overlay);		
		$(window).resize($.proxy(this.resize, this));		
	};

	CampusCooksOverlay.prototype.resize = function(){
		this.overlay.css({ 
			'height' : $(document).height()
		});
	};

	CampusCooksOverlay.prototype.remove = function(){
		this.overlay.css('opacity',1).fadeOut(500, function(){
			$(this).remove();
		});
	};

	//MAIN OBJECT
	var CampusCooks = function(){
		this.pageCache = {};
		this.currentHash = null;
		this.allowedHashes = [];
		this.currentPosition = null;
		this.windowWidth = $(window).width();

		//init
		this.checkWindowSize();
		this.preload();
		this.links();		

		$(document).bind('resize.campuscooks',$.proxy(function(){
			this.updatePageHeight();
		},this));
	};

	CampusCooks.prototype.checkWindowSize = function(){
		this.windowWidth = $(window).width();							
				
		var currwidth; 
		
		$(window).resize($.proxy(function(){
			
			if(currwidth !== document.documentElement.clientWidth)
			{
				this.windowWidth = $(window).width();
				//update pages position
				$('div.page').each($.proxy(function(index,el){
					$(el).stop().css({
						'left' : (0.5 + index) * this.windowWidth
					});
				},this));

				var offset = - this.currentPosition * parseInt(this.windowWidth, 10) + 'px';
				$('#content-wrapper').stop().css({
					'left' : offset
				});
			}
			currwidth = document.documentElement.clientWidth;
						


		},this));		
		
	};

	CampusCooks.prototype.links = function(){				
		$('body').on('click','a',$.proxy(function(e){
			var 
				target = $(e.target).closest('a'),
				href = target.attr('href'),
				hash;
				
				
			//home page
			if (href === CC_base_url) {
				hash = '#home';
			}
			else {
				var 
					rx = new RegExp('^' + CC_base_url + '([_a-zA-Z0-9]+)$'),
					matches = rx.exec($(target).attr('href'));
					
				if (!matches) {
					return true;
				}
				hash = '#' + matches[1];
			}
					
			if ($.inArray(hash, this.allowedHashes) === -1) {
				return true;
			}

			e.preventDefault();			
			window.location.hash = hash;
		},this));

		//external links
		$('a[rel*=external]').live('click',function(e){
			e.preventDefault();
			window.open($(this).attr('href'));						  
		});		
		
		$('form.external').attr('target','_blank');
	};	

	//Preload all pages and sets current page
	CampusCooks.prototype.preload = function(){

		//add overlay and loading graphics
		var overlay = new CampusCooksOverlay();

		$('body').append('<img src="images/loader.gif" alt="Loading" height="17" width="128" id="loading" />');

		//launched after all pages finished loading
		$(document).bind('loaded.campuscooks',$.proxy(function(){

			//activate valid page
			this.hashCheck(true,true);	

			//remove overlay and loading graphics
			$('html').addClass('loaded');
			overlay.remove();

			$('#loading').fadeOut(500, function(){
				$('#loading').remove();
			});			

			// initialize hash check for back button + manual changes functionality
			
			$(window).on('hashchange', $.proxy(function(){
				this.hashCheck();
			},this));
			
		},this));

		/* load content of the main sections */
		this.loadMainPages();

	};

	//Load content for all pages from navigation
	CampusCooks.prototype.loadMainPages = function(){

		var currentSite = $('body').attr('class');
		$('body').removeClass(currentSite);

		var sites = $('#nav a').map($.proxy(function(index, item){
			var href = $(item).attr('href');
			if (href.indexOf('#') >= 0) {
				return;
			}
			
			href = href.replace(CC_base_url,'');

			//special case for the home page link
			var className = (href === '' ? 'home' : href);

			//set current position
			if (currentSite === className) {
				this.currentPosition = index;
				if (window.location.hash === '') {
					window.location.hash = '#'+className;	
				}				
			}

			//add page to the allowed hashes table
			this.allowedHashes.push('#'+className);

			return { 
				url : CC_base_url + href, 
				className : className
			};
		},this));	

		/* add background item to menu */
		$('#nav').append('<span class="indicator"></span>');			

		var 
			loadedPages = 0,
			totalPages = sites.length;			

		//replace the current page content with an empty wrapper
		var contentWrap = $('<div id="content-wrapper" />');		
		$('#content').html(contentWrap);		


		//load content for all items in the navigation
		$.each(sites, $.proxy(function(index, site) {
			var container = $('<div/>',{
				'class' : site.className + ' page',
				'css' : {
					'left' : (0.5 + index) * this.windowWidth
				}
			});

			//append new page container to document				
			contentWrap.append(container);	

			//load content for all pages
			$.ajax({
				url: site.url,
				dataType: "html",
				complete: $.proxy(function(res, status){
					// If successful, inject the HTML into all the matched elements
					if ( status === "success" || status === "notmodified" ) {
						var html = $("<div/>").append(res.responseText.replace(/<script(.|\s)*?\/script>/g, "")).find('div.page').html();
						$(container).html(html);

						loadedPages++;
						if (loadedPages === totalPages){
							$(document).trigger('loaded.campuscooks');
						}
					}
				},this)
			});

		},this));
	}; //load Main Pages


	CampusCooks.prototype.hashCheck = function(){

		var 
			initial = arguments[0] || false,
			instant = arguments[1] || false;
		
		//if hash is empty, set inital value to #home
		if (window.location.hash === ''){
			window.location.hash = '#home';
		}		

		this.currentHash = window.location.hash;

		var index = $.inArray(this.currentHash, this.allowedHashes);
		if (index >= 0) {
			this.activatePage(index, { instant: instant, initial : initial } );
		}
	};	

	//activate page
	CampusCooks.prototype.activatePage = function(position){		
		var options = arguments[1] || {};

		var defaults = {
			instant : false,
			initial : false
		};

		options = $.extend({}, defaults, options);

		if (this.currentPosition === position && options.instant === false) {
			return;
		}			

		//navigation switch		
		this.updateNavigation(position, options.instant);

		//content slide
		this.slideContent(position, options.instant);
		
		$(document).trigger('refresh.pie');
	};

	//animate navigation background
	CampusCooks.prototype.updateNavigation = function(position, instant){
		//remove active from all links
		var 
			active = $('#nav li').removeClass('active').eq(position).addClass('active'),
			a = active.find('a'),
			offset = (position === 0 ? 0 : active.get(0).offsetLeft);

		//sliding background
		if (instant) {
			$('#nav span.indicator').stop().css( { "left": offset, "width" : a.width() });
		}
		else {
			$('#nav span.indicator').stop().animate( { "left": offset, "width" : a.width() } , 500, 'easeInOutBack');
		}		
	};

	CampusCooks.prototype.slideContent = function(position, instant){
		var offset = - position * parseInt(this.windowWidth, 10) + 'px';

		this.currentPosition = position;
		this.updatePageHeight(instant);		

		var container = $('#content-wrapper');

		if (instant) {
			container.stop().css({ 'left': offset });	
		}
		else {			
			container.stop().animate({ 'left': offset }, 3500, 'easeOutExpo');			
		}		
	};

	//update page height for changing content
	CampusCooks.prototype.updatePageHeight = function(){		
		var 
			height = $('div.page').eq(this.currentPosition).height(),
			instant = arguments[0] || false;

		if (instant) {
			return $('#content').stop().css({ 'height': height });
		}

		$('#content').stop().animate( { 'height': height }, 2500, 'easeOutExpo' );		
	};

	//MENU
	var CampusCooksMenu = function(){
		this.menus = $('table.menu');

		if (this.menus.length < 2) {
			return;
		}

		this.current = 0;		
		this.menus.not(':first').hide();		
		this.buildNavigation();
	};

	CampusCooksMenu.prototype.move = function(offset){

		this.menus.eq(this.current).fadeOut();

		this.current = this.current + offset;
		if (this.current < 0) {
			this.current = this.menus.length - 1;
		}
		else if (this.current >= this.menus.length) {
			this.current = 0;
		}

		this.menus.eq(this.current).fadeIn('slow');		
	};

	CampusCooksMenu.prototype.buildNavigation = function(){

		var nav = $('<div id="menu-navigation" />');

		var prevTrigger = $('<a />',{
			'href' : '#',
			'class' : 'prev',
			'text' : 'Previous menu',
			'click' : $.proxy(function(e){
				e.preventDefault();
				this.move(-1);
			},this)
		});

		var nextTrigger = $('<a />',{
			'href' : '#',
			'class' : 'next',
			'text' : 'Next menu',
			'click' : $.proxy(function(e){
				e.preventDefault();
				this.move(1);
			},this)
		});

		nav.append(prevTrigger , $(' <span>|</span> '), nextTrigger);		
		
		$('table.menu:last').after(nav);		
	};


	//CONTACT FORM
	var CampusCooksContactForm = function(){
		this.form = $('#contact-form');
		this.isValid = true;

		if (this.form.length === 0) {
			return;			
		}

		this.form.submit($.proxy(function(e){
			var form = this.form;
			
			e.preventDefault();
			this.clearErrors();
			if (this.validate() === true){
				$.post(form.attr('action'),this.form.serialize(),function(){
					form.find('input[type=text]').val('');
					form.find('textarea').val('');
					form.find('fieldset input[type=radio]').removeAttr('checked');
					form.find('fieldset input[type=radio]:eq(0)').attr('checked','checked');
					
					var msg = $('<p class="info-a">Thank you for sending your message.</p>');
					form.before(msg);
					msg.fadeIn();
					
					setTimeout(function(){
						msg.fadeOut(function(){
							$(this).remove();
							$(document).trigger('resize.campuscooks');							
						});
					},10000);
					$(document).trigger('resize.campuscooks');
				});
			}
		},this));		
	};

	CampusCooksContactForm.prototype.clearErrors = function(){
		this.form.find('strong.err').remove();
		var label = this.form.find('.required').prev('label');

		label.find('em').remove(); //make sure there are no duplicates
		label.append('<em>*</em>');
	};

	CampusCooksContactForm.prototype.addError = function(el, msg) {
		this.isValid = false;
		$(el).after('<strong class="err">'+msg+'</strong>');
		$(el).prev('label').find('em').remove();
	};

	CampusCooksContactForm.prototype.validate = function(){
		this.isValid = true;
		
		this.form.find('.required').each($.proxy(function(index, el){
			if ($.trim($(el).val()) === '') {
				this.addError(el, 'required');
			}
		},this));
		
		this.form.find('.email').each($.proxy(function(index, el){
			var v = $.trim($(el).val());
			if (v === '') {
				return;
			}
			
			if (/^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,6}$/i.test(v) === false) {
				this.addError(el, 'invalid');
			}
		},this));		

		$(document).trigger('resize.campuscooks');

		return this.isValid;					
	};	


	//TESTIMONIALS
	var CampusCooksTestimonials = function(){
		this.count = 0;				

		$('a.testimonial-trigger').live('click',$.proxy(function(e){
			e.preventDefault();			
			this.build(e.target);
		},this));	
	};

	CampusCooksTestimonials.prototype.build = function(trigger){

			var overlay = new CampusCooksOverlay();

			$('body').append('<div id="testimonial-box"><a href="#" class="close">Close</a> <a href="#" class="prev">Previous item</a> <a href="#" class="next">Next item</a><div id="twrap"></div></div>');

			this.load(trigger);

			$('#testimonial-box a.close').click($.proxy(function(e){
				e.preventDefault();
				overlay.remove();
				$('#testimonial-box').fadeOut(500, function(){
					$("#testimonial-box").remove();
				});					
			},this));

			$('#testimonial-box a.prev').click($.proxy(function(e){
				e.preventDefault();
				this.prev();
			},this));				

			$('#testimonial-box a.next').click($.proxy(function(e){
				e.preventDefault();
				this.next();
			},this));				
		};

	CampusCooksTestimonials.prototype.load = function(trigger){

			var append = function(item){
				var html = '<div class="testimonial"><blockquote>'+$(item).find('blockquote').html()+'</blockquote>';
				html += '<p>'+$(item).find('h2').html()+'</p>';
				html += '<p>'+$(item).find('p.about').html()+'</p>';
				$('#twrap').append(html);
			};

			var matches = /^([^#]+)(#([\-a-zA-Z0-9_]+))?$/.exec($(trigger).attr('href'));			

			if (matches === null){
				throw "Invalid testimonials link";
			}

			var testimonialId = matches[3] || null;

			$.ajax({
				url: matches[1],
				dataType: "html",
				complete: $.proxy(function(res, status){
					// If successful, inject the HTML into all the matched elements
					if ( status === "success" || status === "notmodified" ) {

						var temp = $("<div/>").append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""));

						var position = 0;
						temp.find('div.testimonial').each($.proxy(function(index, item){
							append(item);
							this.count++;

							if (testimonialId === item.id) {
								position = index;
							}
						},this));

						this.reposition(position, true);
					}
				},this)
			});
		};

	CampusCooksTestimonials.prototype.reposition = function(){

		var position = arguments[0] || 0;
		var instant = arguments[1] || false;

		this.active = position;

		var diff = 550;
		var widthUnit = 'px';

		$('#testimonial-box div.testimonial').each(function(index, item){
			var offset = parseInt((index - position + 0.5) * diff, 10) + widthUnit;

			if (instant) {
				$(item).stop().css({ 'left': offset });	
			}
			else {
				$(item).stop().animate({ 'left': offset }, 2500, 'easeOutExpo');
			}
		});					
	};



	CampusCooksTestimonials.prototype.prev = function(){
		if (this.active === 0) {
			return false;
		}
		this.active = this.active - 1;

		this.reposition(this.active);
	};

	CampusCooksTestimonials.prototype.next = function(){
		if (this.active === this.count - 1) {
			return false;
		}
		this.active = this.active + 1;

		this.reposition(this.active);
	};

	//clients
	var CampusCooksClients = function(){
		this.count = 0;				

		$('div.clients li a, a.client-trigger, #customers-copy .badge a').live('click',$.proxy(function(e){
			e.preventDefault();			
			this.build($(e.target).closest('a'));
		},this));	
	};

	CampusCooksClients.prototype.build = function(trigger){
		
		var overlay = new CampusCooksOverlay();

		$('body').append('<div id="client-box"><a href="#" class="close">Close</a></div>');

		this.root = $('#client-box');
		
		this.load(trigger);

		this.root.find('a.close').click($.proxy(function(e){
			e.preventDefault();
			overlay.remove();
			this.root.fadeOut(500, function(){
				$(this).remove();
			});					
		},this));

	};

	CampusCooksClients.prototype.load = function(trigger){

		var matches = /^([^#]+)(#([\-a-zA-Z0-9_]+))?$/.exec($(trigger).attr('href'));			

		if (matches === null){
			throw "Invalid client link";
		}

		var clientId = matches[3] || null;
		
		$.ajax({
			url: matches[1],
			dataType: "html",
			complete: $.proxy(function(res, status){
				// If successful, inject the HTML into all the matched elements
				if ( status === "success" || status === "notmodified" ) {

					var temp = $("<div/>").append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""));
					
					this.root.append(temp.find('#clients'));
					
					var index = this.root.find('article').index($('#' + clientId));
					
					this.scrollbar();
										
					var triggers = this.root.find('nav a');
					triggers.click($.proxy(function(e){
						e.preventDefault();
						var index = this.root.find('article').index($($(e.target).hrefId()));
						triggers.removeClass('active');
						$(e.target).addClass('active');
						this.move(index);
					},this));
																					
					triggers.eq(index).triggerHandler('click');
				}
			},this)
		});
	};

	CampusCooksClients.prototype.scrollbar = function(){
		this.root.find('nav').wrapInner('<div class="viewport"><div class="overview"></div></div>').prepend('<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>').addClass('scrolled').data('top',0);
		
		this.root.find('nav').tinyscrollbar({ sizethumb : 18 });
	};

	CampusCooksClients.prototype.move = function(){

		var 
			position = arguments[0] || 0,
			instant = arguments[1] || false;

		if (position < 0) {
			position = 0;
		}

		this.active = position;

		this.root.find('article:visible').hide();
		this.root.find('article').eq(position).fadeIn(instant ? 0 : 'fast');
	};

	//initialization code
	$(document).bind('loaded.campuscooks',function(){
		
		var ccTestimonials = new CampusCooksTestimonials();
		var ccClients = new CampusCooksClients();
		var ccContactForm = new CampusCooksContactForm();		
		var ccMenu = new CampusCooksMenu();
				
		$('div.newsletter form label').compactize();
	});

	CampusCooks = new CampusCooks();
	
}(jQuery));

//IE7 hashchange fix
(function($){
	"use strict";	
	if (!$.browser.msie || (parseInt($.browser.version,10) > 7)) {
		return;
	}
	
	var hash = window.location.hash;
	setInterval(function(){
		if (hash !== window.location.hash) {
			hash = window.location.hash;
			$(window).trigger('hashchange');
		}
	},50);	
	
	$(document).bind('loaded.campuscooks',function(){
		$('#customers-copy,#menu-navigation').append('<span class="after"/>');
		$('#customers-copy .clients').prepend('<span class="before"/>');
	});	

}(jQuery));
