// custom animation algorithm "bouncy"
$.easing.bouncy = function (x, t, b, c, d) {
    var s = 1.70158;
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
// custom tooltip effect for jQuery Tools Tooltip
$.tools.tooltip.addEffect("bouncy",
	// opening animation
	function(done) {
		this.getTip().animate({top: '+=15'}, 500, 'bouncy', done).show();
	},
	// closing animation
	function(done) {
		this.getTip().animate({top: '-=15'}, 500, 'bouncy', function()  {
			$(this).hide();
			done.call();
		});
	}
);




$(function(){

	/* avoid FOUC, see <head> */
	$('body').css('background-image','url(/style/img/tolmien_background.png)');
	$('#lang').css('visibility','visible');
	$('#pano').css('visibility','visible');
	$('#page').css('visibility','visible');
	$('#contact').css('visibility','visible');
	$('#actions').css('visibility','visible');
	$('#footer').css('visibility','visible');



	/* fancybox Verpackungsmaterial*/
	$("#verpackungsmaterial a.foto_verpackung").fancybox({
		'overlayOpacity'	: 0.8,
		'overlayColor'		: '#000',
		'zoomSpeedIn'		: 100,
		'zoomSpeedOut'		: 100,
		'enableEscapeButton'	: true,
		'hideOnOverlayClick'	: true,
		'titlePosition'		: 'over',
		'onComplete'		: function(){
			$("#fancybox-wrap").hover(function(){
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}

	});
	$("#verpackungsmaterial a.foto_verpackung").tipsy({gravity:'s',fade:true});
	
	/* Bestseller, modal */
	$("#verpackungsmaterial a.bestseller_verpackung").fancybox({
		titleShow	: false,
		centerOnScroll : true,
		padding	: 22,
		autoDimensions : false,
		showNavArrows : false,
		overlayOpacity	: 0.8,
	});



	/* panorama background */

	// index
	$("#tab_mass")	.click(function(){$("#pano-photo").css("background-image","url(/img/mass-umzug.jpg)");return false;});
	$("#tab_team")	.click(function(){$("#pano-photo").css("background-image","url(/img/team.jpg)");return false;});
	$("#tab_logi")	.click(function(){$("#pano-photo").css("background-image","url(/img/logistik.jpg)");return false;});
	$("#tab_serv")	.click(function(){$("#pano-photo").css("background-image","url(/img/service.jpg)");return false;});
	$("#tab_buer")	.click(function(){$("#pano-photo").css("background-image","url(/img/buero-profi.jpg)");return false;});
	$("#tab_ausl")	.click(function(){$("#pano-photo").css("background-image","url(/img/ausland.jpg)");return false;});
	$("#tab_sich")	.click(function(){$("#pano-photo").css("background-image","url(/img/sicherheit.jpg)");return false;});
	$("#tab_tipp")	.click(function(){$("#pano-photo").css("background-image","url(/img/tipps.jpg)");return false;});
	$("#tab_chec")	.click(function(){$("#pano-photo").css("background-image","url(/img/checkliste.jpg)");return false;});

	// umzug
	$("#t_umzug")	.click(function(){$("#pano-photo").css("background-image","url(/img/mass-umzug.jpg)");return false;});
	$("#t_check")	.click(function(){$("#pano-photo").css("background-image","url(/img/checkliste.jpg)");return false;});
	$("#t_tipps")	.click(function(){$("#pano-photo").css("background-image","url(/img/tipps.jpg)");return false;});

	// verpackung
	$("#t_verp")	.click(function(){$("#pano-photo").css("background-image","url(/img/verpackung.jpg)");return false;});
	$("#t_shop")	.click(function(){$("#pano-photo").css("background-image","url(/img/shops.jpg)");return false;});
	$("#t_lief")	.click(function(){$("#pano-photo").css("background-image","url(/img/lieferservice.jpg)");return false;});

	// lagerung
	$("#t_lage")	.click(function(){$("#pano-photo").css("background-image","url(/img/container.jpg)");return false;});
	$("#t_serv")	.click(function(){$("#pano-photo").css("background-image","url(/img/container/tolmien_container.jpg)");return false;});
	$("#t_beis")	.click(function(){$("#pano-photo").css("background-image","url(/img/container/tolmien_container_beispiel.jpg)");return false;});



	/* tabs Checkliste */
	$("#tabs").tabs();
	$("#checktabs").tabs();

	/* Uniform Checkliste */
	$('#checktabs input').uniform();
	$('#check-all').click(function(){
		$("#checktabs :checkbox").attr("checked", true);
		$.uniform.update('#checkliste-umzug input[type=checkbox]');
		return false;
	});
	$('#uncheck-all').click(function(){
		$("#checktabs :checkbox").attr("checked", false);
		$.uniform.update('#checkliste-umzug input[type=checkbox]');
		return false;
	});

	$("#print-checklist").button();
	$("#check-all").button();
	$("#uncheck-all").button();

	$('#print-checklist').click(function(){
		$('#checkliste-umzug').printElement({
			printMode:'popup',
			pageTitle:'TOLMIEN Checkliste',
			leaveOpen:true,
			//overrideElementCSS:[{href:'http://www.tolmien.com/style/checkliste-print.css',media:'print'}],
			printBodyOptions: {classNameToAdd : 'checkliste-printview'}
		});
		$.uniform.update();
	});


	/* Niederlassungen */
	$("#niederlassungen").accordion({ header: 'h2',navigation:true });

	/* Kontaktformular */
	$("#mail input:submit").button();



	/* Links ohne title/tooltip */
	$("#nav a").removeAttr("title");
	$("#footer p a:not(.broschuere)").removeAttr("title");

	/* tooltip, footer, header */
	$("#footer a.broschuere").tipsy({gravity:'s',fade:false});
	$("#lang a").tipsy({gravity:'e',fade:true});
	$("#prospekt_sprache option").tipsy({gravity:'e',fade:true});
	$("#upt-kontakt span.mandatory").tipsy({gravity:'w',fade:true});

	/* megabling tooltips */
	$("#amo a").tooltip({position:"top center",opacity:1,tipClass:"tooltip-affil"});
	$("#fedemac a").tooltip({position:"top center",opacity:1,tipClass:"tooltip-affil"});
	$("#start-planning").tooltip({position: "top center",opacity:1,tipClass:"tooltip-start",effect:"bouncy"});
	$("#besichtigungstermin").tooltip({position: "top center",opacity:1,tipClass:"tooltip-start",effect:"bouncy"});


	/* overlay, footer */
	$("#footer a").fancybox({
		titleShow	: false,
		centerOnScroll	: true,
		padding	: 22,
		cyclic		: true,
		autoDimensions : false,
		showNavArrows	: false,
		overlayOpacity	: 0.8
	});

	/* Umzugsformular, modal */
	$("#start-planning").fancybox({
		titleShow	: false,
		width		: '80%',
		height		: '90%',
		centerOnScroll : true,
		showNavArrows	: false,
		hideOnOverlayClick : false,
		overlayOpacity	: 0.8,
		type		: 'iframe'
	});

	/* Besichtigung, modal */
	$("#besichtigungstermin").fancybox({
		titleShow	: false,
		centerOnScroll : true,
		padding	: 22,
		autoDimensions : false,
		showNavArrows : false,
		overlayOpacity	: 0.8,
	});
	
	/* Uniform - Umzgusformular */
	$("#umzug-planen select, #umzug-planen input:checkbox, #umzug-planen input:radio, #umzug-planen input:file").uniform();

	/* Uniform - Broschuere */
	//$("#prospekt_sprache").uniform();





	/* Umzugsformular */
	if ( typeof(wizardSettings) != 'undefined' || typeof(auswertung) != 'undefined' ) {
		$("#umzug-planen input:submit, #umzug-planen input:reset").button();
		//$("#umzug-planen").formwizard( typeof(wizardSettings) == 'undefined' ? {} : wizardSettings);
		$("#umzug-planen").formwizard(wizardSettings);
	}




	/* Prospekt / Umzugsbroschüre */
	$("#prospekt_bestellung input:submit").button();

	/* Prospekt Vorschau */
	/* based on "Alex's page turner", Sitepoint */

	/* constants */
	var pageheight		= 96; /* determined by the page size being displayed */
	var pagewidth		= 138; /* =''= */
	var pageYpos		= 0; /* both pages Y pos */
	var leftpageX		= 0; /* starting Left X pos */
	var rightpageX		= pagewidth; /* starting Right X pos */
	var timedelay		= 60 /*  1/1000th of a second */

	/* apparatus */
	$("#turner").css("overflow-y", "hidden");
	$("#turner img").after("<div id='flip'> </div>");
	$("#flip").after("<div id='leftpage'> </div>");
	$("#leftpage").after("<div id='rightpage'> </div>");
	$("#leftpage").prepend("<div id='curll'></div>");
	$("#rightpage").prepend("<div id='curlr'></div>");

	/* clip out original img */
	$("#turner img").remove();

	/* left page turner animation */
	$("#curll").click( function() {
		pageYpos = pageYpos + pageheight; /* calculate new background position */
		$("#leftpage")
			.css("background-position", "0px "+pageYpos+"px") /* change page by leftpage */
		$("#flip")
			.css("background-position", "top right") /* show midway page turn image overlay */
			.animate({opacity: 1.0}, timedelay, "linear", function() { /* delay for a moment */
				setTimeout(function() {
					$("#flip").css("background-position", "top center"); /* remove page turn - back to default */
					$("#rightpage").css("margin-left", "-4px").css("margin-right", "4px").css("background-position", pagewidth+"px "+pageYpos+"px").animate({marginRight:0, marginLeft:0}, 300, "linear");
					}, timedelay);
			})

	});

	/* right page turner */
	$("#curlr").click( function() {
		pageYpos = pageYpos - pageheight; /* calculate new background position */
		$("#rightpage")
			.css("background-position", pagewidth+"px "+pageYpos+"px") /* change rightpage page */
		$("#flip")
			.css("background-position", "top left")  /* show midway page turn image overlay */
			.animate({opacity: 1.0}, timedelay, "linear", function() { /* delay for a moment */
				setTimeout(function() {
					$("#flip").css("background-position", "top center") /* remove page turn - back to default */
					$("#leftpage").css("margin-left", "4px").css("margin-right", "-4px").css("background-position","0px "+pageYpos+"px").animate({marginRight:0, marginLeft:0}, 300, "linear");
					}, timedelay);
			})
	});


	/* external links */
	$('a[rel="external"]').attr('target','_blank');



	$(".countadd-cb").toggle(
		function () {
			$(this).parent().addClass("checked");
		},
		function () {
			$(this).parent().removeClass("checked");
		}
	);

});




/* Eingabefeld für Anzahl des Umzugsgutes anzeigen */
jQuery('.count-cb').click( function() {
	var checked = jQuery('#' + this.id).attr('checked');
	var counter = jQuery('#' + this.id + '-count');
	checked ? counter.attr('value', 1) : counter.attr('value', '');
} );

jQuery('.count-text').keyup( function(e) {
	var cb = (this.id.match(/(.*)-count/))[1];
	if (e.which==13)
		return false;
	c = jQuery(this).val().replace( /\D/g, '');
	jQuery(this).val(c);
	(jQuery(this).val() == '' || jQuery(this).val() == 0) ? jQuery('#' + cb).attr('checked', false) : jQuery('#' + cb).attr('checked', true);
	jQuery.uniform.update();
} );

jQuery('.numbers-only').keyup( function(e) {
	if (e.which==13)
		return false;
	c = $(this).val().replace( /\D/g, '');
	jQuery(this).val(c);
} );

/* Umzugsgut hinzufügen */
jQuery('.more-item').click( function() {
	var room = (this.id.match(/-(\w+)$/))[1];
	var table = jQuery('#more-table-' + room);
	var rows = table.find('input[name^=' + room + '_wieviel-]').length + 1;
	var tr = table.find('tr:last').clone(true);
	tr.find('input').attr('value', ''); /* clean fields */
	tr.find('input[name^=' + room + '_wieviel-]').attr('name', room + '_wieviel-' + rows);
	tr.find('input[name^=' + room + '_was-]').attr('name', room + '_was-' + rows);
	table.append(tr);
	return false;
} );



/* Datepicker */
/*
if ( typeof(datepickerLang) != 'undefined' ) {
	jQuery.datepicker.setDefaults( jQuery.datepicker.regional[''] );
	jQuery('#u_datum').datepicker( jQuery.datepicker.regional[datepickerLang] );
}*/

