/* javascript */
$(document).ready(function(){	
	var winWidth = $(window).width(), //IE
		winHeight = $(window).height(),
		moving = false;//IE

	setBodyClass();

	
	$(window).resize(function() {
		var winNewWidth = $(window).width(),
			winNewHeight = $(window).height();

		if (winNewWidth !== winWidth || winNewHeight !== winHeight) {
			setBodyClass();

			Cufon.replace('h4, h1 strong, .tastes_list > li');

			winWidth = winNewWidth;
			winHeight = winNewHeight;
		};
	});

	new function() {
		var $products_list = $('.products_list'),
			$products = $products_list.children('li'),
			$thumbs = $products.children('.thumb'),
			progress = false;


		$('.close_details').click(function() {
			close_details();
			return false;
		});


		$thumbs.click(function() {
			var $product = $(this).parent(),
				$selected = $products_list.find('li.selected'),
				$details = $product.find('.product_details');

			if (progress || $product.hasClass('selected')) {
				return false;
			};

			progress = true;

			if ($selected.length) {
				close_details();
			};

			$product.find('.thumb').css('visibility', 'hidden');
			$product.animate({
				width : '100%'
			}, 600, function() {
				$product.animate({
					height : $details.innerHeight()
				}, 600);
				$product.find('.thumb').hide();
				$details.slideDown(function() {
					resizeDetails();
				});
				$product.addClass('selected');
				$("html, body").animate({ scrollTop: $product.position().top }, "slow");
				progress = false;
			});

			return false;
		});

		function close_details() {
			var $selected = $products_list.find('li.selected');
			$selected.find('.product_details').hide();
			$selected.find('.thumb').css('visibility', 'visible').show();
			$selected.removeClass('selected');
			$selected.animate({
				width: 172,
				height: 346
			}, 600, function () {
				progress = false;
				$selected.find('.image_per_taste').hide().end().find('.product_image').show();
				$selected.find('.ingredients_per_taste').hide().end().find('.product_ingredients').show();
			});
		};

		$(window).resize(function() {
			if(progress) {
				return;
			};

			resizeDetails();
		});

		function resizeDetails() {
			var $selected = $products_list.find('li.selected'),
				$details = $selected.find('.product_details');

			$selected.css('height', $details.height());
		};
	};

	$('#scroll_top').click(function() {
		$("html, body").animate({ scrollTop: 0 }, "slow");
	});

	$('.tastes_list li a').click(function() {
		var $taste = $(this),
			selected_taste_id = $taste.attr('rel'),
			$product = $taste.parents('li');
			
		if($product.find('.image_taste_' + selected_taste_id)) {
			$product.find('.product_image').hide();
			$product.find('.image_per_taste').hide();
			$product.find('.image_taste_' + selected_taste_id).show();
		}

		if($product.find('.main' + selected_taste_id)) {
			$product.find('.product_ingredients').hide();
			$product.find('.ingredients_per_taste').hide();
			$product.find('.ingredients_taste_' + selected_taste_id).show();
		}
	});

	new function() {
		var $container = $('.carousel_accents'),
			$list = $('#carousel_accents'),
			$items = $list.find('li'),
			items_length = $items.length,
			item_width = get_widht($items.eq(0)),
			item_height = get_height($items.eq(0)) + 15,
			list_width = items_length * item_width,
			$navigation = $container.find('.paginator'),
			$btn_prev = $container.find('.prev_product'),
			$btn_next = $container.find('.next_product'),
			current = 0,
			progress = false,
			speed = 500;
		
		$list.css({'width': list_width, 'height' : item_height});

		if ($navigation) {
			for(var i = 0; i < items_length; i++) {
				$navigation.append('<a title="" href="javascript:;"><span>'+ i +'</span></a>');
			};

			var $navigation_items = $navigation.find('a');
			$navigation_items.eq(0).addClass('active');

			$navigation_items.live('click', function() {
				var pos = $(this).index();

				move(pos);
			});
		};

		$btn_prev.click(function() {
			move(current - 1);
			return false;
		});

		$btn_next.click(function() {
			move(current + 1);
			return false;
		});

		function move(next) {
			if(progress) {
				return false;
			}

			progress = true;

			if (next < 0) {
				next = items_length - 1;
			} else if (next >= items_length) {
				next = 0;
			};

			var end = (next * item_width) * -1,
				list_height = get_height($items.eq(next));

			$list.animate({
				left : end,
				height : list_height
			}, speed, function() {
				$navigation_items.filter('.active').removeClass('active');
				$navigation_items.eq(next).addClass('active');
				progress = false;
			});

			current = next;
		};

		function get_widht(el) {
			return parseInt(el.width(), 10)
		};

		function get_height(el) {
			return parseInt(el.height(), 10)
		};

		$(window).resize(function() {
			if (!$container.length || progress) {
				return false;
			};

			item_width = get_widht($items.eq(current));
			item_height = get_height($items.eq(current));
			list_width = items_length * item_width,
			list_position = (current * item_width) * -1;
			$list.css({'width': list_width, 'height': item_height, 'left' : list_position});
		});
	}
	
	if(typeof Touch == "object") {
		$('.dropdown_menu').click(function(ev) {
			var children = $(this).find('ul, em');
			var close = function(el, parent) {
				if(el.parents(parent).length > 0 || el.hasClass('dropdown_menu')) {

				} else {
					children.removeClass('focused');
					$('#wrapper').unbind('mouseup');
				}
			};

			if(!children.hasClass('focused')) {
				children.addClass('focused');
				$('#wrapper').bind('mouseup', function(ev) {
					var el = $(ev.target);
					close(el, '.dropdown_menu');
				});
			};
		});
		
		$('.buy_link').click(function() {
			var dropdown = $(this).find('div.dropdown_list');
			var close = function(el, parent) {
				if(el.parents(parent).length > 0 || el.hasClass('buy_link')) {
				} else {
					dropdown.removeClass('focused');
					$('#wrapper').unbind('mouseup');
				}
			};

			if(!dropdown.hasClass('focused')) {
				dropdown.addClass('focused');
				$('#wrapper').bind('mouseup', function(ev) {
					var el = $(ev.target);
					close(el, '.buy_link');
				});
			};
		});
		
	} else {
		$('.dropdown_menu').hover(function() {
			$(this).find('ul, em').addClass('focused');
		}, function() {
			$(this).find('ul, em').removeClass('focused');
		});
		
		$('.buy_link').hover(function() {
			$(this).find('div.dropdown_list').addClass('focused');
		}, function() {
			$(this).find('div.dropdown_list').removeClass('focused');
		});
	}
});


function setBodyClass() {
	if($.browser.msie) {
		var viewpoint_width = document.documentElement.clientWidth;

		if (viewpoint_width < 820) {
			$('body').attr('class', 'max_width_820');
		} else if (viewpoint_width < 1000) {
			$('body').attr('class', 'max_width_1000');
		} else if (viewpoint_width < 1360) {
			$('body').attr('class', 'max_width_1360');
		} else {
			$('body').attr('class', '');
		};

		Cufon.replace('h4, h1 strong, .tastes_list > li');
	};
};

