$(document).ready(function() {
		
		
		var page_width;
		var page_height;
		
		var data_padding = $('#data').outerHeight();
		
		var items_count = 0;
		var items_index = 0;
		
		//initial hash to load after boom-bang
		var hash = document.location.hash;
		
		//controls image "source" > "src" attribute conversion; saves traffic
		var initializing = true;
		
		//stores information whether page is "virgin"
		var fresh_loaded = true;
		
		
		initialize();
		
		
		//window resize handler - to prevent scrollbars and data area width
		$(window).resize(function() {
			initialize();
		});
		
		
		$('#overlay').width(page_width);
		$('#overlay').height(page_height);
		
		
		
		
		
		//menu links positioning
		$('#categories a').css('padding-left', $('#categories input').eq(0).width() + 7);
		
		
		$(document).click(
			function() {
				if (fresh_loaded){
					$('#overlay').css('z-index', -1000);
					fresh_loaded = false;
				}
			}
		);
		
		
		
		
		
		$(window).mousemove(
			function(event) {
				if ($('#menu #products .item.show.active').size() != 0) {
					var position = $('#menu').position();
					var diff = position.left - event.pageX;
					//document.title = diff;
					
					if (diff >= 300) {
						$('#menu').css('opacity', 0);
					}
					else if (diff <= 0) {
						$('#menu').css('opacity', 1);
					}
					else {
						var opacity = 1 - diff / 300;
						//document.title = opacity;
						$('#menu').css('opacity', opacity);
					}
				}
			});
		
		
		
		
		
		$('#categories .category').change(
			function() {
				var category = $(this).val();
				
				if ($(this).attr('checked')) {
					var query = '#menu #products .item[category="' + category + '"]';
					///document.title = query;
					$(query).addClass('show');
				}
				else {
					var query = '#menu #products .item[category="' + category + '"]';
					///document.title = query;
					$(query).removeClass('show');
				}
				
				check_menu();
				
				if (unchecked_all() == true) {
					$('#data > div').css('display', 'none');
					$('#menu #products .item').removeClass('active');
					$('#data').css('background', '');
				}
				
				if (checked_all() == true) {
					$('#categories #category_all').attr('checked', true);
				}
				else {
					$('#categories #category_all').attr('checked', false);
				}
			}
		
		);
		
		
		$('#categories #category_all').change(
			function() {
				var click_first = false;
				
				if (unchecked_all() == true) {
					click_first = true;
				}
				
				
				if (checked_all() == true) {
					$(this).attr('checked', false);
					$('#categories .category').each(
						function () {
							$(this).attr('checked', false);
							$(this).change();
						}
					)
					
					$('#data > div').css('display', 'none');
					$('#data').css('background', '');
				}
				else {
					$(this).attr('checked', true);
					$('#categories .category').each(
						function () {
							$(this).attr('checked', true);
							$(this).change();
						}
					);
				}
				
				
				if (click_first == true) {
					$('#menu #products .item.show').eq(0).click();
					
					var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
					window.location = url;
					$('#langbar_cs').parent().attr('action', '/cs/' + url);
					$('#langbar_en').parent().attr('action', '/en/' + url);
				}
			}
		);
		
		
		
		
		
		$('#menu #products .item').hover(
			function() {
				var thumbnail = $(this).attr('thumbnail');
				var text = '<img id="hover_img" src="/thumbnail/' + thumbnail + '"/>'
				$('#hover').html(text);
				$('#hover').css('z-index', 1500);
			},
			function(){
				$('#hover').html('');
				$('#hover').css('z-index', -1500);
			});
		
		
		
		
		
		$('#menu #products .item').click(function() {
				
				$('#data').css('background-image', 'url("")');
				
				$('#menu #products .item').removeClass('active');
				$('#menu #categories .links a').removeClass('active');
				$(this).addClass('active');
				
				
				container = $(this).attr('container');
				
				if (!initializing) {
					$('#data > div#' + container).find('img').each(
						function() {
							source = $(this).attr('source');
							if (source != '') {
								$(this).attr('src', source);
								$(this).removeAttr('source');
							}
						}
					);
				}
				
				
				$('#data > div').hide();
				
				var container_to_show = '#data > div#' + container;
				$(container_to_show).show();
				
				
				$('#data').css('background-color', '#' + $(this).attr('bg'));
				
				check_menu();
				
				var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
				window.location = url;
				$('#langbar_cs').parent().attr('action', '/cs/' + url);
				$('#langbar_en').parent().attr('action', '/en/' + url);
			}
		);
		
		
		
		
		
		$('#menu #categories .links a').click(function() {
				
				$('#data').css('background-image', 'url("")');
				
				$('#menu #products .item').removeClass('active');
				$('#menu #categories .links a').removeClass('active');
				$(this).addClass('active');
				
				
				container = $(this).attr('container');
				
				if (!initializing) {
					$('#data > div#' + container).find('img').each(
						function() {
							source = $(this).attr('source');
							if (source != '') {
								$(this).attr('src', source);
								$(this).removeAttr('source');
							}
						}
					);
				}
				
				
				$('#data > div').hide();
				
				var container_to_show = '#data > div#' + container;
				$(container_to_show).show();
				
				
				$('#data').css('background-color', '#' + $(this).attr('bg'));
				
				
				var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
				window.location = url;
				$('#langbar_cs').parent().attr('action', '/cs/' + url);
				$('#langbar_en').parent().attr('action', '/en/' + url);
			}
		);
		
		
		
		
		$('#shifter_prev').click(
			function() {
				check_menu();
			
				if (items_count > 0) {
					if (items_index > 0) {
						$('#menu #products .item.show').eq(items_index - 1).click();
						
						var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
						window.location = url;
						$('#langbar_cs').parent().attr('action', '/cs/' + url);
						$('#langbar_en').parent().attr('action', '/en/' + url);
					}
				}
			}
		);
		
		
		$('#shifter_next').click(
			function() {
				check_menu();
			
				if (items_count > 0) {
					if (items_index < items_count - 1) {
						$('#menu #products .item.show').eq(items_index + 1).click();
						
						var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
						window.location = url;
						$('#langbar_cs').parent().attr('action', '/cs/' + url);
						$('#langbar_en').parent().attr('action', '/en/' + url);
					}
				}
			}
		);
		
		
		
		
		//shows page according to URL hash
		if (hash.substr(1) > 0) {
			query = '#menu #products .item[href="' + hash + '"]';
			
			if ($(query).size() == 1) {
				$('#categories #category_all').click();
				$('#categories #category_all').change();
			}
			
			query = 'a[href="' + hash + '"]';
			$(query).click();
			
			var url = hash;
			window.location = url;
			$('#langbar_cs').parent().attr('action', '/cs/' + url);
			$('#langbar_en').parent().attr('action', '/en/' + url);
		}
		
		
		
		
		function checked_all() {
			var checked_all = true;
			
			$('#categories .category').each(
				function() {
					if ($(this).attr('checked') == false) {
						checked_all = false;
					};
				}
			);
			
			if (checked_all == true) {
				return true;
			}
			else {
				return false;
			}
		}
		
		
		
		
		function unchecked_all() {
			var unchecked_all = true;
			
			$('#categories .category').each(
				function() {
					if ($(this).attr('checked') == true) {
						unchecked_all = false;
					};
				}
			);
			
			if (unchecked_all == true) {
				return true;
			}
			else {
				return false;
			}
		}
		
		
		
		
		
		function check_menu() {
			items_count = $('#menu #products .item.show').size();
			
			$('#menu #products .item.show').each(function(index) {
					if ($(this).hasClass('active')) {
						items_index = index;
					}
				});
			
			if ($('#menu #products .item.show.active').size() == 0) {
				$('#menu #products .item.show').eq(0).click();
				
				var url = $('#menu #products .item.show.active').attr('href') || $('#menu #categories .links a.active').attr('href') || '#';
				window.location = url;
				$('#langbar_cs').parent().attr('action', '/cs/' + url);
				$('#langbar_en').parent().attr('action', '/en/' + url);
			}
		}
		
		
		
		function initialize() {
			page_width = $(window).width();
			page_height = $(window).height();
			
			$('#data').width(page_width);
			$('#menu').height(page_height);
			$('#data').css('min-height', page_height - data_padding);
			$('#menu #products').css('height', page_height - 160);
			$('#hover').css('left', page_width / 2);
			$('#hover').css('top', page_height / 2);
			
			page_width = $(window).width();
			page_height = $(window).height();
			
			$('#data').width(page_width);
			$('#menu').height(page_height);
			$('#data').css('min-height', page_height - data_padding);
			$('#menu #products').css('height', page_height - 160);
			$('#hover').css('left', page_width / 2);
			$('#hover').css('top', page_height / 2);
		}
		
		
		
		
		initializing = false;
		
		$('#menu #products .item.show.active').click();
		$('#menu #categories .links a.active').click();
		
		
		
		
 	});

