$(function(){
	clearInputs();
	initCycleFadeBlockGallery();
	animateSocials();
	initFadeHover(400);
	jQuery('.more a, .bookmark a, .respond-form .submit, #contact-area input.submit').animateColor({
		animateObj: 'backgroundColor'
	});
	openClose();
});

// openClose init
function openClose(){
	var duration = 400;
	jQuery('div.openCloseBlock').each(function(){
		_this = jQuery(this);
		var openLink = jQuery('a.opener', _this);
		var slider = jQuery('div.text-slider', _this);
		var closeLink = jQuery('a.close', _this);
		
		openLink.click(function(){
			jQuery(this).hide();
			slider.slideDown(duration);
			return false;
		});
		
		closeLink.click(function(){
			openLink.show();
			slider.slideUp(duration);
			return false;
		});
		
	});
}

function animateSocials(){
	var socials = jQuery('.social-buttons ul');
	var animateFrom = parseInt(socials.css('marginLeft'));
	var animateTo = 0;
	var duration = 500;
	socials.each(function(){
		var _this = jQuery(this);
		_this.mouseenter(function(){
			if(!jQuery(this).is(':animated')){
				jQuery(this).animate({marginLeft: animateTo, duration: duration});
			}
		}).mouseleave(function(){
			if(!jQuery(this).is(':animated')){
				jQuery(this).animate({marginLeft: animateFrom, duration: duration});
			}
			var timer = setInterval(function(){
				if(!jQuery(this).is(':animated')){
					jQuery(this).animate({marginLeft: animateFrom, duration: duration});
					clearInterval(timer);
				}
			}, duration + 50);
		});
	});
}

// animateColor
jQuery.fn.animateColor = function(options){
	var options = jQuery.extend({
		animateObj: false,
		animateSpeed: 200,
		overColor: false
	}, options);
	
	return this.each(function(){
		var $this = jQuery(this),
			_animateObj = options.animateObj,
			_animateSpeed = options.speed,
			_overObj = {},
			_defObj = {};
		
		$this.data('defColor', $this.css(_animateObj));
		
		_defObj[_animateObj] = $this.data('defColor');
		
		$this.mouseenter(function(){
			setTimeout(function(){
				if(!$this.data('newColor')){
					$this.data('newColor', $this.css(_animateObj));
					_overObj[_animateObj] = $this.data('newColor');
					$this.css(_defObj);
				}
				$this.animate(_overObj, {queue: false, duration: _animateSpeed})
			}, 1)
		}).mouseleave(function(){
			$this.animate(_defObj, {queue: false, duration: _animateSpeed})
		})
	})
}

// clear inputs
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		var _el = $(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
};

// initFadeHover
function initFadeHover(duration){
	$('ul.projects > li > a').mouseenter(function(){
		$(this).find('> em.fade').fadeIn(duration);
	}).mouseleave(function(){
		$(this).find('> em.fade').fadeOut(duration);
	});
	$('.contact-block .btn').mouseenter(function(){
		$(this).find('> span.fade').fadeIn(duration);
	}).mouseleave(function(){
		$(this).find('> span.fade').fadeOut(duration);
	});
}

/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */
(function(d){d.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(i,b){d.fx.step[b]=function(a){if(a.state==0){a.start=getColor(a.elem,b);a.end=getRGB(a.end)}a.elem.style[b]="rgb("+[Math.max(Math.min(parseInt((a.pos*(a.end[0]-a.start[0]))+a.start[0]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[1]-a.start[1]))+a.start[1]),255),0),Math.max(Math.min(parseInt((a.pos*(a.end[2]-a.start[2]))+a.start[2]),255),0)].join(",")+")"}});function getRGB(a){var b;if(a&&a.constructor==Array&&a.length==3)return a;if(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))return[parseInt(b[1]),parseInt(b[2]),parseInt(b[3])];if(b=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(a))return[parseFloat(b[1])*2.55,parseFloat(b[2])*2.55,parseFloat(b[3])*2.55];if(b=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(a))return[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16)];if(b=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(a))return[parseInt(b[1]+b[1],16),parseInt(b[2]+b[2],16),parseInt(b[3]+b[3],16)];return e[d.trim(a).toLowerCase()]}function getColor(a,b){var c;do{c=d.curCSS(a,b);if(c!=''&&c!='transparent'||d.nodeName(a,"body"))break;b="backgroundColor"}while(a=a.parentNode);return getRGB(c)};var e={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);

// horizontally fade block gallery init
function initCycleFadeBlockGallery() {
	// settings
	var _activeClass = 'active';
	var _switchTime = 4000;
	var _speed = 1000;

	$('div.slideshow, div.slider').each(function(){
		// gallery options
		var _holder = $(this);
		var _btnPrev = _holder.find('a.link-prev');
		var _btnNext = _holder.find('a.link-next');
		var _pagination = _holder.find('ul.menu li');
		var _switcherClassName = 'switcher';
		var _generatePagination = jQuery('div.nivo-controlNav', _holder);
		var _slidesHolder = _holder.find('div.slides, div.slider-hold');
		var _slider = _slidesHolder.find('div.holder, ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _currentIndex = 0;
		var _sumWidth = 0;
		var _animating = false;
		var _autoRotation = true;
		var _rotateDirection = nextSlide;
		var _pauseOnHover = false;
		var _timer;
		var _fadeGallery = jQuery('div.description > div.holder', _holder);
		var _fadeGalleryHolder = _fadeGallery.children();
		var _fadeGallerySlides = _fadeGalleryHolder.children();
		var nextHeight = [];
		
		_fadeGallerySlides.each(function(ind){
			nextHeight[jQuery(this).index()] = jQuery(this).outerHeight(true);
			jQuery(this).attr('rel', ind);
			jQuery(this).filter('[rel!='+_currentIndex+']').hide();
		});
		
		_slider.css({position: 'relative'});
		if(_generatePagination.length) {
			_generatePagination.empty();
			var _list = jQuery('<ul class="'+_switcherClassName+'" ></ul>');
			for(var i=0; i<_slidesCount; i++) jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_list);
			_list.appendTo(_generatePagination);
			_pagination = _list.children();
		}
		if(_pagination){_pagination.eq(_currentIndex).addClass(_activeClass);}
		// gallery init
		_slides.each(function(ind){
			_sumWidth += $(this).attr('rel',ind).outerWidth(true);
		});
		_slider.prepend(_slides.clone()).append(_slides.clone());
		_slider.css({marginLeft:-_sumWidth});
		
		if(_autoRotation){autoRotate();}
		
		function autoRotate() {
			if(_timer) clearInterval(_timer);
			if(_autoRotation){
				_timer = setInterval(_rotateDirection, _switchTime);
			}
		}
		
		if(_pauseOnHover){
			_holder.hover(function(){
				if(_timer) clearInterval(_timer);
			},function(){
				autoRotate();
			});
		}
		
		// gallery control
		_btnPrev.click(prevSlide);
		_btnNext.click(nextSlide);

		// gallery animation
		function prevSlide() {
			if(!_animating) {
				_currentIndex--;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_currentIndex++;
				switchSlide();
			}
			return false;
		}

		function switchSlide() {
			_animating = true;
			var offset = 0;
			if(_currentIndex>=0) offset = -_sumWidth- _slider.children().eq(_currentIndex ).position().left
			else offset = -_slider.children().eq(_slidesCount+_currentIndex).position().left;
			
			_slider.animate({marginLeft:offset},{duration:_speed, queue:false,complete:function(){
				if(Math.abs(_currentIndex) == _slidesCount) {
					_currentIndex = 0;
					_slider.css({marginLeft:-_sumWidth});
				}
				_animating = false;
			}});
			if(_pagination.length){
				_pagination.removeClass(_activeClass);
				_pagination.eq(_currentIndex).addClass(_activeClass);
				if(_currentIndex > _pagination.length - 1){
					_pagination.eq(0).addClass(_activeClass);
				}
			}
			var switchToInd = Math.abs(_currentIndex);
			if(switchToInd == _slides.length){switchToInd = 0;}
			_fadeGallery.animate({height: nextHeight[switchToInd]}, _speed);
			_fadeGallerySlides.filter('[rel!='+switchToInd+']').fadeOut(_speed);
			_fadeGallerySlides.filter('[rel='+switchToInd+']').fadeIn(_speed);
		}
		
		_pagination.click(function(){
			_currentIndex = jQuery(this).index();
			switchSlide();
			return false;
		});
		
		// event handler
		_holder.bind('scrollto',function(e,h){
			if(!_animating) {
				if(_currentIndex == _slidesCount-1) _currentIndex = _slidesCount;
				else _currentIndex = h.num;
				switchSlide();
			}
		});
		_holder.bind('prevSlideSwitch', function(){
			prevSlide();
		});
		_holder.bind('nextSlideSwitch', function(){
			nextSlide();
		});

		// clone events
		_slider.children().each(function(){
			var link = $(this);
			var ind = parseInt(link.attr('rel'));
			link.click(function(){
				_holder.trigger('setslide',{num:ind});
				return false;
			})
		})
	});
}
