jQuery(document).ready(function($) {	
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$('.content_td .menu_horiz > div').css('float', 'left');
	
	$('div.container_1').each(function(){
		var parent_id = $(this).parent().index();
		var margin_width = 0;
		
		for ( var i = 0; i < parent_id; i++ ) {
			margin_width += parseInt($('.menu_horiz > div:eq('+i+')').width());
		}
		
		$(this).css('marginLeft', '-'+margin_width+'px');
	});
	
	$('.menu_horiz > div').hover(
		function(){
			$('.container_1:animated').hide();
			$('.container_1:visible').hide();
			$(this).find('.container_1').fadeIn();
		},
		function(){
			$(this).find('.container_1').fadeOut();
		}
	);
	
	
	
	$('.content_td .content_tbl .all .gallery .img_sml .mini_gal').each(function(){
		var mini_gall_cont_w = $(this).find('.cont .img_block').length * 416;
		$(this).find('.cont').width(mini_gall_cont_w);
	});
	
	$('.content_td .content_tbl .all .gallery .img_med a:first').fadeIn();
	
	$('.content_td .content_tbl .all .gallery .img_sml .mini_gal .cont a').click(function(){
		var img_id = $(this).index() + 12*$(this).parent().index();
		$('.content_td .content_tbl .all .gallery .img_med a:visible').fadeOut(300, function(){
			$('.content_td .content_tbl .all .gallery .img_med a:eq('+img_id+')').fadeIn();
		});
		return false;
	});
	
	$('.content_td .content_tbl .all .gallery .img_sml .pagination a').click(function(){
		var cur_class = $(this).attr('class');
		var cur_index = $(this).index();
		if ( cur_class != 'active' && $('.content_td .content_tbl .all .gallery .img_sml .mini_gal .cont:animated').length == 0 ) {
			$('.content_td .content_tbl .all .gallery .img_sml .pagination a').removeClass('active');
			$(this).addClass('active');
			$('.content_td .content_tbl .all .gallery .img_sml .mini_gal .cont').animate({
				marginLeft: -(312*cur_index)
			});
		}
		return false;
	});
	
	$('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal').each(function(){
		var mini_gall_cont_w = $(this).find('.cont .img_block').length * 225;
		$(this).find('.cont').width(mini_gall_cont_w);
	});
	
	$('.content_td .content_tbl .content_menu .gallery_small .img_med a:first').fadeIn();
	
	$('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont a').click(function(){
		var img_id = $(this).index() + 9*$(this).parent().index();
		$('.content_td .content_tbl .content_menu .gallery_small .img_med a:visible').fadeOut(300, function(){
			$('.content_td .content_tbl .content_menu .gallery_small .img_med a:eq('+img_id+')').fadeIn();
		});
		return false;
	});
	
	$('.content_td .content_tbl .content_menu .gallery_small').hover(
		function(){
			$(this).find('.roll_left, .roll_right').fadeIn();
		},
		function(){
			$(this).find('.roll_left, .roll_right').fadeOut();
		}
	);
	
	$('.content_td .content_tbl .content_menu .gallery_small .roll_right').click(function(){
		if ( $('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont:animated').length === 0 ) {
			var cur_margin = Math.abs(parseInt($('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont').css('marginLeft')));
			if ( cur_margin+225 < $('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont').width() ) {
				$('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont').animate({
					marginLeft: -(cur_margin+225)
				});
			}
		}
	});
	
	$('.content_td .content_tbl .content_menu .gallery_small .roll_left').click(function(){
		if ( $('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont:animated').length === 0 ) {
			var cur_margin = Math.abs(parseInt($('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont').css('marginLeft')));
			if ( cur_margin-225 >= 0 ) {
				$('.content_td .content_tbl .content_menu .gallery_small .img_sml .mini_gal .cont').animate({
					marginLeft: -(cur_margin-225)
				});
			}
		}
	});
	
	
	$('#left_menu_container div').each(function(){
		if ( $(this).find('.sub_menu').length > 0 ) {
			$(this).children('a').css({
				backgroundImage: 'url(\'/img/sub_menu_icon.gif\')',
				backgroundPosition: 'right center',
				backgroundRepeat: 'no-repeat'
			});
		}
	});
	
	$('#left_menu_container div').children('a').each(function(){
		$(this).parent().height($(this).height()+10);
	});
	
	$('#left_menu_container .sub_menu').hide();
	
	$('#left_menu_container .sub_menu').parent().hover(
		function(){
			$('#left_menu_container .sub_menu:animated').fadeOut();
			$(this).children('.sub_menu').fadeIn();
		},
		function(){
			$(this).children('.sub_menu').fadeOut();
		}
	);
	
	
	
	var season_offers_width = $('.content_td .content .season_offers .container .season_offer').length * 243;
	$('.content_td .content .season_offers .container').width(season_offers_width);
	
	var services_width = $('.content_td .content .services .container .service').length * 246;
	$('.content_td .content .services .container').width(services_width);
	
	$('.content_td .content .season_offers, .content_td .content .services').hover(
		function(){
			$(this).find('.roll_left, .roll_right').fadeIn();
		},
		function(){
			$(this).find('.roll_left, .roll_right').fadeOut();
		}
	);
	
	$('.content_td .content .season_offers .roll_left').click(function(){
		if ( $(this).parent().find('.container:animated').length == 0 ) {
			var target_obj = $(this).parent().find('.container');
			var replace_obj = target_obj.find('.season_offer:last').html();
			target_obj.prepend('<div class="season_offer">'+replace_obj+'</div>');
			target_obj.css('marginLeft', '-243px');
			target_obj.animate({
				marginLeft: 0
			});
			target_obj.find('.season_offer:last').remove();
		}
	});
	
	$('.content_td .content .season_offers .roll_right').click(function(){
		if ( $(this).parent().find('.container:animated').length == 0 ) {
			var target_obj = $(this).parent().find('.container');
			var replace_obj = target_obj.find('.season_offer:first').html();
			target_obj.append('<div class="season_offer">'+replace_obj+'</div>');
			target_obj.animate({
				marginLeft: -243
			}, 400, function(){
				target_obj.find('.season_offer:first').remove();
				target_obj.css('marginLeft', '0');
			});
		}
	});
	
	$('.content_td .content .services .roll_left').click(function(){
		if ( $(this).parent().find('.container:animated').length == 0 ) {
			var target_obj = $(this).parent().find('.container');
			var replace_obj = target_obj.find('.service:last').html();
			target_obj.prepend('<div class="service">'+replace_obj+'</div>');
			target_obj.css('marginLeft', '-246px');
			target_obj.animate({
				marginLeft: 0
			});
			target_obj.find('.service:last').remove();
		}
	});
	
	$('.content_td .content .services .roll_right').click(function(){
		if ( $(this).parent().find('.container:animated').length == 0 ) {
			var target_obj = $(this).parent().find('.container');
			var replace_obj = target_obj.find('.service:first').html();
			target_obj.append('<div class="service">'+replace_obj+'</div>');
			target_obj.animate({
				marginLeft: -246
			}, 400, function(){
				target_obj.find('.service:first').remove();
				target_obj.css('marginLeft', '0');
			});
		}
	});
	
	var season_offers_timer_id = null;
	var season_offers_delta_time = 5000;
	function season_offers_change() {
		$('.content_td .content .season_offers .roll_right').trigger('click');
	}
	$('.content_td .content .season_offers').hover(
		function () {
			clearInterval(season_offers_timer_id);
		},
		function () {
			season_offers_timer_id = setInterval(season_offers_change, season_offers_delta_time);
		}
	);
	season_offers_timer_id = setInterval(season_offers_change, season_offers_delta_time);
	
	var services_timer_id = null;
	var services_delta_time = 7000;
	function services_change() {
		$('.content_td .content .services .roll_right').trigger('click');
	}
	$('.content_td .content .services').hover(
		function () {
			clearInterval(services_timer_id);
		},
		function () {
			services_timer_id = setInterval(services_change, services_delta_time);
		}
	);
	services_timer_id = setInterval(services_change, services_delta_time);
	
	
	
	
	if ( $('#cta_source').length === 1 ) {
		
		$('.content_td .content_tbl .all').append('<table id="cta" border="0" cellpadding="0" cellspacing="0"><tr class="bookmarks"><td class="last"></td></tr><tr><td class="dynamic_cont"></td></tr></table>');
		
		var bookmarks_count = 0;
		$('#cta_source tr').each(function(){
			var bookmark_title = $(this).find('td:first').html();
			var dynamic_cont = $(this).find('td:last').html();
			if ( bookmark_title != '&nbsp;' && bookmark_title != '<br>' && dynamic_cont != '&nbsp;' && dynamic_cont != '<br>' ) {
				$('#cta .bookmarks .last').before('<td><a href="#">'+bookmark_title+'</a></td>');
				$('#cta .dynamic_cont').append('<div class="entity">'+dynamic_cont+'</div>');
				bookmarks_count++;
			}
		});
		
		if ( bookmarks_count > 0 ) {
//		if ( $('#cta .bookmarks a').length > 0 ) {
			$('#cta').fadeIn();
			$('#cta .dynamic_cont').attr('colspan', bookmarks_count+1);
			$('#cta .bookmarks a:eq(0)').addClass('active');
			$('#cta .dynamic_cont > div:eq(0)').fadeIn();
			$('#cta .bookmarks a').click(function(){
				if ( $(this).attr('class') != 'active' && $('#cta .dynamic_cont > div:animated').length == 0 ) {
					var id = $(this).parent().index();
					$('#cta .bookmarks a.active').removeClass('active');
					$(this).addClass('active');
					$('#cta .dynamic_cont > div:visible').fadeOut(300, function(){
						$('#cta .dynamic_cont > div:eq('+id+')').fadeIn();
					});
				}
				return false;
			});
		}
	}
	
	var ads_margin_top = Math.max( ($('body').height() - $('#ad_tbl').height()) / 2, 10 );
	$('#ad_tbl').css('marginTop', ads_margin_top);
	$('#popup_ad').height($('.main_tbl').height());
	
	var mouse_over_ads = false;
	$('#ad_cont').hover(
		function(){
			mouse_over_ads = true;
		},
		function(){
			mouse_over_ads = false;
		}
	);
	
	$('#popup_ad').click(function(){
		if ( !mouse_over_ads ) {
			$(this).fadeOut();
		}
	});
	
	$('#ad_close').click(function(){
		$('#popup_ad').fadeOut();
	});
	
	var content_menu_height = Math.max($('#content_menu_area .menu').height(), $('#content_menu_area .title').height());
	
	$('#content_menu_area .title').height(content_menu_height+40);
	$('#content_menu_area .menu').height(content_menu_height);
	
	var left_menu_width = $('#content_menu_area .menu').width();
	$('#content_menu_area .menu').width(0);
	
	$('#content_menu_area').hover(
		function(){
			$(this).find('.menu').show(0, function(){
				$(this).animate({width: left_menu_width}, 300);
				$('#content_menu_area .title img').attr('src', '/img/left_menu_close.png');
			});
		},
		function(){
			$(this).find('.menu').animate({
				width: 0
			}, 300, function (){
				$('#content_menu_area .title img').attr('src', '/img/left_menu_open.png');
				$(this).hide();
			});
		}
	);
});
