// remove the "$" namespace from jQuery, avoids conflicts with other libraries
jQuery.noConflict();

// closure, mapping jQuery to $, window, document and undefined - useful for minifing tools
(function($, window, document, undefined){

// document ready method
$(function(){

	var $window = $(window),
		$body = $(document.body),
		$bg = $('#bg'),
		$page = $('#page'),
		$header = $('#header'),
		$container = $('#container'),
		$menu = $('#menu'),
		$canvas = $('#canvas');

	if ($.browser.mobile == false)
	{
		// menu hovers
		var tMenu = null,
			hideMenus = function(){
				$menu.find('.level-1 li').removeClass('sub');
				$menu.children('.level-2-active').removeClass('level-2-active');
			};
	
		$menu.find('.level-1 li').each(function(){
			$.data(this, 'sub', (this.id.length ? $('#' + this.id.replace('item-', '')) : $()));
		}).hover(function(){
			clearTimeout(tMenu);
			hideMenus();
			$.data(this, 'sub').addClass('level-2-active');
			$(this).addClass('sub');
		}, function(){
			tMenu = setTimeout(hideMenus, 300);
		});
		$menu.children('.level-2').hover(function(){
			clearTimeout(tMenu);
		}, function(){
			tMenu = setTimeout(hideMenus, 300);
		});

		if ($.browser.msie)
			$body.addClass('ie' + parseInt($.browser.version, 10));
	}
	else
	{
		if (location.pathname)
		{
			var path = location.pathname.split('/');
			var section = '';
			
			for (i=0; i<path.length; i++)
			{
				if (path[i] == 'about' || path[i] == 'work' || path[i] == 'news')
				{
					section = path[i];
					break;
				}
			}
			
			if (section == 'about')
				$('#sub-1').addClass('level-2-active');
			else if (section == 'work')
				$('#sub-2').addClass('level-2-active');
			else if (section == 'news')
				$('#sub-3').addClass('level-2-active');
		}
	}
	
	$(function() {
		$('a.learnmore').click(
			function() 
			{
				$('p.landingintro').slideToggle(300,function(){ doResize(); });
				$('a.learnmore').css({'display' : 'none'});
				$('.learnmore-close').fadeIn(500).css({'display' : 'block'});
				return false;		 
			});
		});
	
	$(function() {
		$('.learnmore-close').click(
			function() {
				$('p.landingintro').slideToggle(300,function(){ doResize(); });
				$('.learnmore-close').css({'display' : 'none'});
				$('a.learnmore').fadeIn(100);
				return false;
			});
		});




	// gallery thumbnails
	$('.gallery1-thumbs').each(
		function() 
		{
			var container = $(this), 
				id = this.id.replace('-triggers', ''), 
				gallery = $('#' + id), 
				links = container.find('.gallery-thumbs-row a');
				
			container
				.find('a')
				.click(
					function()
					{
						if ($(this).find('span').length == 0)
							$(this).append('<span />');
						
						var image = $(this).find('img');
							
						$(this)
							.find('span')
							.css({
								'height': $(image).height()-6,
								'width': $(image).width()-6,
								'left': $(image).position().left + 'px',
								'top': $(image).position().top + 'px'	 
							});
							
						var self = $(this), 
							prev = gallery.find('img').eq(0),
							next = gallery.append('<img class="next" />').find('.next'),
							img = new Image();
							
						img.onload = function() {
							next.attr('src', self.attr('href'));
							links.removeClass('active')
							self.addClass('active');
							prev.fadeOut(700, function() {
								prev.remove();
								next.removeClass('next');
							});
						};
						
						img.src = this.href;
						
						return false;
					});
		});



	// NEWS gallery thumbnails
	$('.newsgallery1-thumbs').each(
		function() 
		{
			var container = $(this), 
				id = this.id.replace('-triggers', ''), 
				gallery = $('#' + id), 
				links = container.find('.newsgallery-thumbs-row a');
				
			container
				.find('a')
				.click(
					function()
					{
						if ($(this).find('span').length == 0)
							$(this).append('<span />');
						
						var image = $(this).find('img');
							
						$(this)
							.find('span')
							.css({
								'height': $(image).height()-6,
								'width': $(image).width()-6,
								'left': $(image).position().left + 'px',
								'top': $(image).position().top + 'px'	 
							});
							
						var self = $(this), 
							prev = gallery.find('img').eq(0),
							next = gallery.append('<img class="next" />').find('.next'),
							img = new Image();
							
						img.onload = function() {
							next.attr('src', self.attr('href'));
							links.removeClass('active')
							self.addClass('active');
							prev.fadeOut(100, function() {
								prev.remove();
								next.removeClass('next');
							});
						};
						
						img.src = this.href;
						
						return false;
					});
		});


	
	if ($.fn.jCarouselLite)
	{
		var slideshows = $('#team, #stories');
		if (slideshows.length)
			slideshows.each(function(){
				var sl = $(this), parent = (this.id == 'team' ? sl.find('.wrap') : sl);
				sl.find('figure').append('<div></div>');
				parent.append('<p class="nav"><a href="#" class="prev disabled"></a><a href="#" class="next"></a></p>');
				sl.find('.slider').jCarouselLite({
					btnPrev: sl.find('.prev'),
					btnNext: sl.find('.next'),
					visible: 4,
					scroll: 1,
					speed: 500,
					circular: false
				});
			});
	}
	

	if ($.fn.selects)
	{
		$('.select1').selects({ skipFirst: true });
	}

	
	if ($.fn.dynamicLabel)
	{
		$('input.dynamic').dynamicLabel();
	}
	
	if ($.fn.boxLinks)
	{
		$('#projects li, #news-bars li, #blue-design, #events li, #news-list li, #stories li').boxLinks();
		$('#team li').boxLinks({
			onEnter: function(li){
				li.find('figcaption').animate({ bottom: 0 }, 250);
			},
			onLeave: function(li){
				li.find('figcaption').animate({ bottom: '-65px' }, 250);
			}
		});
	}
	
	if ($.fn.equalHeights)
	{
		function eq(){
			if ($body.hasClass('inside'))
				$('#content, #side1').equalHeights();
			$('#container').find('.equal1').equalHeights();
		}
		if ($.browser.msie && $.browser.version < 9)
			setTimeout(eq, 100);
		else
			eq();
	}



	var newsSlider = $('#news-slider');
	if (newsSlider.length)
	{
		function ns(){
			var hin = newsSlider.height(),
				hout = newsSlider.outerHeight(),
				pin = { height: hin + 'px' },
				pout = { height: '22px' };
			newsSlider.css(pout).hover(function(){
				newsSlider.stop().animate(pin, 350).addClass('active');
			}, function(){
				newsSlider.stop().animate(pout, 350).removeClass('active');
			});
		}
		if ($.browser.msie && $.browser.version < 9)
			setTimeout(ns, 50);
		else
			ns();
	}



	// stories interactions
	if ($canvas.length)
	{
		var drag = $('#canvas-drag'), 
			details = $('#details'),
			slides = drag.children(),
			stories = drag.find('.story'),
			slideHeight = slides.eq(0).height(),
			slideWidth = slides.eq(0).width(),
			totalWidth = slides.length * slideWidth,
			maxLeft = - totalWidth + $window.width(),
			maxTop = - slideHeight + $window.height(),
			lastPos = 0, lastDrag = 0, dragging = false;
//			lastX = 0, lastY = 0, timerBg = null;

		drag.width(totalWidth);
		slides.css({ position: 'relative', top: 0, left: 0, marginLeft: 0 });


		function activeIndex(){
			return Math.abs(Math.round(parseInt(drag.css('left')) / slideWidth));
		}

		
		$canvas.append('<p class="nav"><a href="#" id="prev"></a><a href="#" id="next"></a></p>').find('.nav a').
			css({ top: Math.round($body.height() / 2) + 'px' }).
			click(function(){
				var index = activeIndex(),
					next = index + (this.id == 'next' ? 1 : -1);
				centerSlide(slides.eq(next), function(){});
				return false;
			});
		var navPrev = $('#prev'), navNext = $('#next');
		navNext.css({ right: (15) + 'px' });
		/*navNext.css({ left: ($body.width() - 55) + 'px' });*/

		function updateNav(){
			var index = activeIndex();
			navPrev[(index == 0 ? 'addClass' : 'removeClass')]('disabled');
			navNext[(index + 1 == slides.length ? 'addClass' : 'removeClass')]('disabled');
		}


		function centerSlide(slide, onComplete){
			var left = slide.index() * slideWidth,
				l = left + Math.round((slideWidth - $window.width()) / 2);

			l = Math.min(Math.max(maxLeft, -l), 0);

			if (onComplete)
			{
				dragging = true;
				$('.slide').css('cursor','-moz-grabbing');
				
				drag.animate({ left: l + 'px'}, 500, function(){
					updateNav();
					onComplete();
					$('.slide').css('cursor','-moz-grab');
					dragging = false;
				})
			}
			else
			{
				drag.css({ left: l + 'px'});
				updateNav();
			}
		}
		centerSlide(slides.filter('.initial'));


		stories.append('<span class="icon"></span>').each(function(){
			var self = $(this), det = self.find('.details');
			$.data(det[0], 'size', {
				width: det.width() + 'px',
				height: det.height() + 'px'
			});
			$.data(this, 'sizeLarge', {
				width: self.width() + 'px',
				height: self.height() + 'px'
			});
			det.children('.content').height(det.height() - 20).
				children('.scroll').height(det.height() - 20);
		});

		if ($.fn.scrolling)
		{
			stories.find('.scroll').scrolling({
				handleHeight: 100
			});
		}

		stories.find('.details').hide();

		stories.each(function(){
			var self = $(this), det = self.find('.details');
			$.data(this, 'sizeSmall', {
				width: (self.width() + 10) + 'px',
				height: self.height() + 'px'
			});
//			console.log(self, $.data(this, 'sizeLarge'), $.data(this, 'sizeSmall'), $.data(det[0], 'size'));
		});

		stories.mousedown(function(ev){
			ev.stopPropagation();
		}).click(function(ev){
			var self = $(this),
				det = self.find('.details'),
				sizeDetails = $.data(det[0], 'size'),
				sizeLarge = $.data(this, 'sizeLarge'),
				sizeSmall = $.data(this, 'sizeSmall');

//			console.log(sizeDetails, sizeSmall, sizeLarge);

			if (self.hasClass('active'))
			{
				if (ev.target.className == 'icon')
				{
					det.animate({ width: 0, height: 0}, 200, function(){
						det.hide();
					});
					self.removeClass('active').animate(sizeSmall, 200);
				}
			}
			else
			{
				var other = stories.filter('.active');
				if (other.length)
					other.find('.icon').click();

				self.animate(sizeLarge, 300).addClass('active');
				det.width(0).height(0).show().animate(sizeDetails, 300);
			}
			return false;
		});

		
		var i = 1, assets = details.find('.other').append('<h5>Project Assets</h5><ul class="assets"></ul>').find('.assets');

		stories.filter('.asset').each(function(){
			var self = $(this),
				type = self.find('.title strong').html(),
				title = self.find('.title em').html();
			this.id = 'asset-' + (i++);
			assets.append('<li class="' + this.className + '"><a href="#' + this.id + '">' + type + ': ' + title + '</a></li>');
		});
		assets.find('a').click(function(){
			details.mouseleave();

			var self = $(this), asset = $(this.hash), pos = asset.position(),
				size = $.data(asset[0], 'sizeLarge'),
				slide = asset.parent(),
				l = slide.index() * slideWidth + pos.left - Math.round(($window.width() - parseInt(size.width)) / 2),
				t = pos.top - Math.round(($window.height() - parseInt(size.height)) / 2);

			l = Math.min(Math.max(maxLeft, -l), 0);
			t = Math.max(Math.min(0, -t), maxTop);

			asset.find('.icon').click();

			dragging = true;
			$('.slide').css('cursor','-moz-grabbing');
			drag.animate({
				left: l + 'px',
				top: t + 'px'
			}, 800, function(){
				updateNav();
				$('.slide').css('cursor','-moz-grab');
				dragging = false;
			});

			return false;
		});


		drag.mousedown(function(ev){
			var a = stories.filter('.active');
			if (a.length)
				a.find('.icon').click();
		}).draggable({
			containment: [maxLeft, maxTop, 0, 0],
			cursor: 'pointer',
			scroll: false,
			start: function(ev, ui){
				lastDrag = 0;
				lastPos = ui.offset.left;
				$('.slide').css('cursor','-moz-grabbing');
				dragging = true;
				drag.stop();
			},
			drag: function(ev, ui){
				lastDrag = ui.offset.left - lastPos;
				lastPos = ui.offset.left;
			},
			stop: function(ev, ui){
				var left = ui.position.left;
				if (Math.abs(lastDrag) > 3)
				{
					var l, d = Math.abs(lastDrag),
						diff = Math.round((d % 10) * 15 + (d / 10) * 30);
					if (lastDrag < 0)
						diff = Math.min(Math.abs(maxLeft - left), diff);
					else
						diff = Math.min(Math.abs(left), diff);
					l = (lastDrag > 0 ? '+=' : '-=') + diff + 'px';
					drag.animate({ left: l }, 300, 'easeOutSine', function(){
						$('.slide').css('cursor','-moz-grab');
						dragging = false;
					});
				}
				else
				{
					$('.slide').css('cursor','-moz-grab');
					dragging = false;
				}
				updateNav();
			}
		});


		if ($.fn.scrolling)
		{
			details.find('.scroll').scrolling({
				handleHeight: 100
			});
		}

		var hin = details.height(),
			hout = details.outerHeight(),
			htitle = details.find('h3').height(),
			pin = { height: hin + 'px' },
			pout = { height: htitle + 'px' },
			timerD1 = null, timerD2 = null;

		details.css(pout).hover(function(){
			clearTimeout(timerD2)
//			timerD1 = setTimeout(function(){
				details.stop().animate(pin, 350).addClass('active');
//			}, 500);
		}, function(){
			if ($('#colorbox').is(':visible'))
				return;
			timerD2 = setTimeout(function(){
//				clearTimeout(timerD1);
				details.stop().animate(pout, 350).removeClass('active');
			}, 1000);
		});

		if ($.fn.colorbox)
		{
			details.find('.scroll a.colorbox').colorbox({
				rel: 'scroll',
				current: '',
				opacity: 0.75,
				onOpen: function(){
					cbNext.add(cbPrev).css({ visibility: 'hidden' });
				}
			});
			
			var cbContent = $('#cboxContent'),
				cbNext = $('#cboxNext'),
				cbPrev = $('#cboxPrevious');

			$('#cboxContent').bind('mousemove', function(ev){
				var pos = cbContent.offset(), w = cbContent.width() / 2, l = ev.pageX - pos.left;
				cbPrev.css({ visibility: (l < w ? 'visible' : 'hidden') });
				cbNext.css({ visibility: (l > w ? 'visible' : 'hidden') });
			});
		}


		function doResize(){
			navNext.css({ left: ($body.width() - 55) + 'px' });
		}
		doResize();
		$(window).resize(doResize);
		//$('#details').trigger('mouseenter');

		$('#details a.controller').click(
			function()
			{	
				if ($('#details').hasClass('active'))
				{
					var hin = details.height(),
						hout = details.outerHeight(),
						htitle = details.find('h3').height(),
						pin = { height: hin + 'px' },
						pout = { height: htitle + 'px' },
						timerD1 = null, timerD2 = null;
						
					details.stop().animate(pout, 350).removeClass('active');
				}
				
				return false;
			});
	}



	// page & body height adjustments
	function doResize()
	{
		var h = Math.max($window.height(), $body.height(), $page.height());
		$bg.height(h);
	}

	// fix page height
	if ($body.hasClass('inside'))
	{
		var hw = $window.height(), hh = $header.outerHeight(), hc = $container.outerHeight();
		if (hw > hh + hc)
			$container.height(hc + (hw - hh - hc) - (hc - $container.height()));
	}

	if ($body.hasClass('landing'))
	{
		var img = $bg.append('<img/>').children('img');
		if (r = $bg.css('backgroundImage').match(/url\(['"]?([^"']+)['"]?\)/))
		{
			img.attr('src', r[1]);
			$bg.css('backgroundImage', 'none');
		}

		doResize();
		setTimeout(doResize, 100);
		setTimeout(doResize, 1000);
		$(window).resize(doResize);
	}


});




// plugins


// selects plugin helper
$.selects = {
	zIndex: 99,
	timer: null,
	cancelTimer: function()
	{
		if ($.selects.timer)
		{
			clearTimeout($.selects.timer);
			$.selects.timer = null;
		}
	},
	setTimer: function(parent, active)
	{
		$.selects.timer = setTimeout(function(){
			parent.removeClass(active);
		}, 150);
	},
	height: function(ul, max)
	{
		setTimeout(function(){
			if (ul.height() > max)
				ul.addClass('scroll').css('height', max);
		}, 10);
	}
};

// selects - jQuery plugin - creates custom select elements from existing <select> elements
$.fn.selects = function(args){
	if (!this.length)
		return this;

	var opts = $.extend({
			className: '',
			markup: '<a class="trigger" href="#"><span></span></a><div class="popup"><ul></ul></div>',
			skipFirst: false,
			maxHeight: 0
		}, args || {});

	return this.each(function(){
		var sel = $(this), 
			type = opts.className.length ? opts.className : this.className.split(' ')[0],
			actives = ['custom-select-active', type + '-active'],
			active = actives.join(' ');

		sel.hide().wrap('<div class="custom-select select-' + sel[0].name + ' ' + type + ' cf"></div>');

		var parent = sel.parent().css({ zIndex: $.selects.zIndex-- }).append(opts.markup),
			trigger = parent.find('.trigger'), popup = parent.find('.popup'), ul = parent.find('ul'),
			i = opts.skipFirst ? 1 : 0, s = '';

		for (; i < this.options.length; i++)
			s += '<li><a href="#" class="index-' + i + '">' + this.options[i].text + '</a></li>';
		ul.append(s);

		if (opts.maxHeight)
			$.selects.height(ul, opts.maxHeight);
		
		trigger
			.find('span').html(this.options[this.selectedIndex].text).end()
			.click(function(){
				$('.' + actives[0]).removeClass(active);
				var d = sel.triggerHandler('selectOpening', [sel, parent]) || 0;
				popup.css('width', trigger.outerWidth() - d);
				parent.toggleClass(active);
				return false;
			});

		popup.css('width', trigger.outerWidth());

		$(trigger).add(popup).hover(function(){
			if (parent.hasClass(actives[0]))
				$.selects.cancelTimer();
		}, function(){
			$.selects.setTimer(parent, active);
		});

		ul.find('a').click(function(){
			var i = parseInt(this.className.split('-')[1]);
			sel[0].selectedIndex = i;
			trigger.find('span').html(this.innerHTML).end().focus();
			parent.removeClass(active);
			sel.triggerHandler('selectClosed', [sel, parent, i]);
			
			if ($('#sort').length > 0) {
				var id = parent.find('select').attr('id');								
				if (id == 'date' || id == 'type') {
					var date, type = null;
					var base = 'index.php';
					if ($('#date').length > 0) {
						if (id == 'date') {
							date = $('#date').find('option').eq(i).attr('value');
						}
						else {
							date = $('#date').val();
						}
					}
					if ($('#type').length > 0) {
						if (id == 'type') {
							type = $('#type').find('option').eq(i).attr('value');
						}
						else {
							type = $('#type').val();
						}
					}
					if (date != null || type != null) {
						var urlvars = new Array();
						if (date != null)
							urlvars.push('year=' + date);
						if (type != null)
							urlvars.push('type=' + type);						
						if (urlvars.length > 0)
							base += '?' + urlvars.join('&');						
					}					
					if (base != 'index.php')
						window.location = base;
				}
			}
			
			return false;
		});

	});
};

// dynamicLabel - jQuery plugin - adds default labels to text inputs
$.fn.dynamicLabel = function() {
	return this.each(function(){
		if (this.id)
		{
			var label = $('label[for="' + this.id + '"]');
			if (label.length)
				this.value = label.html();
		}
		this._default = this.value;
		$(this).bind('focus', function(){
			if (this.value == this._default)
				this.value = '';
		}).bind('blur', function(){
			if (this.value == '')
				this.value = this._default;
		});
	});
};

// boxLinks - jQuery plugin - transforms block elements into clickable items
$.fn.boxLinks = function(args)
{
	if (!this.length) return this;

	var opts = $.extend({
		trigger: 'a',
		classHover: 'hover',
		classFocus: 'focus',
		onEnter: null,
		onLeave: null,
		onClick: function(){
			window.location = $(this).attr('href');
		}
	}, args || {});

	this.has(opts.trigger)
		.each(function(){
			var self = $(this), t = self.css('cursor', 'pointer').find(opts.trigger);
			$.data(this, 'trigger', t);		
			$.data(t[0], 'parent', self);
		}).bind('mouseenter', function(){
			var t = $.data(this, 'trigger'), self = $(this).addClass(opts.classHover);
			opts.onEnter && opts.onEnter(self);
			window.status = t.attr('href');
		}).bind('mouseleave', function(){
			var self = $(this).removeClass(opts.classHover);
			opts.onLeave && opts.onLeave(self);
			window.status = '';
		}).bind('click', function(ev){
			var t = $.data(this, 'trigger');
			t.trigger('click');
			ev.stopPropagation();
			return false;
		});

	this.find(opts.trigger).addClass('trigger')
		.bind('focus', function(){
			var p = $.data(this, 'parent');
			p.addClass(opts.classFocus);
		}).bind('blur', function(){
			var p = $.data(this, 'parent');
			p.removeClass(opts.classFocus);
		}).click(function(ev){
			opts.onClick.apply(this);
			ev.stopPropagation();
			return false;
		});

	this.find('a').not(opts.trigger).click(function(ev){
		ev.stopPropagation();
	});

	return this;
};

// equalHeights - jQuery plugin - forces elements to have the same height (maximum)
$.fn.equalHeights = function(add)
{
	var m = 0;
	this.each(function(){
		m = Math.max(m, $(this).outerHeight());
	});
	return this.each(function(){
		var t = $(this), p = 0;
		$.each(['borderTopWidth', 'paddingTop', 'paddingBottom', 'borderBottomWidth'], function(i,n){
			var v = parseInt(t.css(n));
			p += (isNaN(v) ? 0 : v);
		});
		var h = m - p;
		if (add && add[this.id])
			h += add[this.id];
		if ($.browser.msie && $.browser.version <= 6) 
			t.css('height', h);
		t.css('min-height', h); 
	});
};


// scrolling plugin helper
$.scrolling = {
	wheel: false,
	slide: function(event, ui)
	{
		var sl = $(ui.handle.parentNode),
			ha = $.data(sl[0], 'ha'),
			hw = $.data(sl[0], 'hw'),
			hs = ha - hw,
			t = (- hs * (100 - ui.value) / 100);
		if (hs > 0)
			$('.scroll-area', sl.parents('.scroll-wrap')).css('top', t + 'px');
	},
	up: function(){
		var sb = $(this).parents('.scroll-bar').find('.scroll-bar-in'),
			val = sb.slider('value');
		sb.slider('value', val + 5 * ($.scrolling.wheel ? 3 : 1));
		return false;
	},
	dn: function(){
		var sb = $(this).parents('.scroll-bar').find('.scroll-bar-in'),
			val = sb.slider('value');
		sb.slider('value', val - 5 * ($.scrolling.wheel ? 3 : 1));
		return false;
	},
	reset: function(wrap)
	{
		var area = wrap.find('.scroll-area'),
			ha = area.height(),
			hw = wrap.height(),
			sb = wrap.find('.scroll-bar-in');
		$.data(sb[0], 'ha', ha);
		$.data(sb[0], 'hw', hw);
		sb.slider('value', 100);
		area.css('top', 0);
	}
};


// scrolling - jQuery plugin - creates custom scrolling areas
$.fn.scrolling = function(args){

	if (!this.length)
		return this;

	var opts = $.extend({
			handleHeight: 20
		}, args || {});

	return this.each(function(){
		var wrap = $(this).addClass('scroll-wrap').wrapInner('<div class="scroll-area"></div>'),
			area = wrap.find('.scroll-area'),
			ha = area.height(),
			hw = wrap.height();

		if (ha > hw)
		{
			wrap.append('<div class="scroll-bar"><div class="scroll-bar-in"></div></div>');
			wrap.find('.scroll-bar').css('paddingTop', opts.handleHeight + 'px');
			var sb = wrap.find('.scroll-bar-in');
			$.data(sb[0], 'ha', ha);
			$.data(sb[0], 'hw', hw);
			sb.height(hw - opts.handleHeight).slider({
				orientation: 'vertical',
				min: 0,
				max: 100,
				slide: $.scrolling.slide,
				change: $.scrolling.slide
			}).slider('value', 100);
		}

		wrap.bind('updateScroll', function(){
			var ha = area.height();
			$.data(sb[0], 'ha', ha);
		}).bind('mousewheel', function(ev, delta){
			$.scrolling.wheel = true;
			$.scrolling[(delta > 0 ? 'up' : 'dn')].apply(sb);
			$.scrolling.wheel = false;
			return false;
		});
	});
};



/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 */
// backgroundPosition[X,Y] get hooks
var $div = $('<div style="background-position: 3px 5px">');
$.support.backgroundPosition   = $div.css('backgroundPosition')  === "3px 5px" ? true : false;
$.support.backgroundPositionXY = $div.css('backgroundPositionX') === "3px" ? true : false;
$div = null;

var xy = ["X","Y"];

// helper function to parse out the X and Y values from backgroundPosition
function parseBgPos(bgPos) {
	var parts  = bgPos.split(/\s/),
		values = {
			"X": parts[0],
			"Y": parts[1]
		};
	return values;
}

if (!$.support.backgroundPosition && $.support.backgroundPositionXY) {
	$.cssHooks.backgroundPosition = {
		get: function( elem, computed, extra ) {
			return $.map(xy, function( l, i ) {
				return $.css(elem, "backgroundPosition" + l);
			}).join(" ");
		},
		set: function( elem, value ) {
			$.each(xy, function( i, l ) {
				var values = parseBgPos(value);
				elem.style[ "backgroundPosition" + l ] = values[ l ];
			});
		}
	};
}

if ($.support.backgroundPosition && !$.support.backgroundPositionXY) {
	$.each(xy, function( i, l ) {
		$.cssHooks[ "backgroundPosition" + l ] = {
			get: function( elem, computed, extra ) {
				var values = parseBgPos( $.css(elem, "backgroundPosition") );
				return values[ l ];
			},
			set: function( elem, value ) {
				var values = parseBgPos( $.css(elem, "backgroundPosition") ),
					isX = l === "X";
				elem.style.backgroundPosition = (isX ? value : values[ "X" ]) + " " + 
												(isX ? values[ "Y" ] : value);
			}
		};
		$.fx.step[ "backgroundPosition" + l ] = function( fx ) {
			$.cssHooks[ "backgroundPosition" + l ].set( fx.elem, fx.now + fx.unit );
		};
	});
}


})(jQuery, window, document);

