//jQuery.noConflict();

$(document).ready(function(){
	
	//if iPhone or if iPad
	function isiOS() {
	    return (
	        (navigator.platform.indexOf("iPhone") != -1) ||
	        (navigator.platform.indexOf("iPad") != -1)
	    );
	}
		
	//Current window.location
	var uri = window.location.href;
	var pathname = window.location.pathname;
	var hash = window.location.hash;
	var nohash = uri.replace(location.hash,"")
	
	
	//Label inside of inputfields
	function insidelabel(selector, name) {
		$(selector).val(name);
		$(selector).css({'color':'#999'});
		
		$(selector).focus(function () {
			//Input Value
			if ($(this).val() == name) { $(this).val(''); }
			$(this).css({'color':'#465356'})
		});
		
		$(selector).blur(function () {
			if ($(this).val() == '') { $(this).val(name); }
			if ($(this).val() == name) {
				$(this).css({'color':'#999'});
			}
		});
	}
	
	//Forum
	//insidelabel('.wpf-textarea', 'Dein Beitrag');
	
	//Label inside of inputfields
	(function () {
		var fill = "fill";
		var inputs = jQuery('.input-inside-label input, .search-input-inside-label input, .textarea-inside-label textarea');
		var setLabelStyle = function setLabelStyle () {
			var label = $(this);
			if (label.val().length) {
				label.addClass(fill);
			} else {
				label.removeClass(fill);
			}
		};
		inputs.focus(function () { $(this).addClass(fill); });
		inputs.blur(setLabelStyle);
		inputs.each(setLabelStyle);
	}());
	
	//Animated scroll for anchorlinks
	var highlightCol = '#EEEEDC',
		delayTime = 500,
		fadeInTime = 300,
		fadeOutTime = 300,
		highlightTime = 1000;
		
	$("a[href*='#']").live('click', function(e) {

		//$(this).addClass('anchorLink');
		
		var path = $(this).attr('href').split('#')[0];
		var anchor = $(this).attr('href').split('#')[1];
		
		//if ( uri.indexOf(path) != -1 ) {
			
		//if ( nohash == path ) {

		if ( nohash == path || nohash == path+"/" || nohash == nohash+path ) {
		
			if ( $("a[name='#"+anchor+"']").length > 0 ) {
				e.preventDefault();
				scroll($("a[name='#"+anchor+"']"), true, true, 30);
			} else if ( $("#"+anchor).length > 0 ) {
				e.preventDefault();
				scroll($("#"+anchor), true, true, 30);
			}
		
		}
		
	});
	
	function scroll(selector, animate, highlight, viewOffset) {
		
		var bgCol = $(selector).css('backgroundColor');
		
		var pageOffset = $(selector).offset();
		var scrollPos = pageOffset.top - viewOffset;
		
		if (animate) {
		
			$('html, body').animate({scrollTop : scrollPos + 'px'}, {
		        duration: 1000,
		        easing: 'easeInOutExpo',
		        complete: function() { }
			});
		
		} else {
		
			$('html, body').scrollTop( scrollPos );
			
		}
		
		//highlight element
		if ( highlight && ( bgCol != "rgba(0, 0, 0, 0)" && bgCol != "transparent" ) ) {
			$(selector).delay(delayTime).animate({ backgroundColor: highlightCol }, fadeInTime).delay(highlightTime).animate({ backgroundColor: bgCol }, fadeOutTime);
		}
		
	}
	
	//remove <br>'s from gallery
	$('.gallery br').not(':last-child').each(function() {
		$(this).remove();
	});
	$('.gallery dl.gallery-item').animate({ opacity : 1 }, 'slow');
	
	//Add title attribute to download links
	$("a[href$='.pdf'], a[href$='.xls'], a[href$='.xlsx'], a[href$='.doc'], a[href$='.docx'], a[href$='.ppt'], a[href$='.pptx'], a[href$='.pps'], a[href$='.zip'], a[href$='.gzip'], a[href$='.rar'], a[href$='.txt'], a[href$='.rtf']").attr('title', 'Diese Datei downloaden?');
	
	//Back to top
	$(window).scroll(function () {
		
		if ( $(window).width() > 300 || !isiOS ) {
		
			if ( $(window).scrollTop() > 100 ) {
				$('#back-to-top').fadeIn('fast');
			} else {
				$('#back-to-top').fadeOut('fast');
			}
			
		}
		
    });
	
	$(window).scroll();
	
	loadMap("mapBottomContainer", false);
	
	function loadMap(cont, scroll) {
		
		var latlng = new google.maps.LatLng(47.631243,12.218106);
		var c = new google.maps.LatLng(47.63212507188887,12.218492238098188); //optical center
		var options = {
			zoom: 16,
			center: c,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel: scroll,
			streetViewControl: false,
			navigationControlOptions: {  
			    style: google.maps.NavigationControlStyle.SMALL 
			}
		}

		var map = new google.maps.Map(document.getElementById(cont), options);

		var mapdesc = '<div id="gmContent">'+
		'<h3 id="gmTitle" class="widget-title">Anreise <span class="route"><a href="http://goo.gl/n1Khj" title="Route berechnen?" target="_blank">Route berechnen?</a></span></h3>'+
		'<div id="gmBody">'+
		'<p>Ferienwohnungen und Pizzeria Osl, <b>6341 Ebbs</b>.' +
		'</div>'+
		'</div>';

		var infowindow = new google.maps.InfoWindow({
			content: mapdesc
		});

		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: '6173 Oberperfuss'
		});

		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});

		infowindow.open(map,marker);
		
		/*$('#getCenter').click(function(e) {
			e.preventDefault();
			console.log(map.getCenter());
		});*/
		
	}
	
	//Navigation
	var first = $("#access .menu ul li:first");
	var allOther = $("#access .menu ul li").not(":first, :last");
	var last = $("#access .menu ul li:last");

	var remainingWidth = $("#access .menu").width() - first.width() - last.width();
	allOther.width(remainingWidth / allOther.length).css("text-align", "center");
	
	$('a[href*="pizzapiccolo.at/willkommen/"]').click(function(e) {
		e.preventDefault();
		scroll($("#main"), true, true, 30);
	});
	
	//Validation
	bookingForm_validateFunction();
	
	if(window.location.hash == "#ok") {
	  	notificationOutput("success", "Ihre Anfrage wird von uns bearbeitet.");
	} else if (window.location.hash == "#error") {
		notificationOutput("warning", "Es gab leider einen Fehler beim Senden der Anfrage.");
	}
	
});

$(window).load(function () {
	/*auto height of main section*/
	/*var postheight = $('article.post').outerHeight();
	var sideheight = $('aside#sidebar').outerHeight();
	var main = $('#main');
	var margin = 120;
	
	if (postheight > sideheight) {
		main.height(postheight + margin);
	} else {
		main.height(sideheight + margin);
	}*/
	
	/*add Fancybox link attributes*/	
	$('.gallery .attachment-thumbnail').each(function() {
		$(this).parent().addClass('imggroup');
		$(this).parent().attr('rel', 'imggroup');
	});

	$('.entry a img').each(function() {
		$(this).parent().addClass('imggroup');
		//$(this).parent().attr('rel', 'imggroup');
	});

	$('.entry a[href^="http://www.youtube.com"]').each(function() {
		$(this).addClass('youtube');
	});

	/* Apply fancybox to multiple items */
	$("a.imggroup").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayOpacity':	0.9,
		'overlayColor'	:	'#2A373F',
		'titleShow'		:	true,
		'titlePosition' : 	'inside'
	});

	/*Fancybox for youtubevids*/
	$(".youtube").click(function() {
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'speedIn'		: 300, 
			'speedOut'		: 200, 
			'overlayOpacity': 0.9,
			'overlayColor'	: '#2A373F',
			'title'			: this.title,
			'width'			: 680,
			'height'		: 495,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			   	'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
		return false;
	});
	
});

function bookingForm_validateFunction() {
	$("#bookingForm").validate({
		rules: {
			'vorname': {
				required: true
			},
			'nachname': {
				required: true
			},
			'strasse': {
				required: true
			},
			'ort': {
				required: true
			},
			'land' : {
				required: true
			},
			'tel': {
				required: true,
				number: true
			},
			'email': {
				required: true,
				email: true
			}
		},
		messages: {
			'vorname': {
				required: "* Bitte ausfüllen."
			},
			'nachname': {
				required: "* Bitte ausfüllen."
			},
			'strasse': {
				required: "* Bitte ausfüllen."
			},
			'ort': {
				required: "* Bitte ausfüllen."
			},
			'land' : {
				required: "* Bitte ausfüllen."
			},
			'tel': {
				required: "* Bitte ausfüllen.",
				number: "* Nur Zahlen erlaubt."
			},
			'email': {
				required: "* Bitte ausfüllen.",
				email: "* Keine gültige e-mail Adresse."
			}
		}
	});
}

function notificationOutput(type, message) {
	var note = $('.notificationBar');
	if ( type == "success" ) { note.removeClass('warning').addClass('success'); }
	if ( type == "warning" ) { note.removeClass('success').addClass('warning'); }
	note.html(message);	
	note.slideDown( function() {
		note.delay(5000).slideUp();
		window.location.hash = "#_"
	});
}
