// cufon
Cufon.replace('.page h1', {
	fontFamily: 'NeoSansCyr'
});
Cufon.replace('.page h2', {
	fontFamily: 'NeoSansCyr'
});
Cufon.replace('.hd h3 em', {
	color: '-linear-gradient(#f49e52, #eb6e2f)',
	fontFamily: 'NeoSansCyr',
	textShadow:'2px 2px #9b0f04'
});
Cufon.replace('.topnav em', {
	color: '-linear-gradient(#9e0400, #650000)',
	fontFamily: 'Tahoma',
	textShadow:'1px 1px #fee1ba'
});
Cufon.replace('.cakes a em', {
	fontFamily: 'NeoSansCyr'
});
Cufon.replace('.cakes a.active em', {
	color: '-linear-gradient(#570101, #880000)',
	fontFamily: 'NeoSansBoldCyr',
	textShadow:'1px 1px #e2723a'
});
Cufon.replace('.preview .order-me a em', {
	color: '-linear-gradient(#cc8859, #a56233)',
	fontFamily: 'NeoSansBoldCyr',
	textShadow:'1px 1px #ffffff'
});
Cufon.replace('.bubble-more a em', {
	fontFamily: 'Tahoma',
	textShadow:'1px 1px #ffe945'
});
Cufon.replace('.sidebar a.active', {
	color: '-linear-gradient(#530101, #880000)',
	fontFamily: 'Tahoma',
	textShadow:'1px 1px #e2723a'
});

// cakes preview
function initCarousel(){
	$('div.carousel').each(function(){
		// options
		var _slideSpeed = 700; // slide animation duration
		var _vVisible = 5; // 5 visible items in thumbs gallery
		var _activeEl = 2; // 3rd element in thumbs is active
		
		// vars
		var _holder = $(this);
		var _active = 0;
		var _vViz1 = 0; // thumb gallery first visible element
		var _vViz2 = _vViz1+_vVisible-1; // thumb gallery last visible element
		
		// horizontal
		var _btnLeft = $('div.preview a.btn-prev',_holder);
		var _btnRight = $('div.preview a.btn-next',_holder);
		var _hNode = $('div.preview ul',_holder);
		var _hItem = $('li',_hNode);
		var _hStep = _hItem.eq(0).outerWidth();
		
		// vertical
		var _btnTop = $('div.thumbs a.btn-prev',_holder);
		var _btnBottom = $('div.thumbs a.btn-next',_holder);
		var _vNode = $('div.thumbs ul',_holder);
		var _vItem = $('li',_vNode);
		var _vStep = _vItem.eq(0).outerWidth();
		var _vLink = $('a',_vNode);
		
		// reset
		_vItem.removeClass('active');
		_vItem.eq(_active).addClass('active');
		
		if(_hItem.length == _vItem.length) {
			_btnLeft.hide();
			_btnTop.hide();
			if(_vItem.length<(_vVisible+1)) _btnBottom.hide();
			// click 'left' button
			_btnLeft.click(function(){
				_btnRight.show();
				_vItem.removeClass('active');
				_active--;
				_vItem.eq(_active).addClass('active');
				if(_active==0) _btnLeft.hide();
				_hNode.stop();
				_hNode.animate({
					left:-_hStep*_active
				},_slideSpeed);
				
				if(_active==0) _btnTop.hide();
				if(_active<_vViz1+_activeEl && _vViz1>0) {
					_vViz1--;
					_vViz2--;
					if(_vViz1==0) _btnTop.hide();
					_btnBottom.show();
					_vNode.stop();
					_vNode.animate({
							left:-_vStep*_vViz1
					},_slideSpeed);
				}
				return false;
			});
			// click 'right' button
			_btnRight.click(function(){
				_btnLeft.show();
				_vItem.removeClass('active');
				_active++;
				_vItem.eq(_active).addClass('active');
				if(_active==_hItem.length-1) _btnRight.hide();
				_hNode.stop();
				_hNode.animate({
					left:-_hStep*_active
				},_slideSpeed);
				if(_active==(_vItem.length-1)) _btnBottom.hide();
				if(_active>_vViz1+_activeEl && _vViz2<_hItem.length-1) {
					_vViz1++;
					_vViz2++;
					if(_vViz2==(_hItem.length-1)) _btnBottom.hide();
					_btnTop.show();
					_vNode.stop();
					_vNode.animate({
						left:-_vStep*_vViz1
					},_slideSpeed);
				}
				return false;
			});
			// click 'top' button
			_btnTop.click(function(){
				_btnBottom.show();
				_vViz1--;
				_vViz2--;
				if(_active>_vViz2) {
					_vItem.removeClass('active');
					_active--;
					_vItem.eq(_active).addClass('active');
					_btnRight.show();
					_hNode.stop();
					_hNode.animate({
						left:-_hStep*_active
					},_slideSpeed);
				}
				$('#active-item').text(_active);
				if(_vViz1==0) _btnTop.hide();
				_vNode.stop();
				_vNode.animate({
					left:-_vStep*_vViz1
				},_slideSpeed);
				return false;
			});
			// click 'btm' button
			_btnBottom.click(function(){
				_btnTop.show();
				_vViz1++;
				_vViz2++;
				if(_vViz1>_active) {
					_vItem.removeClass('active');
					_active++;
					_vItem.eq(_active).addClass('active');
					_btnLeft.show();
					_hNode.stop();
					_hNode.animate({
						left:-_hStep*_active
					},_slideSpeed);
				}
				$('#active-item').text(_active);
				if(_vViz2==(_vItem.length-1)) _btnBottom.hide();
				_vNode.stop();
				_vNode.animate({
					left:-_vStep*_vViz1
				},_slideSpeed);
				return false;
			});
			// click thumbnail link
			_vLink.click(function(){
				_vItem.removeClass('active');
				_active = _vLink.index(this);
				_vItem.eq(_active).addClass('active');
				_btnLeft.show();
				_btnRight.show();
				if(_active==0) _btnLeft.hide();
				if(_active==_hItem.length-1) _btnRight.hide();
				_hNode.stop();
				_hNode.animate({
					left:-_hStep*_active
				},_slideSpeed);
				if(_active>(_vViz1+_activeEl) && _vViz2<_hItem.length-1) {
					if(_active==_vViz2 && _vViz2+1<_hItem.length-1) {
						_vViz1 = _vViz1+2;
						_vViz2 = _vViz2+2;
					}else{
						_vViz1++;
						_vViz2++;
					}
					_btnTop.show();
					_btnBottom.show();
					if(_vViz2==(_vItem.length-1)) _btnBottom.hide();
					_vNode.stop();
					_vNode.animate({
						left:-_vStep*_vViz1
					},_slideSpeed);
				}
				if(_active<(_vViz1+_activeEl) && _vViz1>0) {
					if(_active==_vViz1 && _vViz1-1>0){
						_vViz1 = _vViz1-2;
						_vViz2 = _vViz2-2;
					}else{
						_vViz1--;
						_vViz2--;
					}
					_btnTop.show();
					_btnBottom.show();
					if(_vViz1==0) _btnTop.hide();
					_vNode.stop();
					_vNode.animate({
						left:-_vStep*_vViz1
					},_slideSpeed);
				}
				return false;
			});
		}
	});
}
function detectIE6(){
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer"){
	    var b_version = navigator.appVersion;
	    var re = /\MSIE\s+(\d\.\d\b)/;
		var res = b_version.match(re);
		if (res[1] <= 6){
			return true;
		}
	}
	return false;
}

// tabs
function initTabs(){
	$('.tabset').each(function(){
		var _tabset = $(this);
		var _tab = $('a',_tabset);
		// reset
		function resetTabs(){
			_tab.each(function(){
				if($($(this).attr('href')).length) {
					if(!$(this).hasClass('active')){
						$($(this).attr('href')).hide();
					}else{
						$($(this).attr('href')).show();	
					}
				}
			});
			if(detectIE6()) {
				var _footer = $('#footer');
				if(_footer.length){
					_footer.css({
						position:'static'
					});
					_footer.css({
						position:'absolute'
					});
				}
			}
		}
		resetTabs();
		_tab.click(function(){
			if(!$(this).hasClass('active')){
				_tab.removeClass('active');
				$(this).addClass('active');
				resetTabs();
			}
			return false;
		});
	});
}
$(function(){
	initCarousel(); // cakes preview
	initTabs(); // tabs
});
