//jQuery closure
(function($) {
	//document ready
	$(document).ready(function() {
		//init
		if (!isBrowserCapable()) {
			$('html')
			.removeClass('js')
			.addClass('no-js');
			return;
		}
		$.history.init(hashLoad);
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var currentImgUrl = '';
		var currentImgPos = 0;
		var bgUrls = [];
		var projectNavReverse = false;
		var sliderTimerDuration = 5000; //in ms
		var sliderTimer = '';
		$('#bg-images').css('background-image','none');
		if ($(window).width() <= 1024) $('html').addClass('low-res');
		if (navigator.appVersion.indexOf("Mac") != -1) $('html').addClass('mac');
		loadJsData();
		initAjax();
		initLogoHover();
		initMainnavHover();
		setBgImg();
		adjustSubnav();
		initGMap();
		initFaces();
		initContentbox();
		initProjectNav();
		initProjectList();
		$(window).resize(function() {
			if ($(window).width() <= 1024) $('html').addClass('low-res');
			else $('html').removeClass('low-res');
			sizeBgImgs();
			initGMap();
			initFaces();
		});
		
		function loadJsData() {
			if ($('#js-data-isSingleProject').text() == 'true') isSingleProject = true;
			else isSingleProject = false;
			
			if ($('#js-data-isListPage').text() == 'true') isListPage = true;
			else isListPage = false;
			
			adjacentProjectUrls = [$('#js-data-adjacentProjectUrls .previous').text(),$('#js-data-adjacentProjectUrls .next').text()];
		}
		
		function trackPageView(entireUrl) {
			if (googleAnalyticsTrackingCode != '') _gaq.push(['_trackPageview', getSlug(entireUrl)]);
		}
		
		function getSlug(entireUrl) {
			//http://.../projects -> /projects
			var slug = entireUrl;
			if (slug.length > baseUrl.length) slug = slug.substring(baseUrl.length);
			else slug = '';
			return slug;
		}
		
		function hashLoad(hash) {
			if (hash.indexOf(baseUrl) === 0) performAjaxRequest(hash,0);
		}
		
		function initAjax() {
			if (isBrowserCapable()) ajaxBind($('a[href^="' + baseUrl + '"]'));
		}
		
		function isBrowserCapable() {
			var $html = $('html');
			if ($html.hasClass('browser-msie') && ($html.hasClass('renderer-major-version-6') ||
				$html.hasClass('renderer-major-version-7') ||
				$html.hasClass('renderer-major-version-8')))
				return false;
			return true;
		}
		
		function ajaxBind($aObj) {
			initAnimations();
			
			$aObj
			.unbind('click')
			.click(function(e){
				var clickArea = 0;
				$.history.load(this.href.replace(/^.*#/, $(this).attr('href')),true);
				trackPageView(this.href.replace(/^.*#/, $(this).attr('href')));
				if ($(this).closest('#main-nav').size() > 0) {
					//mainnav click
					clickArea = 1;
					//set main-nav selection
					/*$('#main-nav li')
					.removeClass('current-menu-item')
					.removeClass('current-menu-ancestor');
					$(this)
					.closest('li')
					.addClass('current-menu-item');*/
					setActiveMainNav();
					$('#main-nav a').trigger('setBgImage');
				}
				else if ($(this).closest('.subnav').size() > 0) {
					//subnav click
					clickArea = 2;
				}
				else {
					//content
					clickArea = 3;
				}
				
				var url = $(this).attr('href');
				
				var $content = $('#content');
				if (clickArea == 1) { //mainnav -> new subnav
					if ($content.size() > 0) { //content exists
						$content
						.animate({height: 0, paddingTop: 0, paddingBottom: 0},500,function(){
							$('#submenu-nav, #list-nav')
							.animate({width: 0},500,function(){
								performAjaxRequest(url,clickArea);
							});
						});
					}
					else {
						var $submenu = $('#submenu-nav, #list-nav');
						if ($submenu.size() > 0) {
							$('#submenu-nav, #list-nav')
							.animate({width: 0},500,function(){
								performAjaxRequest(url,clickArea);
							});
						}
						else {
							performAjaxRequest(url,clickArea);
						}
					}
				}
				else {
					if ($content.size() > 0) { //content exists
						$content
						.animate({height: 0, paddingTop: 0, paddingBottom: 0},500,function(){
							performAjaxRequest(url,clickArea);
						});
					}
					else {
						performAjaxRequest(url,clickArea);
					}
				}
				
				e.preventDefault();
			});
		}
		
		function initAnimations() {
			var paddingBottom = 10;
			if ($('#content-nav').size() >= 1 && $('#social-nav .expand-reduce').size() == 0) paddingBottom = 45;
			
			$('#content')
			.each(function(){
				if ($(this).data('height') == '') $(this).data('height',$(this).height());
				if ($(this).data('padding-top') == '') $(this).data('padding-top',20);
				if ($(this).data('padding-bottom') == '') $(this).data('padding-bottom',paddingBottom);
			});
			//.data('padding-top',20)
			//.data('padding-bottom',paddingBottom);
		}
		
		function performAjaxRequest(url,clickArea) {
			//alert('ajax');
			var $ajaxSatus = $('#ajax-status');
			var origUrl = url;
			$ajaxSatus.show();
			url = origUrl + ' #dynamic-content>*';
			$('#dynamic-content')
			.load(url,function(response, status, xhr){
				var paddingBottom = 10;
				if ($('#content-nav').size() >= 1 && $('#social-nav .expand-reduce').size() == 0) paddingBottom = 45;
				
				$('#content')
				.data('height',$('#content').height())
				.data('padding-top',20)
				.data('padding-bottom',paddingBottom)
				.css({height: 0, paddingTop: 0, paddingBottom: 0});
				
				if (clickArea == 1) $('#submenu-nav, #list-nav').css({width: 0});
				
				loadJsData();
				initAjax();
				setBgImg();
				initGMap();
				initFaces();
				initContentbox();
				initProjectNav();
				initProjectList();
				
				//set mainnav
				/*$('#main-nav li')
				.removeClass('current-menu-item')
				.removeClass('current-menu-ancestor');
				$('#main-nav a[href="' + origUrl + '"]')
				.closest('li')
				.addClass('current-menu-item');*/
				setActiveMainNav();
				$('#main-nav a').trigger('setBgImage');
				
				$ajaxSatus.hide();
				
				var $content = $('#content');
				var $submenu = $('#submenu-nav, #list-nav');
				if ($submenu.width() < 440) { //expand subnav + content
					$submenu
					.animate({width: 440},500,function(){
						adjustSubnav();
						$content
						.animate({
							height: $content.data('height'),
							paddingTop: $content.data('padding-top'),
							paddingBottom: $content.data('padding-bottom')
						},500);
					});
				}
				else {//expand content only
					adjustSubnav();
					$content
					.animate({
						height: $content.data('height'),
						paddingTop: $content.data('padding-top'),
						paddingBottom: $content.data('padding-bottom')
					},500);
				}
			});
		}
		
		function setBgImg() {
			var behaviour = $('#js-data-bg-image-behaviour').text();
			$('#bg-raster').show();
			if ((behaviour == 'slide' || behaviour.length == 0) && $('#js-data-hasSlideShow').text() == '1' && $('#bg-image-sources li').size() > 0) {
				//slideshow
				if (sliderTimer != '') clearTimeout(sliderTimer);
				sliderTimer = setTimeout(function(){
					slide();
				},sliderTimerDuration);
				if (isSingleProject && projectNavReverse) switchBgImg($('#bg-image-sources li').last().text());
				else switchBgImg($('#bg-image-sources li').first().text());
			}
			else if (behaviour == 'random-static') {
				clearTimeout(sliderTimer);
				var $sourcelist = $('#bg-image-sources li');
				var rand_num = Math.floor(Math.random()*$sourcelist.size());
				switchBgImg($sourcelist.eq(rand_num).text());
			}
			else {
				//standard
				clearTimeout(sliderTimer);
				if (isSingleProject && projectNavReverse) switchBgImg($('#bg-image-sources li').last().text());
				else switchBgImg($('#bg-image-sources li').first().text());
			}
			//set slideshow
			/*if ($('#js-data-hasSlideShow').text() == '1' && $('#bg-image-sources li').size() > 0) {
				if (sliderTimer != '') clearTimeout(sliderTimer);
				sliderTimer = setTimeout(function(){
					slide();
				},sliderTimerDuration);
			}
			else {
				clearTimeout(sliderTimer);
			}
			if (isSingleProject && projectNavReverse) switchBgImg($('#bg-image-sources li').last().text());
			else switchBgImg($('#bg-image-sources li').first().text());*/
		}
		
		function switchBgImg(newUrl) {
			//$('body').css('background-color','#f00');
			currentImgUrl = newUrl;
			if (newUrl == '') return;
			var $ajaxStatus = $('#ajax-status');
			$ajaxStatus.show();
			$('<img />')
			.attr('src',newUrl)
			.load(function(){
				var $this = $(this);
				var $front = $('#bg-images-container>.front');
				var $back = $('#bg-images-container>.back');
				var duration = 1000;
				
				$ajaxStatus.hide();
				
				$this
				.data('originalWidth',$this.width())
				.data('originalHeight',$this.height());
				
				if ($front.find('img').size() == 0) {
					$('#bg-images').fadeIn(1000);
					$front.append($this);
					sizeBgImgs();
					return;
				}
				
				//console.log('back: ' + $back.html() + '\nfront: ' + $front.html());
				
				$back
				.html('')
				.show()
				.append($this);
				
				sizeBgImgs();
				var $imgClone = $this.clone();
				
				$front
				.clearQueue()
				.show()
				.fadeOut(duration)
				.queue(function(){
					$(this)
					.html('')
					.append($imgClone)
					.show()
					.dequeue();
				});
			});
		}
		
		function adjustSubnav() {
			//dynamically adjust list padding to fit subnav
			var i = 19;
			var $container = $('#submenu-nav.adjustable');
			if ($container.size() == 0) return;
			var $list = $container.find('ul');
			var $listItems = $container.find('li');
			var sumLiWidth = 0;
			$list.css({float: 'left', width: 'auto'});
			for (i = 19; i < 50; i++) {
				$listItems.css({paddingLeft: i - 1, paddingRight: i - 1});
				sumLiWidth = 0;
				$listItems.each(function(){
					sumLiWidth += $(this).innerWidth();
				});
				if ($list.innerWidth() >= $container.width()) break;
			}
			if (i == 50) i = 21;
			$('#submenu-nav.adjustable li').css({paddingLeft: i - 2, paddingRight: i - 2});
		}
		
		function initContentbox() {
			//expand-reduce
			var $content = $('#content');
			var $contentSection = $content.children('section');
			var $erButton = $('#content .expand-reduce');
			$contentSection
			.data('originalHeight',$contentSection.height())
			.bind('expand-inner',function(){
				$content
				.removeClass('expand')
				.addClass('reduce')
				.css('height','auto')
				//.css('padding-bottom',45);
				.css('padding-bottom',10);
				var newHeight = 388;
				if ((!isSingleProject || $('#js-data-contentClosed').text() != 'true') && $(this).data('originalHeight') != 0) newHeight = $(this).data('originalHeight');
				//$(this).animate({height: 388},500,function(){
				$(this).animate({height: newHeight},500,function(){
					initVScroll();
				});
			})
			.bind('reduce-inner',function(){
				$content
				.removeClass('reduce')
				.addClass('expand')
				.css('height','auto')
				.css('padding-bottom',10);
				$(this).animate({height: 0},500,function(){
					$(this).closest('article').removeClass('v-scroll');
				});
			});
			$erButton.click(function(e){
				if ($content.hasClass('expand')) $contentSection.trigger('expand-inner');
				else $contentSection.trigger('reduce-inner');
				e.preventDefault();
			});
			
			//scroll events
			$('#content>section')
			.bind('scroll-up',function(){
				$(this).scrollTo({top:'-=388px',left:'+=0'},500);
			})
			.bind('scroll-down',function(){
				$(this).scrollTo({top:'+=388px',left:'+=0'},500);
			})
			.bind('scroll-left',function(){
				$(this).scrollTo({top:'+=0',left:'-=384px'},500);
			})
			.bind('scroll-right',function(){
				$(this).scrollTo({top:'+=0',left:'+=384px'},500);
			});
			
			initVScroll();
			initHScroll();
		}
		
		function initVScroll() {
			//horizontal scroll
			$('#content>section')
			.mousewheel(function(event,delta){
				if (!$('html').hasClass('mac')) { //not for macs (delay bug)
					if (delta > 0) {
						//scroll up
						$(this).trigger('scroll-up');
					}
					else if (delta < 0) {
						//scroll down
						$(this).trigger('scroll-down');
					}
				}
			})
			.each(function(){
				if (this.clientHeight <= 0) {
					$(this).closest('article').removeClass('v-scroll');
				}
				else if (this.clientHeight < this.scrollHeight) {
					if ($('#content').hasClass('listview')) {
						$('#content')
						.css('padding-bottom',10)
						.data('padding-bottom',10);
					}
					else {
						$('#content')
						.css('padding-bottom',45)
						.data('padding-bottom',45);
					}
					
					//has scrollbar
					$(this).closest('article').addClass('v-scroll');
					$('#content-nav .down')
					.addClass('available')
					.click(function(e){
						$('#content>section').trigger('scroll-down');
						e.preventDefault();
					});
					$('#content-nav .up')
					.addClass('available')
					.click(function(e){
						$('#content>section').trigger('scroll-up');
						e.preventDefault();
					});
				}
			});
		}
		
		function initHScroll() {
			if (!isBrowserCapable()) return;
			//arrange list tiles
			var $container = $('#content>section');
			if (!isListPage) {
				$('#content').removeClass('h-scroll');
				return;
			}
			var cols = 0;
			var currentRow = 0;
			var colHeight = 0;
			var sumWidth = 0;
			$container
			.find('.list-row')
			.each(function(){
				if ($(this).outerHeight() + colHeight > $container.height()) {
					//new column
					colHeight = 0;
					cols++;
					currentRow = 0;
					//sumWidth += $(this).width();
					sumWidth += 384;
				}
				$(this).css({left: sumWidth, top: colHeight});
				colHeight += $(this).outerHeight(true);
				currentRow++;
			});
			$('#content')
			.addClass('h-scroll')
			.removeClass('v-scroll');
			
			//buttons
			$('#list-nav .previous')
			.addClass('available')
			.click(function(){
				$('#content>section').trigger('scroll-left');
			});
			$('#list-nav .next')
			.addClass('available')
			.click(function(){
				$('#content>section').trigger('scroll-right');
			});
			if (cols > 0) {
				$('#list-nav .previous, #list-nav .next').show();
			}
		}
		
		function initGMap() {
			windowWidth = $(window).width();
			windowHeight = $(window).height();
			
			$('#bg-map')
			.css('width',windowWidth)
			.css('height',windowHeight);
			
			$("#bg-map")
			.each(function(){
				$('#bg-raster').hide();
			})
			.gMap(gmapSettings);
		}
		
		function initFaces() {
			//generate face tiles
			var cols = Math.round(windowWidth / 135) + 1;
			var rows = Math.round(windowHeight / 180) + 1;
			var targetContainer = $('#bg-faces-container-temp');
			var faces = $('#bg-faces-container article');
			var faceCounter = 0;
			$('#bg-faces-container').hide();
			targetContainer.html('');
			for (var i = 1; i <= rows; i++) {
				for (var j = 1; j <= cols; j++) {
					faces
					.eq(faceCounter)
					.clone()
					.css({
						left: Math.round(135 * (j - 1)),
						top: Math.round(180 * (i - 1))
					})
					.appendTo(targetContainer);
					//if (j == 1) targetContainer.find('article').last().css('clear','both');
					//targetContainer.append(faces.eq(faceCounter));
					faceCounter++;
					if (faceCounter >= faces.size()) faceCounter = 0;
				}
			}
			targetContainer.show();
			
			//add face interactivity
			targetContainer.find('article')
			.bind('expand',function(){
				var $this = $(this);
				if ($this.hasClass('active')) return;
				$('#bg-faces-container-temp article').trigger('reduce');
				$this
				.addClass('active')
				.addClass('opening')
				.animate({width: 270, height: 360},400,function(){
					var caption = $(this).find('section');
					caption.animate({top: Math.round(360 - caption.innerHeight())},200);
					$(this).find('.close-icon').animate({top: 7},200);
				})
				.find('img')
				.animate({width: 270, height: 360},400,function(){
					$(this).closest('article').removeClass('opening');
				});
			})
			.bind('reduce',function(){
				var $this = $(this);
				if (!$this.hasClass('active')) return;
				$this
				.find('section')
				.animate({top: 360},100)
				.closest('article')
				.find('.close-icon')
				.animate({top: -20},100,function(){
					$(this)
					.closest('article')
					.animate({width: 135, height: 180},200)
					.find('img')
					.animate({width: 135, height: 180},200,function(){
						$(this).closest('article').removeClass('active');
					});
				});
			})
			.click(function(e){
				$(this).trigger('expand');
				e.preventDefault();
			})
			.find('.close-icon')
			.click(function(e){
				$(this).trigger('reduce');
				e.preventDefault();
			});
			
			targetContainer.find('article .email').click(function(e){
				e.preventDefault();
				document.location.href = $(this).attr('href');
			});
		}
		
		function initLogoHover() {
			var originalLogoImg = $('#logo-nav img').eq(0);
			$('<img />')
			.attr('src',logoHoverUrl)
			.load(function(){
				var hoverLogoImg = $(this);
				$('#logo-nav')
				.hover(function(){
					$(this)
					.find('a>img')
					.replaceWith(hoverLogoImg);
				},function(){
					$(this)
					.find('a>img')
					.replaceWith(originalLogoImg);
				});
			});
		}
		
		function initMainnavHover() {
			$('#main-nav a').each(function(){
				var $this = $(this);
				var aniPositions = [];
				if ($this.parent().hasClass('news')) aniPositions =      ['-51px -155px','-161px -155px','-271px -155px','-381px -155px','-491px -155px','-601px -155px','-711px -155px','-821px -155px'];
				if ($this.parent().hasClass('projects')) aniPositions =  ['-36px -268px','-146px -268px','-256px -268px','-366px -268px','-476px -268px','-586px -268px','-696px -268px','-806px -268px','-907px -268px'];
				if ($this.parent().hasClass('profile')) aniPositions =   ['-46px -375px','-156px -375px','-266px -375px','-376px -375px','-486px -375px','-596px -375px'];
				if ($this.parent().hasClass('contact')) aniPositions =   ['-41px -482px','-151px -482px','-261px -482px','-371px -482px','-481px -482px','-591px -482px','-701px -482px'];
				var newPosition = aniPositions[0];
				$(this)
				.hover(function(){
					for (var i = 1; i < aniPositions.length; i++) {
						$this
						.animate({backgroundPosition: aniPositions[i]},1)
						.delay(30);
					}
				},function(){
					$this.trigger('setBgImage');
				})
				.bind('setBgImage',function(){
					//alert($this.closest('li').hasClass('current-menu-item'));
					if ($this.closest('li').hasClass('current-menu-item') || $this.closest('li').hasClass('current-menu-ancestor')) {
						newPosition = aniPositions[aniPositions.length - 1];
					}
					else newPosition = aniPositions[0];
					$this
					.clearQueue()
					.animate({backgroundPosition: newPosition},1)
				});
			});
		}
		
		function getBgWidth(imgWidth,imgHeight,windowHeight) {
			return Math.floor((imgWidth*windowHeight)/imgHeight);
		}
		
		function sizeBgImgs() {
			windowWidth = $(window).width();
			windowHeight = $(window).height();
			
			$('#bg-images, #bg-images-container, #bg-images-container>div, #bg-map')
			.css('width',windowWidth)
			.css('height',windowHeight);
			
			$('#bg-images-container img')
			.css('width',getBgWidth($(this).data('originalWidth'),$(this).data('originalHeight'),windowHeight))
			.css('height',windowHeight);
		}
		
		function initProjectNav() {
			$('#picture-nav').show();
			bgUrls = [];
			$('#bg-image-sources li').each(function(){
				bgUrls.push($(this).text());
			});
			for (var i = 0; i < bgUrls.length; i++) {
				if (currentImgUrl == bgUrls[i]) {
					currentImgPos = i;
					refreshProjectPicCount(i + 1,bgUrls.length);
					break;
				}
			}
			$('#picture-nav a.previous')
			.hover(function(){
				if (currentImgPos <= 0) {
					$('#picture-nav')
					.addClass('switch')
					.find('span')
					.text('previous project');
				}
			},function(){
				refreshProjectPicCount();
			})
			.click(function(e){
				if (currentImgPos > 0) {
					currentImgPos--;
					switchBgImg(bgUrls[currentImgPos]);
					refreshProjectPicCount();
					if (currentImgPos == 0) {
						$('#picture-nav')
						.addClass('switch')
						.find('span')
						.text('previous project');
					}
				}
				else {
					//previous project
					projectNavReverse = true;
					if (isBrowserCapable()) performAjaxRequest(adjacentProjectUrls[0],4);
					else document.location.href = adjacentProjectUrls[0];
				}
				e.preventDefault();
			});
			$('#picture-nav a.next')
			.hover(function(){
				if (currentImgPos >= bgUrls.length - 1) {
					$('#picture-nav')
					.addClass('switch')
					.find('span')
					.text('next project');
				}
			},function(){
				refreshProjectPicCount();
			})
			.click(function(e){
				if (currentImgPos < bgUrls.length - 1) {
					currentImgPos++;
					switchBgImg(bgUrls[currentImgPos]);
					refreshProjectPicCount();
					if (currentImgPos == bgUrls.length - 1) {
						$('#picture-nav')
						.addClass('switch')
						.find('span')
						.text('next project');
					}
				}
				else {
					//next project
					projectNavReverse = false;
					if (isBrowserCapable()) performAjaxRequest(adjacentProjectUrls[1],4);
					else document.location.href = adjacentProjectUrls[1];
				}
				e.preventDefault();
			});
		}
		
		function refreshProjectPicCount() {
			currentImgNr = currentImgPos + 1;
			imgCount = bgUrls.length;
			if (currentImgNr < 10) currentImgNr = '0' + currentImgNr;
			if (imgCount < 10) imgCount = '0' + imgCount;
			$('#picture-nav')
			.removeClass('switch')
			.find('span')
			.text(currentImgNr + '/' + imgCount)
			.show();
		}
		
		function initProjectList() {
			$('#content article.project-entry')
			.hover(function(){
				$(this)
				.find('section.caption')
				.animate({top: 0},200);
			},function(){
				$(this)
				.find('section.caption')
				.animate({top: 90},200);
			});
		}
		
		function setActiveMainNav() {
			var activeId = $('#js-data-topMenuItem').text();
			$('#main-nav li')
			.removeClass('current-menu-item')
			.removeClass('current-menu-ancestor');
			$('#main-nav #menu-item-' + activeId)
			.addClass('current-menu-item');
		}
		
		function slide() {
			showNextBgImg();
			sliderTimer = setTimeout(function(){
				slide();
			},sliderTimerDuration);
		}
		
		function showNextBgImg() {
			var bgUrls = [];
			var newUrl = '';
			$('#bg-image-sources li').each(function(){
				bgUrls.push($(this).text());
			});
			for (var i = 0; i < bgUrls.length; i++) {
				if (currentImgUrl == bgUrls[i]) {
					if (i + 1 == bgUrls.length) newUrl = bgUrls[0];
					else newUrl = bgUrls[i + 1];
					break;
				}
			}
			if (newUrl.length > 0) switchBgImg(newUrl);
		}
	});

	
})(jQuery);

/**
 * @author Alexander Farkas
 * v. 1.22
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);
