
$(function() {
	
	/*
	 * accordion
	 */
	$(".toggleHeader").live("click",function() {
		if($('.divAccHeader.active').attr('id') != $(this).attr('id') && !$(this).hasClass('opened')) {
			$('.divAccHeader.active').next().slideToggle();
			$('.divAccHeader.active').toggleClass("active");
		}
		$(this).next().slideToggle();
		$(this).toggleClass("active");
	});
	
	/*
	 * Guide mitscrollen
	 */
	
	if ($('#divAnsprechpartner').offset()) {
		var guideTopMin = $('#divAnsprechpartner').offset().top - 270;
		var guideTopMax = 0;
		
		setTimeout(function(){
			guideTopMax = $('#divFooter').offset().top - 270 - $('#divAnsprechpartner').height() - 30;
		}, 1000);
		
		$(window).bind("scroll", function(){
		
			var offset = $('#divAnsprechpartner').offset();
			if ($(window).scrollTop() > guideTopMin && $(window).scrollTop() < guideTopMax && guideTopMin > 0 && guideTopMax > 0) {
				$('#divAnsprechpartner').css({
					'top': $(window).scrollTop() - $('#divAnsprechpartner').height() + 35 + 'px'
				});
			}
			if ($(window).scrollTop() < guideTopMin && guideTopMin > 0) {
				$('#divAnsprechpartner').css({
					'top': guideTopMin - $('#divAnsprechpartner').height() +  35 + 'px'
				});
			}
			
			if ($(window).scrollTop() > guideTopMax - 35 && guideTopMax > 0) {
				$('#divAnsprechpartner').css({
					'top': guideTopMax - $('#divAnsprechpartner').height() + 'px'
				});
			}
		});
	}
	
	/*
	 * fancyBox
	 */
	if (!fancyBoxCall) {
		$('.fancyBox').fancybox({
			'hideOnContentClick': true
		});
		
		$('.fancyBoxIframe').fancybox({
			'width': 750,
			'height': 500
		});
		
		$('.fancyBoxContent').fancybox({
			'width': 450,
			'height': 550,
			'hideOnContentClick': false
		});
	}

});

function loadTab(url, obj, data) {
	if (obj) {
		$('#divContentTabNav li.active').removeClass('active');
		$(obj).parent('li').addClass('active');
	}
	$("#tabContent").html('<div class="loading"></div>');
	var newData = $.extend({"loadTab": true}, data);
	$.get(url, newData, function(res) {
		smallGalleries = 0;
		smallGalCurrentPic = 0;
		smallGalPics[smallGalleries] = [];
		smallGalAlts[smallGalleries] = [];
		smallGalLink[smallGalleries] = [];
		clearInterval(smallGalInterval[smallGalleries]);
		document.getElementById('tabContent').innerHTML = res;
		eval($("#tabContent script").html());
		$('.fancyBox').fancybox({
			'hideOnContentClick': true
		}, "html");
	});
}

function controlInput(obj, str, action) {
	switch(action) {
		case "focus":
			if($(obj).val() == str) {
				$(obj).val("");
				$(obj).css("color", "#252525");
			} else {
				$(obj).select();
			}
			break;
		case "blur":
			if($(obj).val() == "") {
				$(obj).val(str);
				$(obj).css("color", "#999");
			}
			break;	
	}
}

/*
 * Suckerfish Menu rewritten for jQuery
 */
function sfMenu(navID) {
	$(navID + " li").each(function(i, el) {
		$(this).mouseover(function() {
			$(this).addClass("sfhover");
		});
		$(this).mouseout(function() {
			$(this).removeClass("sfhover");
		});
	});
}

var nextZindex = 2;

function showHomeIMG(id) {
	$('#divHeaderbild').queue(function () {
		if($('#' + id).css('z-index') == "auto") {
			$('#' + id).css('z-index', 0);
		}
		if (nextZindex != $('#' + id).css('z-index')) {
			$('#' + id).css({
				'display': 'none',
				'z-index': ++nextZindex
			}).fadeIn('fast');
			$('#desc').html($('#' + id).attr('alt')).css({
				'display': 'none'
			}).fadeIn('fast');
		}
		$(this).dequeue();
	});
}

var headerInterval = null;

function galleryChgPic(newSrc, alt) {
	if(nextZindex%2 == 0) {
		var id = "galleryPicEven";
	} else {
		var id = "galleryPicOdd";
	}
	
	if($('#' + id).css('z-index') == "auto") {
		$('#' + id).css('z-index', 0);
	}
	
	if (nextZindex != $('#' + id).css('z-index')) {
		$('.galleryActiveIMG').removeClass("galleryActiveIMG");
		$('img[src$=' + newSrc.replace(/.*thn784/, "thn48") + ']').addClass("galleryActiveIMG");
		$('#' + id).parent("a").attr('href', newSrc.replace("thn784/", ""));
		$('#' + id).attr({
			"src": newSrc,
			"alt": alt
		}).css({
			'display': 'none',
			'z-index': ++nextZindex
		}).fadeIn('slow');
		$('#desc').html(alt).css({
			'display': 'none'
		}).fadeIn('fast');
	}
}

var smallGalleryRoomsNextZindex = 2;
var roomPics = [];
var roomPicsTitle = [];
var roomPicsLink = [];
var smallGalRoomsCurrentPic = [];

function navigateSmallGalleryRooms(navTo, gal){

	if (smallGalleryRoomsNextZindex % 2 == 0) {
		var idPic = "#smallGalleryRoomsPicEven" + gal;
		var classPic = ".smallGalleryRoomsPicEven" + gal;
	}
	else {
		var idPic = "#smallGalleryRoomsPicOdd" + gal;
		var classPic = ".smallGalleryRoomsPicOdd" + gal;
	}
	
	switch(navTo) {
		case 'prev':
			navTo = Math.max(0, (smallGalRoomsCurrentPic[gal] - 1));
			if(smallGalRoomsCurrentPic[gal] - 1 == -1) {
				navTo = roomPics[gal].length-1;
			}
			newSrc = roomPics[gal][navTo];
			alt = roomPicsTitle[gal][navTo];
			break;
		case 'next':
			navTo = Math.min((smallGalRoomsCurrentPic[gal] + 1), roomPics[gal].length-1);
			if(roomPics[gal].length == smallGalRoomsCurrentPic[gal] + 1) {
				navTo = 0;
			}
			newSrc = roomPics[gal][navTo];
			alt = roomPicsTitle[gal][navTo];
			break;
	}
	
	$(idPic).attr({
		"src": newSrc,
		"alt": alt
	});
	
	if(roomPicsLink[gal][navTo] == "" || roomPicsLink[gal][navTo] == undefined) {
		$(idPic).parent("a").attr('href', newSrc);
	} else {
		$(idPic).parent("a").attr('href', roomPicsLink[gal][navTo] + "&.jpg");
	}
	
	$(classPic).css({
		'display': 'none',
		'z-index': ++smallGalleryRoomsNextZindex
	}).fadeIn('slow');
	
	$('.desc' + gal).html(alt).css({
		'display': 'none'
	}).fadeIn('fast');
	
	smallGalRoomsCurrentPic[gal] = navTo;
}

var smallGalleryNextZindex = 2;
var smallGalleries = 0;
var smallGalPics = [];
var smallGalAlts = [];
var smallGalLink = [];
var smallGalInterval = [];
var smallGalCurrentPic = 0;

function navigateSmallGallery(navTo, gal, newSrc, alt) {
	
	if(smallGalleryNextZindex%2 == 0) {
		var idPic = "#smallGalleryPicEven" + gal;
		var classPic = ".smallGalleryPicEven";
	} else {
		var idPic = "#smallGalleryPicOdd" + gal;
		var classPic = ".smallGalleryPicOdd";
	}
	
	var dontInterruptInterval = false;
	
	switch(navTo) {
		case 'prev':
			navTo = Math.max(0, (smallGalCurrentPic - 1));
			if(smallGalCurrentPic - 1 == -1) {
				navTo = smallGalPics[gal].length-1;
			}
			newSrc = smallGalPics[gal][navTo];
			alt = smallGalAlts[gal][navTo];
			break;
		case 'nextAuto':
			dontInterruptInterval = true;
		case 'next':
			navTo = Math.min((smallGalCurrentPic + 1), smallGalPics[gal].length-1);
			if(smallGalPics[gal].length == smallGalCurrentPic + 1) {
				navTo = 0;
			}
			newSrc = smallGalPics[gal][navTo];
			alt = smallGalAlts[gal][navTo];
			break;
	}
	
	if(!dontInterruptInterval) {
		clearInterval(smallGalInterval[gal]);
	}
	
	
	$('#divContentRight .gallery .nav .numbersHolder .numbers').animate({
		"margin-left": Math.min(0, Math.min(navTo - 2, smallGalPics[gal].length - 6) * 25 * -1) + "px"
	});

	
	$('#divContentRight .gallery .nav .numbers a.active').removeClass("active");
	
	$('#number' + navTo).addClass("active");
	
	$(idPic).attr({
		"src": newSrc,
		"alt": alt
	});
	
	if(smallGalLink[gal][navTo] == "" || smallGalLink[gal][navTo] == undefined) {
		$(idPic).parent("a").attr('href', newSrc.replace("thn228/", ""));
	} else {
		$(idPic).parent("a").attr('href', smallGalLink[gal][navTo] + "&.jpg");
	}
	
	$(classPic).css({
		'display': 'none',
		'z-index': ++smallGalleryNextZindex
	}).fadeIn('slow');
	
	$('.desc').html(alt).css({
		'display': 'none'
	}).fadeIn('fast');
	
	smallGalCurrentPic = navTo;
	
}

function getDirections(from, to) {
	directions.clear();
	directions.load("from: " + from + " to: " + to);
}

function toggleHolidayPlaner(id, type, btnObj, addTxt, removeTxt) {
	$.post('./ajax.php', {'action': 'toggleHolidayPlanner', 'id': id, 'type': type}, function(res) {
		$('#holidayPlannerCount').html(res.count);
		var offset = $(btnObj).parent().offset();
		var offsetTarget = $('#holidayPlanner').offset();
		if ($(btnObj).children('span').html() == addTxt) {
			$(btnObj).parent()
				.clone()
				.appendTo('body')
				.css({'position': 'absolute', 'left': offset.left+'px', 'top': offset.top+'px', 'z-index': 99999})
				.animate({'top': offsetTarget.top+'px', 'left':offsetTarget.left+'px'}, 1500, function() {
					$(this).fadeOut();
				});
			$(btnObj).children('span').html(removeTxt);
		} else {
			$(btnObj).children('span').html(addTxt);
			$(btnObj).parent()
				.clone()
				.appendTo('body')
				.css({'position': 'absolute', 'left': offsetTarget.left+'px', 'top': offsetTarget.top+'px', 'z-index': 99999})
				.animate({'top': offset.top+'px', 'left':offset.left+'px'}, 1500, function() {
					$(this).fadeOut().remove();
				});
		}
	}, "json");
}

function addListToHolidayPlaner(id, btnObj) {
	$.post('./ajax.php', {'action': 'addListToHolidayPlanner', 'id': id}, function(res) {
		$('#holidayPlannerCount').html(res.count);
		var offset = $(btnObj).parent().offset();
		var offsetTarget = $('#holidayPlanner').offset();
		$(btnObj).parent()
			.clone()
			.appendTo('body')
			.css({'position': 'absolute', 'left': offset.left+'px', 'top': offset.top+'px', 'z-index': 99999})
			.animate({'top': offsetTarget.top+'px', 'left':offsetTarget.left+'px'}, 1500, function() {
				$(this).fadeOut();
			});
		$(btnObj).children('span').html(removeTxt);
	}, "json");
}

function toggleInterMap(params, neueKarte) {
	if(!neueKarte) {
		var neueKarte = false;
	}
	
	if($('#interMapHolder').css('display') == "none") {
		if ($('#interMap iframe').attr('src') == "about:blank;") {
			if (neueKarte == true)	// neue Karte
				$('#interMap iframe').attr('src', 'http://kufstein.intermaps.com/intermaps_gmaps.asp?lang=' + lang);
			else	
				$('#interMap iframe').attr('src', 'http://kufstein.intermaps.com/index.swf?chn=Sommer&lang=' + lang);
		}
		if(params != "") {
			if (neueKarte == true)	// neue Karte
				$('#interMap iframe').attr('src', 'http://kufstein.intermaps.com/intermaps_gmaps.asp?lang=' + lang + '&' + params);
			else	
				$('#interMap iframe').attr('src', 'http://kufstein.intermaps.com/index.swf?chn=Sommer&lang=' + lang + '&' + params);
				
			//$('#interMap iframe').attr('src', $('#interMap iframe').attr('src') );
			$(document).scrollTo("#top");
		}
		$('#interMapHolder').css('height', '1px').animate({
			'height': '580px'
		}).fadeIn();
	} else {
		$('#interMapHolder').slideUp();
	}
	
}

function childrenAge(obj, name) {
	if (obj.value > 0) {
		$('#children').slideDown();
		$('#childrenAge').html('');
		for(var i=1;i<=obj.value;i++) {
			$('#childrenAge').html($('#childrenAge').html() + '<input type="text" name="' + name.replace('%num', i) + '" size="2" /> ');
		}
	}
	else {
		$('#children').slideUp();
		$('#childrenAge').html('');
	}
}

function toggleSitemap(obj) {
	if ($(obj).children('img').attr('src').indexOf('plus') != -1) {
		$(obj).next().slideToggle();
		$(obj).children('img').attr('src', $(obj).children('img').attr('src').replace('plus', 'minus'));
	} else {
		$(obj).next().slideToggle();
		$(obj).children('img').attr('src', $(obj).children('img').attr('src').replace('minus', 'plus'));
	}
}

