var curImage = 0;
var homeImageAnimation;
var home_animation_on = false;
var imageContainerToggler = true;


$(document).ready(function() {

	// Draggable logo BEG
	$('#logo').draggable({
		appendTo: 'body',
		containment: 'body',
		helper: 'original',
		zIndex: 9999,
		stop: function() {
			$(this).animate({
				'top': '0px',
				'left': '109px'
			}, 1000, 'easeOutElastic');
		}
	});

	/* Booking START */
	$('#booking_trigger').unbind('click').bind('click', function() {
		$('#booking_outer_wrapper').stop().animate({
			'width': ($('#booking_outer_wrapper').hasClass('open')) ? '0px' : '332px'
		}).toggleClass('open');
	});

	if ($('#booking_trigger').hasClass('send')) {
		$('#booking_trigger').trigger('click');
	}
	
	$('#booking_inner_wrapper input[type="text"]').unbind('focus').bind('focus', function() {
		if ($(this).attr('value') == $(this).attr('title')) {
			$(this).attr('value', '');
		}
	});
	
	$('#booking_inner_wrapper input[type="text"]').unbind('blur').bind('blur', function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value', $(this).attr('title'));
		}
	});
	
	$('.datepicker').datepicker({
		dateFormat: 'yy.mm.dd'
	});
	
	
	/* Booking END */
	


	/* Main menu START */
	// Disable link for disabled menu_items
	$('#main_menu_wrapper .disabled').unbind('click').bind('click', function(e) {
		e.preventDefault();
	});
	
	var main_menu_item_selector = $('a.main_menu_item');
	var sub_menu_wrapper_selector = $('#sub_menu_wrapper');
	
	var main_menu_wrapper_width = $('#main_menu_wrapper').width();
	var main_menu_item_length = main_menu_item_selector.length;
	var main_menu_item_width = Math.floor(main_menu_wrapper_width / main_menu_item_length);
	
	// Set width of the main_meu_items
	main_menu_item_selector.css({
		'width': main_menu_item_width + 'px'
	});
	
	$('.main_menu_hover_middle').css({
		'width': main_menu_item_width - 20 + 'px'
	});
	
	// Set width of the sub_menu_items
	var sub_menu_wrapper_width = $('.sub_menu_item_absolute_wrapper').innerWidth();

	$('.sub_menu_item_absolute_wrapper').each(function() {
		var sub_menu_item_width = Math.floor(sub_menu_wrapper_width / $('.sub_menu_item', this).length);

		$('.sub_menu_item', this).css({
			'width': (sub_menu_item_width) + 'px'
		});
	});

	 
	main_menu_item_selector.unbind('mouseenter').bind('mouseenter', function() {
		var sub_menu_id = $(this).attr('id').replace('menu_parent_', 'sub_menu_');

		if ($('#' + sub_menu_id).length > 0) {
			sub_menu_wrapper_selector.stop().animate({
				'height': '131px'
			}, 250);
			
			$('#' + sub_menu_id).stop().animate({
				'height': '90px'
			});
		}
	});

	main_menu_item_selector.unbind('mouseleave').bind('mouseleave', function() {
		var sub_menu_id = $(this).attr('id').replace('menu_parent_', 'sub_menu_');

		if ($('#' + sub_menu_id).length > 0) {
			sub_menu_wrapper_selector.stop().animate({
				'height': '0px'
			}, 500);

			$('#' + sub_menu_id).stop().animate({
				'height': '0px'
			});
			
		}
	});

	$('.sub_menu_item_absolute_wrapper').bind('mouseenter', function() {
		main_menu_item_id = $(this).attr('id').replace('sub_menu_', 'menu_parent_');
		$('#' + main_menu_item_id).trigger('mouseenter').children('.main_menu_hover_wrapper').addClass('active');
	});

	$('.sub_menu_item_absolute_wrapper').unbind('mouseleave').bind('mouseleave', function() {
		main_menu_item_id = $(this).attr('id').replace('sub_menu_', 'menu_parent_');
		$(this).stop().animate({
			'height': '90px'
		});
		$('#' + main_menu_item_id).trigger('mouseleave').children('.main_menu_hover_wrapper').removeClass('active');
	});


	/* Main menu END */
	
	$('.sitemap_item, .sitemap_title_item').unbind('mouseenter').bind('mouseenter', function() {
		$(this).stop().animate({
			'left': '10px'
		}, 150);
	});

	$('.sitemap_item, .sitemap_title_item').unbind('mouseleave').bind('mouseleave', function() {
		$(this).stop().animate({
			'left': '0px'
		}, 750);
	});


	// Snowboarder
	$('#snowboarder_wrapper').unbind('mouseenter').bind('mouseenter', function() {
		$('#snowboarder_trigger_wrapper').stop().animate({
			'height': '94px'
		}, 200);
	});

	$('#snowboarder_wrapper').unbind('mouseleave').bind('mouseleave', function() {
		$('#snowboarder_trigger_wrapper').stop().animate({
			'height': '0px'
		});
	});
	
	$('#snowboarder_trigger_wrapper').unbind('mouseenter').bind('mouseenter', function() {
		$('#snowboarder').trigger('mouseenter');
	});

	$('#snowboarder_trigger_wrapper').unbind('mouseleave').bind('mouseleave', function() {
		$('#snowboarder').trigger('mouseleave');
	});
	
	$('.snowboarder_trigger').unbind('mouseenter').bind('mouseenter', function() {
		$('span', this).stop().animate({
			'top': '33px'
		}, 600, 'easeOutElastic');
	});
	
	$('.snowboarder_trigger').unbind('mouseleave').bind('mouseleave', function() {
		$('span', this).stop().animate({
			'top': '-45px'
		}, 250);
	});
	
	$('#snowboarder_trigger_win').unbind('click').bind('click', function() {
		start_stunt('win');
		$('#snowboarder_trigger_wrapper').trigger('mouseleave');
	});
	
	$('#snowboarder_trigger_fail').unbind('click').bind('click', function() {
		start_stunt('fail');
		$('#snowboarder_trigger_wrapper').trigger('mouseleave');
	});


	/* Recommendation START */
	$("#recommendation_toggler").click(function (e) {
		e.preventDefault();

		$("#recommendation_container").animate({
			'top': '-420px',
			'height' : '380px'
		}, 700);
	});

	$("#hide_recommendation").click(function () {
		$("#recommendation_container").animate({
			'top': '0px',
			'height' : '0px'
		}, 700);
	});

	var recommendation_message = '';
	$('#recommendation_message').keyup(function(ekey) {
		$('#recommendation_msg_count').text(100 - $(this).val().length);
		if (recommendation_message.length >= 100 && ekey.which != 8 && ekey.which != 46) {
			$(this).val(recommendation_message);
		} else {
			recommendation_message = $(this).val();
		}
	});

	$("#send_recommendation").click(function () {
		$('<input>')
			.css('display', 'none')
			.attr({
				name: 'recommendation_sent',
				value: 'sent_true'
			})
			.appendTo('#form_recommendation')
		;

		$('#form_recommendation').attr({
			method: 'post'
		}).submit();
	});
	/* Recommendation END */
	
	
	// Galleries
	$('.fe_sd_gallery_item').hover(function() {
		$(this).stop().animate({
			'backgroundColor': '#ffea2b'
		});
	}, function() {
		$(this).stop().animate({
			'backgroundColor': '#D7D7D7'
		});
	});
	
	$('.checkmark, .arrow').unbind('mouseenter').bind('mouseenter', function() {
		$(this).stop().animate({
			'backgroundPosition': '5px 0px'
		});
	});

	$('.checkmark, .arrow').unbind('mouseleave').bind('mouseleave', function() {
		$(this).stop().animate({
			'backgroundPosition': '0px 0px'
		});
	});
	
	// Header animation
	if (typeof homeAnimationImages != 'undefined' && homeAnimationImages) { 
		if (homeAnimationImages.length > 1) {
			$('#header_images').rsanimator({
				'images': homeAnimationImages,
				'width': 909,
				'height': 385/*,
				'indicator': {
					'container': $('#home_animatedcar_trigger'),
					'elementClass': 'carTrigger',
					'elementClassActive': 'current'
				}*/
			});
		} else if (homeAnimationImages.length > 0) {
			$('#header_images').css({
				'backgroundImage': 'url(' + homeAnimationImages[0].imagefile + ')'
			});
		}
	}
});

function start_stunt(mode) {
	var snowboarder, snowboarder_wrapper, crashed_snowboarder, kicker_position;
	
	// selector
	snowboarder = $('#snowboarder');
	snowboarder_wrapper = $('#snowboarder_wrapper');
	crashed_snowboarder = $('#crashed_snowboarder');

	// math
	kicker_position = $(document).height() - 555;
	
	// ritze-rotze-ratzeburg
	snowboarder_wrapper.unbind('mouseenter');
	
	$('#kicker').css({
		'display': 'block'
	});

	if (mode == 'win') {
		snowboarder.rotate({
			duration: 1000,
			animateTo: -45
		});

		snowboarder_wrapper.animate({
			'top': '-144px'
		}, 500, function() {
			snowboarder_wrapper.animate({
				'top': '-10px',
				'left': '378px'
			}, 500, 'linear', function() {
				snowboarder.rotate({
					duration: 700,
					animateTo: 0
				});
				
				snowboarder_wrapper.animate({
					'top': '21px',
					'left': '358px'
				}, 500, 'linear', function() {
					snowboarder_wrapper.animate({
						'top': '24px',
						'left': '100px'
					}, 1000, 'linear', function() {
						snowboarder.rotate({
							duration: 1000,
							animateTo: 30
						});

						snowboarder_wrapper.animate({
							'top': '4px',
							'left': '40px'
						}, 700, 'linear', function() {
							snowboarder.rotate({
								duration: 200,
								animateTo: 0
							});

							snowboarder_wrapper.animate({
								'top': '-23px',
								'left': '0px'
							}, 200, 'linear', function() {
								snowboarder.rotate({
									duration: 100,
									animateTo: -10
								});
								
								snowboarder_wrapper.animate({
									'top': '-15px',
									'left': '-30px'
								}, 100, 'linear', function() {
									snowboarder.rotate({
										duration: 100,
										animateTo: -20
									});

									snowboarder_wrapper.animate({
										'top': '-10px',
										'left': '-45px'
									}, 100, 'linear', function() {
										snowboarder.rotate({
											duration: 100,
											animateTo: -25
										});
										
										snowboarder_wrapper.animate({
											'top': '0px',
											'left': '-60px'
										}, 100, 'linear', function() {
											snowboarder.rotate({
												duration: 100,
												animateTo: -35
											});

											snowboarder_wrapper.animate({
												'top': '10px',
												'left': '-75px'
											}, 100, 'linear', function() {
												snowboarder.rotate({
													duration: 100,
													animateTo: -45
												});

												snowboarder_wrapper.animate({
													'top': '20px',
													'left': '-85px'
												}, 100, 'linear', function() {
													snowboarder.rotate({
														duration: 500,
														animateTo: -80
													});

													snowboarder_wrapper.animate({
														'top': '150px',
														'left': '-110px'
													}, 500, 'linear', function() {
														snowboarder.rotate({
															duration: 50,
															animateTo: -45
														});
														
														$('html,body').animate({
															scrollTop: $('#kicker_helper').offset().top
														}, 2000);
														
														snowboarder_wrapper.animate({
															'top': kicker_position + 'px',
															'left': '-220px'
														}, 1000, 'linear', function() {
															snowboarder.rotate({
																duration: 1000,
																animateTo: -40
															});

															snowboarder_wrapper.animate({
																'top': (kicker_position+60) + 'px',
																'left': '-240px'
															}, 200, 'linear', function() {
																snowboarder.rotate({
																	duration: 50,
																	animateTo: -25
																});

																snowboarder_wrapper.animate({
																	'top': (kicker_position+110) + 'px',
																	'left': '-280px'
																}, 200, 'linear', function() {
																	snowboarder.rotate({
																		duration: 50,
																		animateTo: -10
																	});

																	snowboarder_wrapper.animate({
																		'top': (kicker_position+140) + 'px',
																		'left': '-350px'
																	}, 200, 'linear', function() {
																		snowboarder.rotate({
																			duration: 50,
																			animateTo: 0
																		});

																		snowboarder_wrapper.animate({
																			'top': (kicker_position+155) + 'px',
																			'left': '-500px'
																		}, 200, 'linear', function() {
																			snowboarder_wrapper.animate({
																				'top': (kicker_position+155) + 'px',
																				'left': '-1020px'
																			}, 500, 'linear', function() {
																			});
																		});
																	});
																});
															});
														});
													});
												});
											});
										});
									});
								});
							});
						});
					});
				});
			});
		});
	} else if (mode == 'fail') {
		snowboarder_wrapper.animate({
			'top': '25px',
			'left': '-100px'
		}, 1500, false, function() {
			
			snowboarder.rotate({
				duration: 1500,
				animateTo: 170
			});
			
			snowboarder_wrapper.animate({
				'top': '500px',
				'left': '-100px'
			}, 1000, 'linear', function() {
				
				snowboarder.rotate({
					duration: 2000,
					animateTo: 525
				});

				$('html,body').animate({
					scrollTop: $('#kicker_helper').offset().top
				}, 1000);

				snowboarder_wrapper.animate({
					'top': (kicker_position+94) + 'px',
					'left': '-204px'
				}, 500, 'linear', function() {
					snowboarder.css({
						'display': 'none'
					});

					crashed_snowboarder.css({
						'display': 'block'
					});
				});
			});
			
		});
	}
}



