$(document).ready(function() {
	try {
		Cufon.replace('#menu li a.main', { hover : true });
		Cufon.replace('#middle .act, #right .sem, #right .info, .box h4, #footer .right strong, .page h1, #left .left, h2.article', { }); // , .c h2, .c h3
	
		if (lang == 'cz') {
			$('#footer .box h4 a, #siteamp').each(function () {
				this.onclick = function() {
					elems = document.getElementById('footer').getElementsByTagName('ul');
					for (i = 0; i < elems.length; i++) {
						if (elems[i].style.display == 'none') {
							elems[i].style.display = 'block';
						} else {
							elems[i].style.display = 'none';
						}
					}
					return false;
				};
			});
		} else {
			$('#footer .box h4 a, #siteamp').each(function () {
				$(this).click(function() { return false });
			});
		}
	
		flowplayer('.flvplayer', 'http://cms.omnio.cz/ui/assets/js/flowplayer/flowplayer-3.1.5.swf');
	
		var runningTimeouts = { m1 : [], m2 : [], m3 : [], m4 : [] };
	
		function moveUp(obj, from, to, key) {
			if (from > to) {
				from = from-2;	
				obj.style.backgroundPosition = '0 '+from+'px';
				(runningTimeouts[key]).push(setTimeout(function() {
					moveUp(obj, from, to, key);
				}, 20));
				return;	
			}
		}
		
		function moveDown(obj, from, to, key) {
			if (from < to) {
				from = from+2;		
				obj.style.backgroundPosition = '0 '+from+'px';
				(runningTimeouts[key]).push(setTimeout(function() {
					moveDown(obj, from, to, key);
				}, 20));
				return;
			}
		}
	
		function resetTimeouts(key) {
			for (i in runningTimeouts[key]) {
				clearTimeout(runningTimeouts[key]);
			}
		}
		
		function getFrom(obj, def) {
			y = obj.style.backgroundPosition.split(' ')[1];
			return y ? parseInt(y) : def;
		}
	
		$('#menu li a').each(function() {
			this.onmouseover = function() {
				key = this.parentNode.className.split(' ')[1];
				resetTimeouts(key);
				moveUp(this, getFrom(this, 69), 55, key);
			};
			
			this.onmouseout = function() {
				key = this.parentNode.className.split(' ')[1];
				resetTimeouts(key);
				moveDown(this, getFrom(this, 55), 69, key)
			};
		});
		
		/**
		 * obrazek simulujici checkbox
		 * hleda input s id "input_+id obrazku" a zmeni ho
		 * plus osetreni label for="id obrazk"		 	 
		 **/
		$('img.checkbox').click(function(){
			var imgs = {
				checked : 'img/checkbox.gif',
				notChecked : 'img/checkbox-empty.gif'
			};
			var src = $(this).attr('src');
			var wasChecked = src.indexOf(imgs.checked)+1;
			var id = $(this).attr('id');
			var input = $('#input_'+id);
			if (wasChecked){
				$(this).attr('src', imgs.notChecked);
			} else {
				$(this).attr('src', imgs.checked);
			}
			input.val(wasChecked ? 0 : 1)
		}).each(function(){
			var selfId = $(this).attr('id');
			$('label[for="'+selfId+'"]').click(function(){
				$('#'+selfId).click();
			});
		});
		
		/**
		 * newsletter
		 * pravy sloupec po loginu		 
		 **/
		if(element('#newsletter')){
			new (function(){
				var layer = new Overlayer(1, 1, 'black', 0.5);
				var validEmail = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i;
				var borders = {
					def : '#DADCDE', 
					red : '#e71b27'
				};
				
				document.getElementById('newsletter').onsubmit = function(){ return false; };
				
				$('#newsletter button.show').click(function(){
					var inp = $('#newsletter input.text');	
					if (validEmail.test(inp.val())){
						inp.css('borderColor', borders.def);
						if (!isIE(6) && !isIE(7)){
							layer.show();
						}
						document.getElementById('newsletter').onsubmit = function(){ return true; };
						$('.popup').stop(true, true).animate({
							opacity : 'show', 
							top : '300'
						}, 'slow');
					} else {
						inp.css('color', borders.red)
					}
					
					return false;
				});
				
				$('#confirm').change(function(){
					$(this).attr('checked', 'checked');
				});
				
				$('#overlayer').click(function(){
					$('.popup').css('display', 'none');
				});
				
				$('.cancel').click(function(){
					if (!isIE(6) && !isIE(7)){
						layer.hide();
					}
					$('.popup').css('display', 'none');
				});
			});
		}
		
	} catch (err){
		throw new Error(err);	
	}
});

