jQuery.noConflict();

// page init
jQuery(function(){
	initCufon();
	initGalleries();
	initNavigation();
});

// cufon init
function initCufon() {
	Cufon.replace('.category-title-list li', { fontFamily: 'Bodoni MT Condensed', hover: true });
	Cufon.replace('.product-text h3', { fontFamily: 'Bodoni Std'});
	Cufon.replace('.recipe-text p', { fontFamily: 'Mias Scribblings ~'});
	Cufon.replace('.recipe-text .recipe-title', { fontFamily: 'Mias Scribblings ~'});
	Cufon.replace('.shop-basket h1', { fontFamily: 'Mias Scribblings ~'});
	Cufon.replace('.contact-us h1', { fontFamily: 'Mias Scribblings ~'});
	Cufon.replace('.recipe-text a', { fontFamily: 'Mias Scribblings ~', hover: true});
	Cufon.replace('.category-info', { fontFamily: 'Bodoni Std'});
	Cufon.replace('.category-title', { fontFamily: 'Bodoni Std'});
	Cufon.replace('.main-product h2', { fontFamily: 'Bodoni Std'});
	Cufon.replace('.early-harvest h2', { fontFamily: 'Bodoni MT Condensed' });
	Cufon.replace('.early-harvest h3', { fontFamily: 'Bodoni Std Con'});
	Cufon.replace('.effects-test .shadow', { textShadow: '#555 1px 1px, #000 2px 2px', fontFamily: 'Frutiger' });
	Cufon.replace('.effects-test .gradient', { color: '-linear-gradient(#aaa, 0.45=#888, 0.45=#555, #000)', fontFamily: 'Frutiger' });
}

// slide navigation init
function initNavigation() {
	var _slideSpeed = 350;
	var _activeClass = 'active';
	var _items = jQuery('ul.main-nav > li');

	_items.each(function(){
		var _item = jQuery(this);
		var _link = _item.find('a');

		// default values
		var _originalWidth = 15;
		var _resizedWidth = 124;
		var _diff;

		// calculate values
		if(!_link.hasClass(_activeClass)) _originalWidth = _link.width();
		else {
			return;
			_resizedWidth = _link.width();
		}
		_diff = _originalWidth-_resizedWidth;
		_link.addClass(_activeClass).css({marginRight:_diff});

		// animate
		_link.hover(function(){
			_link.animate({marginRight:0},{duration:_slideSpeed,queue:false});
		},function(){
			_link.animate({marginRight:_diff},{duration:_slideSpeed,queue:false});
		});
	});
}

// slideshow init
function initGalleries() {
	// slideshow
	var _fadeSpeed = (jQuery.browser.msie ? 0 : 450);
	
	jQuery('div.product-slideshow').fadeGallery({
		slideElements:'div.slideset > div.slide',
		btnPrev:'a.left-arrow',
		btnNext:'a.right-arrow',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:true,
		switchTime:5000,
		duration:_fadeSpeed
	});
	jQuery('div.content-scroll').fadeGallery({
		slideElements:'div.slideset > div.slide',
		pagerLinks:'div.early-harvest > div > div',
		btnPrev:'a.left-arrow',
		btnNext:'a.right-arrow',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:true,
		switchTime:5000,
		duration:_fadeSpeed
	});

	// carousel
	jQuery('div.scroll-gallery').scrollGallery({
		sliderHolder: '>div',
		slider:'>ul, .sub-slide > ul',
		slides: '>li',
		btnPrev:'a.prev-arrow1, a.prev-arrow2',
		btnNext:'a.next-arrow1, a.next-arrow2',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:5000,
		duration:450,
		step:1
	});
}

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: '>div',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:5000,
		duration:650,
		easing:'swing',
		event:'click',
		vertical:false,
		step:false
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _sliderHolder = jQuery(_options.sliderHolder, _this);
		var _slider = jQuery(_options.slider, _sliderHolder);
		var _slides = jQuery(_options.slides, _slider.eq(0));
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _easing = _options.easing;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _step = _options.step;
		var _vertical = _options.vertical;

		// gallery init
		if(!_slides.length) return;
		var _currentStep = 0;
		var _sumWidth = 0;
		var _sumHeight = 0;
		var _hover = false;
		var _stepWidth;
		var _stepHeight;
		var _stepCount;
		var _offset;
		var _timer;
		var _animateProperty = (_vertical ? 'marginTop' : 'marginLeft');

		_slides.each(function(){
			_sumWidth+=jQuery(this).outerWidth(true);
			_sumHeight+=jQuery(this).outerHeight(true);
		});

		// calculate gallery offset
		function recalcOffsets() {
			if(_vertical) {
				if(_step) {
					_stepHeight = _slides.eq(_currentStep).outerHeight(true);
					_stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;
					_offset = -_stepHeight*_currentStep;
				} else {
					_stepHeight = _sliderHolder.height();
					_stepCount = Math.ceil(_sumHeight/_stepHeight);
					_offset = -_stepHeight*_currentStep;
					if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;
				}
			} else {
				if(_step) {
					_stepWidth = _slides.eq(_currentStep).outerWidth(true);
					_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;
					_offset = -_stepWidth*_currentStep;
				} else {
					_stepWidth = _sliderHolder.width();
					_stepCount = Math.ceil(_sumWidth/_stepWidth);
					_offset = -_stepWidth*_currentStep;
					if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;
				}
			}
		}

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentStep != _ind) {
						_currentStep = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// gallery animation
		function prevSlide() {
			recalcOffsets();
			if(_currentStep > 0) _currentStep--;
			else _currentStep = _stepCount-1;
			switchSlide();
		}
		function nextSlide() {
			recalcOffsets();
			if(_currentStep < _stepCount-1) _currentStep++;
			else _currentStep = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
		}
		function switchSlide() {
			recalcOffsets();
			if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});
			else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'div.pager a',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		pausedClass:'paused',
		playClass:'playing',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		switchTime:3000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;

		// gallery init
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;

		if(_pagerLinks.length) {
			var _i = _pagerLinks.index(_pagerLinks.filter('.'+_activeClass).eq(0));
			if(_i!=-1) _prevIndex = _currentIndex = _i;
		}

		var _slideCount = _slides.length;
		var _timer;
		if(!_slideCount) return;
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slideCount-1;
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else _currentIndex = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}

function moveAlone() {
	//document.write('Hello World!');
	//var link = document.getElementById(next-arrow2);
	//var _clickMe = jQuery('a.next-arrow2');
	setTimeout(
  	function() 
  		{
  		jQuery('a.next-arrow2').click();
  		}, 1000);
	
}
