



_DOMAIN = 'http://robair.ch/';




$(document).ready(function() {




	// top navigation handling
	
	$('#form-home select#cat').data('diashow_running', false);

	$('#form-home select#cat').change(function() {
	
		var _value = $(this).attr('value');
		if(_value != '') {
		
			if(_value == 'diashow') {
			
				if($(this).data('diashow_running') === false) {
					$(this).data('diashow_running', true);
					ra_diashow.next();
					ra_diashow.start_diashow();
					ra_popup.close();
				}
				
			} else {
		
				if($(this).data('diashow_running') === true) {
					$(this).data('diashow_running', false);
					ra_diashow.stop_diashow();
				}
				
				ra_diashow.next();
				ra_open_window(_value);
				
			}
		
		}
		
		$(this)[0].options.selectedIndex = 0;
	
	});
	
	ra_open_window = function(url) {
	
		//$.address.value(url.replace(_DOMAIN, ''));  
		ra_popup.open(url);
	
	}







	// intro diashow
	
	ra_diashow = {
	
		_interval_duration: 7000,
		_fading_duration: 1500,
		_current_image: false,
		_images: $('#diashow-images div.image'),
		_global_zindex: 2,
		_diashow_interval: false,
	
		//
		//	init
		//
		init: function() {
		
			ra_diashow._images.hide();
			ra_diashow.next();
		
		},
		
		//
		//	next
		//
		next: function() {
		
			_next = (ra_diashow._current_image === false) ? 0 : ra_diashow._current_image+1;
			if(_next >= ra_diashow._images.length) _next = 0;
			
			ra_diashow._current_image = _next;
			
			$(ra_diashow._images[_next]).hide();
			$(ra_diashow._images[_next]).css('z-index', (ra_diashow._global_zindex++));
			$(ra_diashow._images[_next]).fadeIn(ra_diashow._fading_duration);
		
		},
		
		//
		//	start diashow
		//
		start_diashow: function() {
		
			if(!ra_diashow._diashow_interval) {
				ra_diashow._diashow_interval = window.setInterval(function() {
					ra_diashow.next();
				}, ra_diashow._interval_duration);
			}
			
		},
		
		//
		//	stop diashow
		//
		stop_diashow: function() {
		
			if(ra_diashow._diashow_interval) {
				window.clearInterval(ra_diashow._diashow_interval);
				ra_diashow._diashow_interval = false;
			}
		
		}
	
	};
	
	ra_diashow.init();



	
	
	
	
	
	// sub navigation handling
	
	$('select#subcat').change(function() {
	
		var _value = $(this).attr('value');
		if(_value != '') {
		
			//parent.$.address.value(_value.replace(_DOMAIN, '')); 
			window.location.href = _value;
		
		}
	
	});
	
	
	
	
	
	
	
	// team image scaling
	
	$('body.category-team div.entry img').parents('p').addClass('image');
	
	$('body.category-team div.entry img').css({
		'width': 200,
		'height': 'auto',
		'float': 'left',
		'margin-right': 10,
		'cursor': 'pointer'
	}).data('_is_scaling', false);
	
	$('body.category-team div.entry img').click(function() {
	
		if(!$(this).data('_is_scaling')) {
		
			$(this).data('_is_scaling', true);
		
			// enlarge
			if($(this).width() == 200) {
				$(this).parents('p').siblings('p:not(.image)').hide();
				$(this).animate({
					'width': 425
				}, function() { $(this).data('_is_scaling', false); });
				
			// shrink
			} else {
				$(this).animate({
					'width': 200
				}, function() { 
					$(this).data('_is_scaling', false);
					$(this).parents('p').siblings('p:not(.image)').fadeIn();
				});
			}
			
		}
		
	});









	// calendar enhancements
	
	$('table.calendar, table.calendar tr, table.calendar tr td').removeAttr('style');
	
	$('table.calendar tr').each(function(a, b) {
		$(b).find('td:first').addClass('first');
	});
	
	$('table.calendar tr td a').click(function(e) {
	
		e.preventDefault();
		_hash = $(this).attr('href').split('#');
		if(_hash[1] && $('a[name='+_hash[1]+']').length > 0) {
			$.scrollTo($('a[name='+_hash[1]+']'), 800, { offset: { top:-20 }});
		}
	
	});












	// register form enhancements
	
	$('select#select-journey').change(function() {
	
		var _value = $(this).attr('value');
		var _already_set = false;
		
		if(_value != '') {
		
			if($(this).prev('#selected-journeys').length == 0) $(this).before('<ul id="selected-journeys" />');
			
			$(this).prev('#selected-journeys').find('input').each(function(a, b) {
				if($(b).attr('value') == _value) { 
					_already_set = true;
					return false;
				}
			});
			
			if(!_already_set) {
			
				if($(this).parents('form').is('.tandem')) {
				
					$(this).prev('#selected-journeys').prepend('<li><input type="hidden" class="journey-checkbox" name="af_journeys[]" value="'+_value+'" /><input type="text" name="af_amount[]" value="1" class="text amount" /> x '+_value+'<div class="photo-cd"><label><input type="checkbox" class="checkbox" name="af_photocd[]" value="Ja" /> mit Foto-CD vom Flug (25.&mdash;)</label></div><br class="clear" /></li>');
					
					$(this).prev('#selected-journeys').find('input.amount').each(function(a, b) {
						$(b).attr('name', 'af_amount['+a+']');
					});
					
					$(this).prev('#selected-journeys').find('input.checkbox').each(function(a, b) {
						$(b).attr('name', 'af_photocd['+a+']');
					});
				
				} else {
					$(this).prev('#selected-journeys').prepend('<li><input type="checkbox" class="checkbox journey-checkbox" name="af_journeys[]" value="'+_value+'" checked="checked" /> '+_value+'</li>');
				
					$(this).prev('#selected-journeys').find('input').unbind('click').click(function() {
						if($(this).parents('ul').find('li').length == 1) {
							$(this).parents('ul').remove();
						} else {
							$(this).parents('li').remove();
						}
					});
				}
			
			}
			
		}
		
		$(this)[0].options.selectedIndex = 0;
	
	});
	
	$('form#form-anmeldeformular').submit(function(e) {
	
		var _errors = [];
		$(this).find('.error').removeClass('error');
		
		$(this).find('span.required').each(function(a, b) {
	
			if($(b).parent().next().attr('value') == '') {
				_errors.push($(b).parents('p'));
			}
		
		});
		
		if(!$('#agb').is(':checked')) {
			_errors.push($('p.agb'));
		}
		
		if($('input.journey-checkbox').length == 0) {
			_errors.push($('div#journey-selection'));
		}
		
		if(_errors.length > 0) {
		
			e.preventDefault();
		
			if($(this).find('div#form-errors').length == 0) {
				$(this).prepend('<div id="form-errors"><p>Bitte überprüfe die rot markierten Felder.</p></div>');
			}
			
			$.each(_errors, function(a, b) {
				$(b).addClass('error');
			});
		
		}
	
	});











	// print form/sheet enhancements
	
	$('div.trips-print-sheet').each(function(a, b) {
		window.print();
	});
	
	$('input#tripallinfos').click(function() {
		if($(this).is(':checked')) { $('input.checkbox-infos').attr('checked', 'checked'); } 
		else { $('input.checkbox-infos').removeAttr('checked'); }
	});

	$('input#tripallfotos').click(function() {
		if($(this).is(':checked')) { $('input.checkbox-fotos').attr('checked', 'checked'); } 
		else { $('input.checkbox-fotos').removeAttr('checked'); }
	});








	// mark last paragraph
	
	$('div.entry p:last').each(function(a, b) {
	
		// only mark this paragraph as last, if the next 
		// element is NOT an h3 or address element
		if($(b).next('h3').length == 0 && $(b).next('address').length == 0) {
			$(b).addClass('last');
		}
	
	});









	// footer
	
	$('div#footer').css('cursor', 'pointer').click(function(e) {
		e.preventDefault();
		ra_popup.close();
	});



















	// popup handling
	
	ra_popup = {
	
		init: function() {
		
			if($('body').is('.home')) { $('html, body').css('overflow', 'hidden'); }
			
			var dim = ra_popup.dimensions();
			
			$('#inner-content, #inner_content_frame').css({
				height: dim[1],
				width: dim[0],
				top: 50,
				left: ($(window).width()/2)-(dim[0]/2)
			});

			$('#inner-content').draggable({
				handle: $('#inner-content-head'),
				scroll: false,
				containment: [0, 0, ($(window).width()-dim[0]), ($(window).height()-109)],
				iframeFix: true
			});
			
			$(window).resize(function() {
				ra_popup.reset();
			});
		
		},
		
		reset: function() {
		
			var dim = ra_popup.dimensions();
			
			$('#inner-content, #inner_content_frame').css({
				height: dim[1],
				width: dim[0]
			});
			
			$('#inner-content').draggable('option', 'containment', [0, 0, ($(window).width()-dim[0]), ($(window).height()-109)]);
		
		},
		
		dimensions: function() {
		
			var width = 460;
			var height = $(window).height()-100;
			
			return [width, height];
		
		},
	
		open: function(url) {
		
			$('#inner-content').show();
			inner_content_frame.location.href = url;
		
		},
		
		close: function() {
		
			parent.$('#inner-content').hide();
			parent.inner_content_frame.location.href = '/empty';
		
		}
	
	};
	
	ra_popup.init();
	



	













	// popup magic
	
	/*if(window.name != '_content_window' && !$('body').is('.home')) {

		ra_open_window(window.location.href);
		window.location.href = _DOMAIN;

	}*/
	
	if(window.name != 'inner_content_frame') {
	
		// redirect to home if url contains a subpage with no hash
		_page = window.location.href.replace(_DOMAIN, '');
		if(_page != '' && _page.indexOf('#') == -1) {
			window.location.href = _DOMAIN + '#/' + _page;
		}
		
		// open iframe when home is called with a subpage hash
		_hash = window.location.hash;
		if(_hash != '') {
			ra_popup.open(_DOMAIN + _hash.replace('#', ''));
		}
		
	}












});