try {
	Cufon.replace('.header .menu a', { hover : true, fontFamily : 'Helvetica Neue LT Pro' });
 	Cufon.replace('.logos h2', { fontFamily : 'Helvetica Neue LT Pro 45' });
 	Cufon.replace('.content .boxes .item h2', { fontFamily : 'Helvetica Neue LT Pro' });
 	Cufon.replace('#middle h1, .fbLayoutBox h1, #right h3', { letterSpacing : '0px', fontFamily : 'Helvetica Neue LT Pro 45' });
 	Cufon.replace('.logos .item h3', { fontFamily : 'Palatino Linotype', hover : true });
 	Cufon.replace('.page h3', { fontFamily : 'Helvetica Neue LT Pro' });
 	Cufon.replace('.page h2', { fontFamily : 'Palatino Linotype' }); 	
 	Cufon.replace('#left li a.cufon', { fontFamily : 'Helvetica Neue LT Pro', hover : true });
 	Cufon.replace('form.reser .welcome h1', { fontFamily : 'Palatino Linotype' });
 	Cufon.replace('form.reser fieldset h3', { fontFamily : 'Helvetica Neue LT Pro' });
 	Cufon.replace('form.reser fieldset button, button', { fontFamily : 'Helvetica Neue LT Pro' });
 	Cufon.replace('form.reser div.rgt h1', { fontFamily : 'Palatino Linotype' });
 	Cufon.replace('.anketa h3', { fontFamily : 'Palatino Linotype' });

 	Cufon.now();
} catch(err){
	throw new Error(err);
}

function validateEmail(mail){
	var emailPattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return emailPattern.test(mail);
}

function validatePhone(number){
	var phonePattern = /^[+]?[()/0-9. -]{9,30}$/i;
	return phonePattern.test(number);
}

function checkFields(fields){
	var valid = true;
	$.each(fields, function(index, value) {
		var inputValue = trim($('#'+value).val());
		if(inputValue == '' || inputValue == null){
			$('#'+value).addClass('emptyField');
			$('#invalid li.'+value).show();
			valid = false;
		} else {
			$('#'+value).removeClass('emptyField');
			$('#invalid li.'+value).hide();
		}
	});
	return valid;
}

function markActivePart(valid, number){
	if(valid){
		$('.d'+number).addClass('active');
	} else {
		$('.d'+number).removeClass('active');
	}
}

function specialTimes(){
	var timeVal1 = $('#hours option:selected').val();
	var dateVal1 = $('#day option:selected').val();
	var dateVal2 = $('#month option:selected').val();
	var dateVal3 = $('#year option:selected').val();
	var restValue = $('#restaurant option:selected').val();

	var begin = 8;
	var end = 24;
	if(restValue == 'slovansky-dum' && dateVal1 == '7' && dateVal2 == '6' && dateVal3 == '2011'){
		end = 15;
	}

	//$("#hours option").remove();
	$('#hours').html('');

	//$("#hours").append('<option value=""></option>');
	var str = '<option value=""></option>';
	for(i = begin; i <= end; i++){
	    if(i < 10){
			value = '0'+i;
		} else {
			value = i;
		}
        //$("#hours").append('<option value="'+value+'">'+value+'</option>');
        str += '<option value="'+value+'">'+value+'</option>';
	}

	$('#hours').html(str);

	$("#hours option[value='"+timeVal1+"']").attr('selected', true);
	if($('#hours').val() != '' && $('#minutes').val() != ''){
		$('#cardTime').html($('#hours').val()+':'+$('#minutes').val());
	} else {
		$('#cardTime').html('');
	}
}

function checkReservationForm(component){

	if($('#regInfo').css('display') == 'none'){
		$('#regInfo').show();
		$('#doneInfo').hide();
		$('#cardName').html('');
		$('#cardTime').html('');
		$('#cardDate').html('');
		$('.card-restaurant').hide();
	}

	specialTimes();
	
	if(component){
		switch(component.id){
			case 'restaurant':
				var restValue = $('#restaurant option:selected').val();
				$('.card-restaurant').hide();
				$('#card-'+restValue).show();
				$('#card-over-'+restValue).show();

				if(restValue == 'slovansky-dum' || restValue == 'spindleruv-mlyn'){
					$('#noSmokeSpan').hide();
					$('#smoke').attr('checked', 'checked');
				} else {
					$('#noSmokeSpan').show();
				}
				
				break;
			case 'first_name':
			case 'last_name':
				$('#cardName').html($('#first_name').val()+' '+$('#last_name').val());
				break;
			case 'hours':
			case 'minutes':
				if($('#hours').val() != '' && $('#minutes').val() != ''){
					$('#cardTime').html($('#hours').val()+':'+$('#minutes').val());
				} else {
					$('#cardTime').html('');
				}
				break;
			case 'day':
			case 'month':
			case 'year':
				if($('#day').val() != '' && $('#month').val() != '' && $('#year').val() != ''){
					$('#cardDate').html($('#day').val()+'.'+$('#month').val()+'.'+$('#year').val());
				} else {
					$('#cardDate').html('');
				}
				break;
		}
	}

	// Check all items
	var valid_1 = checkFields(['restaurant']);
	
	if($('input[name="environment"]:checked').val()){
		if($('input[name="environment"]:checked').val() == 'no_smoke' && $('#restaurant').val() == 'slovansky-dum'){
			$('#environmentLabel').addClass('emptyField');
			$('#invalid li.smoke').show();
			valid_1 = false;
		} else {
			$('#environmentLabel').removeClass('emptyField');
			$('#invalid li.smoke').hide();
		}
	} else {
		$('#environmentLabel').addClass('emptyField');
		$('#invalid li.smoke').show();
		valid_1 = false;
	}

	var valid_2 = checkFields(['hours', 'minutes', 'day', 'month', 'year']);

	var valid_3 = true;
	var over_limit = false;
	
	if (parseInt($('#persons').val()) > 0){
		$('#persons').removeClass('emptyField');
		$('#invalid li.persons').hide();
		
		var overLimit = 10;
		if($('#restaurant').val() == 'havelska'){
			overLimit = 20;
		}

		if(parseInt($('#persons').val()) >= overLimit){
			over_limit = true;
		}
	} else {
		$('#persons').addClass('emptyField');
		$('#invalid li.persons').show();
		valid_3 = false;
	}
	
	if(over_limit){
		$('#submitButton').hide();
		$('#cardOverlimitTable').show();
		$('#cardTable').hide();
	} else {
		$('#submitButton').show();
		$('#cardOverlimitTable').hide();
		$('#cardTable').show();
	}
	
	var valid_4 = checkFields(['first_name', 'last_name', 'phone', 'email']);

	if(trim($('#email').val()) != ''){
		if(validateEmail($('#email').val())){
			$('#email').removeClass('emptyField');
			$('#invalid li.email').hide();
		} else {
			$('#email').addClass('emptyField');
			$('#invalid li.email').show();
			valid_4 = false;
		}
	}

	if(trim($('#phone').val()) != ''){
		if(validatePhone($('#phone').val())){
			$('#phone').removeClass('emptyField');
			$('#invalid li.phone').hide();
		} else {
			$('#phone').addClass('emptyField');
			$('#invalid li.phone').show();
			valid_4 = false;
		}
	}



	// dodatecne cekne datum a cas - druhy selektbox
	var timeVal1 = $('#hours option:selected').val();
	var timeVal2 = $('#minutes option:selected').val();
	var dateVal1 = $('#day option:selected').val();
	var dateVal2 = $('#month option:selected').val();
	
	if (timeVal1 && timeVal2){
		$('#invalid li.hours').hide();
	} else {
		$('#invalid li.hours').show();
	}
	
	if (dateVal1 && dateVal2){
		$('#invalid li.day').hide();
	} else {
		$('#invalid li.day').show();
	}
	
	

	var valid;
	if(valid_1 && valid_2 && valid_3 && valid_4){
		valid = true;
	} else {
		valid = false;
	}

	markActivePart(valid_1, 1);
	markActivePart(valid_2, 2);
	markActivePart(valid_3, 3);
	markActivePart(valid_4, 4);
	
	return valid;
}



$(document).ready(function(){
	try {
		// function Overlayer(animate, closeOnClick, bgColor, opacity)
		var lay = new Overlayer(true, true, 'white');
	
		if (element('#newsletter')){
			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.submit').click(function(){
				var inp = $('#newsletter input.text');	
				if (validEmail.test(inp.val())){
					inp.css('borderColor', borders.def);
					document.getElementById('newsletter').onsubmit = function(){ return true; };
					return true;
				} else {
					inp.css('borderColor', borders.red);
					return false;
				}
			});
		}
		
		$('h2.tabnav a').click(function(){
			return false;
		});


		$('form#reservationForm').submit(function(){
			return false;
		});

		
		if(typeof(_calendar_config) != "undefined"){
			g_l.DAYS_3 = _calendar_config.dayShorts;
			g_l['MONTHS'] = _calendar_config.monthNames;
			var calendarObject = new JsDatePick({
				useMode    : 1,
				isStripped : true,
				target     : 'calendar',
				weekStartDay : 1,
				yearsRange : _calendar_config.yearsRange
			});

			calendarObject.setOnSelectedDelegate(function(){
				var obj = calendarObject.getSelectedDay();
				$('#year').val(obj.year);
				$('#month').val(obj.month);
				$('#day').val(obj.day);
				checkReservationForm({id: 'day'});
				$('#calendarOutbox').hide();
			});

			var restValue = $('#restaurant option:selected').val();
			$('.card-restaurant').hide();
			$('#card-'+restValue).show();
			$('#card-over-'+restValue).show();

			if(restValue == 'slovansky-dum' || restValue == 'spindleruv-mlyn'){
				$('#noSmokeSpan').hide();
				$('#smoke').attr('checked', 'checked');
			} else {
				$('#noSmokeSpan').show();
			}

			$('#useCalendar').click(function(){
				if($('#calendarOutbox').css('display') == 'none'){
					$('#calendarOutbox').show();
				} else {
					$('#calendarOutbox').hide();
				}
			});
			
			$('#submitButton').click(function(){
				var valid = checkReservationForm();
				if (!valid){
					lay.show();
					$('#invalid').fadeIn();
					return false;
				}
			
				$.post('kogo_reservation/', $('#reservationForm').serialize(),
				   function(data) {
					$(':input','#reservationForm')
						.not(':button, :submit, :reset, :hidden, :radio')
						.val('')
						.removeAttr('selected');
						
						$('#regInfo').hide();
						$('#regNo').html(data);
						$('#doneInfo').show();
				   });
			});
			
			$('#invalid .close').click(function(){
				$('#overlayer').click();
				$('#invalid').fadeOut();
			});

			$('#reservationForm fieldset').click(function(){
				$('#reservationForm fieldset').removeClass('active');
				$(this).addClass('active');
			});
			
			$('#reservationForm input, #reservationForm textarea, #reservationForm select').focus(function(){
				$('#reservationForm fieldset').removeClass('active');
				if($(this).attr('type') == 'radio'){
					$('#f1').addClass('active');
				} else {
					$(this).parent().addClass('active');
				}
			});

			
		}
		
		$('#overlayer').click(function(){
			$('#invalid, #popup, .map-popup').fadeOut();
		});
		
		$('#popup button.cancel').click(function(){
			$('#invalid, #popup').fadeOut();
			lay.hide();
		});
	

		/**
		 * podstrana
		 * slide temat		 
		 **/
		if (element('.slide .inner img')){
			new (function(){
				var num = $('.slide .inner img').size();
				var wid = $('.slide').width();
				
				$('.slide .inner').css('width', (num*wid)+'px');
				$('.slide .nav a').each(function(i){
					$(this).attr('id', 'ref_item_'+i);
				});
				
				function parseId(elem){
					return ($(elem).attr('id')).replace(/^ref_item_([0-9]+)$/i, '$1');
				}
				
				function slideIt(self, auto){
					var cnt = parseId(self);
					var move = (cnt*wid)*(-1)+'px';
					$('.slide .nav a').removeClass('active');
					
					$(self).addClass('active');
					
					$('.slide .nav a span').each(function(){
						var tp = parseInt($(this).css('top'));
						if (tp == 0){
							$(this).stop().animate({
								top : '9px'
							}, 1000, null, function(){
								$('.slide .nav a.active span').stop().animate({
									top : 0
								}, 1000);
							});
						}
					});
					
					$('.slide .inner').stop().animate({
						left : move
					}, 2000, null, function(){
						if (auto){
							autoSlide();
						}
					});
				}
				
				$('.slide .nav a:first-child span').css({ top : 0 });
				
				$('.slide .nav a').click(function(){
					autoSliding = false;
					slideIt(this);
					return false;
				});
				
				var autoSliding = true;
				var autoTime = 5000;
				var autoSmer = -1; // to je do prava
				var tajmaut = null;
				
				var autoSlide = function(){
					if (!$('.nav a').size()){
						return false;
					}
				
					clearTimeout(tajmaut);
					tajmaut = setTimeout(function(){
						if (autoSliding){
							var id = parseInt(parseId('.nav a.active'));
							if ((autoSmer < 0 && id+1 >= num) || (autoSmer > 0 && id <= 0)){
								autoSmer = autoSmer*(-1);
							}
							id = id + (1*-autoSmer);
							slideIt($('.nav a#ref_item_'+id), true);
						}
					}, autoTime);
				};
				
				autoSlide();
			});
		}
		
		/**
		 * homepage slide novinek - zavisle na vypnuti banneru
		 * zavirani banneru pres novinky		 		 
		 **/	 
		if (element('.rests .inner')){
			new (function(){
				/*
				$('.rests .inner .item').hover(function(){
					$('.shipka-left, .shipka-right').css('display', 'block');
				});
				$('.content, .logos, .header').hover(function(){
					$('.shipka-left, .shipka-right').css('display', 'none');
				});
				*/
				var freshClosed = getCookie('fresh_closed');
				if (!freshClosed){
					$('.fresh').css({ top : '0px' });
					var secs = 5000;
					var intv = setInterval(function(){
						var innerSecs = parseInt($('.fresh .close em').html());
						var nextSecs = innerSecs-1;
						if (nextSecs >= 0){
							$('.fresh .close em').html(nextSecs);
						} else {
							$('.fresh .close').click();
							clearInterval(intv);
						}
					}, 1000);
				} else {
					/*
					$('.shipka-left-layer, .shipka-right-layer').hover(function(){
						$('.shipka-left, .shipka-right').css('display', 'block');
					});
					*/
				}
				$('.fresh .close em').html(secs/1000);
				$('.fresh').click(function(){
					$('.fresh').animate({
						top : '-351px'
					}, 1000, null, function(){
						$('.fresh').css('display', 'none');
						$('.fresh').css('margin-top', '0px');
						/*
						$('.shipka-left-layer, .shipka-right-layer').hover(function(){
							$('.shipka-left, .shipka-right').css('display', 'block');
						});
						*/
						setCookie('fresh_closed', 1, 1);
					});
					clearInterval(intv);
					return false;
				});
				$('.shipka-left, .shipka-right').css('display', 'block');
			});
		}
		
		if (element('.map-popup')){
			new (function(){
				var icoWid = 40;
				var icoHgt = 41;
				var icoImg = 'img/ikona.gif';
				
				var createMarker = function(_latitude, _longitude, _content){
					var ico = new GIcon();
					ico.image = icoImg;
					ico.iconSize = new GSize(icoWid, icoHgt);
					ico.iconAnchor = new GPoint(icoWid, icoHgt);
					ico.infoWindowAnchor = new GPoint(icoWid, icoHgt);
					ico.imageMap = [icoWid, icoHgt];
					var point = new GLatLng(_latitude, _longitude);
					var marker = new GMarker(point, { icon : ico });
					GEvent.addListener(marker, 'click', function() {
						marker.openInfoWindowHtml(_content, { className : 'marker' });
					});
					return marker;
				};
				
				var centerMap = [50.086091, 14.424716];
				var zoomMap = 16;
				
				var map = new GMap2(document.getElementById('map1'));
				map.addControl(new GSmallMapControl());
				map.setCenter(new GLatLng(centerMap[0], centerMap[1]), zoomMap);
				map.setMapType(G_NORMAL_MAP);
				map.setUIToDefault();
				
				window.markers = [
					createMarker(50.085732, 14.421986, '<strong>Ristorante Pizzeria KOGO Havelská</strong><br />Havelská 499/27, Praha 1<br />Tel/Phone: 224 210 259, 224 214 543<br />Fax: 224 210 260'),
					createMarker(50.086233, 14.427731, '<strong>Ristorante KOGO Slovanský Dům</strong><br />Na Příkopě 22, Praha 1<br />Tel/Phone: 221 451 258 - 59'),
					createMarker(50.086091, 14.424716, '<strong>KOGO Kolowrat Palace</strong><br />Ovocný trh 6, Praha 1, 110 00<br />Tel/Phone: +420 224 242 507<br />GSM: +420 724 841 282'),
				];
				
				for (i = 0; i < window.markers.length; i++){
					var mar = window.markers[i];
					if (typeof mar == 'object'){
						map.addOverlay(mar);
						if (!mar.isHidden()){
							mar.hide();
						}
					}
				}

				$('.google-map').click(function(){
					var rel = $(this).attr('rel');
					for (i = 0; i < window.markers.length; i++){
						var mar = window.markers[i];
						if (typeof mar == 'object'){
							if (i == rel){
								mar.show();
							} else {
								if (!mar.isHidden()){
									mar.hide();
									mar.closeInfoWindow();
								}
							}
						}
					}
					
					$('.map-popup').fadeIn();
					lay.show();
					
					map.setCenter(new GLatLng(centerMap[0], centerMap[1]), zoomMap);
					map.setUIToDefault();
					
					return false;
				});
				
				$('.map-popup button').click(function(){
					lay.hide();
					$(this).parent().fadeOut();
					return false;
				});
				
				$('.map-popup').hide().css({ visibility : 'visible' });
			});
		}
		
		if (element('.share-popup')){
			$('.share-popup').click(function(){
				var url = $(this).attr('rel');
				le = Math.ceil((screen.width/2)-400);
				to = 100;
				url = url.replace('&amp;', '&');
				window.open(url, '_blank', 'menubar=no,toolbar=yes,resizable=no,scrollbars=yes,status=no, left='+le+', top='+to+', width=800, height=700');
				return false;
			});
		}
	} catch(err){
		if (typeof console != 'undefined'){
			console.log(getException(err));
		} else {
			throw new Error(getException(err));
		}
	}
});

