$(document).ready(function(){

	if ($('.flexcroll').length) {
		correctFlexHeight();
	}
	
	$('.b-widget-btns > dd').hover(function(){
		$(this).addClass('w-btn-hover');	
	},function(){
		$(this).removeClass('w-btn-hover');		
	});
	
	if ($('.b-widget-sections').length) {
		
		$('.w-dots a').mousedown(function(){
			window.clearInterval(window.cycle);
		});
		
		$('.w-dots a').click(function(){
			if ($(this).hasClass('active-dot')) {
				return false;
			}
			if ($('.b-widget-btns').hasClass('animated')) {
				return false;
			}
			
			$('.b-widget-btns').addClass('animated');
			if ($(this).parent().parent().hasClass('w-btn-active')) {
				q1 = $(this).prevAll().length;
				q2 = $('.w-btn-active').prevAll().length;
				if ($('.b-widget').hasClass('ready')) {
					$('.b-widget-section:eq(' + q2 + ') .b-widget-sub-sections').animate({top:(q1 * -450) + 'px'},500,function(){
						$('.b-widget-btns').removeClass('animated');
					});
				} else {
					$('.b-widget-section:eq(' + q2 + ') .b-widget-sub-sections').css({top:(q1 * -450) + 'px'});
					$('.b-widget-btns').removeClass('animated');
				}
			} else {
				$(this).parent().parent().addClass('will-be-active');
				if ($('.b-widget').hasClass('ready')) {
					window.setTimeout("switchPoint()",250);	
					q2 = $('.will-be-active').prevAll().length;
				} else {
					switchPoint();
					q2 = $('.w-btn-active').prevAll().length;
				}
				q1 = $(this).prevAll().length;
				
				$('.b-widget-section:eq(' + q2 + ') .b-widget-sub-sections').css({top:(q1 * -450) + 'px'});
				if (!$('.b-widget').hasClass('ready')) {
					$('.b-widget-sections').css({left:(q2 * -960) + 'px'});
					$('.b-widget-btns').removeClass('animated');
					$('.color-line').css({left:(15 + q2 * 240) + 'px'});
				} else {
					$('.b-widget-sections').animate({left:(q2 * -960) + 'px'},500,function(){
						$('.b-widget-btns').removeClass('animated');
					});
					$('.color-line').animate({left:15 + q2 * 240 + 'px'},450);
				}
				
			}
			
			$('.active-dot').removeClass('active-dot');
			$(this).addClass('active-dot');
			document.location.href = $(this).attr('href');
			return false;
		});
		
		prts = document.location.href.split('#');
		if (prts.length > 1 && prts[1] != '' && $('.w-dots a[href="#' + prts[1] + '"]').length) {
			$('.w-dots a[href="#' + prts[1] + '"]').click();
		} else {
			$('.w-dots a:eq(0)').click();
			window.cycle = window.setInterval("switchHomeSlides()",5000);
		}
		
		$('.b-widget-sub-section .ar-r').click(function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			l = parseInt($(this).prevAll('ul:eq(0)').css('left').replace('px','').replace('auto','').replace('pt','0'));
			$(this).prevAll('ul:eq(0)').animate({left:l - 960 + 'px'},400,function(){
				$(this).nextAll('.ar-l:eq(0):hidden').show();
				l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0')) * -1;
				w = parseInt($(this).css('width').replace('px','').replace('auto','').replace('pt','0'));
				if (l == w - 960) {
					$(this).nextAll('.ar-r:eq(0)').hide();
				}
				$('.ar-r.blocked').removeClass('blocked');
			});
			return false;
		});

		$('.b-widget-sub-section .ar-l').click(function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			l = parseInt($(this).prevAll('ul:eq(0)').css('left').replace('px','').replace('auto','').replace('pt','0'));
			$(this).prevAll('ul:eq(0)').animate({left:l + 960 + 'px'},400,function(){
				$(this).nextAll('.ar-r:eq(0):hidden').show();
				l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0')) * -1;
				if (l == 0) {
					$(this).nextAll('.ar-l:eq(0)').hide();
				}
				$('.ar-l.blocked').removeClass('blocked');
			});
			return false;
		});
		
		$('.b-widget').addClass('ready');
		
	} else {
		
		$('.w-dots a').click(function(){
			document.location.href = $(this).attr('href');
			return false;
		});
		
	}
	
	$('.w-dots a').hover(function(){
		$(this).parent().next().find('span').text($(this).attr('longdesc'));
	},function(){
		$(this).parent().next().find('span').text("");
	});

	$('.b-widget-section-info .btn-1').mousedown(function(){
		$(this).addClass('btn-1-active');
	}).mouseup(function(){
		$(this).removeClass('btn-1-active');
	}).mouseout(function(){
		$(this).removeClass('btn-1-active');
	});
	
	$('.b-widget-btns dd').click(function(){
		if ($(this).find('.active-dot:visible').length || !$(this).hasClass('w-btn-active')) {
			if (!$(this).hasClass('w-btn-active')) {
				$(this).find('.w-dots a:eq(0)').click();
			} else {
				if ($('.active-dot').next('a').length) {
					$(this).find('.w-dots .active-dot').next('a').click();
				} else {
					$(this).find('.w-dots a:eq(0)').click();
				}
			}
		} else {
			if ($('.b-special-offers .ar-r:visible').length) {
				$('.b-special-offers .ar-r').click();
				$('.b-special-offers .ar-l').show();
				$('.b-special-offers .ar-l').show();
			} else {
				$('.b-special-offers ul').animate({left:0},400);
				$('.b-special-offers .ar-l').hide();
				$('.b-special-offers .ar-r').show();
			}
		}
		window.clearInterval(window.cycle);
		return false;
	});

	$('.ar-l,.ar-r').hover(function(){
		$(this).addClass('ar-hover');
	},function(){
		$(this).removeClass('ar-hover');
		$(this).removeClass('ar-active');
	}).mousedown(function(){
		$(this).addClass('ar-active');
	}).mouseup(function(){
		$(this).removeClass('ar-active');
	});
	
	if ($('.b-big-gallery-thumbs').length) {
	
		$('.b-big-gallery-thumbs .ar-l').click(function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-big-gallery-thumbs .mark').prev().prev().prev().prev().length) {	
				$('.b-big-gallery-thumbs .mark').prev().prev().prev().prev().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').prev().prev().prev().length) {			
				$('.b-big-gallery-thumbs .mark').prev().prev().prev().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').prev().prev().length) {
				$('.b-big-gallery-thumbs .mark').prev().prev().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').prev().length) {
				$('.b-big-gallery-thumbs .mark').prev().addClass('mark');
			}
			$('.mark:last').removeClass('mark');			
			correctBigGalleryShift();			
			return false;
		});
		
		$('.b-big-gallery-screen-inner img').live('click',function(e){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');			
			if (typeof e.layerX != 'undefined' && e.layerX < 450) {
				if ($('.b-big-gallery-thumbs .active').prev().length) {
					$('.b-big-gallery-thumbs .active').prev().click();
				} else {
					$('.b-big-gallery-thumbs li:last').click();
				}
			} else {
				if ($('.b-big-gallery-thumbs .active').next().length) {
					$('.b-big-gallery-thumbs .active').next().click();
				} else {
					$('.b-big-gallery-thumbs li:eq(0)').click();
				}
			}
			return false;
		});
		
		$('.b-big-gallery-thumbs .ar-r').live('click',function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-big-gallery-thumbs .mark').next().next().next().next().length) {	
				$('.b-big-gallery-thumbs .mark').next().next().next().next().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').next().next().next().length) {			
				$('.b-big-gallery-thumbs .mark').next().next().next().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').next().next().length) {
				$('.b-big-gallery-thumbs .mark').next().next().addClass('mark');
			} else if ($('.b-big-gallery-thumbs .mark').next().length) {
				$('.b-big-gallery-thumbs .mark').next().addClass('mark');
			}	
			$('.mark:eq(0)').removeClass('mark');			
			correctBigGalleryShift();			
			return false;
		});
		
	
		$('.b-big-gallery-thumbs li').click(function(){
			prts = $(this).find('img').attr('src').split('/');
			path = prts[prts.length - 1];
			code = '<img src="/thumb/900x280xM/' + path + '" height="280" width="900" alt="" onload="imgLoaded($(this));" />';
			$('.b-big-gallery-screen-inner li:last').html(code);
			$('.b-big-gallery-thumbs li.active').removeClass('active');

			prts = document.location.href.split('#');
			document.location.href = $(this).find('a').attr('href');

			$(this).addClass('active');
			$('.mark').removeClass('mark');
			$(this).addClass('mark');
			correctBigGalleryShift();
		});
	
		$('.b-big-gallery-thumbs ul li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		
		prts = document.location.href.split('#');
		if (prts.length == 2 && $('.b-big-gallery-thumbs ul li a[href="#' + prts[1] + '"]').length) {
			$('.b-big-gallery-thumbs ul li a[href="#' + prts[1] + '"]').parent().click();
		} else {
			$('.b-big-gallery-thumbs ul li:eq(0)').click();
		}
		
	}
	
	if ($('.b-middle-gallery-thumbs').length) {
		
		$('.b-middle-gallery-thumbs li').each(function(i){
			$(this).attr('index',i);
		});
		
		$('.b-middle-gallery-thumbs li').click(function(){
			if ($(this).hasClass('active')) {
				return false;
			}
			i = $(this).attr('index');
			prts = $(this).find('img').attr('src').split('/');
			path = prts[prts.length - 1];
			code = '<img src="/thumb/660x280xM/' + path + '" height="280" width="660" alt="" onload="imgLoaded($(this));" />';
			$('.b-middle-gallery-screen-inner li:last').html(code);
			$('.b-middle-gallery-thumbs li.active').removeClass('active');
			$(this).addClass('active');
			$('.b-middle-gallery-description:visible').hide();
			$('.b-middle-gallery-description:eq(' + i + ')').fadeIn(500);
			prts = document.location.href.split('#');
			document.location.href = $(this).find('a').attr('href');
			correctFlexHeight();
			fleXenv.fleXcrollMain($('.b-middle-gallery-description:visible .flexcroll')[0]);
			$('.mark').removeClass('mark');
			$(this).addClass('mark');
			correctMiddleGalleryShift();
		});
	
		$('.b-middle-gallery-thumbs .ar-l').click(function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-middle-gallery-thumbs .mark').prev().prev().prev().prev().length) {	
				$('.b-middle-gallery-thumbs .mark').prev().prev().prev().prev().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').prev().prev().prev().length) {			
				$('.b-middle-gallery-thumbs .mark').prev().prev().prev().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').prev().prev().length) {
				$('.b-middle-gallery-thumbs .mark').prev().prev().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').prev().length) {
				$('.b-middle-gallery-thumbs .mark').prev().addClass('mark');
			}
			$('.mark:last').removeClass('mark');			
			correctMiddleGalleryShift();			
			return false;
		});
			
		$('.b-middle-gallery-thumbs .ar-r').live('click',function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-middle-gallery-thumbs .mark').next().next().next().next().length) {	
				$('.b-middle-gallery-thumbs .mark').next().next().next().next().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').next().next().next().length) {			
				$('.b-middle-gallery-thumbs .mark').next().next().next().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').next().next().length) {
				$('.b-middle-gallery-thumbs .mark').next().next().addClass('mark');
			} else if ($('.b-middle-gallery-thumbs .mark').next().length) {
				$('.b-middle-gallery-thumbs .mark').next().addClass('mark');
			}	
			$('.mark:eq(0)').removeClass('mark');			
			correctMiddleGalleryShift();			
			return false;
		});
		
		$('.b-middle-gallery-screen-inner img').live('click',function(e){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if (typeof e.layerX != 'undefined' && e.layerX < 330) {
				if ($('.b-middle-gallery-thumbs .active').prev().length == 0) {
					$('.b-middle-gallery-thumbs li:last').click();
				} else {
					$('.b-middle-gallery-thumbs .active').prev().click();
				}
			} else {
				if ($('.b-middle-gallery-thumbs .active').next().length == 0) {
					$('.b-middle-gallery-thumbs li:eq(0)').click();
				} else {
					$('.b-middle-gallery-thumbs .active').next().click();
				}
			}
			return false;
		});
	
		prts = document.location.href.split('#');
		if (prts.length == 2 && $('.b-middle-gallery-thumbs ul li a[href="#' + prts[1] + '"]').length) {
			$('.b-middle-gallery-thumbs ul li a[href="#' + prts[1] + '"]').parent().click();
		} else {
			$('.b-middle-gallery-thumbs ul li:eq(0)').click();
		}
		
		$('.b-middle-gallery-thumbs ul li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		
	}	
	
	if ($('.b-small-gallery-thumbs').length) {
		
		$('.b-small-gallery-thumbs li').each(function(i){
			$(this).attr('index',i);
		});
		
		$('.b-small-gallery-thumbs li').click(function(){
			if ($(this).hasClass('active')) {
				return false;
			}
			i = $(this).attr('index');
			prts = $(this).find('img').attr('src').split('/');
			path = prts[prts.length - 1];
			code = '<img src="/thumb/420x280xM/' + path + '" height="280" width="420" alt="" onload="imgLoaded($(this));" />';
			$('.b-small-gallery-screen-inner li:last').html(code);
			$('.b-small-gallery-thumbs li.active').removeClass('active');
			$(this).addClass('active');
			$('.b-cart-description:visible').hide();
			$('.b-cart-description:eq(' + i + ')').fadeIn(500);
			prts = document.location.href.split('#');
			document.location.href = $(this).find('a').attr('href');
			correctFlexHeight();
			fleXenv.fleXcrollMain($('.b-cart-description:visible .flexcroll')[0]);
			$('.mark').removeClass('mark');
			$(this).addClass('mark');
			correctSmallGalleryShift();
		});
	
		$('.b-small-gallery-thumbs .ar-l').click(function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-small-gallery-thumbs .mark').prev().prev().prev().prev().length) {	
				$('.b-small-gallery-thumbs .mark').prev().prev().prev().prev().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').prev().prev().prev().length) {			
				$('.b-small-gallery-thumbs .mark').prev().prev().prev().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').prev().prev().length) {
				$('.b-small-gallery-thumbs .mark').prev().prev().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').prev().length) {
				$('.b-small-gallery-thumbs .mark').prev().addClass('mark');
			}
			$('.mark:last').removeClass('mark');			
			correctSmallGalleryShift();			
			return false;
		});
			
		$('.b-small-gallery-thumbs .ar-r').live('click',function(){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if ($('.b-small-gallery-thumbs .mark').next().next().next().next().length) {	
				$('.b-small-gallery-thumbs .mark').next().next().next().next().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').next().next().next().length) {			
				$('.b-small-gallery-thumbs .mark').next().next().next().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').next().next().length) {
				$('.b-small-gallery-thumbs .mark').next().next().addClass('mark');
			} else if ($('.b-small-gallery-thumbs .mark').next().length) {
				$('.b-small-gallery-thumbs .mark').next().addClass('mark');
			}	
			$('.mark:eq(0)').removeClass('mark');			
			correctSmallGalleryShift();			
			return false;
		});
		
		$('.b-small-gallery-screen-inner img').live('click',function(e){
			if ($(this).hasClass('blocked')) {
				return false;
			}
			$(this).addClass('blocked');
			if (typeof e.layerX != 'undefined' && e.layerX < 210) {
				if ($('.b-small-gallery-thumbs .active').prev().length == 0) {
					$('.b-small-gallery-thumbs li:eq(0)').click();
				} else {
					$('.b-small-gallery-thumbs .active').prev().click();
				}
			} else {
				if ($('.b-small-gallery-thumbs .active').next().length == 0) {
					$('.b-small-gallery-thumbs li:eq(0)').click();
				} else {
					$('.b-small-gallery-thumbs .active').next().click();
				}
			}
			return false;
		});
	
		prts = document.location.href.split('#');
		if (prts.length == 2 && $('.b-small-gallery-thumbs ul li a[href="#' + prts[1] + '"]').length) {
			$('.b-small-gallery-thumbs ul li a[href="#' + prts[1] + '"]').parent().click();
		} else {
			$('.b-small-gallery-thumbs ul li:eq(0)').click();
		}
		
		$('.b-small-gallery-thumbs ul li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		
	}
	
	$('a.target-blank').attr('target','_blank');

});

function switchPoint() {
	
	$('.w-btn-active').removeClass('w-btn-active');
	$('.will-be-active').removeClass('will-be-active').addClass('w-btn-active');
	
}

function imgLoaded($img) {
	
	$img.hide().css('visibility','visible').fadeIn(400);
	
}

function correctFlexHeight() {

	h = $('h2:visible').height();
	$('.b-middle-gallery-description:visible .flexcroll').css('height',(260 - h) + 'px');
	$('.b-cart-description:visible .flexcroll').css('height',(340 - h) + 'px');
	
}

function correctMiddleGalleryShift() {
	
	if ($('.b-middle-gallery-thumbs li').length < 5) {
		return false;
	}
	
	l = -130 * $('.b-middle-gallery-thumbs ul .mark').prevAll().length + 385;
	w = parseInt($('.b-middle-gallery-thumbs ul').css('width').replace('px','').replace('auto','').replace('pt','0'));
	if (l > 40) {
		l = 40;
	}
	if ($('.b-middle-gallery-thumbs ul .mark').prevAll().length == 0) {
		$('.ar-l').hide();
	} else {
		$('.ar-l:hidden').show();
	}
	if (l < -1 * (w - 860)) {
		l = -1 * (w - 860);
	}
	if ($('.b-middle-gallery-thumbs ul .mark').nextAll().length == 0) {
		$('.ar-r').hide();
	} else {
		$('.ar-r:hidden').show();
	}
	$('.b-middle-gallery-thumbs ul').animate({left:l + 'px'},300,function(){
		l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0'));
		w = parseInt($(this).css('width').replace('px','').replace('auto','').replace('pt','0'));
		$('.blocked').removeClass('blocked');
	});
	
}

function correctMiddleGalleryShift() {
	
	if ($('.b-middle-gallery-thumbs li').length < 7) {
		return false;
	}
	
	l = -130 * $('.b-middle-gallery-thumbs ul .mark').prevAll().length + 385;
	w = parseInt($('.b-middle-gallery-thumbs ul').css('width').replace('px','').replace('auto','').replace('pt','0'));
	if (l > 40) {
		l = 40;
	}
	if ($('.b-middle-gallery-thumbs ul .mark').prevAll().length == 0) {
		$('.ar-l').hide();
	} else {
		$('.ar-l:hidden').show();
	}
	if (l < -1 * (w - 860)) {
		l = -1 * (w - 860);
	}
	if ($('.b-middle-gallery-thumbs ul .mark').nextAll().length == 0) {
		$('.ar-r').hide();
	} else {
		$('.ar-r:hidden').show();
	}
	$('.b-middle-gallery-thumbs ul').animate({left:l + 'px'},300,function(){
		l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0'));
		w = parseInt($(this).css('width').replace('px','').replace('auto','').replace('pt','0'));
		$('.blocked').removeClass('blocked');
	});
	
}

function correctSmallGalleryShift() {
	
	if ($('.b-small-gallery-thumbs li').length < 5) {
		return false;
	}
	
	l = -85 * $('.b-small-gallery-thumbs ul .mark').prevAll().length + 170;
	w = parseInt($('.b-small-gallery-thumbs ul').css('width').replace('px','').replace('auto','').replace('pt','0'));
	if (l > 40) {
		l = 40;
	}
	if ($('.b-small-gallery-thumbs ul .mark').prevAll().length == 0) {
		$('.ar-l').hide();
	} else {
		$('.ar-l:hidden').show();
	}
	if (l < -1 * (w - 380)) {
		l = -1 * (w - 380);
	}
	if ($('.b-small-gallery-thumbs ul .mark').nextAll().length == 0) {
		$('.ar-r').hide();
	} else {
		$('.ar-r:hidden').show();
	}
	$('.b-small-gallery-thumbs ul').animate({left:l + 'px'},300,function(){
		l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0'));
		w = parseInt($(this).css('width').replace('px','').replace('auto','').replace('pt','0'));
		$('.blocked').removeClass('blocked');
	});
	
}

function correctBigGalleryShift() {
	
	if ($('.b-big-gallery-thumbs li').length < 6) {
		return false;
	}
	
	l = -171 * $('.b-big-gallery-thumbs ul .mark').prevAll().length + 365;	
	w = parseInt($('.b-big-gallery-thumbs ul').css('width').replace('px','').replace('auto','').replace('pt','0'));
	if (l > 40) {
		l = 40;
	}
	if ($('.b-big-gallery-thumbs ul .mark').prevAll().length == 0) {
		$('.ar-l').hide();
	} else {
		$('.ar-l:hidden').show();
	}
	if (l < -1 * (w - 860)) {
		l = -1 * (w - 860);
	}
	if ($('.b-big-gallery-thumbs ul .mark').nextAll().length == 0) {
		$('.ar-r').hide();
	} else {
		$('.ar-r:hidden').show();
	}
	$('.b-big-gallery-thumbs ul').animate({left:l + 'px'},300,function(){
		l = parseInt($(this).css('left').replace('px','').replace('auto','').replace('pt','0'));
		w = parseInt($(this).css('width').replace('px','').replace('auto','').replace('pt','0'));
		$('.blocked').removeClass('blocked');
	});
	
}

function switchHomeSlides() {
	
	if ($('.active-dot').next('a').length) {
		$('.active-dot').next('a').click();
	} else {
		if ($('.w-btn-active').next().length) {
			$('.w-btn-active').next().find('a:eq(0)').click();
		} else {
			$('.w-btn-1 a:eq(0)').click();
		}
	}
	
	
}
